View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors
3    * All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    *
8    * 
9    */
10  package edu.uci.ics.jung.visualization.annotations;
11  
12  import java.awt.Color;
13  import java.awt.Component;
14  import java.awt.Dimension;
15  import java.awt.Graphics;
16  import java.awt.Graphics2D;
17  import java.awt.Paint;
18  import java.awt.Shape;
19  import java.awt.geom.AffineTransform;
20  import java.awt.geom.Point2D;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import javax.swing.JComponent;
26  
27  import edu.uci.ics.jung.visualization.Layer;
28  import edu.uci.ics.jung.visualization.RenderContext;
29  import edu.uci.ics.jung.visualization.VisualizationServer.Paintable;
30  import edu.uci.ics.jung.visualization.transform.AffineTransformer;
31  import edu.uci.ics.jung.visualization.transform.LensTransformer;
32  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
33  
34  /**
35   * handles the actual drawing of annotations
36   * 
37   * @author Tom Nelson - tomnelson@dev.java.net
38   *
39   */
40  public class AnnotationPaintable implements Paintable {
41  	
42      @SuppressWarnings("rawtypes")
43  	protected Set<Annotation> annotations = new HashSet<Annotation>();
44      protected AnnotationRenderer annotationRenderer;
45  
46  	protected RenderContext<?,?> rc;
47  	protected AffineTransformer transformer;
48  	
49  	public AnnotationPaintable(RenderContext<?,?> rc, AnnotationRenderer annotationRenderer) {
50  		this.rc = rc;
51  		this.annotationRenderer = annotationRenderer;
52  		MutableTransformer mt = rc.getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
53  		if(mt instanceof AffineTransformer) {
54  			transformer = (AffineTransformer)mt;
55  		} else if(mt instanceof LensTransformer) {
56  			transformer = (AffineTransformer)((LensTransformer)mt).getDelegate();
57  		}
58  	}
59  	
60      public void add(Annotation<?> annotation) {
61  		annotations.add(annotation);
62  	}
63  	
64      public void remove(Annotation<?> annotation) {
65  		annotations.remove(annotation);
66  	}
67  	
68      /**
69  	 * @return the annotations
70  	 */
71      @SuppressWarnings("rawtypes")
72  	public Set<Annotation> getAnnotations() {
73  		return Collections.unmodifiableSet(annotations);
74  	}
75  
76      public void paint(Graphics g) {
77      	Graphics2D g2d = (Graphics2D)g;
78          Color oldColor = g.getColor();
79          for(Annotation<?> annotation : annotations) {
80          	Object ann = annotation.getAnnotation();
81          	if(ann instanceof Shape) {
82              	Shape shape = (Shape)ann;
83              	Paint paint = annotation.getPaint();
84              	Shape s = transformer.transform(shape);
85              	g2d.setPaint(paint);
86              	if(annotation.isFill()) {
87              		g2d.fill(s);
88              	} else {
89              		g2d.draw(s);
90              	}
91          	} else if(ann instanceof String) {
92              	Point2D p = annotation.getLocation();
93              	String label = (String)ann;
94                  Component component = prepareRenderer(rc, annotationRenderer, label);
95                  component.setForeground((Color)annotation.getPaint());
96                  if(annotation.isFill()) {
97                  	((JComponent)component).setOpaque(true);
98                  	component.setBackground((Color)annotation.getPaint());
99                  	component.setForeground(Color.black);
100                 }
101                 Dimension d = component.getPreferredSize();
102                 AffineTransform old = g2d.getTransform();
103                 AffineTransform base = new AffineTransform(old);
104                 AffineTransform xform = transformer.getTransform();
105 
106                 double rotation = transformer.getRotation();
107                 // unrotate the annotation
108                 AffineTransform unrotate = 
109                 	AffineTransform.getRotateInstance(-rotation, p.getX(), p.getY());
110                 base.concatenate(xform);
111                 base.concatenate(unrotate);
112                 g2d.setTransform(base);
113                 rc.getRendererPane().paintComponent(g, component, rc.getScreenDevice(), 
114                         (int)p.getX(), (int)p.getY(),
115                         d.width, d.height, true);
116                 g2d.setTransform(old);
117         	}
118         }
119         g.setColor(oldColor);
120     }
121     
122 	public Component prepareRenderer(RenderContext<?,?> rc, AnnotationRenderer annotationRenderer, Object value) {
123 		return annotationRenderer.getAnnotationRendererComponent(rc.getScreenDevice(), value);
124 	}
125 
126     public boolean useTransform() {
127         return true;
128     }
129 }