KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > tools > AbstractDragTracker


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 13.04.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.tools;
9
10 import java.util.List JavaDoc;
11
12 import org.eclipse.draw2d.IFigure;
13 import org.eclipse.draw2d.geometry.PrecisionRectangle;
14 import org.eclipse.gef.GraphicalEditPart;
15 import org.eclipse.gef.Request;
16 import org.eclipse.gef.SnapToHelper;
17 import org.eclipse.gef.commands.Command;
18 import org.eclipse.gef.handles.HandleBounds;
19 import org.eclipse.gef.tools.ResizeTracker;
20 import org.eclipse.gef.tools.SimpleDragTracker;
21 import org.eclipse.gef.tools.ToolUtilities;
22
23 import com.nightlabs.editor2d.custom.EditorCursors;
24 import com.nightlabs.editor2d.request.EditorRequestConstants;
25 import com.nightlabs.editor2d.util.EditorUtil;
26
27
28 public abstract class AbstractDragTracker
29 extends SimpleDragTracker
30 implements EditorRequestConstants
31 {
32   protected static int FLAG_TARGET_FEEDBACK = SimpleDragTracker.MAX_FLAG << 1;
33   protected static final int MAX_FLAG = FLAG_TARGET_FEEDBACK;
34   
35   protected GraphicalEditPart owner;
36   protected PrecisionRectangle sourceRect;
37   protected SnapToHelper snapToHelper;
38   
39   public AbstractDragTracker(GraphicalEditPart owner)
40   {
41     super();
42     this.owner = owner;
43     setDisabledCursor(EditorCursors.NO);
44   }
45
46   public void activate()
47   {
48     super.activate();
49     if (owner != null)
50     {
51       if (getTargetEditPart() != null)
52         snapToHelper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class);
53     
54       IFigure figure = owner.getFigure();
55       if (figure instanceof HandleBounds)
56         sourceRect = new PrecisionRectangle(((HandleBounds)figure).getHandleBounds());
57       else
58         sourceRect = new PrecisionRectangle(figure.getBounds());
59       figure.translateToAbsolute(sourceRect);
60     }
61   }
62   
63   protected List JavaDoc createOperationSet()
64   {
65     List JavaDoc list = super.createOperationSet();
66     ToolUtilities.filterEditPartsUnderstanding(list, getSourceRequest());
67     return list;
68   }
69   
70   /**
71    * The TargetEditPart is the parent of the EditPart being dragged.
72    *
73    * @return The target EditPart; may be <code>null</code> in 2.1 applications that use
74    * the now deprecated {@link ResizeTracker#ResizeTracker(int) constructor}.
75    */

76   protected GraphicalEditPart getTargetEditPart()
77   {
78     if (owner != null)
79     {
80       GraphicalEditPart targetEditPart = (GraphicalEditPart)owner.getParent();
81       return targetEditPart;
82     }
83     return null;
84   }
85   
86   protected void eraseTargetFeedback()
87   {
88     if (!getFlag(FLAG_TARGET_FEEDBACK))
89       return;
90     if (getTargetEditPart() != null)
91       getTargetEditPart().eraseTargetFeedback(getSourceRequest());
92     setFlag(FLAG_TARGET_FEEDBACK, false);
93   }
94      
95   public void deactivate()
96   {
97     eraseTargetFeedback();
98     sourceRect = null;
99     snapToHelper = null;
100     super.deactivate();
101   }
102   
103   /**
104    * @see org.eclipse.gef.tools.AbstractTool#getCommand()
105    */

106   protected Command getCommand()
107   {
108     return getTargetEditPart().getCommand(getSourceRequest());
109   }
110   
111   protected boolean handleButtonUp(int button)
112   {
113     if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) {
114       eraseSourceFeedback();
115       eraseTargetFeedback();
116       performDrag();
117       // added to repaint the handle
118
performSelection();
119     }
120     return true;
121   }
122   
123   protected boolean handleDragInProgress()
124   {
125     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS))
126     {
127       updateSourceRequest();
128       showSourceFeedback();
129       showTargetFeedback();
130       setCurrentCommand(getCommand());
131     }
132     return true;
133   }
134   
135   protected void showTargetFeedback()
136   {
137     setFlag(FLAG_TARGET_FEEDBACK, true);
138     if (getTargetEditPart() != null)
139       getTargetEditPart().showTargetFeedback(getSourceRequest());
140   }
141   
142   protected boolean isInDragInProgress() {
143     return isInState(STATE_DRAG_IN_PROGRESS | STATE_ACCESSIBLE_DRAG_IN_PROGRESS);
144   }
145   
146   protected String JavaDoc getDebugName()
147   {
148     return "Debug "+getCommandName();
149   }
150   
151   protected abstract Request createSourceRequest();
152   protected abstract void updateSourceRequest();
153   protected abstract String JavaDoc getCommandName();
154 // protected abstract Cursor getDefaultCursor();
155

156 // protected Point getRealLocation()
157
// {
158
// Point p = getLocation();
159
// Point realLocation;
160
//
161
// ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer) getCurrentViewer();
162
// FigureCanvas canvas = (FigureCanvas) viewer.getControl();
163
// Viewport viewport = canvas.getViewport();
164
// Point viewLocation = viewport.getViewLocation();
165
// realLocation = p.getTranslated(viewLocation);
166
//
167
// return realLocation;
168
// }
169

170   protected void performSelection()
171   {
172     EditorUtil.selectEditPart(owner);
173   }
174    
175 }
176
Free Books   Free Magazines  
Popular Tags