View Javadoc

1   /*
2   * Copyright (c) 2003, 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   */
10  package edu.uci.ics.jung.visualization;
11  
12  import java.awt.Dimension;
13  import java.awt.event.KeyListener;
14  import java.awt.event.MouseAdapter;
15  import java.awt.event.MouseEvent;
16  import java.awt.event.MouseListener;
17  import java.awt.event.MouseMotionListener;
18  import java.awt.event.MouseWheelListener;
19  import java.awt.geom.Point2D;
20  
21  import javax.swing.ToolTipManager;
22  
23  import org.apache.commons.collections15.Transformer;
24  
25  import edu.uci.ics.jung.algorithms.layout.Layout;
26  import edu.uci.ics.jung.visualization.control.GraphMouseListener;
27  import edu.uci.ics.jung.visualization.control.MouseListenerTranslator;
28  
29  /**
30   * Adds mouse behaviors and tooltips to the graph visualization base class
31   * 
32   * @author Joshua O'Madadhain
33   * @author Tom Nelson 
34   * @author Danyel Fisher
35   */
36  @SuppressWarnings("serial")
37  public class VisualizationViewer<V,E> extends BasicVisualizationServer<V,E> {
38  
39  	protected Transformer<V,String> vertexToolTipTransformer;
40  	protected Transformer<E,String> edgeToolTipTransformer;
41  	protected Transformer<MouseEvent,String> mouseEventToolTipTransformer;
42  	
43      /**
44       * provides MouseListener, MouseMotionListener, and MouseWheelListener
45       * events to the graph
46       */
47      protected GraphMouse graphMouse;
48      
49      protected MouseListener requestFocusListener = new MouseAdapter() {
50  		public void mouseClicked(MouseEvent e) {
51  			requestFocusInWindow();
52  		}
53      };
54  
55  
56      /**
57       * Create an instance with passed parameters.
58       * 
59       * @param layout		The Layout to apply, with its associated Graph
60       * @param renderer		The Renderer to draw it with
61       */
62  	public VisualizationViewer(Layout<V,E> layout) {
63  	    this(new DefaultVisualizationModel<V,E>(layout));
64  	}
65  	
66      /**
67       * Create an instance with passed parameters.
68       * 
69       * @param layout		The Layout to apply, with its associated Graph
70       * @param renderer		The Renderer to draw it with
71       * @param preferredSize the preferred size of this View
72       */
73  	public VisualizationViewer(Layout<V,E> layout, Dimension preferredSize) {
74  	    this(new DefaultVisualizationModel<V,E>(layout, preferredSize), preferredSize);
75  	}
76  	
77  	/**
78  	 * Create an instance with passed parameters.
79  	 * 
80  	 * @param model
81  	 * @param renderer
82  	 */
83  	public VisualizationViewer(VisualizationModel<V,E> model) {
84  	    this(model, new Dimension(600,600));
85  	}
86  	/**
87  	 * Create an instance with passed parameters.
88  	 * 
89  	 * @param model
90  	 * @param renderer
91  	 * @param preferredSize initial preferred size of the view
92  	 */
93  	@SuppressWarnings("unchecked")
94      public VisualizationViewer(VisualizationModel<V,E> model,
95  	        Dimension preferredSize) {
96          super(model, preferredSize);
97  		setFocusable(true);
98          addMouseListener(requestFocusListener);
99  	}
100 	
101 	/**
102 	 * a setter for the GraphMouse. This will remove any
103 	 * previous GraphMouse (including the one that
104 	 * is added in the initMouseClicker method.
105 	 * @param graphMouse new value
106 	 */
107 	public void setGraphMouse(GraphMouse graphMouse) {
108 	    this.graphMouse = graphMouse;
109 	    MouseListener[] ml = getMouseListeners();
110 	    for(int i=0; i<ml.length; i++) {
111 	        if(ml[i] instanceof GraphMouse) {
112 	            removeMouseListener(ml[i]);
113 	        }
114 	    }
115 	    MouseMotionListener[] mml = getMouseMotionListeners();
116 	    for(int i=0; i<mml.length; i++) {
117 	        if(mml[i] instanceof GraphMouse) {
118 	            removeMouseMotionListener(mml[i]);
119 	        }
120 	    }
121 	    MouseWheelListener[] mwl = getMouseWheelListeners();
122 	    for(int i=0; i<mwl.length; i++) {
123 	        if(mwl[i] instanceof GraphMouse) {
124 	            removeMouseWheelListener(mwl[i]);
125 	        }
126 	    }
127 	    addMouseListener(graphMouse);
128 	    addMouseMotionListener(graphMouse);
129 	    addMouseWheelListener(graphMouse);
130 	}
131 	
132 	/**
133 	 * @return the current <code>GraphMouse</code>
134 	 */
135 	public GraphMouse getGraphMouse() {
136 	    return graphMouse;
137 	}
138 
139 	/**
140 	 * This is the interface for adding a mouse listener. The GEL
141 	 * will be called back with mouse clicks on vertices.
142 	 * @param gel
143 	 */
144 	public void addGraphMouseListener( GraphMouseListener<V> gel ) {
145 		addMouseListener( new MouseListenerTranslator<V,E>( gel, this ));
146 	}
147 	
148 	/** 
149 	 * Override to request focus on mouse enter, if a key listener is added
150 	 * @see java.awt.Component#addKeyListener(java.awt.event.KeyListener)
151 	 */
152 	@Override
153 	public synchronized void addKeyListener(KeyListener l) {
154 		super.addKeyListener(l);
155 //		setFocusable(true);
156 //		addMouseListener(requestFocusListener);
157 	}
158 	
159 	/**
160 	 * @param edgeToolTipTransformer the edgeToolTipTransformer to set
161 	 */
162 	public void setEdgeToolTipTransformer(
163 			Transformer<E, String> edgeToolTipTransformer) {
164 		this.edgeToolTipTransformer = edgeToolTipTransformer;
165 		ToolTipManager.sharedInstance().registerComponent(this);
166 	}
167 
168 	/**
169 	 * @param mouseEventToolTipTransformer the mouseEventToolTipTransformer to set
170 	 */
171 	public void setMouseEventToolTipTransformer(
172 			Transformer<MouseEvent, String> mouseEventToolTipTransformer) {
173 		this.mouseEventToolTipTransformer = mouseEventToolTipTransformer;
174 		ToolTipManager.sharedInstance().registerComponent(this);
175 	}
176 
177 	/**
178 	 * @param vertexToolTipTransformer the vertexToolTipTransformer to set
179 	 */
180 	public void setVertexToolTipTransformer(
181 			Transformer<V, String> vertexToolTipTransformer) {
182 		this.vertexToolTipTransformer = vertexToolTipTransformer;
183 		ToolTipManager.sharedInstance().registerComponent(this);
184 	}
185 
186     /**
187      * called by the superclass to display tooltips
188      */
189     public String getToolTipText(MouseEvent event) {
190         Layout<V,E> layout = getGraphLayout();
191         Point2D p = null;
192         if(vertexToolTipTransformer != null) {
193             p = event.getPoint();
194             	//renderContext.getBasicTransformer().inverseViewTransform(event.getPoint());
195             V vertex = getPickSupport().getVertex(layout, p.getX(), p.getY());
196             if(vertex != null) {
197             	return vertexToolTipTransformer.transform(vertex);
198             }
199         }
200         if(edgeToolTipTransformer != null) {
201         	if(p == null) p = renderContext.getMultiLayerTransformer().inverseTransform(Layer.VIEW, event.getPoint());
202             E edge = getPickSupport().getEdge(layout, p.getX(), p.getY());
203             if(edge != null) {
204             	return edgeToolTipTransformer.transform(edge);
205             }
206         }
207         if(mouseEventToolTipTransformer != null) {
208         	return mouseEventToolTipTransformer.transform(event);
209         }
210         return super.getToolTipText(event);
211     }
212 
213     /**
214      * a convenience type to represent a class that
215      * processes all types of mouse events for the graph
216      */
217     public interface GraphMouse extends MouseListener, MouseMotionListener, MouseWheelListener {}
218     
219 }