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