Coverage details for edu.uci.ics.jung.visualization.control.EditingGraphMousePlugin

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  */
9 package edu.uci.ics.jung.visualization.control;
10  
11 import java.awt.Color;
12 import java.awt.Graphics;
13 import java.awt.Graphics2D;
14 import java.awt.Shape;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionListener;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.CubicCurve2D;
20 import java.awt.geom.Point2D;
21 import java.util.Iterator;
22  
23 import edu.uci.ics.jung.graph.Graph;
24 import edu.uci.ics.jung.graph.Vertex;
25 import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
26 import edu.uci.ics.jung.graph.impl.SparseVertex;
27 import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
28 import edu.uci.ics.jung.visualization.ArrowFactory;
29 import edu.uci.ics.jung.visualization.Layout;
30 import edu.uci.ics.jung.visualization.PickSupport;
31 import edu.uci.ics.jung.visualization.SettableVertexLocationFunction;
32 import edu.uci.ics.jung.visualization.VisualizationViewer;
33 import edu.uci.ics.jung.visualization.VisualizationViewer.Paintable;
34  
35 /**
36  * A plugin that can create vertices, undirected edges, and directed edges
37  * using mouse gestures.
38  *
39  * @author Tom Nelson - RABA Technologies
40  *
41  */
42 public class EditingGraphMousePlugin extends AbstractGraphMousePlugin implements
43     MouseListener, MouseMotionListener {
44     
45     SettableVertexLocationFunction vertexLocations;
46     Vertex startVertex;
47     Point2D down;
48     
490    CubicCurve2D rawEdge = new CubicCurve2D.Float();
50     Shape edgeShape;
51     Shape rawArrowShape;
52     Shape arrowShape;
53     Paintable edgePaintable;
54     Paintable arrowPaintable;
55     boolean edgeIsDirected;
56     
57     public EditingGraphMousePlugin() {
580        this(MouseEvent.BUTTON1_MASK);
590    }
60  
61     /**
62      * create instance and prepare shapes for visual effects
63      * @param modifiers
64      */
65     public EditingGraphMousePlugin(int modifiers) {
660        super(modifiers);
670        rawEdge.setCurve(0.0f, 0.0f, 0.33f, 100, .66f, -50,
68                 1.0f, 0.0f);
690        rawArrowShape = ArrowFactory.getNotchedArrow(20, 16, 8);
700        edgePaintable = new EdgePaintable();
710        arrowPaintable = new ArrowPaintable();
720    }
73     
74     /**
75      * sets the vertex locations. Needed to place new vertices
76      * @param vertexLocations
77      */
78     public void setVertexLocations(SettableVertexLocationFunction vertexLocations) {
790        this.vertexLocations = vertexLocations;
800    }
81     
82     /**
83      * overrided to be more flexible, and pass events with
84      * key combinations. The default responds to both ButtonOne
85      * and ButtonOne+Shift
86      */
87     public boolean checkModifiers(MouseEvent e) {
880        return (e.getModifiers() & modifiers) != 0;
89     }
90  
91     /**
92      * If the mouse is pressed in an empty area, create a new vertex there.
93      * If the mouse is pressed on an existing vertex, prepare to create
94      * an edge from that vertex to another
95      */
96     public void mousePressed(MouseEvent e) {
970        if(checkModifiers(e)) {
980            final VisualizationViewer vv =
99                 (VisualizationViewer)e.getSource();
1000            final Point2D p = vv.inverseViewTransform(e.getPoint());
1010            PickSupport pickSupport = vv.getPickSupport();
1020            if(pickSupport != null) {
1030                final Vertex vertex = pickSupport.getVertex(p.getX(), p.getY());
1040                if(vertex != null) { // get ready to make an edge
1050                    startVertex = vertex;
1060                    down = e.getPoint();
1070                    transformEdgeShape(down, down);
1080                    vv.addPostRenderPaintable(edgePaintable);
1090                    if((e.getModifiers() & MouseEvent.SHIFT_MASK) != 0) {
1100                        edgeIsDirected = true;
1110                        transformArrowShape(down, e.getPoint());
1120                        vv.addPostRenderPaintable(arrowPaintable);
113                     }
114                 } else { // make a new vertex
1150                    Graph graph = vv.getGraphLayout().getGraph();
1160                    Vertex newVertex = new SparseVertex();
1170                    vertexLocations.setLocation(newVertex, vv.inverseTransform(e.getPoint()));
1180                    Layout layout = vv.getGraphLayout();
1190                    for(Iterator iterator=graph.getVertices().iterator(); iterator.hasNext(); ) {
1200                        layout.lockVertex((Vertex)iterator.next());
121                     }
1220                    graph.addVertex(newVertex);
1230                    vv.getModel().restart();
1240                    for(Iterator iterator=graph.getVertices().iterator(); iterator.hasNext(); ) {
1250                        layout.unlockVertex((Vertex)iterator.next());
126                     }
1270                    vv.repaint();
128                 }
129             }
130         }
1310    }
132     
133     /**
134      * If startVertex is non-null, and the mouse is released over an
135      * existing vertex, create an undirected edge from startVertex to
136      * the vertex under the mouse pointer. If shift was also pressed,
137      * create a directed edge instead.
138      */
139     public void mouseReleased(MouseEvent e) {
1400        if(checkModifiers(e)) {
1410            final VisualizationViewer vv =
142                 (VisualizationViewer)e.getSource();
1430            final Point2D p = vv.inverseViewTransform(e.getPoint());
1440            PickSupport pickSupport = vv.getPickSupport();
1450            if(pickSupport != null) {
1460                final Vertex vertex = pickSupport.getVertex(p.getX(), p.getY());
1470                if(vertex != null && startVertex != null) {
1480                    Graph graph = vv.getGraphLayout().getGraph();
1490                    if(edgeIsDirected) {
1500                        graph.addEdge(new DirectedSparseEdge(startVertex, vertex));
151                     } else {
1520                        graph.addEdge(new UndirectedSparseEdge(startVertex, vertex));
153                     }
1540                    vv.repaint();
155                 }
156             }
1570            startVertex = null;
1580            down = null;
1590            edgeIsDirected = false;
1600            vv.removePostRenderPaintable(edgePaintable);
1610            vv.removePostRenderPaintable(arrowPaintable);
162         }
1630    }
164  
165     /**
166      * If startVertex is non-null, stretch an edge shape between
167      * startVertex and the mouse pointer to simulate edge creation
168      */
169     public void mouseDragged(MouseEvent e) {
1700        if(checkModifiers(e)) {
1710            if(startVertex != null) {
1720                transformEdgeShape(down, e.getPoint());
1730                if(edgeIsDirected) {
1740                    transformArrowShape(down, e.getPoint());
175                 }
176             }
177         }
1780    }
179     
180     /**
181      * code lifted from PluggableRenderer to move an edge shape into an
182      * arbitrary position
183      */
184     private void transformEdgeShape(Point2D down, Point2D out) {
1850        float x1 = (float) down.getX();
1860        float y1 = (float) down.getY();
1870        float x2 = (float) out.getX();
1880        float y2 = (float) out.getY();
189  
1900        AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);
191         
1920        float dx = x2-x1;
1930        float dy = y2-y1;
1940        float thetaRadians = (float) Math.atan2(dy, dx);
1950        xform.rotate(thetaRadians);
1960        float dist = (float) Math.sqrt(dx*dx + dy*dy);
1970        xform.scale(dist / rawEdge.getBounds().getWidth(), 1.0);
1980        edgeShape = xform.createTransformedShape(rawEdge);
1990    }
200     
201     private void transformArrowShape(Point2D down, Point2D out) {
2020        float x1 = (float) down.getX();
2030        float y1 = (float) down.getY();
2040        float x2 = (float) out.getX();
2050        float y2 = (float) out.getY();
206  
2070        AffineTransform xform = AffineTransform.getTranslateInstance(x2, y2);
208         
2090        float dx = x2-x1;
2100        float dy = y2-y1;
2110        float thetaRadians = (float) Math.atan2(dy, dx);
2120        xform.rotate(thetaRadians);
2130        arrowShape = xform.createTransformedShape(rawArrowShape);
2140    }
215     
216     /**
217      * Used for the edge creation visual effect during mouse drag
218      */
219     class EdgePaintable implements Paintable {
220         
221         public void paint(Graphics g) {
222             if(edgeShape != null) {
223                 Color oldColor = g.getColor();
224                 g.setColor(Color.black);
225                 ((Graphics2D)g).draw(edgeShape);
226                 g.setColor(oldColor);
227             }
228         }
229         
230         public boolean useTransform() {
231             return false;
232         }
233     }
234     
235     /**
236      * Used for the directed edge creation visual effect during mouse drag
237      */
238     class ArrowPaintable implements Paintable {
239         
240         public void paint(Graphics g) {
241             if(arrowShape != null) {
242                 Color oldColor = g.getColor();
243                 g.setColor(Color.black);
244                 ((Graphics2D)g).fill(arrowShape);
245                 g.setColor(oldColor);
246             }
247         }
248         
249         public boolean useTransform() {
250             return false;
251         }
252     }
2530    public void mouseClicked(MouseEvent e) {}
2540    public void mouseEntered(MouseEvent e) {}
2550    public void mouseExited(MouseEvent e) {}
2560    public void mouseMoved(MouseEvent e) {}
257 }

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.