View Javadoc
1   /*
2    * Copyright (c) 2005, The JUNG Authors 
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    * Created on Mar 8, 2005
10   *
11   */
12  package edu.uci.ics.jung.visualization.control;
13  
14  import java.awt.Cursor;
15  import java.awt.event.InputEvent;
16  import java.awt.event.MouseEvent;
17  import java.awt.event.MouseListener;
18  import java.awt.geom.Point2D;
19  
20  import javax.swing.JOptionPane;
21  
22  import com.google.common.base.Function;
23  
24  import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
25  import edu.uci.ics.jung.algorithms.layout.Layout;
26  import edu.uci.ics.jung.algorithms.util.MapSettableTransformer;
27  import edu.uci.ics.jung.visualization.Layer;
28  import edu.uci.ics.jung.visualization.VisualizationViewer;
29  
30  /** 
31   * 
32   * 
33   * @author Tom Nelson
34   */
35  public class LabelEditingGraphMousePlugin<V, E> extends AbstractGraphMousePlugin
36      implements MouseListener {
37  
38  	/**
39  	 * the picked Vertex, if any
40  	 */
41      protected V vertex;
42      
43      /**
44       * the picked Edge, if any
45       */
46      protected E edge;
47      
48      /**
49  	 * create an instance with default settings
50  	 */
51  	public LabelEditingGraphMousePlugin() {
52  	    this(InputEvent.BUTTON1_MASK);
53  	}
54  
55  	/**
56  	 * create an instance with overrides
57  	 * @param selectionModifiers for primary selection
58  	 */
59      public LabelEditingGraphMousePlugin(int selectionModifiers) {
60          super(selectionModifiers);
61          this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
62      }
63      
64  	/**
65  	 * For primary modifiers (default, MouseButton1):
66  	 * pick a single Vertex or Edge that
67       * is under the mouse pointer. If no Vertex or edge is under
68       * the pointer, unselect all picked Vertices and edges, and
69       * set up to draw a rectangle for multiple selection
70       * of contained Vertices.
71       * For additional selection (default Shift+MouseButton1):
72       * Add to the selection, a single Vertex or Edge that is
73       * under the mouse pointer. If a previously picked Vertex
74       * or Edge is under the pointer, it is un-picked.
75       * If no vertex or Edge is under the pointer, set up
76       * to draw a multiple selection rectangle (as above)
77       * but do not unpick previously picked elements.
78  	 * 
79  	 * @param e the event
80  	 */
81      @SuppressWarnings("unchecked")
82      public void mouseClicked(MouseEvent e) {
83      	if (e.getModifiers() == modifiers && e.getClickCount() == 2) {
84      		VisualizationViewer<V,E> vv = (VisualizationViewer<V, E>)e.getSource();
85      		GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
86      		if (pickSupport != null) {
87      			Function<? super V,String> vs = vv.getRenderContext().getVertexLabelTransformer();
88      			if (vs instanceof MapSettableTransformer) {
89      				MapSettableTransformer<? super V, String> mst =
90      					(MapSettableTransformer<? super V, String>)vs;
91      				Layout<V,E> layout = vv.getGraphLayout();
92      				// p is the screen point for the mouse event
93      				Point2D p = e.getPoint();
94  
95      				V vertex = pickSupport.getVertex(layout, p.getX(), p.getY());
96      				if(vertex != null) {
97      					String newLabel = vs.apply(vertex);
98      					newLabel = JOptionPane.showInputDialog("New Vertex Label for "+vertex);
99      					if(newLabel != null) {
100     						mst.set(vertex, newLabel);
101     						vv.repaint();
102     					}
103     					return;
104     				}
105     			}
106     			Function<? super E,String> es = vv.getRenderContext().getEdgeLabelTransformer();
107     			if (es instanceof MapSettableTransformer) {
108     				MapSettableTransformer<? super E, String> mst =
109     					(MapSettableTransformer<? super E, String>)es;
110     				Layout<V,E> layout = vv.getGraphLayout();
111     				// p is the screen point for the mouse event
112     				Point2D p = e.getPoint();
113     				// take away the view transform
114     				Point2D ip = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW, p);
115     				E edge = pickSupport.getEdge(layout, ip.getX(), ip.getY());
116     				if(edge != null) {
117     					String newLabel = JOptionPane.showInputDialog("New Edge Label for "+edge);
118     					if(newLabel != null) {
119     						mst.set(edge, newLabel);
120     						vv.repaint();
121     					}
122     					return;
123     				}
124     			}
125     		}
126      		e.consume();
127      	}
128     }
129 
130     /**
131 	 * If the mouse is dragging a rectangle, pick the
132 	 * Vertices contained in that rectangle
133 	 * 
134 	 * clean up settings from mousePressed
135 	 */
136     public void mouseReleased(MouseEvent e) {
137     }
138     
139     /**
140 	 * If the mouse is over a picked vertex, drag all picked
141 	 * vertices with the mouse.
142 	 * If the mouse is not over a Vertex, draw the rectangle
143 	 * to select multiple Vertices
144 	 * 
145 	 */
146 
147     public void mousePressed(MouseEvent e) {
148     }
149 
150 	public void mouseEntered(MouseEvent e) {
151 	}
152 
153 	public void mouseExited(MouseEvent e) {
154 	}
155 
156 }