Line | Hits | Source |
---|---|---|
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; | |
13 | ||
14 | import java.awt.event.MouseAdapter; | |
15 | import java.awt.event.MouseEvent; | |
16 | import java.awt.event.MouseWheelEvent; | |
17 | import java.awt.geom.Point2D; | |
18 | ||
19 | import javax.swing.SwingUtilities; | |
20 | ||
21 | import edu.uci.ics.jung.graph.Edge; | |
22 | import edu.uci.ics.jung.graph.Vertex; | |
23 | ||
24 | /** | |
25 | * SimpleGraphMouse is the original GraphMouse class that was | |
26 | * nested in VisualizationViewer and installed as a listener | |
27 | * for mouse events and mouse motion events. Users can restore | |
28 | * previous capability by creating an instance of this class | |
29 | * and using VisualizationViewer.setGraphMouse() to re-install it. | |
30 | * Changes in this code from the nested version are mainly those | |
31 | * necessary to move it outside the VisualizationViewer class, | |
32 | * and to properly handle the transform values of the VisualizationViewer | |
33 | * (the zoom and pan) | |
34 | * | |
35 | * | |
36 | */ | |
37 | public class SimpleGraphMouse extends MouseAdapter | |
38 | implements VisualizationViewer.GraphMouse { | |
39 | ||
40 | /** | |
41 | * the VisualizationViewer who's mouse events call these | |
42 | * methods. | |
43 | */ | |
44 | protected VisualizationViewer vv; | |
45 | ||
46 | /** | |
47 | * how far the mouse point x is from the vertex center x | |
48 | */ | |
49 | protected float offsetx; | |
50 | ||
51 | /** | |
52 | * how far the mouse point y is from the vertex center y | |
53 | */ | |
54 | protected float offsety; | |
55 | ||
56 | /** | |
57 | * the vertex to drag with a mouseDragged operation | |
58 | */ | |
59 | protected Vertex vertexToDrag; | |
60 | ||
61 | /** | |
62 | * create an instance for the passed VisualizationViewer | |
63 | * @param vv | |
64 | */ | |
65 | 0 | public SimpleGraphMouse(VisualizationViewer vv) { |
66 | 0 | this.vv = vv; |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * Uses the layout class to attempt to pick a vertex in | |
71 | * the graph. | |
72 | */ | |
73 | public void mousePressed(MouseEvent e) { | |
74 | 0 | PickSupport pickSupport = vv.getPickSupport(); |
75 | 0 | PickedState pickedState = vv.getPickedState(); |
76 | ||
77 | 0 | if(pickSupport != null && pickedState != null) { |
78 | 0 | Layout layout = vv.getGraphLayout(); |
79 | 0 | if(SwingUtilities.isLeftMouseButton(e)) { |
80 | // p is the screen point for the mouse event | |
81 | 0 | Point2D p = e.getPoint(); |
82 | // transform it to graph coordinates: | |
83 | 0 | Point2D gp = vv.inverseTransform(p); |
84 | ||
85 | 0 | Vertex v = pickSupport.getVertex(gp.getX(), gp.getY()); |
86 | 0 | if(v != null) { |
87 | 0 | pickedState.clearPickedVertices(); |
88 | 0 | pickedState.pick(v, true); |
89 | 0 | vertexToDrag = v; |
90 | // layout.getLocation applies the layout transformer so | |
91 | // q is transformed by the layout transformer only | |
92 | 0 | Point2D q = layout.getLocation(v); |
93 | // i need to put it back in the graph coordinates: | |
94 | 0 | Point2D gq = vv.getLayoutTransformer().inverseTransform(q); |
95 | 0 | offsetx = (float) (gp.getX()-gq.getX()); |
96 | 0 | offsety = (float) (gp.getY()-gq.getY()); |
97 | } else { | |
98 | 0 | Edge edge = pickSupport.getEdge(gp.getX(), gp.getY()); |
99 | 0 | if(edge != null) { |
100 | 0 | pickedState.clearPickedEdges(); |
101 | 0 | pickedState.pick(edge, true); |
102 | } | |
103 | } | |
104 | 0 | vv.repaint(); |
105 | ||
106 | 0 | } else if(SwingUtilities.isMiddleMouseButton(e)) { |
107 | 0 | Point2D p = vv.inverseTransform(e.getPoint()); |
108 | 0 | Vertex v = pickSupport.getVertex(p.getX(), p.getY()); |
109 | 0 | if(v != null) { |
110 | 0 | boolean wasThere = pickedState.pick(v, !pickedState.isPicked(v)); |
111 | 0 | if(wasThere) { |
112 | 0 | vertexToDrag = null; |
113 | } else { | |
114 | 0 | vertexToDrag = v; |
115 | 0 | Point2D point = vv.getLayoutTransformer().inverseTransform(layout.getLocation(v)); |
116 | 0 | offsetx = (float) (p.getX()-point.getX()); |
117 | 0 | offsety = (float) (p.getY()-point.getY()); |
118 | // } | |
119 | } | |
120 | } else { | |
121 | 0 | Edge edge = pickSupport.getEdge(p.getX(), p.getY()); |
122 | 0 | if(edge != null) { |
123 | 0 | pickedState.pick(edge, !pickedState.isPicked(edge)); |
124 | } | |
125 | } | |
126 | 0 | vv.repaint(); |
127 | } | |
128 | } | |
129 | 0 | } |
130 | ||
131 | ||
132 | /** | |
133 | * clean up after the pick of a Vertex or Edge | |
134 | */ | |
135 | public void mouseReleased(MouseEvent e) { | |
136 | 0 | vertexToDrag = null; |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * if a Vertex was picked in the mousePressed method, use the | |
141 | * layout class to force that Vertex to move, following the | |
142 | * motion of the mouse. | |
143 | */ | |
144 | public void mouseDragged(MouseEvent e) { | |
145 | 0 | if (vertexToDrag != null) { |
146 | 0 | Point2D p = vv.inverseTransform(e.getPoint()); |
147 | 0 | Layout layout = vv.getGraphLayout(); |
148 | 0 | layout.forceMove(vertexToDrag, p.getX()-offsetx, p.getY()-offsety); |
149 | 0 | vv.repaint(); |
150 | } | |
151 | 0 | } |
152 | ||
153 | /** | |
154 | * no-op here | |
155 | */ | |
156 | public void mouseMoved(MouseEvent e) { | |
157 | 0 | return; |
158 | } | |
159 | ||
160 | /** | |
161 | * no-op here | |
162 | */ | |
163 | public void mouseWheelMoved(MouseWheelEvent e) { | |
164 | 0 | return; |
165 | } | |
166 | } | |
167 |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |