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.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.VisualizationViewer; | |
21 | import edu.uci.ics.jung.visualization.transform.LensTransformer; | |
22 | import edu.uci.ics.jung.visualization.transform.MutableTransformer; | |
23 | ||
24 | /** | |
25 | * Extends TranslatingGraphMousePlugin and adds the capability | |
26 | * to drag and resize the viewing | |
27 | * lens in the graph view. Mouse1 in the center moves the lens, | |
28 | * mouse1 on the edge resizes the lens. The default mouse button and | |
29 | * modifiers can be overridden in the constructor. | |
30 | * | |
31 | * | |
32 | * @author Tom Nelson | |
33 | */ | |
34 | public class LensTranslatingGraphMousePlugin extends TranslatingGraphMousePlugin | |
35 | implements MouseListener, MouseMotionListener { | |
36 | ||
37 | protected boolean dragOnLens; | |
38 | protected boolean dragOnEdge; | |
39 | protected double edgeOffset; | |
40 | /** | |
41 | * create an instance with default modifiers | |
42 | */ | |
43 | public LensTranslatingGraphMousePlugin() { | |
44 | 0 | this(MouseEvent.BUTTON1_MASK); |
45 | 0 | } |
46 | ||
47 | /** | |
48 | * create an instance with passed modifer value | |
49 | * @param modifiers the mouse event modifier to activate this function | |
50 | */ | |
51 | public LensTranslatingGraphMousePlugin(int modifiers) { | |
52 | 0 | super(modifiers); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Check the event modifiers. Set the 'down' point for later | |
57 | * use. If this event satisfies the modifiers, change the cursor | |
58 | * to the system 'move cursor' | |
59 | * @param e the event | |
60 | */ | |
61 | public void mousePressed(MouseEvent e) { | |
62 | 0 | VisualizationViewer vv = (VisualizationViewer)e.getSource(); |
63 | 0 | boolean accepted = checkModifiers(e); |
64 | 0 | if(accepted) { |
65 | 0 | vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); |
66 | 0 | testViewCenter(vv.getLayoutTransformer(), e.getPoint()); |
67 | 0 | testViewCenter(vv.getViewTransformer(), e.getPoint()); |
68 | 0 | vv.repaint(); |
69 | } | |
70 | 0 | super.mousePressed(e); |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * called to change the location of the lens | |
75 | * @param transformer | |
76 | * @param point | |
77 | */ | |
78 | private void setViewCenter(MutableTransformer transformer, Point2D point) { | |
79 | 0 | if(transformer instanceof LensTransformer) { |
80 | 0 | LensTransformer ht = |
81 | (LensTransformer)transformer; | |
82 | 0 | ht.setViewCenter(point); |
83 | } | |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * called to change the radius of the lens | |
88 | * @param transformer | |
89 | * @param point | |
90 | */ | |
91 | private void setViewRadius(MutableTransformer transformer, Point2D point) { | |
92 | 0 | if(transformer instanceof LensTransformer) { |
93 | 0 | LensTransformer ht = |
94 | (LensTransformer)transformer; | |
95 | 0 | double distanceFromCenter = ht.getDistanceFromCenter(point); |
96 | 0 | ht.setViewRadius(distanceFromCenter+edgeOffset); |
97 | } | |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * called to set up translating the lens center or changing the size | |
102 | * @param transformer | |
103 | * @param point | |
104 | */ | |
105 | private void testViewCenter(MutableTransformer transformer, Point2D point) { | |
106 | 0 | if(transformer instanceof LensTransformer) { |
107 | 0 | LensTransformer ht = |
108 | (LensTransformer)transformer; | |
109 | 0 | double distanceFromCenter = ht.getDistanceFromCenter(point); |
110 | 0 | if(distanceFromCenter < 10) { |
111 | 0 | ht.setViewCenter(point); |
112 | 0 | dragOnLens = true; |
113 | 0 | } else if(Math.abs(distanceFromCenter - ht.getViewRadius()) < 10) { |
114 | 0 | edgeOffset = ht.getViewRadius() - distanceFromCenter; |
115 | 0 | ht.setViewRadius(distanceFromCenter+edgeOffset); |
116 | 0 | dragOnEdge = true; |
117 | } | |
118 | } | |
119 | 0 | } |
120 | ||
121 | /** | |
122 | * unset the 'down' point and change the cursoe back to the system | |
123 | * default cursor | |
124 | */ | |
125 | public void mouseReleased(MouseEvent e) { | |
126 | 0 | super.mouseReleased(e); |
127 | 0 | dragOnLens = false; |
128 | 0 | dragOnEdge = false; |
129 | 0 | edgeOffset = 0; |
130 | 0 | } |
131 | ||
132 | /** | |
133 | * check the modifiers. If accepted, move or resize the lens according | |
134 | * to the dragging of the mouse pointer | |
135 | * @param e the event | |
136 | */ | |
137 | public void mouseDragged(MouseEvent e) { | |
138 | 0 | VisualizationViewer vv = (VisualizationViewer)e.getSource(); |
139 | 0 | boolean accepted = checkModifiers(e); |
140 | 0 | if(accepted ) { |
141 | 0 | MutableTransformer modelTransformer = vv.getLayoutTransformer(); |
142 | 0 | vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); |
143 | ||
144 | 0 | if(dragOnLens) { |
145 | ||
146 | 0 | setViewCenter(modelTransformer, vv.inverseViewTransform(e.getPoint())); |
147 | 0 | setViewCenter(vv.getViewTransformer(), e.getPoint()); |
148 | 0 | e.consume(); |
149 | 0 | vv.repaint(); |
150 | ||
151 | 0 | } else if(dragOnEdge) { |
152 | ||
153 | 0 | setViewRadius(modelTransformer, e.getPoint()); |
154 | 0 | setViewRadius(vv.getViewTransformer(), e.getPoint()); |
155 | 0 | e.consume(); |
156 | 0 | vv.repaint(); |
157 | ||
158 | } else { | |
159 | 0 | super.mouseDragged(e); |
160 | } | |
161 | } | |
162 | 0 | } |
163 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |