# Coverage details for edu.uci.ics.jung.visualization.control.EditingModalGraphMouse

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.Dimension;`
15 |  | `import java.awt.ItemSelectable;`
16 |  | `import java.awt.event.InputEvent;`
17 |  | `import java.awt.event.ItemEvent;`
18 |  | `import java.awt.event.ItemListener;`
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 |  | `import edu.uci.ics.jung.visualization.SettableVertexLocationFunction;`
29 |  | ``
30 |  | ``
31 |  | `/**`
32 |  | `*`
33 |  | `*`
34 |  | `* @author Tom Nelson`
35 |  | `*/`
36 |  | `public class EditingModalGraphMouse extends PluggableGraphMouse`
37 |  | `implements ModalGraphMouse, ItemSelectable {`
38 |  | ``
39 |  | `/**`
40 |  | `* used by the scaling plugins for zoom in`
41 |  | `*/`
42 |  | `protected float in;`
43 |  | `/**`
44 |  | `* used by the scaling plugins for zoom out`
45 |  | `*/`
46 |  | `protected float out;`
47 |  | `/**`
48 |  | `* a listener for mode changes`
49 |  | `*/`
50 |  | `protected ItemListener modeListener;`
51 |  | `/**`
52 |  | `* a JComboBox control available to set the mode`
53 |  | `*/`
54 |  | `protected JComboBox modeBox;`
55 |  | `/**`
56 |  | `* a menu available to set the mode`
57 |  | `*/`
58 |  | `protected JMenu modeMenu;`
59 |  | `/**`
60 |  | `* the current mode`
61 |  | `*/`
62 |  | `protected Mode mode;`
63 |  | `/**`
64 |  | `* listeners for mode changes`
65 |  | `*/`
66 | 0 | `protected EventListenerList listenerList = new EventListenerList();`
67 |  | ``
68 |  | `protected GraphMousePlugin pickingPlugin;`
69 |  | `protected GraphMousePlugin translatingPlugin;`
70 |  | `protected GraphMousePlugin animatedPickingPlugin;`
71 |  | `protected GraphMousePlugin scalingPlugin;`
72 |  | `protected GraphMousePlugin rotatingPlugin;`
73 |  | `protected GraphMousePlugin shearingPlugin;`
74 |  | `protected GraphMousePlugin editingPlugin;`
75 |  | ``
76 |  | `/**`
77 |  | `* create an instance with default values`
78 |  | `*`
79 |  | `*/`
80 |  | `public EditingModalGraphMouse() {`
81 | 0 | `this(1.1f, 1/1.1f);`
82 | 0 | `}`
83 |  | ``
84 |  | `/**`
85 |  | `* create an instance with passed values`
86 |  | `* @param in override value for scale in`
87 |  | `* @param out override value for scale out`
88 |  | `*/`
89 | 0 | `public EditingModalGraphMouse(float in, float out) {`
90 | 0 | `this.in = in;`
91 | 0 | `this.out = out;`
92 | 0 | `loadPlugins();`
93 | 0 | `}`
94 |  | ``
95 |  | `/**`
96 |  | `* create the plugins, and load the plugins for TRANSFORMING mode`
97 |  | `*`
98 |  | `*/`
99 |  | `protected void loadPlugins() {`
100 | 0 | `pickingPlugin = new PickingGraphMousePlugin();`
101 | 0 | `animatedPickingPlugin = new AnimatedPickingGraphMousePlugin();`
102 | 0 | `translatingPlugin = new TranslatingGraphMousePlugin(InputEvent.BUTTON1_MASK);`
103 | 0 | `scalingPlugin = new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, in, out);`
104 | 0 | `rotatingPlugin = new RotatingGraphMousePlugin();`
105 | 0 | `shearingPlugin = new ShearingGraphMousePlugin();`
106 | 0 | `editingPlugin = new EditingGraphMousePlugin();`
107 |  | ``
108 | 0 | `add(scalingPlugin);`
109 | 0 | `setMode(Mode.EDITING);`
110 | 0 | `}`
111 |  | `public void setVertexLocations(SettableVertexLocationFunction vertexLocations) {`
112 | 0 | `((EditingGraphMousePlugin)editingPlugin).setVertexLocations(vertexLocations);`
113 | 0 | `}`
114 |  | ``
115 |  | `/**`
116 |  | `* setter for the Mode.`
117 |  | `*/`
118 |  | `public void setMode(Mode mode) {`
119 | 0 | `if(this.mode != mode) {`
120 | 0 | `fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,`
121 |  | `this.mode, ItemEvent.DESELECTED));`
122 | 0 | `this.mode = mode;`
123 | 0 | `if(mode == Mode.TRANSFORMING) {`
124 | 0 | `setTransformingMode();`
125 | 0 | `} else if(mode == Mode.PICKING) {`
126 | 0 | `setPickingMode();`
127 | 0 | `} else if(mode == Mode.EDITING) {`
128 | 0 | `setEditingMode();`
129 |  | `}`
130 | 0 | `if(modeBox != null) {`
131 | 0 | `modeBox.setSelectedItem(mode);`
132 |  | `}`
133 | 0 | `fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));`
134 |  | `}`
135 | 0 | `}`
136 |  | `/* (non-Javadoc)`
137 |  | `* @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setPickingMode()`
138 |  | `*/`
139 |  | `protected void setPickingMode() {`
140 | 0 | `remove(translatingPlugin);`
141 | 0 | `remove(rotatingPlugin);`
142 | 0 | `remove(shearingPlugin);`
143 | 0 | `remove(editingPlugin);`
144 | 0 | `add(pickingPlugin);`
145 | 0 | `add(animatedPickingPlugin);`
146 | 0 | `}`
147 |  | ``
148 |  | `/* (non-Javadoc)`
149 |  | `* @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setTransformingMode()`
150 |  | `*/`
151 |  | `protected void setTransformingMode() {`
152 | 0 | `remove(pickingPlugin);`
153 | 0 | `remove(animatedPickingPlugin);`
154 | 0 | `remove(editingPlugin);`
155 | 0 | `add(translatingPlugin);`
156 | 0 | `add(rotatingPlugin);`
157 | 0 | `add(shearingPlugin);`
158 | 0 | `}`
159 |  | ``
160 |  | `protected void setEditingMode() {`
161 | 0 | `remove(pickingPlugin);`
162 | 0 | `remove(animatedPickingPlugin);`
163 | 0 | `remove(translatingPlugin);`
164 | 0 | `remove(rotatingPlugin);`
165 | 0 | `remove(shearingPlugin);`
166 | 0 | `add(editingPlugin);`
167 | 0 | `}`
168 |  | ``
169 |  | `/**`
170 |  | `* @param zoomAtMouse The zoomAtMouse to set.`
171 |  | `*/`
172 |  | `public void setZoomAtMouse(boolean zoomAtMouse) {`
173 | 0 | `((ScalingGraphMousePlugin) scalingPlugin).setZoomAtMouse(zoomAtMouse);`
174 | 0 | `}`
175 |  | ``
176 |  | `/**`
177 |  | `* listener to set the mode from an external event source`
178 |  | `*/`
179 |  | `class ModeListener implements ItemListener {`
180 |  | `public void itemStateChanged(ItemEvent e) {`
181 |  | `setMode((Mode) e.getItem());`
182 |  | `}`
183 |  | `}`
184 |  | ``
185 |  | `/* (non-Javadoc)`
186 |  | `* @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#getModeListener()`
187 |  | `*/`
188 |  | `public ItemListener getModeListener() {`
189 | 0 | `if (modeListener == null) {`
190 | 0 | `modeListener = new ModeListener();`
191 |  | `}`
192 | 0 | `return modeListener;`
193 |  | `}`
194 |  | ``
195 |  | `/**`
196 |  | `* @return Returns the modeBox.`
197 |  | `*/`
198 |  | `public JComboBox getModeComboBox() {`
199 | 0 | `if(modeBox == null) {`
200 | 0 | `modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING, Mode.EDITING});`
201 | 0 | `modeBox.addItemListener(getModeListener());`
202 |  | `}`
203 | 0 | `modeBox.setSelectedItem(mode);`
204 | 0 | `return modeBox;`
205 |  | `}`
206 |  | ``
207 |  | `/**`
208 |  | `* create (if necessary) and return a menu that will change`
209 |  | `* the mode`
210 |  | `* @return the menu`
211 |  | `*/`
212 |  | `public JMenu getModeMenu() {`
213 | 0 | `if(modeMenu == null) {`
214 | 0 | `modeMenu = new JMenu();// {`
215 | 0 | `Icon icon = BasicIconFactory.getMenuArrowIcon();`
216 | 0 | `modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon());`
217 | 0 | `modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10,`
218 |  | `icon.getIconHeight()+10));`
219 |  | ``
220 | 0 | `final JRadioButtonMenuItem transformingButton =`
221 |  | `new JRadioButtonMenuItem(Mode.TRANSFORMING.toString());`
222 | 0 | `transformingButton.addItemListener(new ItemListener() {`
223 |  | `public void itemStateChanged(ItemEvent e) {`
224 |  | `if(e.getStateChange() == ItemEvent.SELECTED) {`
225 |  | `setMode(Mode.TRANSFORMING);`
226 |  | `}`
227 |  | `}});`
228 |  | ``
229 | 0 | `final JRadioButtonMenuItem pickingButton =`
230 |  | `new JRadioButtonMenuItem(Mode.PICKING.toString());`
231 | 0 | `pickingButton.addItemListener(new ItemListener() {`
232 |  | `public void itemStateChanged(ItemEvent e) {`
233 |  | `if(e.getStateChange() == ItemEvent.SELECTED) {`
234 |  | `setMode(Mode.PICKING);`
235 |  | `}`
236 |  | `}});`
237 |  | ``
238 | 0 | `final JRadioButtonMenuItem editingButton =`
239 |  | `new JRadioButtonMenuItem(Mode.EDITING.toString());`
240 | 0 | `editingButton.addItemListener(new ItemListener() {`
241 |  | `public void itemStateChanged(ItemEvent e) {`
242 |  | `if(e.getStateChange() == ItemEvent.SELECTED) {`
243 |  | `setMode(Mode.EDITING);`
244 |  | `}`
245 |  | `}});`
246 |  | ``
247 | 0 | `ButtonGroup radio = new ButtonGroup();`
248 | 0 | `radio.add(transformingButton);`
249 | 0 | `radio.add(pickingButton);`
250 | 0 | `radio.add(editingButton);`
251 | 0 | `transformingButton.setSelected(true);`
252 | 0 | `modeMenu.add(transformingButton);`
253 | 0 | `modeMenu.add(pickingButton);`
254 | 0 | `modeMenu.add(editingButton);`
255 | 0 | `modeMenu.setToolTipText("Menu for setting Mouse Mode");`
256 | 0 | `addItemListener(new ItemListener() {`
257 |  | `public void itemStateChanged(ItemEvent e) {`
258 |  | `if(e.getStateChange() == ItemEvent.SELECTED) {`
259 |  | `if(e.getItem() == Mode.TRANSFORMING) {`
260 |  | `transformingButton.setSelected(true);`
261 |  | `} else if(e.getItem() == Mode.PICKING) {`
262 |  | `pickingButton.setSelected(true);`
263 |  | `} else if(e.getItem() == Mode.EDITING) {`
264 |  | `editingButton.setSelected(true);`
265 |  | `}`
266 |  | `}`
267 |  | `}});`
268 |  | `}`
269 | 0 | `return modeMenu;`
270 |  | `}`
271 |  | ``
272 |  | `/**`
273 |  | `* add a listener for mode changes`
274 |  | `*/`
275 |  | `public void addItemListener(ItemListener aListener) {`
276 | 0 | `listenerList.add(ItemListener.class,aListener);`
277 | 0 | `}`
278 |  | ``
279 |  | `/**`
280 |  | `* remove a listener for mode changes`
281 |  | `*/`
282 |  | `public void removeItemListener(ItemListener aListener) {`
283 | 0 | `listenerList.remove(ItemListener.class,aListener);`
284 | 0 | `}`
285 |  | ``
286 |  | `/**`
287 |  | `* Returns an array of all the <code>ItemListener</code>s added`
288 |  | `* to this JComboBox with addItemListener().`
289 |  | `*`
290 |  | `* @return all of the <code>ItemListener</code>s added or an empty`
291 |  | `* array if no listeners have been added`
292 |  | `* @since 1.4`
293 |  | `*/`
294 |  | `public ItemListener[] getItemListeners() {`
295 | 0 | `return (ItemListener[])listenerList.getListeners(ItemListener.class);`
296 |  | `}`
297 |  | ``
298 |  | `public Object[] getSelectedObjects() {`
299 | 0 | `if ( mode == null )`
300 | 0 | `return new Object[0];`
301 |  | `else {`
302 | 0 | `Object result[] = new Object[1];`
303 | 0 | `result[0] = mode;`
304 | 0 | `return result;`
305 |  | `}`
306 |  | `}`
307 |  | ``
308 |  | `/**`
309 |  | `* Notifies all listeners that have registered interest for`
310 |  | `* notification on this event type.`
311 |  | `* @param e the event of interest`
312 |  | `*`
313 |  | `* @see EventListenerList`
314 |  | `*/`
315 |  | `protected void fireItemStateChanged(ItemEvent e) {`
316 |  | `// Guaranteed to return a non-null array`
317 | 0 | `Object[] listeners = listenerList.getListenerList();`
318 |  | `// Process the listeners last to first, notifying`
319 |  | `// those that are interested in this event`
320 | 0 | `for ( int i = listeners.length-2; i>=0; i-=2 ) {`
321 | 0 | `if ( listeners[i]==ItemListener.class ) {`
322 | 0 | `((ItemListener)listeners[i+1]).itemStateChanged(e);`
323 |  | `}`
324 |  | `}`
325 | 0 | `}`
326 |  | `}`

this report was generated by version 1.0.5 of jcoverage. visit www.jcoverage.com for updates. | copyright © 2003, jcoverage ltd. all rights reserved. Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.