001: package prefuse.controls;
002:
003: import java.awt.event.MouseEvent;
004: import java.awt.geom.Point2D;
005:
006: import prefuse.Display;
007: import prefuse.action.layout.Layout;
008: import prefuse.visual.VisualItem;
009:
010: /**
011: * Follows the mouse cursor, updating the anchor parameter for any number
012: * of layout instances to match the current cursor position. Will also
013: * run a given activity in response to cursor updates.
014: *
015: * @author <a href="http://jheer.org">jeffrey heer</a>
016: */
017: public class AnchorUpdateControl extends ControlAdapter {
018:
019: private boolean m_anchorOverItem;
020: private Layout[] m_layouts;
021: private String m_action;
022: private Point2D m_tmp = new Point2D.Double();
023:
024: /**
025: * Create a new AnchorUpdateControl.
026: * @param layout the layout for which to update the anchor point
027: */
028: public AnchorUpdateControl(Layout layout) {
029: this (layout, null);
030: }
031:
032: /**
033: * Create a new AnchorUpdateControl.
034: * @param layout the layout for which to update the anchor point
035: * @param action the name of an action to run upon anchor updates
036: */
037: public AnchorUpdateControl(Layout layout, String action) {
038: this (new Layout[] { layout }, action);
039: }
040:
041: /**
042: * Create a new AnchorUpdateControl.
043: * @param layout the layout for which to update the anchor point
044: * @param action the name of an action to run upon anchor updates
045: * @param overItem indicates if anchor update events should be processed
046: * while the mouse cursor is hovered over a VisualItem.
047: */
048: public AnchorUpdateControl(Layout layout, String action,
049: boolean overItem) {
050: this (new Layout[] { layout }, action, overItem);
051: }
052:
053: /**
054: * Create a new AnchorUpdateControl.
055: * @param layout the layouts for which to update the anchor point
056: * @param action the name of an action to run upon anchor updates
057: */
058: public AnchorUpdateControl(Layout[] layout, String action) {
059: this (layout, action, true);
060: }
061:
062: /**
063: * Create a new AnchorUpdateControl.
064: * @param layout the layouts for which to update the anchor point
065: * @param action the name of an action to run upon anchor updates
066: * @param overItem indicates if anchor update events should be processed
067: * while the mouse cursor is hovered over a VisualItem.
068: */
069: public AnchorUpdateControl(Layout[] layout, String action,
070: boolean overItem) {
071: m_layouts = (Layout[]) layout.clone();
072: m_action = action;
073: m_anchorOverItem = overItem;
074: }
075:
076: // ------------------------------------------------------------------------
077:
078: /**
079: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
080: */
081: public void mouseExited(MouseEvent e) {
082: for (int i = 0; i < m_layouts.length; i++)
083: m_layouts[i].setLayoutAnchor(null);
084: runAction(e);
085: }
086:
087: /**
088: * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
089: */
090: public void mouseMoved(MouseEvent e) {
091: moveEvent(e);
092: }
093:
094: /**
095: * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
096: */
097: public void mouseDragged(MouseEvent e) {
098: moveEvent(e);
099: }
100:
101: /**
102: * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
103: */
104: public void itemDragged(VisualItem item, MouseEvent e) {
105: if (m_anchorOverItem)
106: moveEvent(e);
107: }
108:
109: /**
110: * @see prefuse.controls.Control#itemMoved(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
111: */
112: public void itemMoved(VisualItem item, MouseEvent e) {
113: if (m_anchorOverItem)
114: moveEvent(e);
115: }
116:
117: /**
118: * Registers a mouse move event, updating the anchor point for all
119: * registered layout instances.
120: * @param e the MouseEvent
121: */
122: public void moveEvent(MouseEvent e) {
123: Display d = (Display) e.getSource();
124: d.getAbsoluteCoordinate(e.getPoint(), m_tmp);
125: for (int i = 0; i < m_layouts.length; i++)
126: m_layouts[i].setLayoutAnchor(m_tmp);
127: runAction(e);
128: }
129:
130: /**
131: * Runs an optional action upon anchor update.
132: * @param e MouseEvent
133: */
134: private void runAction(MouseEvent e) {
135: if (m_action != null) {
136: Display d = (Display) e.getSource();
137: d.getVisualization().run(m_action);
138: }
139: }
140:
141: } // end of class AnchorUpdateControl
|