KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > PanControl


1 package prefuse.controls;
2
3 import java.awt.Cursor JavaDoc;
4 import java.awt.event.MouseEvent JavaDoc;
5
6 import prefuse.Display;
7 import prefuse.util.ui.UILib;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Pans the display, changing the viewable region of the visualization.
13  * By default, panning is accomplished by clicking on the background of a
14  * visualization with the left mouse button and then dragging.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class PanControl extends ControlAdapter {
19
20     private boolean m_panOverItem;
21     private int m_xDown, m_yDown;
22     private int m_button;
23     
24     /**
25      * Create a new PanControl.
26      */

27     public PanControl() {
28         this(LEFT_MOUSE_BUTTON, false);
29     }
30     
31     /**
32      * Create a new PanControl.
33      * @param panOverItem if true, the panning control will work even while
34      * the mouse is over a visual item.
35      */

36     public PanControl(boolean panOverItem) {
37         this(LEFT_MOUSE_BUTTON, panOverItem);
38     }
39     
40     /**
41      * Create a new PanControl.
42      * @param mouseButton the mouse button that should initiate a pan. One of
43      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
44      * or {@link Control#RIGHT_MOUSE_BUTTON}.
45      */

46     public PanControl(int mouseButton) {
47         this(mouseButton, false);
48     }
49     
50     /**
51      * Create a new PanControl
52      * @param mouseButton the mouse button that should initiate a pan. One of
53      * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
54      * or {@link Control#RIGHT_MOUSE_BUTTON}.
55      * @param panOverItem if true, the panning control will work even while
56      * the mouse is over a visual item.
57      */

58     public PanControl(int mouseButton, boolean panOverItem) {
59         m_button = mouseButton;
60         m_panOverItem = panOverItem;
61     }
62     
63     // ------------------------------------------------------------------------
64

65     /**
66      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
67      */

68     public void mousePressed(MouseEvent JavaDoc e) {
69         if ( UILib.isButtonPressed(e, m_button) ) {
70             e.getComponent().setCursor(
71                 Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
72             m_xDown = e.getX();
73             m_yDown = e.getY();
74         }
75     }
76     
77     /**
78      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
79      */

80     public void mouseDragged(MouseEvent JavaDoc e) {
81         if ( UILib.isButtonPressed(e, m_button) ) {
82             Display display = (Display)e.getComponent();
83             int x = e.getX(), y = e.getY();
84             int dx = x-m_xDown, dy = y-m_yDown;
85             display.pan(dx,dy);
86             m_xDown = x;
87             m_yDown = y;
88             display.repaint();
89         }
90     }
91     
92     /**
93      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
94      */

95     public void mouseReleased(MouseEvent JavaDoc e) {
96         if ( UILib.isButtonPressed(e, m_button) ) {
97             e.getComponent().setCursor(Cursor.getDefaultCursor());
98             m_xDown = -1;
99             m_yDown = -1;
100         }
101     }
102     
103     /**
104      * @see prefuse.controls.Control#itemPressed(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
105      */

106     public void itemPressed(VisualItem item, MouseEvent JavaDoc e) {
107         if ( m_panOverItem )
108             mousePressed(e);
109     }
110
111     /**
112      * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
113      */

114     public void itemDragged(VisualItem item, MouseEvent JavaDoc e) {
115         if ( m_panOverItem )
116             mouseDragged(e);
117     }
118     
119     /**
120      * @see prefuse.controls.Control#itemReleased(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
121      */

122     public void itemReleased(VisualItem item, MouseEvent JavaDoc e) {
123         if ( m_panOverItem )
124             mouseReleased(e);
125     }
126     
127 } // end of class PanControl
128
Popular Tags