KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.tools;
29
30 import java.util.List JavaDoc;
31
32 import org.eclipse.draw2d.IFigure;
33 import org.eclipse.draw2d.geometry.PrecisionRectangle;
34 import org.eclipse.gef.GraphicalEditPart;
35 import org.eclipse.gef.Request;
36 import org.eclipse.gef.SnapToHelper;
37 import org.eclipse.gef.commands.Command;
38 import org.eclipse.gef.handles.HandleBounds;
39 import org.eclipse.gef.tools.ResizeTracker;
40 import org.eclipse.gef.tools.SimpleDragTracker;
41 import org.eclipse.gef.tools.ToolUtilities;
42
43 import org.nightlabs.editor2d.custom.EditorCursors;
44 import org.nightlabs.editor2d.request.EditorRequestConstants;
45 import org.nightlabs.editor2d.util.EditorUtil;
46
47
48 public abstract class AbstractDragTracker
49 extends SimpleDragTracker
50 implements EditorRequestConstants
51 {
52   protected static int FLAG_TARGET_FEEDBACK = SimpleDragTracker.MAX_FLAG << 1;
53   protected static final int MAX_FLAG = FLAG_TARGET_FEEDBACK;
54   
55   protected GraphicalEditPart owner;
56   protected PrecisionRectangle sourceRect;
57   protected SnapToHelper snapToHelper;
58   
59   public AbstractDragTracker(GraphicalEditPart owner)
60   {
61     super();
62     this.owner = owner;
63     setDisabledCursor(EditorCursors.NO);
64   }
65
66   public void activate()
67   {
68     super.activate();
69     if (owner != null)
70     {
71       if (getTargetEditPart() != null)
72         snapToHelper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class);
73     
74       IFigure figure = owner.getFigure();
75       if (figure instanceof HandleBounds)
76         sourceRect = new PrecisionRectangle(((HandleBounds)figure).getHandleBounds());
77       else
78         sourceRect = new PrecisionRectangle(figure.getBounds());
79       figure.translateToAbsolute(sourceRect);
80     }
81   }
82   
83   protected List JavaDoc createOperationSet()
84   {
85     List JavaDoc list = super.createOperationSet();
86     ToolUtilities.filterEditPartsUnderstanding(list, getSourceRequest());
87     return list;
88   }
89   
90   /**
91    * The TargetEditPart is the parent of the EditPart being dragged.
92    *
93    * @return The target EditPart; may be <code>null</code> in 2.1 applications that use
94    * the now deprecated {@link ResizeTracker#ResizeTracker(int) constructor}.
95    */

96   protected GraphicalEditPart getTargetEditPart()
97   {
98     if (owner != null)
99     {
100       GraphicalEditPart targetEditPart = (GraphicalEditPart)owner.getParent();
101       return targetEditPart;
102     }
103     return null;
104   }
105   
106   protected void eraseTargetFeedback()
107   {
108     if (!getFlag(FLAG_TARGET_FEEDBACK))
109       return;
110     if (getTargetEditPart() != null)
111       getTargetEditPart().eraseTargetFeedback(getSourceRequest());
112     setFlag(FLAG_TARGET_FEEDBACK, false);
113   }
114      
115   public void deactivate()
116   {
117     eraseTargetFeedback();
118     sourceRect = null;
119     snapToHelper = null;
120     super.deactivate();
121   }
122   
123   /**
124    * @see org.eclipse.gef.tools.AbstractTool#getCommand()
125    */

126   protected Command getCommand()
127   {
128     return getTargetEditPart().getCommand(getSourceRequest());
129   }
130   
131   protected boolean handleButtonUp(int button)
132   {
133     if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) {
134       eraseSourceFeedback();
135       eraseTargetFeedback();
136       performDrag();
137       // added to repaint the handle
138
performSelection();
139     }
140     return true;
141   }
142   
143   protected boolean handleDragInProgress()
144   {
145     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS))
146     {
147       updateSourceRequest();
148       showSourceFeedback();
149       showTargetFeedback();
150       setCurrentCommand(getCommand());
151     }
152     return true;
153   }
154   
155   protected void showTargetFeedback()
156   {
157     setFlag(FLAG_TARGET_FEEDBACK, true);
158     if (getTargetEditPart() != null)
159       getTargetEditPart().showTargetFeedback(getSourceRequest());
160   }
161   
162   protected boolean isInDragInProgress() {
163     return isInState(STATE_DRAG_IN_PROGRESS | STATE_ACCESSIBLE_DRAG_IN_PROGRESS);
164   }
165   
166   protected String JavaDoc getDebugName()
167   {
168     return "Debug "+getCommandName();
169   }
170   
171   protected abstract Request createSourceRequest();
172   protected abstract void updateSourceRequest();
173   protected abstract String JavaDoc getCommandName();
174 // protected abstract Cursor getDefaultCursor();
175

176 // protected Point getRealLocation()
177
// {
178
// Point p = getLocation();
179
// Point realLocation;
180
//
181
// ScrollingGraphicalViewer viewer = (ScrollingGraphicalViewer) getCurrentViewer();
182
// FigureCanvas canvas = (FigureCanvas) viewer.getControl();
183
// Viewport viewport = canvas.getViewport();
184
// Point viewLocation = viewport.getViewLocation();
185
// realLocation = p.getTranslated(viewLocation);
186
//
187
// return realLocation;
188
// }
189

190   protected void performSelection()
191   {
192     EditorUtil.selectEditPart(owner);
193   }
194    
195 }
196
Popular Tags