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

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.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.event.MouseMotionListener;
19 import java.awt.geom.Point2D;
20  
21 import javax.swing.JComponent;
22  
23 import edu.uci.ics.jung.graph.Vertex;
24 import edu.uci.ics.jung.visualization.Layout;
25 import edu.uci.ics.jung.visualization.PickSupport;
26 import edu.uci.ics.jung.visualization.PickedState;
27 import edu.uci.ics.jung.visualization.VisualizationViewer;
28  
29 /**
30  * AnimatedPickingGraphMousePlugin supports the picking of one Graph
31  * Vertex. When the mouse is released, the graph is translated so that
32  * the picked Vertex is moved to the center of the view. This translateion
33  * is conducted in an animation Thread so that the graph slides to its
34  * new position
35  *
36  * @author Tom Nelson
37  */
38 public class AnimatedPickingGraphMousePlugin extends AbstractGraphMousePlugin
39     implements MouseListener, MouseMotionListener {
40  
41     /**
42      * the picked Vertex
43      */
44     protected Vertex vertex;
45     
46     /**
47      * create an instance with default modifiers
48      *
49      */
50     public AnimatedPickingGraphMousePlugin() {
510        this(InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK);
520    }
53  
54     /**
55      * create an instance, overriding the default modifiers
56      * @param selectionModifiers
57      */
58     public AnimatedPickingGraphMousePlugin(int selectionModifiers) {
590        super(selectionModifiers);
600        this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
610    }
62  
63     /**
64      * If the event occurs on a Vertex, pick that single Vertex
65      * @param e the event
66      */
67     public void mousePressed(MouseEvent e) {
680        if (e.getModifiers() == modifiers) {
690            VisualizationViewer vv = (VisualizationViewer) e.getSource();
700            PickSupport pickSupport = vv.getPickSupport();
710            PickedState pickedState = vv.getPickedState();
720            if (pickSupport != null && pickedState != null) {
73                 // p is the screen point for the mouse event
740                Point2D p = e.getPoint();
75                 // take away the view transform
760                Point2D ip = vv.inverseViewTransform(p);
77  
780                vertex = pickSupport.getVertex(ip.getX(), ip.getY());
790                if (vertex != null) {
800                    if (pickedState.isPicked(vertex) == false) {
810                        pickedState.clearPickedVertices();
820                        pickedState.pick(vertex, true);
83                     }
84                 }
85             }
860            e.consume();
87         }
880    }
89  
90  
91 /**
92  * If a Vertex was picked in the mousePressed event, start a Thread
93  * to animate the translation of the graph so that the picked Vertex
94  * moves to the center of the view
95  *
96  * @param e the event
97  */
98     public void mouseReleased(MouseEvent e) {
990        if (e.getModifiers() == modifiers) {
1000            final VisualizationViewer vv = (VisualizationViewer) e.getSource();
1010            if (vertex != null) {
1020                Layout layout = vv.getGraphLayout();
1030                Point2D q = layout.getLocation(vertex);
1040                Point2D lvc = vv.inverseTransform(vv.getCenter());
1050                final double dx = (lvc.getX() - q.getX()) / 10;
1060                final double dy = (lvc.getY() - q.getY()) / 10;
107  
1080                Runnable animator = new Runnable() {
109  
110                     public void run() {
111                         for (int i = 0; i < 10; i++) {
112                             vv.getLayoutTransformer().translate(dx, dy);
113                             try {
114                                 Thread.sleep(100);
115                             } catch (InterruptedException ex) {
116                             }
117                         }
118                     }
119                 };
1200                Thread thread = new Thread(animator);
1210                thread.start();
122             }
123         }
1240    }
125      
126     public void mouseClicked(MouseEvent e) {
1270    }
128  
129     /**
130      * show a special cursor while the mouse is inside the window
131      */
132     public void mouseEntered(MouseEvent e) {
1330        JComponent c = (JComponent)e.getSource();
1340        c.setCursor(cursor);
1350    }
136  
137     /**
138      * revert to the default cursor when the mouse leaves this window
139      */
140     public void mouseExited(MouseEvent e) {
1410        JComponent c = (JComponent)e.getSource();
1420        c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
1430    }
144  
145     public void mouseMoved(MouseEvent e) {
1460    }
147  
148     public void mouseDragged(MouseEvent arg0) {
1490    }
150 }

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.