| 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.ItemEvent; | |
| 17 | import java.awt.event.ItemListener; | |
| 18 | import java.awt.event.MouseEvent; | |
| 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 | * AbstractModalGraphMouse is a PluggableGraphMouse class that | |
| 32 | * manages a collection of plugins for picking and | |
| 33 | * transforming the graph. Additionally, it carries the notion | |
| 34 | * of a Mode: Picking or Translating. Switching between modes | |
| 35 | * allows for a more natural choice of mouse modifiers to | |
| 36 | * be used for the various plugins. The default modifiers are | |
| 37 | * intended to mimick those of mainstream software applications | |
| 38 | * in order to be intuitive to users. | |
| 39 | * | |
| 40 | * To change between modes, two different controls are offered, | |
| 41 | * a combo box and a menu system. These controls are lazily created | |
| 42 | * in their respective 'getter' methods so they don't impact | |
| 43 | * code that does not intend to use them. | |
| 44 | * The menu control can be placed in an unused corner of the | |
| 45 | * GraphZoomScrollPane, which is a common location for mouse | |
| 46 | * mode selection menus in mainstream applications. | |
| 47 | * | |
| 48 | * Users must implement the loadPlugins() method to create and | |
| 49 | * install the GraphMousePlugins. The order of the plugins is | |
| 50 | * important, as they are evaluated against the mask parameters | |
| 51 | * in the order that they are added. | |
| 52 | * | |
| 53 | * @author Tom Nelson | |
| 54 | */ | |
| 55 | 0 | public abstract class AbstractModalGraphMouse extends PluggableGraphMouse |
| 56 | implements ModalGraphMouse, ItemSelectable { | |
| 57 | ||
| 58 | /** | |
| 59 | * used by the scaling plugins for zoom in | |
| 60 | */ | |
| 61 | protected float in; | |
| 62 | /** | |
| 63 | * used by the scaling plugins for zoom out | |
| 64 | */ | |
| 65 | protected float out; | |
| 66 | /** | |
| 67 | * a listener for mode changes | |
| 68 | */ | |
| 69 | protected ItemListener modeListener; | |
| 70 | /** | |
| 71 | * a JComboBox control available to set the mode | |
| 72 | */ | |
| 73 | protected JComboBox modeBox; | |
| 74 | /** | |
| 75 | * a menu available to set the mode | |
| 76 | */ | |
| 77 | protected JMenu modeMenu; | |
| 78 | /** | |
| 79 | * the current mode | |
| 80 | */ | |
| 81 | protected Mode mode; | |
| 82 | /** | |
| 83 | * listeners for mode changes | |
| 84 | */ | |
| 85 | 0 | protected EventListenerList listenerList = new EventListenerList(); |
| 86 | ||
| 87 | /* | |
| 88 | * default mask override for Apple | |
| 89 | */ | |
| 90 | 0 | private static int scalingMask = MouseEvent.SHIFT_MASK; |
| 91 | static { | |
| 92 | 0 | if(System.getProperty("os.name").startsWith("Mac")) { |
| 93 | 0 | scalingMask = MouseEvent.META_MASK; |
| 94 | } | |
| 95 | 0 | } |
| 96 | ||
| 97 | protected GraphMousePlugin pickingPlugin; | |
| 98 | protected GraphMousePlugin translatingPlugin; | |
| 99 | protected GraphMousePlugin animatedPickingPlugin; | |
| 100 | protected GraphMousePlugin scalingPlugin; | |
| 101 | protected GraphMousePlugin rotatingPlugin; | |
| 102 | protected GraphMousePlugin shearingPlugin; | |
| 103 | ||
| 104 | /** | |
| 105 | * create the plugins, and load the plugins for TRANSFORMING mode | |
| 106 | * | |
| 107 | */ | |
| 108 | protected abstract void loadPlugins(); | |
| 109 | ||
| 110 | /** | |
| 111 | * setter for the Mode. | |
| 112 | */ | |
| 113 | public void setMode(Mode mode) { | |
| 114 | 0 | if(this.mode != mode) { |
| 115 | 0 | fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, |
| 116 | this.mode, ItemEvent.DESELECTED)); | |
| 117 | 0 | this.mode = mode; |
| 118 | 0 | if(mode == Mode.TRANSFORMING) { |
| 119 | 0 | setTransformingMode(); |
| 120 | 0 | } else if(mode == Mode.PICKING) { |
| 121 | 0 | setPickingMode(); |
| 122 | } | |
| 123 | 0 | if(modeBox != null) { |
| 124 | 0 | modeBox.setSelectedItem(mode); |
| 125 | } | |
| 126 | 0 | fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED)); |
| 127 | } | |
| 128 | 0 | } |
| 129 | /* (non-Javadoc) | |
| 130 | * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setPickingMode() | |
| 131 | */ | |
| 132 | protected void setPickingMode() { | |
| 133 | 0 | remove(translatingPlugin); |
| 134 | 0 | remove(rotatingPlugin); |
| 135 | 0 | remove(shearingPlugin); |
| 136 | 0 | add(pickingPlugin); |
| 137 | 0 | add(animatedPickingPlugin); |
| 138 | 0 | } |
| 139 | ||
| 140 | /* (non-Javadoc) | |
| 141 | * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setTransformingMode() | |
| 142 | */ | |
| 143 | protected void setTransformingMode() { | |
| 144 | 0 | remove(pickingPlugin); |
| 145 | 0 | remove(animatedPickingPlugin); |
| 146 | 0 | add(translatingPlugin); |
| 147 | 0 | add(rotatingPlugin); |
| 148 | 0 | add(shearingPlugin); |
| 149 | 0 | } |
| 150 | ||
| 151 | /** | |
| 152 | * @param zoomAtMouse The zoomAtMouse to set. | |
| 153 | */ | |
| 154 | public void setZoomAtMouse(boolean zoomAtMouse) { | |
| 155 | 0 | ((ScalingGraphMousePlugin) scalingPlugin).setZoomAtMouse(zoomAtMouse); |
| 156 | 0 | } |
| 157 | ||
| 158 | /** | |
| 159 | * listener to set the mode from an external event source | |
| 160 | */ | |
| 161 | 0 | class ModeListener implements ItemListener { |
| 162 | public void itemStateChanged(ItemEvent e) { | |
| 163 | setMode((Mode) e.getItem()); | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | /* (non-Javadoc) | |
| 168 | * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#getModeListener() | |
| 169 | */ | |
| 170 | public ItemListener getModeListener() { | |
| 171 | 0 | if (modeListener == null) { |
| 172 | 0 | modeListener = new ModeListener(); |
| 173 | } | |
| 174 | 0 | return modeListener; |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * @return Returns the modeBox. | |
| 179 | */ | |
| 180 | public JComboBox getModeComboBox() { | |
| 181 | 0 | if(modeBox == null) { |
| 182 | 0 | modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING}); |
| 183 | 0 | modeBox.addItemListener(getModeListener()); |
| 184 | } | |
| 185 | 0 | modeBox.setSelectedItem(mode); |
| 186 | 0 | return modeBox; |
| 187 | } | |
| 188 | ||
| 189 | /** | |
| 190 | * create (if necessary) and return a menu that will change | |
| 191 | * the mode | |
| 192 | * @return the menu | |
| 193 | */ | |
| 194 | public JMenu getModeMenu() { | |
| 195 | 0 | if(modeMenu == null) { |
| 196 | 0 | modeMenu = new JMenu();// { |
| 197 | 0 | Icon icon = BasicIconFactory.getMenuArrowIcon(); |
| 198 | 0 | modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon()); |
| 199 | 0 | modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10, |
| 200 | icon.getIconHeight()+10)); | |
| 201 | ||
| 202 | 0 | final JRadioButtonMenuItem transformingButton = |
| 203 | new JRadioButtonMenuItem(Mode.TRANSFORMING.toString()); | |
| 204 | 0 | transformingButton.addItemListener(new ItemListener() { |
| 205 | public void itemStateChanged(ItemEvent e) { | |
| 206 | if(e.getStateChange() == ItemEvent.SELECTED) { | |
| 207 | setMode(Mode.TRANSFORMING); | |
| 208 | } | |
| 209 | }}); | |
| 210 | ||
| 211 | 0 | final JRadioButtonMenuItem pickingButton = |
| 212 | new JRadioButtonMenuItem(Mode.PICKING.toString()); | |
| 213 | 0 | pickingButton.addItemListener(new ItemListener() { |
| 214 | public void itemStateChanged(ItemEvent e) { | |
| 215 | if(e.getStateChange() == ItemEvent.SELECTED) { | |
| 216 | setMode(Mode.PICKING); | |
| 217 | } | |
| 218 | }}); | |
| 219 | 0 | ButtonGroup radio = new ButtonGroup(); |
| 220 | 0 | radio.add(transformingButton); |
| 221 | 0 | radio.add(pickingButton); |
| 222 | 0 | transformingButton.setSelected(true); |
| 223 | 0 | modeMenu.add(transformingButton); |
| 224 | 0 | modeMenu.add(pickingButton); |
| 225 | 0 | modeMenu.setToolTipText("Menu for setting Mouse Mode"); |
| 226 | 0 | addItemListener(new ItemListener() { |
| 227 | public void itemStateChanged(ItemEvent e) { | |
| 228 | if(e.getStateChange() == ItemEvent.SELECTED) { | |
| 229 | if(e.getItem() == Mode.TRANSFORMING) { | |
| 230 | transformingButton.setSelected(true); | |
| 231 | } else if(e.getItem() == Mode.PICKING) { | |
| 232 | pickingButton.setSelected(true); | |
| 233 | } | |
| 234 | } | |
| 235 | }}); | |
| 236 | } | |
| 237 | 0 | return modeMenu; |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * add a listener for mode changes | |
| 242 | */ | |
| 243 | public void addItemListener(ItemListener aListener) { | |
| 244 | 0 | listenerList.add(ItemListener.class,aListener); |
| 245 | 0 | } |
| 246 | ||
| 247 | /** | |
| 248 | * remove a listener for mode changes | |
| 249 | */ | |
| 250 | public void removeItemListener(ItemListener aListener) { | |
| 251 | 0 | listenerList.remove(ItemListener.class,aListener); |
| 252 | 0 | } |
| 253 | ||
| 254 | /** | |
| 255 | * Returns an array of all the <code>ItemListener</code>s added | |
| 256 | * to this JComboBox with addItemListener(). | |
| 257 | * | |
| 258 | * @return all of the <code>ItemListener</code>s added or an empty | |
| 259 | * array if no listeners have been added | |
| 260 | * @since 1.4 | |
| 261 | */ | |
| 262 | public ItemListener[] getItemListeners() { | |
| 263 | 0 | return (ItemListener[])listenerList.getListeners(ItemListener.class); |
| 264 | } | |
| 265 | ||
| 266 | public Object[] getSelectedObjects() { | |
| 267 | 0 | if ( mode == null ) |
| 268 | 0 | return new Object[0]; |
| 269 | else { | |
| 270 | 0 | Object result[] = new Object[1]; |
| 271 | 0 | result[0] = mode; |
| 272 | 0 | return result; |
| 273 | } | |
| 274 | } | |
| 275 | ||
| 276 | /** | |
| 277 | * Notifies all listeners that have registered interest for | |
| 278 | * notification on this event type. | |
| 279 | * @param e the event of interest | |
| 280 | * | |
| 281 | * @see EventListenerList | |
| 282 | */ | |
| 283 | protected void fireItemStateChanged(ItemEvent e) { | |
| 284 | // Guaranteed to return a non-null array | |
| 285 | 0 | Object[] listeners = listenerList.getListenerList(); |
| 286 | // Process the listeners last to first, notifying | |
| 287 | // those that are interested in this event | |
| 288 | 0 | for ( int i = listeners.length-2; i>=0; i-=2 ) { |
| 289 | 0 | if ( listeners[i]==ItemListener.class ) { |
| 290 | 0 | ((ItemListener)listeners[i+1]).itemStateChanged(e); |
| 291 | } | |
| 292 | } | |
| 293 | 0 | } |
| 294 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |