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    * 
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("unchecked")
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  	@SuppressWarnings("unchecked")
61      public void add(Annotation annotation) {
62  		annotations.add(annotation);
63  	}
64  	
65  	@SuppressWarnings("unchecked")
66      public void remove(Annotation annotation) {
67  		annotations.remove(annotation);
68  	}
69  	
70      /**
71  	 * @return the annotations
72  	 */
73  	@SuppressWarnings("unchecked")
74      public Set<Annotation> getAnnotations() {
75  		return Collections.unmodifiableSet(annotations);
76  	}
77  
78  	@SuppressWarnings("unchecked")
79      public void paint(Graphics g) {
80      	Graphics2D g2d = (Graphics2D)g;
81          Color oldColor = g.getColor();
82          for(Annotation annotation : annotations) {
83          	Object ann = annotation.getAnnotation();
84          	if(ann instanceof Shape) {
85              	Shape shape = (Shape)ann;
86              	Paint paint = annotation.getPaint();
87              	Shape s = transformer.transform(shape);
88              	g2d.setPaint(paint);
89              	if(annotation.isFill()) {
90              		g2d.fill(s);
91              	} else {
92              		g2d.draw(s);
93              	}
94          	} else if(ann instanceof String) {
95              	Point2D p = annotation.getLocation();
96              	String label = (String)ann;
97                  Component component = prepareRenderer(rc, annotationRenderer, label);
98                  component.setForeground((Color)annotation.getPaint());
99                  if(annotation.isFill()) {
100                 	((JComponent)component).setOpaque(true);
101                 	component.setBackground((Color)annotation.getPaint());
102                 	component.setForeground(Color.black);
103                 }
104                 Dimension d = component.getPreferredSize();
105                 AffineTransform old = g2d.getTransform();
106                 AffineTransform base = new AffineTransform(old);
107                 AffineTransform xform = transformer.getTransform();
108 
109                 double rotation = transformer.getRotation();
110                 // unrotate the annotation
111                 AffineTransform unrotate = 
112                 	AffineTransform.getRotateInstance(-rotation, p.getX(), p.getY());
113                 base.concatenate(xform);
114                 base.concatenate(unrotate);
115                 g2d.setTransform(base);
116                 rc.getRendererPane().paintComponent(g, component, rc.getScreenDevice(), 
117                         (int)p.getX(), (int)p.getY(),
118                         d.width, d.height, true);
119                 g2d.setTransform(old);
120         	}
121         }
122         g.setColor(oldColor);
123     }
124     
125 	public Component prepareRenderer(RenderContext<?,?> rc, AnnotationRenderer annotationRenderer, Object value) {
126 		return annotationRenderer.getAnnotationRendererComponent(rc.getScreenDevice(), value);
127 	}
128 
129     public boolean useTransform() {
130         return true;
131     }
132 }