KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > RotationControl


1 package prefuse.controls;
2
3 import java.awt.Cursor JavaDoc;
4 import java.awt.Point JavaDoc;
5 import java.awt.event.MouseEvent JavaDoc;
6
7 import prefuse.Display;
8 import prefuse.util.ui.UILib;
9
10
11 /**
12  * Control that can be used to rotate the display. This results in a
13  * transformation of the display itself, such that all aspects are
14  * rotated. For example, after a rotation of 180 degrees, upright
15  * text strings will subsequently upside down. To rotate item positions
16  * but leave other aspects such as orientation intact, you can
17  * instead create a new {@link prefuse.action.Action} module that rotates
18  * just the item co-ordinates.
19  *
20  * @author <a HREF="http://jheer.org">jeffrey heer</a>
21  */

22 public class RotationControl extends ControlAdapter {
23
24     private Point JavaDoc down = new Point JavaDoc();
25     private double baseAngle = 0; // the baseline angle of the rotation
26
private int m_button; // the mouse button to use
27

28     /**
29      * Create a new RotateControl. Rotations will be initiated by dragging
30      * the mouse with the left mouse button pressed.
31      */

32     public RotationControl() {
33         this(Control.LEFT_MOUSE_BUTTON);
34     }
35     
36     /**
37      * Create a new RotateControl
38      * @param mouseButton the mouse button that should initiate a rotation. One
39      * of {@link Control#LEFT_MOUSE_BUTTON},
40      * {@link Control#MIDDLE_MOUSE_BUTTON}, or
41      * {@link Control#RIGHT_MOUSE_BUTTON}.
42      */

43     public RotationControl(int mouseButton) {
44         m_button = mouseButton;
45     }
46     
47     /**
48      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
49      */

50     public void mousePressed(MouseEvent JavaDoc e) {
51         if ( UILib.isButtonPressed(e, m_button) ) {
52             Display display = (Display)e.getComponent();
53             display.setCursor(
54                 Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
55             down.setLocation(e.getPoint());
56             baseAngle = Double.NaN;
57         }
58     }
59     
60     /**
61      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
62      */

63     public void mouseDragged(MouseEvent JavaDoc e) {
64         if ( UILib.isButtonPressed(e, m_button) ) {
65             int dy = e.getY() - down.y;
66             int dx = e.getX() - down.x;
67             double angle = Math.atan2(dy, dx);
68             
69             // only rotate once the base angle has been established
70
if ( !Double.isNaN(baseAngle) ) {
71                 Display display = (Display)e.getComponent();
72                 display.rotate(down, angle-baseAngle);
73             }
74             baseAngle = angle;
75         }
76     }
77     
78     /**
79      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
80      */

81     public void mouseReleased(MouseEvent JavaDoc e) {
82         if ( UILib.isButtonPressed(e, m_button) ) {
83             e.getComponent().setCursor(Cursor.getDefaultCursor());
84         }
85     }
86     
87 } // end of class RotationControl
88
Popular Tags