Coverage details for edu.uci.ics.jung.visualization.SimpleGraphMouse

LineHitsSource
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;
13  
14 import java.awt.event.MouseAdapter;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseWheelEvent;
17 import java.awt.geom.Point2D;
18  
19 import javax.swing.SwingUtilities;
20  
21 import edu.uci.ics.jung.graph.Edge;
22 import edu.uci.ics.jung.graph.Vertex;
23  
24 /**
25  * SimpleGraphMouse is the original GraphMouse class that was
26  * nested in VisualizationViewer and installed as a listener
27  * for mouse events and mouse motion events. Users can restore
28  * previous capability by creating an instance of this class
29  * and using VisualizationViewer.setGraphMouse() to re-install it.
30  * Changes in this code from the nested version are mainly those
31  * necessary to move it outside the VisualizationViewer class,
32  * and to properly handle the transform values of the VisualizationViewer
33  * (the zoom and pan)
34  *
35  *
36  */
37 public class SimpleGraphMouse extends MouseAdapter
38     implements VisualizationViewer.GraphMouse {
39     
40     /**
41      * the VisualizationViewer who's mouse events call these
42      * methods.
43      */
44     protected VisualizationViewer vv;
45     
46     /**
47      * how far the mouse point x is from the vertex center x
48      */
49     protected float offsetx;
50     
51     /**
52      * how far the mouse point y is from the vertex center y
53      */
54     protected float offsety;
55     
56     /**
57      * the vertex to drag with a mouseDragged operation
58      */
59     protected Vertex vertexToDrag;
60  
61     /**
62      * create an instance for the passed VisualizationViewer
63      * @param vv
64      */
650    public SimpleGraphMouse(VisualizationViewer vv) {
660        this.vv = vv;
670    }
68  
69     /**
70      * Uses the layout class to attempt to pick a vertex in
71      * the graph.
72      */
73     public void mousePressed(MouseEvent e) {
740        PickSupport pickSupport = vv.getPickSupport();
750        PickedState pickedState = vv.getPickedState();
76         
770        if(pickSupport != null && pickedState != null) {
780            Layout layout = vv.getGraphLayout();
790            if(SwingUtilities.isLeftMouseButton(e)) {
80                 // p is the screen point for the mouse event
810                Point2D p = e.getPoint();
82                 // transform it to graph coordinates:
830                Point2D gp = vv.inverseTransform(p);
84  
850                Vertex v = pickSupport.getVertex(gp.getX(), gp.getY());
860                if(v != null) {
870                    pickedState.clearPickedVertices();
880                    pickedState.pick(v, true);
890                    vertexToDrag = v;
90                     // layout.getLocation applies the layout transformer so
91                     // q is transformed by the layout transformer only
920                    Point2D q = layout.getLocation(v);
93                     // i need to put it back in the graph coordinates:
940                    Point2D gq = vv.getLayoutTransformer().inverseTransform(q);
950                    offsetx = (float) (gp.getX()-gq.getX());
960                    offsety = (float) (gp.getY()-gq.getY());
97                 } else {
980                    Edge edge = pickSupport.getEdge(gp.getX(), gp.getY());
990                    if(edge != null) {
1000                        pickedState.clearPickedEdges();
1010                        pickedState.pick(edge, true);
102                     }
103                 }
1040                vv.repaint();
105                 
1060            } else if(SwingUtilities.isMiddleMouseButton(e)) {
1070                Point2D p = vv.inverseTransform(e.getPoint());
1080                Vertex v = pickSupport.getVertex(p.getX(), p.getY());
1090                if(v != null) {
1100                    boolean wasThere = pickedState.pick(v, !pickedState.isPicked(v));
1110                    if(wasThere) {
1120                        vertexToDrag = null;
113                     } else {
1140                        vertexToDrag = v;
1150                            Point2D point = vv.getLayoutTransformer().inverseTransform(layout.getLocation(v));
1160                            offsetx = (float) (p.getX()-point.getX());
1170                            offsety = (float) (p.getY()-point.getY());
118 // }
119                     }
120                 } else {
1210                    Edge edge = pickSupport.getEdge(p.getX(), p.getY());
1220                    if(edge != null) {
1230                        pickedState.pick(edge, !pickedState.isPicked(edge));
124                     }
125                 }
1260                vv.repaint();
127             }
128         }
1290    }
130         
131     
132     /**
133      * clean up after the pick of a Vertex or Edge
134      */
135     public void mouseReleased(MouseEvent e) {
1360        vertexToDrag = null;
1370    }
138     
139     /**
140      * if a Vertex was picked in the mousePressed method, use the
141      * layout class to force that Vertex to move, following the
142      * motion of the mouse.
143      */
144     public void mouseDragged(MouseEvent e) {
1450        if (vertexToDrag != null) {
1460            Point2D p = vv.inverseTransform(e.getPoint());
1470            Layout layout = vv.getGraphLayout();
1480            layout.forceMove(vertexToDrag, p.getX()-offsetx, p.getY()-offsety);
1490            vv.repaint();
150         }
1510    }
152     
153     /**
154      * no-op here
155      */
156     public void mouseMoved(MouseEvent e) {
1570        return;
158     }
159     
160     /**
161      * no-op here
162      */
163     public void mouseWheelMoved(MouseWheelEvent e) {
1640        return;
165     }
166 }
167  

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.