1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.visualization.control;
13
14 import java.awt.Dimension;
15 import java.awt.ItemSelectable;
16 import java.awt.event.ItemEvent;
17 import java.awt.event.ItemListener;
18 import java.awt.event.KeyListener;
19
20 import javax.swing.ButtonGroup;
21 import javax.swing.Icon;
22 import javax.swing.JComboBox;
23 import javax.swing.JMenu;
24 import javax.swing.JRadioButtonMenuItem;
25 import javax.swing.event.EventListenerList;
26 import javax.swing.plaf.basic.BasicIconFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class AbstractModalGraphMouse extends PluggableGraphMouse
56 implements ModalGraphMouse, ItemSelectable {
57
58
59
60
61 protected float in;
62
63
64
65 protected float out;
66
67
68
69 protected ItemListener modeListener;
70
71
72
73 protected JComboBox modeBox;
74
75
76
77 protected JMenu modeMenu;
78
79
80
81 protected Mode mode;
82
83
84
85 protected EventListenerList listenerList = new EventListenerList();
86
87 protected GraphMousePlugin pickingPlugin;
88 protected GraphMousePlugin translatingPlugin;
89 protected GraphMousePlugin animatedPickingPlugin;
90 protected GraphMousePlugin scalingPlugin;
91 protected GraphMousePlugin rotatingPlugin;
92 protected GraphMousePlugin shearingPlugin;
93 protected KeyListener modeKeyListener;
94
95
96 protected AbstractModalGraphMouse(float in, float out) {
97 this.in = in;
98 this.out = out;
99 }
100
101
102
103
104
105 protected abstract void loadPlugins();
106
107
108
109
110 public void setMode(Mode mode) {
111 if(this.mode != mode) {
112 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
113 this.mode, ItemEvent.DESELECTED));
114 this.mode = mode;
115 if(mode == Mode.TRANSFORMING) {
116 setTransformingMode();
117 } else if(mode == Mode.PICKING) {
118 setPickingMode();
119 }
120 if(modeBox != null) {
121 modeBox.setSelectedItem(mode);
122 }
123 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
124 }
125 }
126
127
128
129 protected void setPickingMode() {
130 remove(translatingPlugin);
131 remove(rotatingPlugin);
132 remove(shearingPlugin);
133 add(pickingPlugin);
134 add(animatedPickingPlugin);
135 }
136
137
138
139
140 protected void setTransformingMode() {
141 remove(pickingPlugin);
142 remove(animatedPickingPlugin);
143 add(translatingPlugin);
144 add(rotatingPlugin);
145 add(shearingPlugin);
146 }
147
148
149
150
151 public void setZoomAtMouse(boolean zoomAtMouse) {
152 ((ScalingGraphMousePlugin) scalingPlugin).setZoomAtMouse(zoomAtMouse);
153 }
154
155
156
157
158 class ModeListener implements ItemListener {
159 public void itemStateChanged(ItemEvent e) {
160 setMode((Mode) e.getItem());
161 }
162 }
163
164
165
166
167 public ItemListener getModeListener() {
168 if (modeListener == null) {
169 modeListener = new ModeListener();
170 }
171 return modeListener;
172 }
173
174
175
176
177 public KeyListener getModeKeyListener() {
178 return modeKeyListener;
179 }
180
181
182
183
184 public void setModeKeyListener(KeyListener modeKeyListener) {
185 this.modeKeyListener = modeKeyListener;
186 }
187
188
189
190
191 public JComboBox getModeComboBox() {
192 if(modeBox == null) {
193 modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING});
194 modeBox.addItemListener(getModeListener());
195 }
196 modeBox.setSelectedItem(mode);
197 return modeBox;
198 }
199
200
201
202
203
204
205 public JMenu getModeMenu() {
206 if(modeMenu == null) {
207 modeMenu = new JMenu();
208 Icon icon = BasicIconFactory.getMenuArrowIcon();
209 modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon());
210 modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10,
211 icon.getIconHeight()+10));
212
213 final JRadioButtonMenuItem transformingButton =
214 new JRadioButtonMenuItem(Mode.TRANSFORMING.toString());
215 transformingButton.addItemListener(new ItemListener() {
216 public void itemStateChanged(ItemEvent e) {
217 if(e.getStateChange() == ItemEvent.SELECTED) {
218 setMode(Mode.TRANSFORMING);
219 }
220 }});
221
222 final JRadioButtonMenuItem pickingButton =
223 new JRadioButtonMenuItem(Mode.PICKING.toString());
224 pickingButton.addItemListener(new ItemListener() {
225 public void itemStateChanged(ItemEvent e) {
226 if(e.getStateChange() == ItemEvent.SELECTED) {
227 setMode(Mode.PICKING);
228 }
229 }});
230 ButtonGroup radio = new ButtonGroup();
231 radio.add(transformingButton);
232 radio.add(pickingButton);
233 transformingButton.setSelected(true);
234 modeMenu.add(transformingButton);
235 modeMenu.add(pickingButton);
236 modeMenu.setToolTipText("Menu for setting Mouse Mode");
237 addItemListener(new ItemListener() {
238 public void itemStateChanged(ItemEvent e) {
239 if(e.getStateChange() == ItemEvent.SELECTED) {
240 if(e.getItem() == Mode.TRANSFORMING) {
241 transformingButton.setSelected(true);
242 } else if(e.getItem() == Mode.PICKING) {
243 pickingButton.setSelected(true);
244 }
245 }
246 }});
247 }
248 return modeMenu;
249 }
250
251
252
253
254 public void addItemListener(ItemListener aListener) {
255 listenerList.add(ItemListener.class,aListener);
256 }
257
258
259
260
261 public void removeItemListener(ItemListener aListener) {
262 listenerList.remove(ItemListener.class,aListener);
263 }
264
265
266
267
268
269
270
271
272
273 public ItemListener[] getItemListeners() {
274 return listenerList.getListeners(ItemListener.class);
275 }
276
277 public Object[] getSelectedObjects() {
278 if ( mode == null )
279 return new Object[0];
280 else {
281 Object result[] = new Object[1];
282 result[0] = mode;
283 return result;
284 }
285 }
286
287
288
289
290
291
292
293
294 protected void fireItemStateChanged(ItemEvent e) {
295
296 Object[] listeners = listenerList.getListenerList();
297
298
299 for ( int i = listeners.length-2; i>=0; i-=2 ) {
300 if ( listeners[i]==ItemListener.class ) {
301 ((ItemListener)listeners[i+1]).itemStateChanged(e);
302 }
303 }
304 }
305 }