001: package prefuse.controls;
002:
003: import java.awt.Cursor;
004: import java.awt.event.MouseEvent;
005:
006: import prefuse.Display;
007: import prefuse.util.ui.UILib;
008: import prefuse.visual.VisualItem;
009:
010: /**
011: * Pans the display, changing the viewable region of the visualization.
012: * By default, panning is accomplished by clicking on the background of a
013: * visualization with the left mouse button and then dragging.
014: *
015: * @author <a href="http://jheer.org">jeffrey heer</a>
016: */
017: public class PanControl extends ControlAdapter {
018:
019: private boolean m_panOverItem;
020: private int m_xDown, m_yDown;
021: private int m_button;
022:
023: /**
024: * Create a new PanControl.
025: */
026: public PanControl() {
027: this (LEFT_MOUSE_BUTTON, false);
028: }
029:
030: /**
031: * Create a new PanControl.
032: * @param panOverItem if true, the panning control will work even while
033: * the mouse is over a visual item.
034: */
035: public PanControl(boolean panOverItem) {
036: this (LEFT_MOUSE_BUTTON, panOverItem);
037: }
038:
039: /**
040: * Create a new PanControl.
041: * @param mouseButton the mouse button that should initiate a pan. One of
042: * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
043: * or {@link Control#RIGHT_MOUSE_BUTTON}.
044: */
045: public PanControl(int mouseButton) {
046: this (mouseButton, false);
047: }
048:
049: /**
050: * Create a new PanControl
051: * @param mouseButton the mouse button that should initiate a pan. One of
052: * {@link Control#LEFT_MOUSE_BUTTON}, {@link Control#MIDDLE_MOUSE_BUTTON},
053: * or {@link Control#RIGHT_MOUSE_BUTTON}.
054: * @param panOverItem if true, the panning control will work even while
055: * the mouse is over a visual item.
056: */
057: public PanControl(int mouseButton, boolean panOverItem) {
058: m_button = mouseButton;
059: m_panOverItem = panOverItem;
060: }
061:
062: // ------------------------------------------------------------------------
063:
064: /**
065: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
066: */
067: public void mousePressed(MouseEvent e) {
068: if (UILib.isButtonPressed(e, m_button)) {
069: e.getComponent().setCursor(
070: Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
071: m_xDown = e.getX();
072: m_yDown = e.getY();
073: }
074: }
075:
076: /**
077: * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
078: */
079: public void mouseDragged(MouseEvent e) {
080: if (UILib.isButtonPressed(e, m_button)) {
081: Display display = (Display) e.getComponent();
082: int x = e.getX(), y = e.getY();
083: int dx = x - m_xDown, dy = y - m_yDown;
084: display.pan(dx, dy);
085: m_xDown = x;
086: m_yDown = y;
087: display.repaint();
088: }
089: }
090:
091: /**
092: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
093: */
094: public void mouseReleased(MouseEvent e) {
095: if (UILib.isButtonPressed(e, m_button)) {
096: e.getComponent().setCursor(Cursor.getDefaultCursor());
097: m_xDown = -1;
098: m_yDown = -1;
099: }
100: }
101:
102: /**
103: * @see prefuse.controls.Control#itemPressed(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
104: */
105: public void itemPressed(VisualItem item, MouseEvent e) {
106: if (m_panOverItem)
107: mousePressed(e);
108: }
109:
110: /**
111: * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
112: */
113: public void itemDragged(VisualItem item, MouseEvent e) {
114: if (m_panOverItem)
115: mouseDragged(e);
116: }
117:
118: /**
119: * @see prefuse.controls.Control#itemReleased(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
120: */
121: public void itemReleased(VisualItem item, MouseEvent e) {
122: if (m_panOverItem)
123: mouseReleased(e);
124: }
125:
126: } // end of class PanControl
|