KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > ZoomControl


1 package prefuse.controls;
2
3 import java.awt.Cursor JavaDoc;
4 import java.awt.event.MouseEvent JavaDoc;
5 import java.awt.geom.Point2D JavaDoc;
6
7 import prefuse.Display;
8 import prefuse.util.ui.UILib;
9 import prefuse.visual.VisualItem;
10
11
12 /**
13  * Zooms the display, changing the scale of the viewable region. By default,
14  * zooming is achieved by pressing the right mouse button on the background
15  * of the visualization and dragging the mouse up or down. Moving the mouse up
16  * zooms out the display around the spot the mouse was originally pressed.
17  * Moving the mouse down similarly zooms in the display, making items
18  * larger.
19  *
20  * @author <a HREF="http://jheer.org">jeffrey heer</a>
21  */

22 public class ZoomControl extends AbstractZoomControl {
23     
24     private int yLast;
25     private Point2D JavaDoc down = new Point2D.Float JavaDoc();
26     private int button = RIGHT_MOUSE_BUTTON;
27     
28     /**
29      * Create a new zoom control.
30      */

31     public ZoomControl() {
32         // do nothing
33
}
34     
35     /**
36      * Create a new zoom control.
37      * @param mouseButton the mouse button that should initiate a zoom. One of
38      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
39      * or {@link Control#RIGHT_MOUSE_BUTTON}.
40      */

41     public ZoomControl(int mouseButton) {
42         button = mouseButton;
43     }
44     
45     /**
46      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
47      */

48     public void mousePressed(MouseEvent JavaDoc e) {
49         if ( UILib.isButtonPressed(e, button) ) {
50             Display display = (Display)e.getComponent();
51             if (display.isTranformInProgress()) {
52                 yLast = -1;
53                 System.err.println("can't move");
54                 return;
55             }
56             display.setCursor(
57                 Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
58             display.getAbsoluteCoordinate(e.getPoint(), down);
59             yLast = e.getY();
60         }
61     }
62     
63     /**
64      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
65      */

66     public void mouseDragged(MouseEvent JavaDoc e) {
67         if ( UILib.isButtonPressed(e, button) ) {
68             Display display = (Display)e.getComponent();
69             if (display.isTranformInProgress() || yLast == -1) {
70                 yLast = -1;
71                 return;
72             }
73             
74             int y = e.getY();
75             int dy = y-yLast;
76             double zoom = 1 + ((double)dy) / 100;
77             
78             int status = zoom(display, down, zoom, true);
79             int cursor = Cursor.N_RESIZE_CURSOR;
80             if ( status == NO_ZOOM )
81                 cursor = Cursor.WAIT_CURSOR;
82             display.setCursor(Cursor.getPredefinedCursor(cursor));
83             
84             yLast = y;
85         }
86     }
87
88     /**
89      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
90      */

91     public void mouseReleased(MouseEvent JavaDoc e) {
92         if ( UILib.isButtonPressed(e, button) ) {
93             e.getComponent().setCursor(Cursor.getDefaultCursor());
94         }
95     }
96     
97     /**
98      * @see prefuse.controls.Control#itemPressed(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
99      */

100     public void itemPressed(VisualItem item, MouseEvent JavaDoc e) {
101         if ( m_zoomOverItem )
102             mousePressed(e);
103     }
104
105     /**
106      * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
107      */

108     public void itemDragged(VisualItem item, MouseEvent JavaDoc e) {
109         if ( m_zoomOverItem )
110             mouseDragged(e);
111     }
112     
113     /**
114      * @see prefuse.controls.Control#itemReleased(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
115      */

116     public void itemReleased(VisualItem item, MouseEvent JavaDoc e) {
117         if ( m_zoomOverItem )
118             mouseReleased(e);
119     }
120     
121 } // end of class ZoomControl
122
Popular Tags