KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > controls > DragControl


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 javax.swing.SwingUtilities JavaDoc;
8
9 import prefuse.Display;
10 import prefuse.data.Table;
11 import prefuse.data.event.EventConstants;
12 import prefuse.data.event.TableListener;
13 import prefuse.visual.VisualItem;
14
15
16 /**
17  * Changes a node's location when dragged on screen. Other effects
18  * include fixing a node's position when the mouse if over it, and
19  * changing the mouse cursor to a hand when the mouse passes over an
20  * item.
21  *
22  * @author <a HREF="http://jheer.org">jeffrey heer</a>
23  */

24 public class DragControl extends ControlAdapter implements TableListener {
25
26     private VisualItem activeItem;
27     protected String JavaDoc action;
28     protected Point2D JavaDoc down = new Point2D.Double JavaDoc();
29     protected Point2D JavaDoc temp = new Point2D.Double JavaDoc();
30     protected boolean dragged, wasFixed, resetItem;
31     private boolean fixOnMouseOver = true;
32     protected boolean repaint = true;
33     
34     /**
35      * Creates a new drag control that issues repaint requests as an item
36      * is dragged.
37      */

38     public DragControl() {
39     }
40     
41     /**
42      * Creates a new drag control that optionally issues repaint requests
43      * as an item is dragged.
44      * @param repaint indicates whether or not repaint requests are issued
45      * as drag events occur. This can be set to false if other activities
46      * (for example, a continuously running force simulation) are already
47      * issuing repaint events.
48      */

49     public DragControl(boolean repaint) {
50         this.repaint = repaint;
51     }
52     
53     /**
54      * Creates a new drag control that optionally issues repaint requests
55      * as an item is dragged.
56      * @param repaint indicates whether or not repaint requests are issued
57      * as drag events occur. This can be set to false if other activities
58      * (for example, a continuously running force simulation) are already
59      * issuing repaint events.
60      * @param fixOnMouseOver indicates if object positions should become
61      * fixed (made stationary) when the mouse pointer is over an item.
62      */

63     public DragControl(boolean repaint, boolean fixOnMouseOver) {
64         this.repaint = repaint;
65         this.fixOnMouseOver = fixOnMouseOver;
66     }
67     
68     /**
69      * Creates a new drag control that invokes an action upon drag events.
70      * @param action the action to run when drag events occur.
71      */

72     public DragControl(String JavaDoc action) {
73         this.repaint = false;
74         this.action = action;
75     }
76     
77     /**
78      * Creates a new drag control that invokes an action upon drag events.
79      * @param action the action to run when drag events occur
80      * @param fixOnMouseOver indicates if object positions should become
81      * fixed (made stationary) when the mouse pointer is over an item.
82      */

83     public DragControl(String JavaDoc action, boolean fixOnMouseOver) {
84         this.repaint = false;
85         this.fixOnMouseOver = fixOnMouseOver;
86         this.action = action;
87     }
88     
89     /**
90      * Determines whether or not an item should have it's position fixed
91      * when the mouse moves over it.
92      * @param s whether or not item position should become fixed upon
93      * mouse over.
94      */

95     public void setFixPositionOnMouseOver(boolean s) {
96         fixOnMouseOver = s;
97     }
98     
99     /**
100      * @see prefuse.controls.Control#itemEntered(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
101      */

102     public void itemEntered(VisualItem item, MouseEvent JavaDoc e) {
103         Display d = (Display)e.getSource();
104         d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
105         activeItem = item;
106         if ( fixOnMouseOver ) {
107             wasFixed = item.isFixed();
108             resetItem = true;
109             item.setFixed(true);
110             item.getTable().addTableListener(this);
111         }
112     }
113     
114     /**
115      * @see prefuse.controls.Control#itemExited(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
116      */

117     public void itemExited(VisualItem item, MouseEvent JavaDoc e) {
118         if ( activeItem == item ) {
119             activeItem = null;
120             item.getTable().removeTableListener(this);
121             if ( resetItem ) item.setFixed(wasFixed);
122         }
123         Display d = (Display)e.getSource();
124         d.setCursor(Cursor.getDefaultCursor());
125     } //
126

127     /**
128      * @see prefuse.controls.Control#itemPressed(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
129      */

130     public void itemPressed(VisualItem item, MouseEvent JavaDoc e) {
131         if (!SwingUtilities.isLeftMouseButton(e)) return;
132         if ( !fixOnMouseOver ) {
133             wasFixed = item.isFixed();
134             resetItem = true;
135             item.setFixed(true);
136             item.getTable().addTableListener(this);
137         }
138         dragged = false;
139         Display d = (Display)e.getComponent();
140         d.getAbsoluteCoordinate(e.getPoint(), down);
141     }
142     
143     /**
144      * @see prefuse.controls.Control#itemReleased(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
145      */

146     public void itemReleased(VisualItem item, MouseEvent JavaDoc e) {
147         if (!SwingUtilities.isLeftMouseButton(e)) return;
148         if ( dragged ) {
149             activeItem = null;
150             item.getTable().removeTableListener(this);
151             if ( resetItem ) item.setFixed(wasFixed);
152             dragged = false;
153         }
154     }
155     
156     /**
157      * @see prefuse.controls.Control#itemDragged(prefuse.visual.VisualItem, java.awt.event.MouseEvent)
158      */

159     public void itemDragged(VisualItem item, MouseEvent JavaDoc e) {
160         if (!SwingUtilities.isLeftMouseButton(e)) return;
161         dragged = true;
162         Display d = (Display)e.getComponent();
163         d.getAbsoluteCoordinate(e.getPoint(), temp);
164         double dx = temp.getX()-down.getX();
165         double dy = temp.getY()-down.getY();
166         double x = item.getX();
167         double y = item.getY();
168
169         item.setStartX(x); item.setStartY(y);
170         item.setX(x+dx); item.setY(y+dy);
171         item.setEndX(x+dx); item.setEndY(y+dy);
172         
173         if ( repaint )
174             item.getVisualization().repaint();
175         
176         down.setLocation(temp);
177         if ( action != null )
178             d.getVisualization().run(action);
179     }
180
181     /**
182      * @see prefuse.data.event.TableListener#tableChanged(prefuse.data.Table, int, int, int, int)
183      */

184     public void tableChanged(Table t, int start, int end, int col, int type) {
185         if ( activeItem == null || type != EventConstants.UPDATE
186                 || col != t.getColumnNumber(VisualItem.FIXED) )
187             return;
188         int row = activeItem.getRow();
189         if ( row >= start && row <= end )
190             resetItem = false;
191     }
192
193 } // end of class DragControl
194
Popular Tags