KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > AnchorUpdateControl


1 package prefuse.controls;
2
3 import java.awt.event.MouseEvent JavaDoc;
4 import java.awt.geom.Point2D JavaDoc;
5
6 import prefuse.Display;
7 import prefuse.action.layout.Layout;
8 import prefuse.visual.VisualItem;
9
10
11 /**
12  * Follows the mouse cursor, updating the anchor parameter for any number
13  * of layout instances to match the current cursor position. Will also
14  * run a given activity in response to cursor updates.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class AnchorUpdateControl extends ControlAdapter {
19     
20     private boolean m_anchorOverItem;
21     private Layout[] m_layouts;
22     private String JavaDoc m_action;
23     private Point2D JavaDoc m_tmp = new Point2D.Double JavaDoc();
24     
25     /**
26      * Create a new AnchorUpdateControl.
27      * @param layout the layout for which to update the anchor point
28      */

29     public AnchorUpdateControl(Layout layout) {
30         this(layout,null);
31     }
32
33     /**
34      * Create a new AnchorUpdateControl.
35      * @param layout the layout for which to update the anchor point
36      * @param action the name of an action to run upon anchor updates
37      */

38     public AnchorUpdateControl(Layout layout, String JavaDoc action) {
39         this(new Layout[] {layout}, action);
40     }
41
42     /**
43      * Create a new AnchorUpdateControl.
44      * @param layout the layout for which to update the anchor point
45      * @param action the name of an action to run upon anchor updates
46      * @param overItem indicates if anchor update events should be processed
47      * while the mouse cursor is hovered over a VisualItem.
48      */

49     public AnchorUpdateControl(Layout layout, String JavaDoc action, boolean overItem)
50     {
51         this(new Layout[] {layout}, action, overItem);
52     }
53     
54     /**
55      * Create a new AnchorUpdateControl.
56      * @param layout the layouts for which to update the anchor point
57      * @param action the name of an action to run upon anchor updates
58      */

59     public AnchorUpdateControl(Layout[] layout, String JavaDoc action) {
60         this(layout, action, true);
61     }
62     
63     /**
64      * Create a new AnchorUpdateControl.
65      * @param layout the layouts for which to update the anchor point
66      * @param action the name of an action to run upon anchor updates
67      * @param overItem indicates if anchor update events should be processed
68      * while the mouse cursor is hovered over a VisualItem.
69      */

70     public AnchorUpdateControl(Layout[] layout, String JavaDoc action, boolean overItem)
71     {
72         m_layouts = (Layout[])layout.clone();
73         m_action = action;
74         m_anchorOverItem = overItem;
75     }
76     
77     // ------------------------------------------------------------------------
78

79     /**
80      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
81      */

82     public void mouseExited(MouseEvent JavaDoc e) {
83         for ( int i=0; i<m_layouts.length; i++ )
84             m_layouts[i].setLayoutAnchor(null);
85         runAction(e);
86     }
87     
88     /**
89      * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
90      */

91     public void mouseMoved(MouseEvent JavaDoc e) {
92         moveEvent(e);
93     }
94     
95     /**
96      * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
97      */

98     public void mouseDragged(MouseEvent JavaDoc e) {
99         moveEvent(e);
100     }
101     
102     /**
103      * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
104      */

105     public void itemDragged(VisualItem item, MouseEvent JavaDoc e) {
106         if ( m_anchorOverItem ) 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 JavaDoc e) {
113         if ( m_anchorOverItem ) moveEvent(e);
114     }
115     
116     /**
117      * Registers a mouse move event, updating the anchor point for all
118      * registered layout instances.
119      * @param e the MouseEvent
120      */

121     public void moveEvent(MouseEvent JavaDoc e) {
122         Display d = (Display)e.getSource();
123         d.getAbsoluteCoordinate(e.getPoint(), m_tmp);
124         for ( int i=0; i<m_layouts.length; i++ )
125             m_layouts[i].setLayoutAnchor(m_tmp);
126         runAction(e);
127     }
128
129     /**
130      * Runs an optional action upon anchor update.
131      * @param e MouseEvent
132      */

133     private void runAction(MouseEvent JavaDoc e) {
134         if ( m_action != null ) {
135             Display d = (Display)e.getSource();
136             d.getVisualization().run(m_action);
137         }
138     }
139         
140 } // end of class AnchorUpdateControl
141
Popular Tags