View Javadoc

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.Component;
15  import java.awt.Cursor;
16  import java.awt.ItemSelectable;
17  import java.awt.event.InputEvent;
18  import java.awt.event.KeyAdapter;
19  import java.awt.event.KeyEvent;
20  
21  
22  /** 
23   * 
24   * DefaultModalGraphMouse is a PluggableGraphMouse class that
25   * pre-installs a large collection of plugins for picking and
26   * transforming the graph. Additionally, it carries the notion
27   * of a Mode: Picking or Translating. Switching between modes
28   * allows for a more natural choice of mouse modifiers to
29   * be used for the various plugins. The default modifiers are
30   * intended to mimick those of mainstream software applications
31   * in order to be intuitive to users.
32   * 
33   * To change between modes, two different controls are offered,
34   * a combo box and a menu system. These controls are lazily created
35   * in their respective 'getter' methods so they don't impact
36   * code that does not intend to use them.
37   * The menu control can be placed in an unused corner of the
38   * GraphZoomScrollPane, which is a common location for mouse
39   * mode selection menus in mainstream applications.
40   * 
41   * @author Tom Nelson
42   */
43  public class DefaultModalGraphMouse<V,E> extends AbstractModalGraphMouse 
44      implements ModalGraphMouse, ItemSelectable {
45      
46      /**
47       * create an instance with default values
48       *
49       */
50      public DefaultModalGraphMouse() {
51          this(1.1f, 1/1.1f);
52      }
53      
54      /**
55       * create an instance with passed values
56       * @param in override value for scale in
57       * @param out override value for scale out
58       */
59      public DefaultModalGraphMouse(float in, float out) {
60          super(in,out);
61          loadPlugins();
62  		setModeKeyListener(new ModeKeyAdapter(this));
63      }
64      
65      /**
66       * create the plugins, and load the plugins for TRANSFORMING mode
67       *
68       */
69      @Override
70      protected void loadPlugins() {
71          pickingPlugin = new PickingGraphMousePlugin<V,E>();
72          animatedPickingPlugin = new AnimatedPickingGraphMousePlugin<V,E>();
73          translatingPlugin = new TranslatingGraphMousePlugin(InputEvent.BUTTON1_MASK);
74          scalingPlugin = new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, in, out);
75          rotatingPlugin = new RotatingGraphMousePlugin();
76          shearingPlugin = new ShearingGraphMousePlugin();
77  
78          add(scalingPlugin);
79          setMode(Mode.TRANSFORMING);
80      }
81      
82      public static class ModeKeyAdapter extends KeyAdapter {
83      	private char t = 't';
84      	private char p = 'p';
85      	protected ModalGraphMouse graphMouse;
86  
87      	public ModeKeyAdapter(ModalGraphMouse graphMouse) {
88  			this.graphMouse = graphMouse;
89  		}
90  
91  		public ModeKeyAdapter(char t, char p, ModalGraphMouse graphMouse) {
92  			this.t = t;
93  			this.p = p;
94  			this.graphMouse = graphMouse;
95  		}
96  		
97  		@Override
98          public void keyTyped(KeyEvent event) {
99  			char keyChar = event.getKeyChar();
100 			if(keyChar == t) {
101 				((Component)event.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
102 				graphMouse.setMode(Mode.TRANSFORMING);
103 			} else if(keyChar == p) {
104 				((Component)event.getSource()).setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
105 				graphMouse.setMode(Mode.PICKING);
106 			}
107 		}
108     }
109 }