View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University 
3    * of California
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * http://jung.sourceforge.net/license.txt for a description.
9    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.Cursor;
15  import java.awt.event.MouseEvent;
16  import java.awt.event.MouseListener;
17  import java.awt.event.MouseMotionListener;
18  import java.awt.geom.Point2D;
19  
20  import edu.uci.ics.jung.visualization.Layer;
21  import edu.uci.ics.jung.visualization.VisualizationViewer;
22  import edu.uci.ics.jung.visualization.transform.LensTransformer;
23  import edu.uci.ics.jung.visualization.transform.MutableTransformer;
24  
25  /** 
26   * Extends TranslatingGraphMousePlugin and adds the capability 
27   * to drag and resize the viewing
28   * lens in the graph view. Mouse1 in the center moves the lens,
29   * mouse1 on the edge resizes the lens. The default mouse button and
30   * modifiers can be overridden in the constructor.
31   * 
32   * 
33   * @author Tom Nelson
34   */
35  public class LensTranslatingGraphMousePlugin extends TranslatingGraphMousePlugin
36  implements MouseListener, MouseMotionListener {
37      
38      protected boolean dragOnLens;
39      protected boolean dragOnEdge;
40      protected double edgeOffset;
41      /**
42       * create an instance with default modifiers
43       */
44      public LensTranslatingGraphMousePlugin() {
45          this(MouseEvent.BUTTON1_MASK);
46      }
47      
48      /**
49       * create an instance with passed modifer value
50       * @param modifiers the mouse event modifier to activate this function
51       */
52      public LensTranslatingGraphMousePlugin(int modifiers) {
53          super(modifiers);
54      }
55      
56      /**
57       * Check the event modifiers. Set the 'down' point for later
58       * use. If this event satisfies the modifiers, change the cursor
59       * to the system 'move cursor'
60       * @param e the event
61       */
62      public void mousePressed(MouseEvent e) {
63          VisualizationViewer vv = (VisualizationViewer)e.getSource();
64          MutableTransformer vt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
65          if(vt instanceof LensTransformer) {
66          	vt = ((LensTransformer)vt).getDelegate();
67          }
68          Point2D p = vt.inverseTransform(e.getPoint());
69          boolean accepted = checkModifiers(e);
70          if(accepted) {
71              vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
72              testViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT), p);
73              testViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
74              vv.repaint();
75          }
76          super.mousePressed(e);
77      }
78      
79      /**
80       * called to change the location of the lens
81       * @param transformer
82       * @param point
83       */
84      private void setViewCenter(MutableTransformer transformer, Point2D point) {
85          if(transformer instanceof LensTransformer) {
86              LensTransformer ht =
87                  (LensTransformer)transformer;
88              ht.setViewCenter(point);
89          }
90      }
91      
92      /**
93       * called to change the radius of the lens
94       * @param transformer
95       * @param point
96       */
97      private void setViewRadius(MutableTransformer transformer, Point2D point) {
98          if(transformer instanceof LensTransformer) {
99              LensTransformer ht =
100                 (LensTransformer)transformer;
101             double distanceFromCenter = ht.getDistanceFromCenter(point);
102             ht.setViewRadius(distanceFromCenter+edgeOffset);
103         }
104     }
105     
106     /**
107      * called to set up translating the lens center or changing the size
108      * @param transformer
109      * @param point
110      */
111     private void testViewCenter(MutableTransformer transformer, Point2D point) {
112         if(transformer instanceof LensTransformer) {
113             LensTransformer ht =
114                 (LensTransformer)transformer;
115             double distanceFromCenter = ht.getDistanceFromCenter(point);
116             if(distanceFromCenter < 10) {
117                 ht.setViewCenter(point);
118                 dragOnLens = true;
119             } else if(Math.abs(distanceFromCenter - ht.getViewRadius()) < 10) {
120                 edgeOffset = ht.getViewRadius() - distanceFromCenter;
121                 ht.setViewRadius(distanceFromCenter+edgeOffset);
122                 dragOnEdge = true;
123             }
124         }
125     }
126     
127     /**
128      * unset the 'down' point and change the cursoe back to the system
129      * default cursor
130      */
131     public void mouseReleased(MouseEvent e) {
132         super.mouseReleased(e);
133         dragOnLens = false;
134         dragOnEdge = false;
135         edgeOffset = 0;
136     }
137     
138     /**
139      * check the modifiers. If accepted, move or resize the lens according
140      * to the dragging of the mouse pointer
141      * @param e the event
142      */
143     public void mouseDragged(MouseEvent e) {
144         VisualizationViewer vv = (VisualizationViewer)e.getSource();
145         MutableTransformer vt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
146         if(vt instanceof LensTransformer) {
147         	vt = ((LensTransformer)vt).getDelegate();
148         }
149         Point2D p = vt.inverseTransform(e.getPoint());
150         boolean accepted = checkModifiers(e);
151 
152         if(accepted ) {
153             MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
154             vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
155             if(dragOnLens) {
156                 setViewCenter(modelTransformer, p);
157                 setViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
158                 e.consume();
159                 vv.repaint();
160 
161             } else if(dragOnEdge) {
162 
163                 setViewRadius(modelTransformer, p);
164                 setViewRadius(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
165                 e.consume();
166                 vv.repaint();
167                 
168             } else {
169             	
170             	MutableTransformer mt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
171                 Point2D iq = vt.inverseTransform(down);
172                 iq = mt.inverseTransform(iq);
173                 Point2D ip = vt.inverseTransform(e.getPoint());
174                 ip = mt.inverseTransform(ip);
175                 float dx = (float) (ip.getX()-iq.getX());
176                 float dy = (float) (ip.getY()-iq.getY());
177                 
178                 modelTransformer.translate(dx, dy);
179                 down.x = e.getX();
180                 down.y = e.getY();
181             }
182         }
183     }
184 }