View Javadoc
1   /*
2    * Copyright (c) 2003, 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    */
10  /*
11   * Created on Feb 17, 2004
12   */
13  package edu.uci.ics.jung.visualization.control;
14  
15  import java.awt.event.MouseAdapter;
16  import java.awt.event.MouseEvent;
17  import java.awt.geom.Point2D;
18  
19  import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
20  import edu.uci.ics.jung.algorithms.layout.Layout;
21  import edu.uci.ics.jung.visualization.VisualizationViewer;
22  
23  /**
24   * This class translates mouse clicks into vertex clicks
25   * 
26   * @author danyelf
27   */
28  public class MouseListenerTranslator<V, E> extends MouseAdapter {
29  
30  	private VisualizationViewer<V,E> vv;
31  	private GraphMouseListener<V> gel;
32  
33  	/**
34  	 * @param gel listens for mouse events
35  	 * @param vv the viewer used for visualization
36  	 */
37  	public MouseListenerTranslator(GraphMouseListener<V> gel, VisualizationViewer<V,E> vv) {
38  		this.gel = gel;
39  		this.vv = vv;
40  	}
41  	
42  	/**
43  	 * Transform the point to the coordinate system in the
44  	 * VisualizationViewer, then use either PickSuuport
45  	 * (if available) or Layout to find a Vertex
46  	 * @param point
47  	 * @return
48  	 */
49  	private V getVertex(Point2D point) {
50  	    // adjust for scale and offset in the VisualizationViewer
51  	    Point2D p = point;
52  	    	//vv.getRenderContext().getBasicTransformer().inverseViewTransform(point);
53  	    GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
54          Layout<V,E> layout = vv.getGraphLayout();
55  	    V v = null;
56  	    if(pickSupport != null) {
57  	        v = pickSupport.getVertex(layout, p.getX(), p.getY());
58  	    } 
59  	    return v;
60  	}
61  	/**
62  	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
63  	 */
64  	public void mouseClicked(MouseEvent e) {
65  	    V v = getVertex(e.getPoint());
66  		if ( v != null ) {
67  			gel.graphClicked(v, e );
68  		}
69  	}
70  
71  	/**
72  	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
73  	 */
74  	public void mousePressed(MouseEvent e) {
75  		V v = getVertex(e.getPoint());
76  		if ( v != null ) {
77  			gel.graphPressed(v, e );
78  		}
79  	}
80  
81  	/**
82  	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
83  	 */
84  	public void mouseReleased(MouseEvent e) {
85  		V v = getVertex(e.getPoint());
86  		if ( v != null ) {
87  			gel.graphReleased(v, e );
88  		}
89  	}
90  }