View Javadoc

1   /*
2    * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    *
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    *
8    * 
9    */
10  package edu.uci.ics.jung.visualization.annotations;
11  
12  import java.awt.Component;
13  import java.awt.Cursor;
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  import java.awt.event.KeyAdapter;
20  import java.awt.event.KeyEvent;
21  
22  import javax.swing.ButtonGroup;
23  import javax.swing.Icon;
24  import javax.swing.JComboBox;
25  import javax.swing.JMenu;
26  import javax.swing.JRadioButtonMenuItem;
27  import javax.swing.plaf.basic.BasicIconFactory;
28  
29  import edu.uci.ics.jung.visualization.MultiLayerTransformer;
30  import edu.uci.ics.jung.visualization.RenderContext;
31  import edu.uci.ics.jung.visualization.control.AbstractModalGraphMouse;
32  import edu.uci.ics.jung.visualization.control.AnimatedPickingGraphMousePlugin;
33  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
34  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
35  import edu.uci.ics.jung.visualization.control.PickingGraphMousePlugin;
36  import edu.uci.ics.jung.visualization.control.RotatingGraphMousePlugin;
37  import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
38  import edu.uci.ics.jung.visualization.control.ShearingGraphMousePlugin;
39  import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
40  
41  /**
42   * a graph mouse that supplies an annotations mode
43   * 
44   * @author Tom Nelson - tomnelson@dev.java.net
45   *
46   * @param <V>
47   * @param <E>
48   */
49  public class AnnotatingModalGraphMouse<V,E> extends AbstractModalGraphMouse 
50  	implements ModalGraphMouse, ItemSelectable {
51  
52  	protected AnnotatingGraphMousePlugin<V,E> annotatingPlugin;
53  	protected MultiLayerTransformer basicTransformer;
54  	protected RenderContext<V,E> rc;
55  
56  	/**
57  	 * create an instance with default values
58  	 *
59  	 */
60  	public AnnotatingModalGraphMouse(RenderContext<V,E> rc, 
61  			AnnotatingGraphMousePlugin<V,E> annotatingPlugin) {
62  		this(rc, annotatingPlugin, 1.1f, 1/1.1f);
63  	}
64  
65  	/**
66  	 * create an instance with passed values
67  	 * @param in override value for scale in
68  	 * @param out override value for scale out
69  	 */
70  	public AnnotatingModalGraphMouse(RenderContext<V,E> rc,
71  			AnnotatingGraphMousePlugin<V,E> annotatingPlugin,
72  			float in, float out) {
73  		super(in,out);
74  		this.rc = rc;
75  		this.basicTransformer = rc.getMultiLayerTransformer();
76  		this.annotatingPlugin = annotatingPlugin;
77  		loadPlugins();
78  		setModeKeyListener(new ModeKeyAdapter(this));
79  	}
80  
81  	/**
82  	 * create the plugins, and load the plugins for TRANSFORMING mode
83  	 *
84  	 */
85  	@Override
86      protected void loadPlugins() {
87  		this.pickingPlugin = new PickingGraphMousePlugin<V,E>();
88  		this.animatedPickingPlugin = new AnimatedPickingGraphMousePlugin<V,E>();
89  		this.translatingPlugin = new TranslatingGraphMousePlugin(InputEvent.BUTTON1_MASK);
90  		this.scalingPlugin = new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, in, out);
91  		this.rotatingPlugin = new RotatingGraphMousePlugin();
92  		this.shearingPlugin = new ShearingGraphMousePlugin();
93  		add(scalingPlugin);
94  		setMode(Mode.TRANSFORMING);
95  	}
96  
97  	/**
98  	 * setter for the Mode.
99  	 */
100 	@Override
101 	public void setMode(Mode mode) {
102 		if(this.mode != mode) {
103 			fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
104 					this.mode, ItemEvent.DESELECTED));
105 			this.mode = mode;
106 			if(mode == Mode.TRANSFORMING) {
107 				setTransformingMode();
108 			} else if(mode == Mode.PICKING) {
109 				setPickingMode();
110 			} else if(mode == Mode.ANNOTATING) {
111 				setAnnotatingMode();
112 			}
113 			if(modeBox != null) {
114 				modeBox.setSelectedItem(mode);
115 			}
116 			fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, mode, ItemEvent.SELECTED));
117 		}
118 	}
119 	
120 	/* (non-Javadoc)
121 	 * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setPickingMode()
122 	 */
123 	@Override
124 	protected void setPickingMode() {
125 		remove(translatingPlugin);
126 		remove(rotatingPlugin);
127 		remove(shearingPlugin);
128 		remove(annotatingPlugin);
129 		add(pickingPlugin);
130 		add(animatedPickingPlugin);
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see edu.uci.ics.jung.visualization.control.ModalGraphMouse#setTransformingMode()
135 	 */
136 	@Override
137 	protected void setTransformingMode() {
138 		remove(pickingPlugin);
139 		remove(animatedPickingPlugin);
140 		remove(annotatingPlugin);
141 		add(translatingPlugin);
142 		add(rotatingPlugin);
143 		add(shearingPlugin);
144 	}
145 
146 	protected void setEditingMode() {
147 		remove(pickingPlugin);
148 		remove(animatedPickingPlugin);
149 		remove(translatingPlugin);
150 		remove(rotatingPlugin);
151 		remove(shearingPlugin);
152 		remove(annotatingPlugin);
153 	}
154 
155 	protected void setAnnotatingMode() {
156 		remove(pickingPlugin);
157 		remove(animatedPickingPlugin);
158 		remove(translatingPlugin);
159 		remove(rotatingPlugin);
160 		remove(shearingPlugin);
161 		add(annotatingPlugin);
162 	}
163 
164 
165 	/**
166 	 * @return Returns the modeBox.
167 	 */
168 	@Override
169 	public JComboBox getModeComboBox() {
170 		if(modeBox == null) {
171 			modeBox = new JComboBox(new Mode[]{Mode.TRANSFORMING, Mode.PICKING, Mode.ANNOTATING});
172 			modeBox.addItemListener(getModeListener());
173 		}
174 		modeBox.setSelectedItem(mode);
175 		return modeBox;
176 	}
177 
178 	/**
179 	 * create (if necessary) and return a menu that will change
180 	 * the mode
181 	 * @return the menu
182 	 */
183 	@Override
184     public JMenu getModeMenu() {
185 		if(modeMenu == null) {
186 			modeMenu = new JMenu();// {
187 			Icon icon = BasicIconFactory.getMenuArrowIcon();
188 			modeMenu.setIcon(BasicIconFactory.getMenuArrowIcon());
189 			modeMenu.setPreferredSize(new Dimension(icon.getIconWidth()+10, 
190 					icon.getIconHeight()+10));
191 
192 			final JRadioButtonMenuItem transformingButton = 
193 				new JRadioButtonMenuItem(Mode.TRANSFORMING.toString());
194 			transformingButton.addItemListener(new ItemListener() {
195 				public void itemStateChanged(ItemEvent e) {
196 					if(e.getStateChange() == ItemEvent.SELECTED) {
197 						setMode(Mode.TRANSFORMING);
198 					}
199 				}});
200 
201 			final JRadioButtonMenuItem pickingButton =
202 				new JRadioButtonMenuItem(Mode.PICKING.toString());
203 			pickingButton.addItemListener(new ItemListener() {
204 				public void itemStateChanged(ItemEvent e) {
205 					if(e.getStateChange() == ItemEvent.SELECTED) {
206 						setMode(Mode.PICKING);
207 					}
208 				}});
209 
210 			ButtonGroup radio = new ButtonGroup();
211 			radio.add(transformingButton);
212 			radio.add(pickingButton);
213 			transformingButton.setSelected(true);
214 			modeMenu.add(transformingButton);
215 			modeMenu.add(pickingButton);
216 			modeMenu.setToolTipText("Menu for setting Mouse Mode");
217 			addItemListener(new ItemListener() {
218 				public void itemStateChanged(ItemEvent e) {
219 					if(e.getStateChange() == ItemEvent.SELECTED) {
220 						if(e.getItem() == Mode.TRANSFORMING) {
221 							transformingButton.setSelected(true);
222 						} else if(e.getItem() == Mode.PICKING) {
223 							pickingButton.setSelected(true);
224 						}
225 					}
226 				}});
227 		}
228 		return modeMenu;
229 	}
230 	
231     public static class ModeKeyAdapter extends KeyAdapter {
232     	private char t = 't';
233     	private char p = 'p';
234     	private char a = 'a';
235     	protected ModalGraphMouse graphMouse;
236 
237     	public ModeKeyAdapter(ModalGraphMouse graphMouse) {
238 			this.graphMouse = graphMouse;
239 		}
240 
241 		public ModeKeyAdapter(char t, char p, char a, ModalGraphMouse graphMouse) {
242 			this.t = t;
243 			this.p = p;
244 			this.a = a;
245 			this.graphMouse = graphMouse;
246 		}
247 		
248 		@Override
249     public void keyTyped(KeyEvent event) {
250 			char keyChar = event.getKeyChar();
251 			if(keyChar == t) {
252 				((Component)event.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
253 				graphMouse.setMode(Mode.TRANSFORMING);
254 			} else if(keyChar == p) {
255 				((Component)event.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
256 				graphMouse.setMode(Mode.PICKING);
257 			} else if(keyChar == a) {
258 				((Component)event.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
259 				graphMouse.setMode(Mode.ANNOTATING);
260 			}
261 		}
262     }
263 }
264