| Line | Hits | Source |
|---|---|---|
| 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() { | |
| 51 | 0 | this(InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK); |
| 52 | 0 | } |
| 53 | ||
| 54 | /** | |
| 55 | * create an instance, overriding the default modifiers | |
| 56 | * @param selectionModifiers | |
| 57 | */ | |
| 58 | public AnimatedPickingGraphMousePlugin(int selectionModifiers) { | |
| 59 | 0 | super(selectionModifiers); |
| 60 | 0 | this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); |
| 61 | 0 | } |
| 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) { | |
| 68 | 0 | if (e.getModifiers() == modifiers) { |
| 69 | 0 | VisualizationViewer vv = (VisualizationViewer) e.getSource(); |
| 70 | 0 | PickSupport pickSupport = vv.getPickSupport(); |
| 71 | 0 | PickedState pickedState = vv.getPickedState(); |
| 72 | 0 | if (pickSupport != null && pickedState != null) { |
| 73 | // p is the screen point for the mouse event | |
| 74 | 0 | Point2D p = e.getPoint(); |
| 75 | // take away the view transform | |
| 76 | 0 | Point2D ip = vv.inverseViewTransform(p); |
| 77 | ||
| 78 | 0 | vertex = pickSupport.getVertex(ip.getX(), ip.getY()); |
| 79 | 0 | if (vertex != null) { |
| 80 | 0 | if (pickedState.isPicked(vertex) == false) { |
| 81 | 0 | pickedState.clearPickedVertices(); |
| 82 | 0 | pickedState.pick(vertex, true); |
| 83 | } | |
| 84 | } | |
| 85 | } | |
| 86 | 0 | e.consume(); |
| 87 | } | |
| 88 | 0 | } |
| 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) { | |
| 99 | 0 | if (e.getModifiers() == modifiers) { |
| 100 | 0 | final VisualizationViewer vv = (VisualizationViewer) e.getSource(); |
| 101 | 0 | if (vertex != null) { |
| 102 | 0 | Layout layout = vv.getGraphLayout(); |
| 103 | 0 | Point2D q = layout.getLocation(vertex); |
| 104 | 0 | Point2D lvc = vv.inverseTransform(vv.getCenter()); |
| 105 | 0 | final double dx = (lvc.getX() - q.getX()) / 10; |
| 106 | 0 | final double dy = (lvc.getY() - q.getY()) / 10; |
| 107 | ||
| 108 | 0 | 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 | }; | |
| 120 | 0 | Thread thread = new Thread(animator); |
| 121 | 0 | thread.start(); |
| 122 | } | |
| 123 | } | |
| 124 | 0 | } |
| 125 | ||
| 126 | public void mouseClicked(MouseEvent e) { | |
| 127 | 0 | } |
| 128 | ||
| 129 | /** | |
| 130 | * show a special cursor while the mouse is inside the window | |
| 131 | */ | |
| 132 | public void mouseEntered(MouseEvent e) { | |
| 133 | 0 | JComponent c = (JComponent)e.getSource(); |
| 134 | 0 | c.setCursor(cursor); |
| 135 | 0 | } |
| 136 | ||
| 137 | /** | |
| 138 | * revert to the default cursor when the mouse leaves this window | |
| 139 | */ | |
| 140 | public void mouseExited(MouseEvent e) { | |
| 141 | 0 | JComponent c = (JComponent)e.getSource(); |
| 142 | 0 | c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
| 143 | 0 | } |
| 144 | ||
| 145 | public void mouseMoved(MouseEvent e) { | |
| 146 | 0 | } |
| 147 | ||
| 148 | public void mouseDragged(MouseEvent arg0) { | |
| 149 | 0 | } |
| 150 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |