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 Jul 21, 2005
9    */
10  
11  package edu.uci.ics.jung.visualization.transform;
12  
13  import java.awt.Color;
14  import java.awt.Graphics;
15  import java.awt.Graphics2D;
16  import java.awt.Paint;
17  import java.awt.geom.RectangularShape;
18  
19  import edu.uci.ics.jung.visualization.VisualizationViewer;
20  import edu.uci.ics.jung.visualization.VisualizationServer.Paintable;
21  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
22  /**
23   * A class to make it easy to add an
24   * examining lens to a jung graph application. See HyperbolicTransformerDemo,
25   * ViewLensSupport and LayoutLensSupport
26   * for examples of how to use it.
27   * 
28   * @author Tom Nelson
29   *
30   *
31   */
32  public abstract class AbstractLensSupport<V,E> implements LensSupport {
33  
34      protected VisualizationViewer<V,E> vv;
35      protected VisualizationViewer.GraphMouse graphMouse;
36      protected LensTransformer lensTransformer;
37      protected ModalGraphMouse lensGraphMouse;
38      protected Lens lens;
39      protected LensControls lensControls;
40      protected String defaultToolTipText;
41  
42      protected static final String instructions = 
43          "<html><center>Mouse-Drag the Lens center to move it<p>"+
44          "Mouse-Drag the Lens edge to resize it<p>"+
45          "Ctrl+MouseWheel to change magnification</center></html>";
46      
47      /**
48       * create the base class, setting common members and creating
49       * a custom GraphMouse
50       * @param vv the VisualizationViewer to work on
51       */
52      public AbstractLensSupport(VisualizationViewer<V,E> vv, ModalGraphMouse lensGraphMouse) {
53          this.vv = vv;
54          this.graphMouse = vv.getGraphMouse();
55          this.defaultToolTipText = vv.getToolTipText();
56          this.lensGraphMouse = lensGraphMouse;
57      }
58  
59      public void activate(boolean state) {
60          if(state) activate();
61          else deactivate();
62      }
63      
64      public LensTransformer getLensTransformer() {
65          return lensTransformer;
66      }
67  
68      /**
69       * @return Returns the hyperbolicGraphMouse.
70       */
71      public ModalGraphMouse getGraphMouse() {
72          return lensGraphMouse;
73      }
74  
75      /**
76       * the background for the hyperbolic projection
77       * @author Tom Nelson 
78       *
79       *
80       */
81      public static class Lens implements Paintable {
82          LensTransformer lensTransformer;
83          RectangularShape lensShape;
84          Paint paint = Color.decode("0xdddddd");
85          
86          public Lens(LensTransformer lensTransformer) {
87              this.lensTransformer = lensTransformer;
88              this.lensShape = lensTransformer.getLensShape();
89          }
90          
91          /**
92  		 * @return the paint
93  		 */
94  		public Paint getPaint() {
95  			return paint;
96  		}
97  
98  		/**
99  		 * @param paint the paint to set
100 		 */
101 		public void setPaint(Paint paint) {
102 			this.paint = paint;
103 		}
104 
105 		/**
106          * @return Returns the hyperbolicTransformer.
107          */
108 
109         public void paint(Graphics g) {
110             
111             Graphics2D g2d = (Graphics2D)g;
112             g2d.setPaint(paint);
113             g2d.fill(lensShape);
114         }
115 
116         public boolean useTransform() {
117             return true;
118         }
119     }
120     
121     /**
122      * the background for the hyperbolic projection
123      * @author Tom Nelson 
124      *
125      *
126      */
127     public static class LensControls  implements Paintable {
128         LensTransformer lensTransformer;
129         RectangularShape lensShape;
130         Paint paint = Color.gray;
131         
132         public LensControls(LensTransformer lensTransformer) {
133             this.lensTransformer = lensTransformer;
134             this.lensShape = lensTransformer.getLensShape();
135         }
136         
137         /**
138 		 * @return the paint
139 		 */
140 		public Paint getPaint() {
141 			return paint;
142 		}
143 
144 		/**
145 		 * @param paint the paint to set
146 		 */
147 		public void setPaint(Paint paint) {
148 			this.paint = paint;
149 		}
150 
151 		/**
152          * @return Returns the hyperbolicTransformer.
153          */
154 
155         public void paint(Graphics g) {
156             
157             Graphics2D g2d = (Graphics2D)g;
158             g2d.setPaint(paint);
159             g2d.draw(lensShape);
160             int centerX = (int)Math.round(lensShape.getCenterX());
161             int centerY = (int)Math.round(lensShape.getCenterY());
162             g.drawOval(centerX-10, centerY-10, 20, 20);
163         }
164 
165         public boolean useTransform() {
166             return true;
167         }
168     }
169 
170 	/**
171 	 * @return the lens
172 	 */
173 	public Lens getLens() {
174 		return lens;
175 	}
176 
177 	/**
178 	 * @param lens the lens to set
179 	 */
180 	public void setLens(Lens lens) {
181 		this.lens = lens;
182 	}
183 
184 	/**
185 	 * @return the lensControls
186 	 */
187 	public LensControls getLensControls() {
188 		return lensControls;
189 	}
190 
191 	/**
192 	 * @param lensControls the lensControls to set
193 	 */
194 	public void setLensControls(LensControls lensControls) {
195 		this.lensControls = lensControls;
196 	}
197 
198 }