KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > ui > VVMouseDragListener


1 /*
2  * VVMouseDragListener.java
3  *
4  * Created on 28 August 2003, 13:53
5  */

6
7 package com.thoughtriver.open.vectorvisuals.ui;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.geom.*;
12 import java.util.*;
13
14 import com.thoughtriver.open.vectorvisuals.*;
15
16 /**
17  * This class extends the basic <CODE>VVMouseListener</CODE> to add support
18  * for mouse drag activities, such as dragging the viewport and individual
19  * objects around.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 09:00:37 $
23  */

24 public class VVMouseDragListener extends VVMouseListener implements MouseMotionListener {
25
26     /** The current object being dragged, or null if there is no drag occurring */
27     private Object JavaDoc objectBeingDragged = null;
28
29     /** When a drag is occurring, this is the last known mouse location */
30     private Point lastKnownPoint = null;
31
32     /** Whether or not the viewport itself can be moved by dragging */
33     private boolean viewportDraggable = false;
34
35     /** The <CODE>VisualObject</CODE>s that can be moved by dragging */
36     private final Set<VisualObject> draggableObjects = new HashSet<VisualObject>();
37
38     /**
39      * Creates a new instance of <CODE>VVMouseDragListener</CODE> associated
40      * with the supplied <CODE>VVDisplay</CODE>.
41      *
42      * @param vvDisplay the <CODE>VVDisplay</CODE> for which events are being
43      * handled
44      */

45     public VVMouseDragListener(final VVDisplay vvDisplay) {
46         super(vvDisplay);
47     }
48
49     /**
50      * Creates a new instance of <CODE>VVMouseDragListener</CODE> associated
51      * with the supplied <CODE>VVDisplay</CODE> and <CODE>SelectionManager</CODE>.
52      *
53      * @param vvDisplay the <CODE>VVDisplay</CODE> for which events are being
54      * handled
55      * @param selectionManager the <CODE>SelectionManager</CODE> that will be
56      * communicated with regarding selection activities
57      */

58     public VVMouseDragListener(final VVDisplay vvDisplay, final SelectionManager selectionManager) {
59         super(vvDisplay, selectionManager);
60     }
61
62     /**
63      * Returns whether or not the viewport itself can be moved by dragging.
64      *
65      * @return whether or not the viewport itself can be moved by dragging
66      */

67     public boolean isViewportDraggable() {
68         return viewportDraggable;
69     }
70
71     /**
72      * Sets whether or not the viewport itself can be moved by dragging.
73      *
74      * @param viewportDraggable whether or not the viewport itself can be moved
75      * by dragging
76      */

77     public void setViewportDraggable(final boolean viewportDraggable) {
78         this.viewportDraggable = viewportDraggable;
79     }
80
81     /**
82      * Returns whether or not the supplied object can be moved by dragging.
83      *
84      * @param obj the <CODE>VisualObject</CODE> to investigate
85      * @return whether or not the supplied object can be moved by dragging
86      */

87     public boolean isObjectDraggable(final VisualObject obj) {
88         return draggableObjects.contains(obj);
89     }
90
91     /**
92      * Sets whether or not the supplied object is movable by dragging.
93      *
94      * @param obj the <CODE>VisualObject</CODE> to modify
95      * @param draggable whether or not the object can be moved by dragging
96      */

97     public void setObjectDraggable(final VisualObject obj, final boolean draggable) {
98         if (draggable) {
99             draggableObjects.add(obj);
100         }
101         else {
102             draggableObjects.remove(obj);
103         }
104     }
105
106     /**
107      * Invoked when a mouse button is pressed on a component and then dragged.
108      *
109      * @param e the event to be processed
110      */

111     @Override JavaDoc
112     public void mouseDragged(final MouseEvent e) {
113
114         VVDisplay vvDisplay = getVVDisplay();
115
116         Point currentPoint = e.getPoint();
117         if (objectBeingDragged != null) {
118
119             // Change the mouse cursor to a crosshair
120
vvDisplay.getViewPane().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
121
122             // Translate the object based on the movement
123
double diffX = currentPoint.getX() - lastKnownPoint.getX();
124             double diffY = currentPoint.getY() - lastKnownPoint.getY();
125
126             // Figure out what type of object is being dragged, and respond
127
// appropriately
128
if (objectBeingDragged == vvDisplay) {
129                 Point2D trans = vvDisplay.getWorldViewTranslation();
130                 double newX = trans.getX() + diffX;
131                 double newY = trans.getY() + diffY;
132                 vvDisplay.setWorldViewTranslation(newX, newY);
133             }
134             else if (objectBeingDragged instanceof VisualObject) {
135                 translateObjectBeingDragged((VisualObject) objectBeingDragged, diffX, diffY);
136
137                 // Refresh the view to reflect the object's new location
138
vvDisplay.getViewPane().repaint();
139             }
140         }
141
142         // Record the current mouse location
143
lastKnownPoint = currentPoint;
144     }
145
146     /**
147      * Translates the supplied <CODE>VisualObject</CODE> by the given amounts
148      * of X and Y in response to being dragged. Subclasses can override this
149      * method to create custom drag activities.
150      *
151      * @param obj the <CODE>VisualObject</CODE> being dragged
152      * @param dx the change in the object's X position
153      * @param dy the change in the object's Y position
154      */

155     protected void translateObjectBeingDragged(final VisualObject obj, final double dx, final double dy) {
156
157         double diffX = dx;
158         double diffY = dy;
159
160         AffineTransform trans = obj.getTransform();
161
162         double scale = getVVDisplay().getWorldViewScale();
163         diffX /= (scale * trans.getScaleX());
164         diffY /= (scale * trans.getScaleY());
165
166         trans.translate(diffX, diffY);
167         obj.setTransform(trans);
168     }
169
170     /**
171      * Invoked when the mouse cursor has been moved onto a component but no
172      * buttons have been pressed.
173      *
174      * @param e the event to be processed
175      */

176     @Override JavaDoc
177     public void mouseMoved(final MouseEvent e) {
178
179         // Record the current mouse location
180
lastKnownPoint = e.getPoint();
181     }
182
183     /**
184      * Invoked when a mouse button has been pressed on a component.
185      *
186      * @param e the event to be processed
187      */

188     @Override JavaDoc
189     public void mousePressed(final MouseEvent e) {
190         super.mousePressed(e);
191
192         // Figure out if the press occurred on a draggable object
193
determineObjectForDrag(e);
194
195         // Record the location of the mouse
196
lastKnownPoint = e.getPoint();
197     }
198
199     /**
200      * Invoked when a mouse button has been released on a component.
201      *
202      * @param e the event to be processed
203      */

204     @Override JavaDoc
205     public void mouseReleased(final MouseEvent e) {
206         super.mouseReleased(e);
207
208         // If a drag is occurring, wrap it up
209
if (objectBeingDragged != null) {
210             objectBeingDragged = null;
211
212             lastKnownPoint = e.getPoint();
213         }
214
215         // Resets the default cursor
216
getVVDisplay().getViewPane().setCursor(Cursor.getDefaultCursor());
217     }
218
219     /**
220      * Given a <CODE>MouseEvent</CODE>, determines the object that would be
221      * dragged based on the location and sets the <CODE>objectBeingDragged</CODE>
222      * field to the appropriate value.
223      *
224      * @param e the <CODE>MouseEvent</CODE> that generated the analysis
225      */

226     private void determineObjectForDrag(final MouseEvent e) {
227
228         // Determine the VisualObject that's under the mouse
229
VisualObject visObj = getVVDisplay().getVisualObjectAt(e.getX(), e.getY());
230
231         // Figure out the nearest draggable object
232
while ((visObj != null) && (!isObjectDraggable(visObj))) {
233             visObj = visObj.getParent();
234         }
235         objectBeingDragged = visObj;
236
237         // If no draggable object was found, consider the viewport
238
if ((objectBeingDragged == null) && (isViewportDraggable())) {
239             objectBeingDragged = getVVDisplay();
240         }
241
242     }
243
244 }
245
Popular Tags