1
2
3
4
5
6
7
8
9
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
31
32
33
34
35
36
37
38 public class AnimatedPickingGraphMousePlugin<V, E> extends AbstractGraphMousePlugin
39 implements MouseListener, MouseMotionListener {
40
41
42
43
44 protected V vertex;
45
46
47
48
49
50 public AnimatedPickingGraphMousePlugin() {
51 this(InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK);
52 }
53
54
55
56
57
58 public AnimatedPickingGraphMousePlugin(int selectionModifiers) {
59 super(selectionModifiers);
60 this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
61 }
62
63
64
65
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
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
92
93
94
95
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
131
132 public void mouseEntered(MouseEvent e) {
133 JComponent c = (JComponent)e.getSource();
134 c.setCursor(cursor);
135 }
136
137
138
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 }