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

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.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      */
660    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() {
810        this(1.1f, 1/1.1f);
820    }
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      */
890    public EditingModalGraphMouse(float in, float out) {
900        this.in = in;
910        this.out = out;
920        loadPlugins();
930    }
94     
95     /**
96      * create the plugins, and load the plugins for TRANSFORMING mode
97      *
98      */
99     protected void loadPlugins() {
1000        pickingPlugin = new PickingGraphMousePlugin();
1010        animatedPickingPlugin = new AnimatedPickingGraphMousePlugin();
1020        translatingPlugin = new TranslatingGraphMousePlugin(InputEvent.BUTTON1_MASK);
1030        scalingPlugin = new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, in, out);
1040        rotatingPlugin = new RotatingGraphMousePlugin();
1050        shearingPlugin = new ShearingGraphMousePlugin();
1060        editingPlugin = new EditingGraphMousePlugin();
107         
1080        add(scalingPlugin);
1090        setMode(Mode.EDITING);
1100    }
111     public void setVertexLocations(SettableVertexLocationFunction vertexLocations) {
1120        ((EditingGraphMousePlugin)editingPlugin).setVertexLocations(vertexLocations);
1130    }
114  
115     /**
116      * setter for the Mode.
117      */
118     public void setMode(Mode mode) {
1190        if(this.mode != mode) {
1200            fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
121                     this.mode, ItemEvent.DESELECTED));
1220            this.mode = mode;
1230            if(mode == Mode.TRANSFORMING) {
1240                setTransformingMode();
1250            } else if(mode == Mode.PICKING) {
1260                setPickingMode();
1270            } else if(mode == Mode.EDITING) {
1280                setEditingMode();
129             }
1300            if(modeBox != null) {
1310                modeBox.setSelectedItem(mode);
132             }
1330            fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
134         }
1350    }
136     /* (non-Javadoc)
137      * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setPickingMode()
138      */
139     protected void setPickingMode() {
1400        remove(translatingPlugin);
1410        remove(rotatingPlugin);
1420        remove(shearingPlugin);
1430        remove(editingPlugin);
1440        add(pickingPlugin);
1450        add(animatedPickingPlugin);
1460    }
147     
148     /* (non-Javadoc)
149      * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setTransformingMode()
150      */
151     protected void setTransformingMode() {
1520        remove(pickingPlugin);
1530        remove(animatedPickingPlugin);
1540        remove(editingPlugin);
1550        add(translatingPlugin);
1560        add(rotatingPlugin);
1570        add(shearingPlugin);
1580    }
159     
160     protected void setEditingMode() {
1610        remove(pickingPlugin);
1620        remove(animatedPickingPlugin);
1630        remove(translatingPlugin);
1640        remove(rotatingPlugin);
1650        remove(shearingPlugin);
1660        add(editingPlugin);
1670   }
168  
169     /**
170      * @param zoomAtMouse The zoomAtMouse to set.
171      */
172     public void setZoomAtMouse(boolean zoomAtMouse) {
1730        ((ScalingGraphMousePlugin) scalingPlugin).setZoomAtMouse(zoomAtMouse);
1740    }
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() {
1890        if (modeListener == null) {
1900            modeListener = new ModeListener();
191         }
1920        return modeListener;
193     }
194     
195     /**
196      * @return Returns the modeBox.
197      */
198     public JComboBox getModeComboBox() {
1990        if(modeBox == null) {
2000            modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING, Mode.EDITING});
2010            modeBox.addItemListener(getModeListener());
202         }
2030        modeBox.setSelectedItem(mode);
2040        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() {
2130        if(modeMenu == null) {
2140            modeMenu = new JMenu();// {
2150            Icon icon = BasicIconFactory.getMenuArrowIcon();
2160            modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon());
2170            modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10,
218                     icon.getIconHeight()+10));
219  
2200            final JRadioButtonMenuItem transformingButton =
221                 new JRadioButtonMenuItem(Mode.TRANSFORMING.toString());
2220            transformingButton.addItemListener(new ItemListener() {
223                 public void itemStateChanged(ItemEvent e) {
224                     if(e.getStateChange() == ItemEvent.SELECTED) {
225                         setMode(Mode.TRANSFORMING);
226                     }
227                 }});
228             
2290            final JRadioButtonMenuItem pickingButton =
230                 new JRadioButtonMenuItem(Mode.PICKING.toString());
2310            pickingButton.addItemListener(new ItemListener() {
232                 public void itemStateChanged(ItemEvent e) {
233                     if(e.getStateChange() == ItemEvent.SELECTED) {
234                         setMode(Mode.PICKING);
235                     }
236                 }});
237  
2380            final JRadioButtonMenuItem editingButton =
239                 new JRadioButtonMenuItem(Mode.EDITING.toString());
2400            editingButton.addItemListener(new ItemListener() {
241                 public void itemStateChanged(ItemEvent e) {
242                     if(e.getStateChange() == ItemEvent.SELECTED) {
243                         setMode(Mode.EDITING);
244                     }
245                 }});
246  
2470            ButtonGroup radio = new ButtonGroup();
2480            radio.add(transformingButton);
2490            radio.add(pickingButton);
2500            radio.add(editingButton);
2510            transformingButton.setSelected(true);
2520            modeMenu.add(transformingButton);
2530            modeMenu.add(pickingButton);
2540            modeMenu.add(editingButton);
2550            modeMenu.setToolTipText("Menu for setting Mouse Mode");
2560            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         }
2690        return modeMenu;
270     }
271     
272     /**
273      * add a listener for mode changes
274      */
275     public void addItemListener(ItemListener aListener) {
2760        listenerList.add(ItemListener.class,aListener);
2770    }
278  
279     /**
280      * remove a listener for mode changes
281      */
282     public void removeItemListener(ItemListener aListener) {
2830        listenerList.remove(ItemListener.class,aListener);
2840    }
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() {
2950        return (ItemListener[])listenerList.getListeners(ItemListener.class);
296     }
297     
298     public Object[] getSelectedObjects() {
2990        if ( mode == null )
3000            return new Object[0];
301         else {
3020            Object result[] = new Object[1];
3030            result[0] = mode;
3040            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
3170        Object[] listeners = listenerList.getListenerList();
318         // Process the listeners last to first, notifying
319         // those that are interested in this event
3200        for ( int i = listeners.length-2; i>=0; i-=2 ) {
3210            if ( listeners[i]==ItemListener.class ) {
3220                ((ItemListener)listeners[i+1]).itemStateChanged(e);
323             }
324         }
3250    }
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.