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