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.InputEvent;
16  import java.awt.event.MouseEvent;
17  import java.awt.event.MouseListener;
18  import java.awt.event.MouseMotionListener;
19  import java.awt.geom.Point2D;
20  
21  import javax.swing.JComponent;
22  
23  import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
24  import edu.uci.ics.jung.algorithms.layout.Layout;
25  import edu.uci.ics.jung.visualization.Layer;
26  import edu.uci.ics.jung.visualization.VisualizationViewer;
27  import edu.uci.ics.jung.visualization.picking.PickedState;
28  
29  /** 
30   * AnimatedPickingGraphMousePlugin supports the picking of one Graph
31   * Vertex. When the mouse is released, the graph is translated so that
32   * the picked Vertex is moved to the center of the view. This translateion
33   * is conducted in an animation Thread so that the graph slides to its
34   * new position
35   * 
36   * @author Tom Nelson
37   */
38  public class AnimatedPickingGraphMousePlugin<V, E> extends AbstractGraphMousePlugin
39      implements MouseListener, MouseMotionListener {
40  
41  	/**
42  	 * the picked Vertex
43  	 */
44      protected V vertex;
45      
46      /**
47  	 * create an instance with default modifiers
48  	 * 
49  	 */
50  	public AnimatedPickingGraphMousePlugin() {
51  	    this(InputEvent.BUTTON1_MASK  | InputEvent.CTRL_MASK);
52  	}
53  
54  	/**
55  	 * create an instance, overriding the default modifiers
56  	 * @param selectionModifiers
57  	 */
58      public AnimatedPickingGraphMousePlugin(int selectionModifiers) {
59          super(selectionModifiers);
60          this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
61      }
62  
63  	/**
64  	 * If the event occurs on a Vertex, pick that single Vertex
65  	 * @param e the event
66  	 */
67      @SuppressWarnings("unchecked")
68      public void mousePressed(MouseEvent e) {
69  		if (e.getModifiers() == modifiers) {
70  			VisualizationViewer<V,E> vv = (VisualizationViewer) e.getSource();
71  			GraphElementAccessor<V, E> pickSupport = vv.getPickSupport();
72  			PickedState<V> pickedVertexState = vv.getPickedVertexState();
73              Layout<V,E> layout = vv.getGraphLayout();
74  			if (pickSupport != null && pickedVertexState != null) {
75  				// p is the screen point for the mouse event
76  				Point2D p = e.getPoint();
77  				vertex = pickSupport.getVertex(layout, p.getX(), p.getY());
78  				if (vertex != null) {
79  					if (pickedVertexState.isPicked(vertex) == false) {
80  						pickedVertexState.clear();
81  						pickedVertexState.pick(vertex, true);
82  					}
83  				}
84  			}
85              e.consume();
86  		}
87  	}
88  
89  
90  /**
91   * If a Vertex was picked in the mousePressed event, start a Thread
92   * to animate the translation of the graph so that the picked Vertex
93   * moves to the center of the view
94   * 
95   * @param e the event
96   */
97      @SuppressWarnings("unchecked")
98      public void mouseReleased(MouseEvent e) {
99  		if (e.getModifiers() == modifiers) {
100 			final VisualizationViewer<V,E> vv = (VisualizationViewer<V,E>) e.getSource();
101 			if (vertex != null) {
102 				Layout<V,E> layout = vv.getGraphLayout();
103 				Point2D q = layout.transform(vertex);
104 				Point2D lvc = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(vv.getCenter());
105 				final double dx = (lvc.getX() - q.getX()) / 10;
106 				final double dy = (lvc.getY() - q.getY()) / 10;
107 
108 				Runnable animator = new Runnable() {
109 
110 					public void run() {
111 						for (int i = 0; i < 10; i++) {
112 							vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).translate(dx, dy);
113 							try {
114 								Thread.sleep(100);
115 							} catch (InterruptedException ex) {
116 							}
117 						}
118 					}
119 				};
120 				Thread thread = new Thread(animator);
121 				thread.start();
122 			}
123 		}
124 	}
125      
126     public void mouseClicked(MouseEvent e) {
127     }
128 
129     /**
130      * show a special cursor while the mouse is inside the window
131      */
132     public void mouseEntered(MouseEvent e) {
133         JComponent c = (JComponent)e.getSource();
134         c.setCursor(cursor);
135     }
136 
137     /**
138      * revert to the default cursor when the mouse leaves this window
139      */
140     public void mouseExited(MouseEvent e) {
141         JComponent c = (JComponent)e.getSource();
142         c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
143     }
144 
145     public void mouseMoved(MouseEvent e) {
146     }
147 
148 	public void mouseDragged(MouseEvent arg0) {
149 	}
150 }