1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.gef.editpolicies;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.draw2d.ColorConstants;
|
17
|
import org.eclipse.draw2d.Cursors;
|
18
|
import org.eclipse.draw2d.FigureUtilities;
|
19
|
import org.eclipse.draw2d.FocusBorder;
|
20
|
import org.eclipse.draw2d.Graphics;
|
21
|
import org.eclipse.draw2d.IFigure;
|
22
|
import org.eclipse.draw2d.Locator;
|
23
|
import org.eclipse.draw2d.PositionConstants;
|
24
|
import org.eclipse.draw2d.RectangleFigure;
|
25
|
import org.eclipse.draw2d.geometry.PrecisionRectangle;
|
26
|
import org.eclipse.draw2d.geometry.Rectangle;
|
27
|
|
28
|
import org.eclipse.gef.DragTracker;
|
29
|
import org.eclipse.gef.GraphicalEditPart;
|
30
|
import org.eclipse.gef.Request;
|
31
|
import org.eclipse.gef.RequestConstants;
|
32
|
import org.eclipse.gef.SharedCursors;
|
33
|
import org.eclipse.gef.commands.Command;
|
34
|
import org.eclipse.gef.handles.AbstractHandle;
|
35
|
import org.eclipse.gef.handles.HandleBounds;
|
36
|
import org.eclipse.gef.handles.NonResizableHandleKit;
|
37
|
import org.eclipse.gef.handles.ResizableHandleKit;
|
38
|
import org.eclipse.gef.requests.AlignmentRequest;
|
39
|
import org.eclipse.gef.requests.ChangeBoundsRequest;
|
40
|
import org.eclipse.gef.tools.DragEditPartsTracker;
|
41
|
import org.eclipse.gef.tools.ResizeTracker;
|
42
|
import org.eclipse.gef.tools.SelectEditPartTracker;
|
43
|
|
44
|
/**
|
45
|
* Provide support for selecting and positioning a non-resizable editpart.
|
46
|
* Selection is indicated via four square handles at each corner of the
|
47
|
* editpart's figure, and a rectangular handle that outlines the editpart with a
|
48
|
* 1-pixel black line. All of these handles return
|
49
|
* {@link org.eclipse.gef.tools.DragEditPartsTracker}s, which allows the current
|
50
|
* selection to be dragged.
|
51
|
* <P>
|
52
|
* During feedback, a rectangle filled using XOR and outlined with dashes is
|
53
|
* drawn. Subclasses can tailor the feedback.
|
54
|
*
|
55
|
* @author hudsonr
|
56
|
*/
|
57
|
public class NonResizableEditPolicy extends SelectionHandlesEditPolicy {
|
58
|
|
59
|
private IFigure focusRect;
|
60
|
private IFigure feedback;
|
61
|
private boolean isDragAllowed = true;
|
62
|
|
63
|
/**
|
64
|
* Creates the figure used for feedback.
|
65
|
*
|
66
|
* @return the new feedback figure
|
67
|
*/
|
68
|
protected IFigure createDragSourceFeedbackFigure() {
|
69
|
// Use a ghost rectangle for feedback
|
70
|
RectangleFigure r = new RectangleFigure();
|
71
|
FigureUtilities.makeGhostShape(r);
|
72
|
r.setLineStyle(Graphics.LINE_DOT);
|
73
|
r.setForegroundColor(ColorConstants.white);
|
74
|
r.setBounds(getInitialFeedbackBounds());
|
75
|
addFeedback(r);
|
76
|
return r;
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* @see org.eclipse.gef.editpolicies.SelectionHandlesEditPolicy#createSelectionHandles()
|
81
|
*/
|
82
|
protected List createSelectionHandles() {
|
83
|
List list = new ArrayList();
|
84
|
createMoveHandle(list);
|
85
|
createDragHandle(list, PositionConstants.NORTH_EAST);
|
86
|
createDragHandle(list, PositionConstants.NORTH_WEST);
|
87
|
createDragHandle(list, PositionConstants.SOUTH_EAST);
|
88
|
createDragHandle(list, PositionConstants.SOUTH_WEST);
|
89
|
return list;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Creates a 'resize'/'drag' handle, which uses a
|
94
|
* {@link DragEditPartsTracker} in case {@link #isDragAllowed()} returns
|
95
|
* true, and a {@link SelectEditPartTracker} otherwise.
|
96
|
*
|
97
|
* @param handles
|
98
|
* The list of handles to add the resize handle to
|
99
|
* @param direction
|
100
|
* A position constant indicating the direction to create the
|
101
|
* handle for
|
102
|
* @since 3.7
|
103
|
*/
|
104
|
protected void createDragHandle(List handles, int direction) {
|
105
|
if (isDragAllowed()) {
|
106
|
// display 'resize' handles to allow dragging (drag tracker)
|
107
|
NonResizableHandleKit
|
108
|
.addHandle((GraphicalEditPart) getHost(), handles,
|
109
|
direction, getDragTracker(), SharedCursors.SIZEALL);
|
110
|
} else {
|
111
|
// display 'resize' handles to indicate selection only (selection
|
112
|
// tracker)
|
113
|
NonResizableHandleKit
|
114
|
.addHandle((GraphicalEditPart) getHost(), handles,
|
115
|
direction, getSelectTracker(), SharedCursors.ARROW);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Returns a selection tracker to use by a selection handle.
|
121
|
*
|
122
|
* @return a new {@link ResizeTracker}
|
123
|
* @since 3.7
|
124
|
*/
|
125
|
protected SelectEditPartTracker getSelectTracker() {
|
126
|
return new SelectEditPartTracker(getHost());
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Returns a drag tracker to use by a resize handle.
|
131
|
*
|
132
|
* @return a new {@link ResizeTracker}
|
133
|
* @since 3.7
|
134
|
*/
|
135
|
protected DragEditPartsTracker getDragTracker() {
|
136
|
return new DragEditPartsTracker(getHost());
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Creates a 'move' handle, which uses a {@link DragEditPartsTracker} in
|
141
|
* case {@link #isDragAllowed()} returns true, and a
|
142
|
* {@link SelectEditPartTracker} otherwise.
|
143
|
*
|
144
|
* @param handles
|
145
|
* The list of handles to add the move handle to.
|
146
|
* @since 3.7
|
147
|
*/
|
148
|
protected void createMoveHandle(List handles) {
|
149
|
if (isDragAllowed()) {
|
150
|
// display 'move' handle to allow dragging
|
151
|
ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(),
|
152
|
handles, getDragTracker(), Cursors.SIZEALL);
|
153
|
} else {
|
154
|
// display 'move' handle only to indicate selection
|
155
|
ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(),
|
156
|
handles, getSelectTracker(), SharedCursors.ARROW);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* @see org.eclipse.gef.EditPolicy#deactivate()
|
162
|
*/
|
163
|
public void deactivate() {
|
164
|
if (feedback != null) {
|
165
|
removeFeedback(feedback);
|
166
|
feedback = null;
|
167
|
}
|
168
|
hideFocus();
|
169
|
super.deactivate();
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Erases drag feedback. This method called whenever an erase feedback
|
174
|
* request is received of the appropriate type.
|
175
|
*
|
176
|
* @param request
|
177
|
* the request
|
178
|
*/
|
179
|
protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request) {
|
180
|
if (feedback != null) {
|
181
|
removeFeedback(feedback);
|
182
|
}
|
183
|
feedback = null;
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* @see org.eclipse.gef.EditPolicy#eraseSourceFeedback(org.eclipse.gef.Request)
|
188
|
*/
|
189
|
public void eraseSourceFeedback(Request request) {
|
190
|
if ((REQ_MOVE.equals(request.getType()) && isDragAllowed())
|
191
|
|| REQ_CLONE.equals(request.getType())
|
192
|
|| REQ_ADD.equals(request.getType()))
|
193
|
eraseChangeBoundsFeedback((ChangeBoundsRequest) request);
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* @see org.eclipse.gef.EditPolicy#getCommand(org.eclipse.gef.Request)
|
198
|
*/
|
199
|
public Command getCommand(Request request) {
|
200
|
Object type = request.getType();
|
201
|
|
202
|
if (REQ_MOVE.equals(type) && isDragAllowed())
|
203
|
return getMoveCommand((ChangeBoundsRequest) request);
|
204
|
if (REQ_ORPHAN.equals(type))
|
205
|
return getOrphanCommand(request);
|
206
|
if (REQ_ALIGN.equals(type))
|
207
|
return getAlignCommand((AlignmentRequest) request);
|
208
|
|
209
|
return null;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Lazily creates and returns the feedback figure used during drags.
|
214
|
*
|
215
|
* @return the feedback figure
|
216
|
*/
|
217
|
protected IFigure getDragSourceFeedbackFigure() {
|
218
|
if (feedback == null)
|
219
|
feedback = createDragSourceFeedbackFigure();
|
220
|
return feedback;
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Returns the command contribution to an alignment request
|
225
|
*
|
226
|
* @param request
|
227
|
* the alignment request
|
228
|
* @return the contribution to the alignment
|
229
|
*/
|
230
|
protected Command getAlignCommand(AlignmentRequest request) {
|
231
|
AlignmentRequest req = new AlignmentRequest(REQ_ALIGN_CHILDREN);
|
232
|
req.setEditParts(getHost());
|
233
|
req.setAlignment(request.getAlignment());
|
234
|
req.setAlignmentRectangle(request.getAlignmentRectangle());
|
235
|
return getHost().getParent().getCommand(req);
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* Returns the bounds of the host's figure by reference to be used to
|
240
|
* calculate the initial location of the feedback. The returned Rectangle
|
241
|
* should not be modified. Uses handle bounds if available.
|
242
|
*
|
243
|
* @return the host figure's bounding Rectangle
|
244
|
*/
|
245
|
protected Rectangle getInitialFeedbackBounds() {
|
246
|
if (((GraphicalEditPart) getHost()).getFigure() instanceof HandleBounds)
|
247
|
return ((HandleBounds) ((GraphicalEditPart) getHost()).getFigure())
|
248
|
.getHandleBounds();
|
249
|
return ((GraphicalEditPart) getHost()).getFigure().getBounds();
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Returns the command contribution to a change bounds request. The
|
254
|
* implementation actually redispatches the request to the host's parent
|
255
|
* editpart as a {@link RequestConstants#REQ_MOVE_CHILDREN} request. The
|
256
|
* parent's contribution is returned.
|
257
|
*
|
258
|
* @param request
|
259
|
* the change bounds request
|
260
|
* @return the command contribution to the request
|
261
|
*/
|
262
|
protected Command getMoveCommand(ChangeBoundsRequest request) {
|
263
|
ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_MOVE_CHILDREN);
|
264
|
req.setEditParts(getHost());
|
265
|
|
266
|
req.setMoveDelta(request.getMoveDelta());
|
267
|
req.setSizeDelta(request.getSizeDelta());
|
268
|
req.setLocation(request.getLocation());
|
269
|
req.setExtendedData(request.getExtendedData());
|
270
|
return getHost().getParent().getCommand(req);
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* Subclasses may override to contribute to the orphan request. By default,
|
275
|
* <code>null</code> is returned to indicate no participation. Orphan
|
276
|
* requests are not forwarded to the host's parent here. That is done in
|
277
|
* {@link ComponentEditPolicy}. So, if the host has a component editpolicy,
|
278
|
* then the parent will already have a chance to contribute.
|
279
|
*
|
280
|
* @param req
|
281
|
* the orphan request
|
282
|
* @return <code>null</code> by default
|
283
|
*/
|
284
|
protected Command getOrphanCommand(Request req) {
|
285
|
return null;
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* Hides the focus rectangle displayed in <code>showFocus()</code>.
|
290
|
*
|
291
|
* @see #showFocus()
|
292
|
* @see org.eclipse.gef.editpolicies.SelectionEditPolicy#hideFocus()
|
293
|
*/
|
294
|
protected void hideFocus() {
|
295
|
if (focusRect != null)
|
296
|
removeFeedback(focusRect);
|
297
|
focusRect = null;
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* Returns true if this EditPolicy allows its EditPart to be dragged.
|
302
|
*
|
303
|
* @return true if the EditPart can be dragged.
|
304
|
*/
|
305
|
public boolean isDragAllowed() {
|
306
|
return isDragAllowed;
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Sets the dragability of the EditPolicy to the given value. If the value
|
311
|
* is false, the EditPolicy should not allow its EditPart to be dragged.
|
312
|
*
|
313
|
* @param isDragAllowed
|
314
|
* whether or not the EditPolicy can be dragged.
|
315
|
*/
|
316
|
public void setDragAllowed(boolean isDragAllowed) {
|
317
|
if (isDragAllowed == this.isDragAllowed)
|
318
|
return;
|
319
|
this.isDragAllowed = isDragAllowed;
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* Shows or updates feedback for a change bounds request.
|
324
|
*
|
325
|
* @param request
|
326
|
* the request
|
327
|
*/
|
328
|
protected void showChangeBoundsFeedback(ChangeBoundsRequest request) {
|
329
|
IFigure feedback = getDragSourceFeedbackFigure();
|
330
|
|
331
|
PrecisionRectangle rect = new PrecisionRectangle(
|
332
|
getInitialFeedbackBounds().getCopy());
|
333
|
getHostFigure().translateToAbsolute(rect);
|
334
|
rect.translate(request.getMoveDelta());
|
335
|
rect.resize(request.getSizeDelta());
|
336
|
|
337
|
feedback.translateToRelative(rect);
|
338
|
feedback.setBounds(rect);
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* Shows a focus rectangle around the host's figure. The focus rectangle is
|
343
|
* expanded by 5 pixels from the figure's bounds.
|
344
|
*
|
345
|
* @see org.eclipse.gef.editpolicies.SelectionEditPolicy#showFocus()
|
346
|
*/
|
347
|
protected void showFocus() {
|
348
|
focusRect = new AbstractHandle((GraphicalEditPart) getHost(),
|
349
|
new Locator() {
|
350
|
public void relocate(IFigure target) {
|
351
|
IFigure figure = getHostFigure();
|
352
|
Rectangle r;
|
353
|
if (figure instanceof HandleBounds)
|
354
|
r = ((HandleBounds) figure).getHandleBounds()
|
355
|
.getCopy();
|
356
|
else
|
357
|
r = getHostFigure().getBounds().getResized(-1, -1);
|
358
|
getHostFigure().translateToAbsolute(r);
|
359
|
target.translateToRelative(r);
|
360
|
target.setBounds(r.expand(5, 5).resize(1, 1));
|
361
|
}
|
362
|
}) {
|
363
|
{
|
364
|
setBorder(new FocusBorder());
|
365
|
}
|
366
|
|
367
|
protected DragTracker createDragTracker() {
|
368
|
return null;
|
369
|
}
|
370
|
};
|
371
|
addFeedback(focusRect);
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Calls other methods as appropriate.
|
376
|
*
|
377
|
* @see org.eclipse.gef.EditPolicy#showSourceFeedback(org.eclipse.gef.Request)
|
378
|
*/
|
379
|
public void showSourceFeedback(Request request) {
|
380
|
if ((REQ_MOVE.equals(request.getType()) && isDragAllowed())
|
381
|
|| REQ_ADD.equals(request.getType())
|
382
|
|| REQ_CLONE.equals(request.getType()))
|
383
|
showChangeBoundsFeedback((ChangeBoundsRequest) request);
|
384
|
}
|
385
|
|
386
|
/**
|
387
|
* Returns <code>true</code> for move, align, add, and orphan request types.
|
388
|
* This method is never called for some of these types, but they are
|
389
|
* included for possible future use.
|
390
|
*
|
391
|
* @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
|
392
|
*/
|
393
|
public boolean understandsRequest(Request request) {
|
394
|
if (REQ_MOVE.equals(request.getType()))
|
395
|
return isDragAllowed();
|
396
|
else if (REQ_CLONE.equals(request.getType())
|
397
|
|| REQ_ADD.equals(request.getType())
|
398
|
|| REQ_ORPHAN.equals(request.getType())
|
399
|
|| REQ_ALIGN.equals(request.getType()))
|
400
|
return true;
|
401
|
return super.understandsRequest(request);
|
402
|
}
|
403
|
|
404
|
}
|