View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    *
8    * Created on Aug 23, 2005
9    */
10  package edu.uci.ics.jung.visualization.renderers;
11  
12  import java.awt.Component;
13  import java.awt.Dimension;
14  import java.awt.Rectangle;
15  import java.awt.Shape;
16  import java.awt.geom.Point2D;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.apache.commons.collections15.Transformer;
21  
22  import edu.uci.ics.jung.algorithms.layout.Layout;
23  import edu.uci.ics.jung.graph.Graph;
24  import edu.uci.ics.jung.graph.util.Context;
25  import edu.uci.ics.jung.visualization.Layer;
26  import edu.uci.ics.jung.visualization.RenderContext;
27  import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
28  
29  /**
30   * Renders Vertex Labels, but can also supply Shapes for vertices.
31   * This has the effect of making the vertex label the actual vertex
32   * shape. The user will probably want to center the vertex label
33   * on the vertex location.
34   * 
35   * @author Tom Nelson
36   *
37   * @param <V>
38   * @param <E>
39   */
40  public class VertexLabelAsShapeRenderer<V,E> 
41  	implements Renderer.VertexLabel<V,E>, Transformer<V,Shape> {
42  
43  	protected Map<V,Shape> shapes = new HashMap<V,Shape>();
44  	protected RenderContext<V,E> rc;
45  	
46  	public VertexLabelAsShapeRenderer(RenderContext<V, E> rc) {
47  		this.rc = rc;
48  	}
49  
50  	public Component prepareRenderer(RenderContext<V,E> rc, VertexLabelRenderer graphLabelRenderer, Object value, 
51  			boolean isSelected, V vertex) {
52  		return rc.getVertexLabelRenderer().<V>getVertexLabelRendererComponent(rc.getScreenDevice(), value, 
53  				rc.getVertexFontTransformer().transform(vertex), isSelected, vertex);
54  	}
55  
56  	/**
57  	 * Labels the specified vertex with the specified label.  
58  	 * Uses the font specified by this instance's 
59  	 * <code>VertexFontFunction</code>.  (If the font is unspecified, the existing
60  	 * font for the graphics context is used.)  If vertex label centering
61  	 * is active, the label is centered on the position of the vertex; otherwise
62       * the label is offset slightly.
63       */
64      public void labelVertex(RenderContext<V,E> rc, Layout<V,E> layout, V v, String label) {
65      	Graph<V,E> graph = layout.getGraph();
66          if (rc.getVertexIncludePredicate().evaluate(Context.<Graph<V,E>,V>getInstance(graph,v)) == false) {
67          	return;
68          }
69          GraphicsDecorator g = rc.getGraphicsContext();
70          Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(), label,
71          		rc.getPickedVertexState().isPicked(v), v);
72          Dimension d = component.getPreferredSize();
73          
74          int h_offset = -d.width / 2;
75          int v_offset = -d.height / 2;
76          
77          Point2D p = layout.transform(v);
78          p = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p);
79  
80          int x = (int)p.getX();
81          int y = (int)p.getY();
82  
83          g.draw(component, rc.getRendererPane(), x+h_offset, y+v_offset, d.width, d.height, true);
84  
85          Dimension size = component.getPreferredSize();
86          Rectangle bounds = new Rectangle(-size.width/2 -2, -size.height/2 -2, size.width+4, size.height);
87          shapes.put(v, bounds);
88      }
89  
90  	public Shape transform(V v) {
91  		Component component = prepareRenderer(rc, rc.getVertexLabelRenderer(), rc.getVertexLabelTransformer().transform(v),
92  				rc.getPickedVertexState().isPicked(v), v);
93          Dimension size = component.getPreferredSize();
94          Rectangle bounds = new Rectangle(-size.width/2 -2, -size.height/2 -2, size.width+4, size.height);
95          return bounds;
96  //		Shape shape = shapes.get(v);
97  //		if(shape == null) {
98  //			return new Rectangle(-20,-20,40,40);
99  //		}
100 //		else return shape;
101 	}
102 
103 	public Renderer.VertexLabel.Position getPosition() {
104 		return Renderer.VertexLabel.Position.CNTR;
105 	}
106 
107 	public Renderer.VertexLabel.Positioner getPositioner() {
108 		return new Positioner() {
109 			public Renderer.VertexLabel.Position getPosition(float x, float y, Dimension d) {
110 				return Renderer.VertexLabel.Position.CNTR;
111 			}};
112 	}
113 
114 	public void setPosition(Renderer.VertexLabel.Position position) {
115 		// noop
116 	}
117 
118 	public void setPositioner(Renderer.VertexLabel.Positioner positioner) {
119 		//noop
120 	}
121 }