Coverage details for edu.uci.ics.jung.visualization.control.AbstractModalGraphMouse

LineHitsSource
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  */
550public 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      */
850    protected EventListenerList listenerList = new EventListenerList();
86  
87     /*
88      * default mask override for Apple
89      */
900    private static int scalingMask = MouseEvent.SHIFT_MASK;
91     static {
920        if(System.getProperty("os.name").startsWith("Mac")) {
930            scalingMask = MouseEvent.META_MASK;
94         }
950    }
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) {
1140        if(this.mode != mode) {
1150            fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
116                     this.mode, ItemEvent.DESELECTED));
1170            this.mode = mode;
1180            if(mode == Mode.TRANSFORMING) {
1190                setTransformingMode();
1200            } else if(mode == Mode.PICKING) {
1210                setPickingMode();
122             }
1230            if(modeBox != null) {
1240                modeBox.setSelectedItem(mode);
125             }
1260            fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
127         }
1280    }
129     /* (non-Javadoc)
130      * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setPickingMode()
131      */
132     protected void setPickingMode() {
1330        remove(translatingPlugin);
1340        remove(rotatingPlugin);
1350        remove(shearingPlugin);
1360        add(pickingPlugin);
1370        add(animatedPickingPlugin);
1380    }
139     
140     /* (non-Javadoc)
141      * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setTransformingMode()
142      */
143     protected void setTransformingMode() {
1440        remove(pickingPlugin);
1450        remove(animatedPickingPlugin);
1460        add(translatingPlugin);
1470        add(rotatingPlugin);
1480        add(shearingPlugin);
1490    }
150  
151     /**
152      * @param zoomAtMouse The zoomAtMouse to set.
153      */
154     public void setZoomAtMouse(boolean zoomAtMouse) {
1550        ((ScalingGraphMousePlugin) scalingPlugin).setZoomAtMouse(zoomAtMouse);
1560    }
157     
158     /**
159      * listener to set the mode from an external event source
160      */
1610    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() {
1710        if (modeListener == null) {
1720            modeListener = new ModeListener();
173         }
1740        return modeListener;
175     }
176     
177     /**
178      * @return Returns the modeBox.
179      */
180     public JComboBox getModeComboBox() {
1810        if(modeBox == null) {
1820            modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING});
1830            modeBox.addItemListener(getModeListener());
184         }
1850        modeBox.setSelectedItem(mode);
1860        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() {
1950        if(modeMenu == null) {
1960            modeMenu = new JMenu();// {
1970            Icon icon = BasicIconFactory.getMenuArrowIcon();
1980            modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon());
1990            modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10,
200                     icon.getIconHeight()+10));
201  
2020            final JRadioButtonMenuItem transformingButton =
203                 new JRadioButtonMenuItem(Mode.TRANSFORMING.toString());
2040            transformingButton.addItemListener(new ItemListener() {
205                 public void itemStateChanged(ItemEvent e) {
206                     if(e.getStateChange() == ItemEvent.SELECTED) {
207                         setMode(Mode.TRANSFORMING);
208                     }
209                 }});
210             
2110            final JRadioButtonMenuItem pickingButton =
212                 new JRadioButtonMenuItem(Mode.PICKING.toString());
2130            pickingButton.addItemListener(new ItemListener() {
214                 public void itemStateChanged(ItemEvent e) {
215                     if(e.getStateChange() == ItemEvent.SELECTED) {
216                         setMode(Mode.PICKING);
217                     }
218                 }});
2190            ButtonGroup radio = new ButtonGroup();
2200            radio.add(transformingButton);
2210            radio.add(pickingButton);
2220            transformingButton.setSelected(true);
2230            modeMenu.add(transformingButton);
2240            modeMenu.add(pickingButton);
2250            modeMenu.setToolTipText("Menu for setting Mouse Mode");
2260            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         }
2370        return modeMenu;
238     }
239     
240     /**
241      * add a listener for mode changes
242      */
243     public void addItemListener(ItemListener aListener) {
2440        listenerList.add(ItemListener.class,aListener);
2450    }
246  
247     /**
248      * remove a listener for mode changes
249      */
250     public void removeItemListener(ItemListener aListener) {
2510        listenerList.remove(ItemListener.class,aListener);
2520    }
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() {
2630        return (ItemListener[])listenerList.getListeners(ItemListener.class);
264     }
265     
266     public Object[] getSelectedObjects() {
2670        if ( mode == null )
2680            return new Object[0];
269         else {
2700            Object result[] = new Object[1];
2710            result[0] = mode;
2720            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
2850        Object[] listeners = listenerList.getListenerList();
286         // Process the listeners last to first, notifying
287         // those that are interested in this event
2880        for ( int i = listeners.length-2; i>=0; i-=2 ) {
2890            if ( listeners[i]==ItemListener.class ) {
2900                ((ItemListener)listeners[i+1]).itemStateChanged(e);
291             }
292         }
2930    }
294 }

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.