# Coverage details for edu.uci.ics.jung.visualization.control.RotatingGraphMousePlugin

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.BasicStroke;`
15 |  | `import java.awt.Color;`
16 |  | `import java.awt.Cursor;`
17 |  | `import java.awt.Dimension;`
18 |  | `import java.awt.Graphics2D;`
19 |  | `import java.awt.Point;`
20 |  | `import java.awt.RenderingHints;`
21 |  | `import java.awt.Toolkit;`
22 |  | `import java.awt.event.MouseEvent;`
23 |  | `import java.awt.event.MouseListener;`
24 |  | `import java.awt.event.MouseMotionListener;`
25 |  | `import java.awt.geom.Point2D;`
26 |  | `import java.awt.image.BufferedImage;`
27 |  | `import java.util.Collections;`
28 |  | ``
29 |  | `import edu.uci.ics.jung.visualization.VisualizationViewer;`
30 |  | `import edu.uci.ics.jung.visualization.transform.MutableTransformer;`
31 |  | ``
32 |  | `/**`
33 |  | `* RotatingGraphMouse provides the abiity to rotate the graph using`
34 |  | `* the mouse. By default, it is activated by mouse button one drag`
35 |  | `* with the shift key pressed. The modifiers can be overridden so that`
36 |  | `* a different mouse/key combination activates the rotation`
37 |  | `*`
38 |  | `* @author Tom Nelson`
39 |  | `*/`
40 |  | `public class RotatingGraphMousePlugin extends AbstractGraphMousePlugin`
41 |  | `implements MouseListener, MouseMotionListener {`
42 |  | ``
43 |  | `/**`
44 |  | `* create an instance with default modifier values`
45 |  | `*/`
46 |  | `public RotatingGraphMousePlugin() {`
47 | 0 | `this(MouseEvent.BUTTON1_MASK | MouseEvent.SHIFT_MASK);`
48 | 0 | `}`
49 |  | ``
50 |  | `/**`
51 |  | `* create an instance with passed zoom in/out values`
52 |  | `* @param modifiers the event modifiers to trigger rotation`
53 |  | `*/`
54 |  | `public RotatingGraphMousePlugin(int modifiers) {`
55 | 0 | `super(modifiers);`
56 | 0 | `Dimension cd = Toolkit.getDefaultToolkit().getBestCursorSize(16,16);`
57 | 0 | `BufferedImage cursorImage =`
58 |  | `new BufferedImage(cd.width,cd.height,BufferedImage.TYPE_INT_ARGB);`
59 | 0 | `Graphics2D g = cursorImage.createGraphics();`
60 | 0 | `g.addRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));`
61 | 0 | `g.setColor(new Color(0,0,0,0));`
62 | 0 | `g.fillRect(0,0,16,16);`
63 |  | ``
64 | 0 | `g.setColor(Color.white);`
65 | 0 | `g.setStroke(new BasicStroke(3));`
66 | 0 | `int left = 0;`
67 | 0 | `int top = 0;`
68 | 0 | `int right = 15;`
69 | 0 | `int bottom = 15;`
70 |  | ``
71 | 0 | `g.setColor(Color.black);`
72 | 0 | `g.setStroke(new BasicStroke(1));`
73 |  | `// top bent line`
74 | 0 | `g.drawLine(left+2,top+6,right/2+1,top);`
75 | 0 | `g.drawLine(right/2+1,top,right-2,top+5);`
76 |  | `// bottom bent line`
77 | 0 | `g.drawLine(left+2,bottom-6,right/2,bottom);`
78 | 0 | `g.drawLine(right/2,bottom,right-2,bottom-6);`
79 |  | `// top arrow`
80 | 0 | `g.drawLine(left+2,top+6,left+5,top+6);`
81 | 0 | `g.drawLine(left+2,top+6,left+2,top+3);`
82 |  | `// bottom arrow`
83 | 0 | `g.drawLine(right-2,bottom-6,right-6,bottom-6);`
84 | 0 | `g.drawLine(right-2, bottom-6,right-2,bottom-3);`
85 |  | ``
86 | 0 | `g.dispose();`
87 |  | ``
88 | 0 | `cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point(), "RotateCursor");`
89 | 0 | `}`
90 |  | ``
91 |  | `/**`
92 |  | `* save the 'down' point and check the modifiers. If the`
93 |  | `* modifiers are accepted, set the cursor to the 'hand' cursor`
94 |  | `* @param e the event`
95 |  | `*/`
96 |  | `public void mousePressed(MouseEvent e) {`
97 | 0 | `VisualizationViewer vv = (VisualizationViewer)e.getSource();`
98 | 0 | `boolean accepted = checkModifiers(e);`
99 | 0 | `down = e.getPoint();`
100 | 0 | `if(accepted) {`
101 | 0 | `vv.setCursor(cursor);`
102 |  | `}`
103 | 0 | `}`
104 |  | ``
105 |  | `/**`
106 |  | `* unset the down point and change the cursor back to the default`
107 |  | `*/`
108 |  | `public void mouseReleased(MouseEvent e) {`
109 | 0 | `VisualizationViewer vv = (VisualizationViewer)e.getSource();`
110 | 0 | `down = null;`
111 | 0 | `vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));`
112 | 0 | `}`
113 |  | ``
114 |  | `/**`
115 |  | `* check the modifiers. If accepted, use the mouse drag motion`
116 |  | `* to rotate the graph`
117 |  | `*/`
118 |  | `public void mouseDragged(MouseEvent e) {`
119 | 0 | `if(down == null) return;`
120 | 0 | `VisualizationViewer vv = (VisualizationViewer)e.getSource();`
121 | 0 | `boolean accepted = checkModifiers(e);`
122 | 0 | `if(accepted) {`
123 | 0 | `MutableTransformer modelTransformer =`
124 |  | `vv.getLayoutTransformer();`
125 |  | `// rotate`
126 | 0 | `vv.setCursor(cursor);`
127 |  | ``
128 | 0 | `Point2D center = vv.getCenter();`
129 | 0 | `Point2D q = down;`
130 | 0 | `Point2D p = e.getPoint();`
131 | 0 | `Point2D v1 = new Point2D.Double(center.getX()-p.getX(), center.getY()-p.getY());`
132 | 0 | `Point2D v2 = new Point2D.Double(center.getX()-q.getX(), center.getY()-q.getY());`
133 | 0 | `double theta = angleBetween(v1, v2);`
134 | 0 | `modelTransformer.rotate(theta, vv.inverseViewTransform(center));`
135 | 0 | `down.x = e.getX();`
136 | 0 | `down.y = e.getY();`
137 |  | ``
138 | 0 | `e.consume();`
139 |  | `}`
140 | 0 | `}`
141 |  | ``
142 |  | `/**`
143 |  | `* Returns the angle between two vectors from the origin`
144 |  | `* to points v1 and v2.`
145 |  | `* @param v1`
146 |  | `* @param v2`
147 |  | `* @return`
148 |  | `*/`
149 |  | `protected double angleBetween(Point2D v1, Point2D v2) {`
150 | 0 | `double x1 = v1.getX();`
151 | 0 | `double y1 = v1.getY();`
152 | 0 | `double x2 = v2.getX();`
153 | 0 | `double y2 = v2.getY();`
154 |  | `// cross product for direction`
155 | 0 | `double cross = x1*y2 - x2*y1;`
156 | 0 | `int cw = 1;`
157 | 0 | `if(cross > 0) {`
158 | 0 | `cw = -1;`
159 |  | `}`
160 |  | `// dot product for angle`
161 | 0 | `double angle =`
162 |  | `cw*Math.acos( ( x1*x2 + y1*y2 ) /`
163 |  | `( Math.sqrt( x1*x1 + y1*y1 ) *`
164 |  | `Math.sqrt( x2*x2 + y2*y2 ) ) );`
165 | 0 | `if(Double.isNaN(angle)) {`
166 | 0 | `angle = 0;`
167 |  | `}`
168 | 0 | `return angle;`
169 |  | `}`
170 |  | ``
171 |  | `public void mouseClicked(MouseEvent e) {`
172 | 0 | `}`
173 |  | ``
174 |  | `public void mouseEntered(MouseEvent e) {`
175 | 0 | `}`
176 |  | ``
177 |  | `public void mouseExited(MouseEvent e) {`
178 | 0 | `}`
179 |  | ``
180 |  | `public void mouseMoved(MouseEvent e) {`
181 | 0 | `}`
182 |  | `}`

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.