Skip to main content
summaryrefslogtreecommitdiffstats
blob: e184e3ee46c8deea381f1a20a0a76e46bac1a65c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/******************************************************************************
 * Copyright (c) 2002, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    IBM Corporation - initial API and implementation 
 ****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.common.snap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.internal.ruler.SnapToHelperUtil;
import org.eclipse.papyrus.infra.gmfdiag.common.snap.copy.DragEditPartsTrackerEx;

/**
 * A dervied DragEditPartsTRacker that sends REQ_DRAG instead of REQ_ORPHAN
 * and REQ_DROP instead of REQ_ADD
 * 
 * @author melaasar
 */
//TODO see Bug 424007. CustomDragEditPartsTracker and others classes in package snap.copy will be destroyed when the gef bug 424007 will be resolved.
@SuppressWarnings("restriction")
public class PapyrusDragEditPartsTrackerEx extends DragEditPartsTrackerEx {

	/**
	 * if true, we snap on the four corner of the figures
	 */
	protected final boolean snapOnCorners;

	/**
	 * if true, we snap on the four middle side of the figures
	 */
	protected final boolean snapOnMiddles;

	/**
	 * if true, we snap on the center of the figure
	 */
	protected final boolean snapOnCenter;


	/**
	 * @param sourceEditPart
	 */
	public PapyrusDragEditPartsTrackerEx(final EditPart sourceEditPart) {
		this(sourceEditPart, true, false, false);
	}

	/**
	 * 
	 * Constructor.
	 * 
	 * @param sourceEditPart
	 * @param snapOnCorners
	 * @param snapOnMiddles
	 * @param snapOnCenter
	 */
	public PapyrusDragEditPartsTrackerEx(final EditPart sourceEditPart, final boolean snapOnCorners, final boolean snapOnMiddles, final boolean snapOnCenter) {
		super(sourceEditPart);
		this.snapOnCorners = snapOnCorners;
		this.snapOnMiddles = snapOnMiddles;
		this.snapOnCenter = snapOnCenter;
	}

	/**
	 * This method can be overridden by clients to customize the snapping
	 * behavior.
	 * 
	 * @param request
	 *        the <code>ChangeBoundsRequest</code> from which the move delta
	 *        can be extracted and updated
	 * @since 3.4
	 */
	@SuppressWarnings({ "unchecked" })
	protected void snapPoint(ChangeBoundsRequest request) {
		if(getSnapToHelper() != null && request.isSnapToEnabled() && request.getEditParts().size() > 0) {

			//test to know if we are moving using keyboard
			if(!getCurrentInput().isAnyButtonDown()) {
				calculateSnapPointFromArrowKey(request);
				return;
			}
			int restrictedDirection = 0;
			restrictedDirection = restrictedDirection | PositionConstants.EAST;
			restrictedDirection = restrictedDirection | PositionConstants.WEST;
			restrictedDirection = restrictedDirection | PositionConstants.SOUTH;
			restrictedDirection = restrictedDirection | PositionConstants.NORTH;
			request.getExtendedData().put(SnapToHelperUtil.RESTRICTED_DIRECTIONS, restrictedDirection);

			final Map<Double, PrecisionPoint> distVSPoint = new HashMap<Double, PrecisionPoint>();
			if(this.snapOnCorners) {
				distVSPoint.putAll(getCornerDistances(request));
			}

			if(this.snapOnMiddles) {
				distVSPoint.putAll(getMiddleDistances(request));
			}

			if(this.snapOnCenter) {
				distVSPoint.putAll(getCenterDistances(request));
			}

			final List<Double> distances = new ArrayList<Double>(distVSPoint.keySet());
			if(distances.size() > 0) {
				double min = distances.get(0);
				for(int i = 1; i < distances.size() - 1; i++) {
					min = Math.min(min, distances.get(i));
				}
				request.setMoveDelta(distVSPoint.get(min));
			}
		}
	}

	/**
	 * 
	 * @param request
	 *        a move request
	 * @return
	 *         the restricted direction for the request
	 */
	protected final int getRestrictedDirection(final ChangeBoundsRequest request) {
		int restrictedDirection = 0;
		final Point delta = request.getMoveDelta();
		if(delta.x > 0) {
			restrictedDirection = restrictedDirection | PositionConstants.EAST;
			restrictedDirection = restrictedDirection | PositionConstants.WEST;
		}
		if(delta.x < 0) {
			restrictedDirection = restrictedDirection | PositionConstants.EAST;
			restrictedDirection = restrictedDirection | PositionConstants.WEST;
		}
		if(delta.y > 0) {
			restrictedDirection = restrictedDirection | PositionConstants.SOUTH;
			restrictedDirection = restrictedDirection | PositionConstants.NORTH;
		}
		if(delta.y < 0) {
			restrictedDirection = restrictedDirection | PositionConstants.SOUTH;
			restrictedDirection = restrictedDirection | PositionConstants.NORTH;
		}
		return restrictedDirection;
	}

	/**
	 * 
	 * @param request
	 *        the move request
	 */
	@SuppressWarnings("unchecked")
	protected void calculateSnapPointFromArrowKey(final ChangeBoundsRequest request) {
		if(request.getEditParts().size() == 0) {
			return;
		}
		final Object ep = request.getEditParts().get(0);
		if(!(ep instanceof IGraphicalEditPart)) {
			return;
		}
		final RootEditPart root = ((IGraphicalEditPart)ep).getRoot();
		if(!(root instanceof DiagramRootEditPart)) {
			return;
		}
		final double gridSpacing = ((DiagramRootEditPart)root).getGridSpacing();
		int max = (int)(1 + gridSpacing);
		int restrictedDirection = getRestrictedDirection(request);
		final Point delta = request.getMoveDelta();
		final Point newDelta = new Point(0, 0);
		int newMove = 0;
		while(newMove < max) {
			newMove++;
			if(delta.x > 0) {
				newDelta.x = (int)newMove + delta.x;
			}
			if(delta.x < 0) {
				newDelta.x = (-newMove) + delta.x;
			}
			if(delta.y > 0) {
				newDelta.y = newMove + delta.y;
			}
			if(delta.y < 0) {
				newDelta.y = (-newMove) + delta.y;
			}
			request.setMoveDelta(newDelta);
			request.getExtendedData().put(SnapToHelperUtil.RESTRICTED_DIRECTIONS, restrictedDirection);

			final Map<Double, PrecisionPoint> distVSPoint = new HashMap<Double, PrecisionPoint>();
			if(this.snapOnCorners) {
				distVSPoint.putAll(getCornerDistances(request));
			}

			if(this.snapOnMiddles) {
				distVSPoint.putAll(getMiddleDistances(request));
			}

			if(this.snapOnCenter) {
				distVSPoint.putAll(getCenterDistances(request));
			}

			final List<Double> distances = new ArrayList<Double>(distVSPoint.keySet());
			if(distances.size() > 0) {
				double min = distances.get(0);
				//We look for the minus distance
				for(int i = 1; i < distances.size() - 1; i++) {
					min = Math.min(min, distances.get(i));
				}
				final Point minPoint = distVSPoint.get(min);
				//the distance can't be null
				if(minPoint.x != 0 || minPoint.y != 0) {
					//the calculate move must be in the same direction than the keyboard move 
					if(Integer.signum(minPoint.x) == Integer.signum(delta.x) && Integer.signum(minPoint.y) == Integer.signum(delta.y)) {
						request.setMoveDelta(distVSPoint.get(min));
						return;
					}
				}
			}
		}
	}

	/**
	 * 
	 * @param request
	 * @return
	 *         a map with the couple distance and delta point to anchor by the corner of the figure
	 */
	protected Map<Double, PrecisionPoint> getCornerDistances(final ChangeBoundsRequest request) {
		final Map<Double, PrecisionPoint> distVSPoints = new HashMap<Double, PrecisionPoint>();
		if(getSnapToHelper() != null && request.isSnapToEnabled()) {
			final Point moveDelta = request.getMoveDelta();
			PrecisionRectangle jointRect = getCompoundSourceRectangle();
			jointRect.translate(moveDelta);

			//calculate the delta to anchor on the top left corner
			final PrecisionPoint topLeftCornerDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectTopLeft = getSourceRectangle();
			baseRectTopLeft.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectTopLeft, jointRect }, topLeftCornerDelta);

			//calculate the delta to anchor on the top right corner
			final PrecisionPoint topRightCornerDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectTopRight = getSourceRectangle();
			baseRectTopRight.setX(baseRectTopRight.x + baseRectTopRight.width);
			baseRectTopRight.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectTopRight, jointRect }, topRightCornerDelta);

			//calculate the delta to anchor on the bottom left corner
			final PrecisionPoint bottomLeftCornerDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectBottomLeft = getSourceRectangle();
			baseRectBottomLeft.setY(baseRectBottomLeft.y + baseRectBottomLeft.height);
			baseRectBottomLeft.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectBottomLeft, jointRect }, bottomLeftCornerDelta);

			//calculate the delta to anchor on the bottom right corner
			final PrecisionPoint bottomRightCornerDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectBottomRight = getSourceRectangle();
			baseRectBottomRight.setX(baseRectBottomRight.x + baseRectBottomRight.width);
			baseRectBottomRight.setY(baseRectBottomRight.y + baseRectBottomRight.height);
			baseRectBottomRight.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectBottomRight, jointRect }, bottomRightCornerDelta);

			final Point ref = baseRectTopLeft.getTopLeft();
			distVSPoints.put(distance(ref, topLeftCornerDelta), topLeftCornerDelta);
			distVSPoints.put(distance(ref, topRightCornerDelta), topRightCornerDelta);
			distVSPoints.put(distance(ref, bottomLeftCornerDelta), bottomLeftCornerDelta);
			distVSPoints.put(distance(ref, bottomRightCornerDelta), bottomRightCornerDelta);
		}
		return distVSPoints;
	}

	/**
	 * 
	 * @param request
	 * @return
	 *         a map with the couple distance and delta point to anchor by the middle of each side of the figure
	 */
	protected Map<Double, PrecisionPoint> getMiddleDistances(final ChangeBoundsRequest request) {
		final Map<Double, PrecisionPoint> distVSPoints = new HashMap<Double, PrecisionPoint>();
		if(getSnapToHelper() != null && request.isSnapToEnabled()) {
			final Point moveDelta = request.getMoveDelta();

			PrecisionRectangle jointRect = getCompoundSourceRectangle();
			jointRect.translate(moveDelta);
			//calculate the delta to anchor on the middle top point
			final PrecisionPoint middleTopDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectMiddleTop = getSourceRectangle();
			baseRectMiddleTop.setPreciseLocation(baseRectMiddleTop.preciseX() + (baseRectMiddleTop.preciseWidth() / 2), baseRectMiddleTop.preciseY());
			baseRectMiddleTop.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectMiddleTop, jointRect }, middleTopDelta);

			//calculate the delta to anchor on the middle left point
			final PrecisionPoint middleLeftDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectMiddleLeft = getSourceRectangle();
			baseRectMiddleLeft.setPreciseLocation(baseRectMiddleLeft.preciseX(), baseRectMiddleLeft.preciseY() + (baseRectMiddleLeft.preciseWidth() / 2));
			baseRectMiddleLeft.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectMiddleLeft, jointRect }, middleLeftDelta);

			//calculate the delta to anchor on the middle right point
			final PrecisionPoint middleRightDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectMiddleRight = getSourceRectangle();
			baseRectMiddleRight.setPreciseLocation(baseRectMiddleRight.preciseX() + baseRectMiddleRight.preciseWidth(), baseRectMiddleRight.preciseY() + (baseRectMiddleRight.preciseHeight() / 2));
			baseRectMiddleRight.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectMiddleRight, jointRect }, middleRightDelta);

			//calculate the delta to anchor on the middle bottom
			final PrecisionPoint middleBottomDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectMiddleBottom = getSourceRectangle();
			baseRectMiddleBottom.setPreciseLocation(baseRectMiddleBottom.preciseX() + (baseRectMiddleBottom.preciseWidth() / 2), baseRectMiddleBottom.preciseY() + baseRectMiddleBottom.preciseHeight());
			baseRectMiddleBottom.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectMiddleBottom, jointRect }, middleBottomDelta);

			final Point ref = baseRectMiddleTop.getTopLeft();
			distVSPoints.put(distance(ref, middleTopDelta), middleTopDelta);
			distVSPoints.put(distance(ref, middleLeftDelta), middleLeftDelta);
			distVSPoints.put(distance(ref, middleRightDelta), middleRightDelta);
			distVSPoints.put(distance(ref, middleBottomDelta), middleBottomDelta);
		}

		return distVSPoints;
	}

	/**
	 * 
	 * @param request
	 * @return
	 *         a map with the couple distance and delta point to anchor by the center of the figure
	 */
	protected Map<Double, PrecisionPoint> getCenterDistances(final ChangeBoundsRequest request) {
		final Map<Double, PrecisionPoint> distVSPoints = new HashMap<Double, PrecisionPoint>();
		if(getSnapToHelper() != null && request.isSnapToEnabled()) {
			final Point moveDelta = request.getMoveDelta();

			PrecisionRectangle jointRect = getCompoundSourceRectangle();
			jointRect.translate(moveDelta);
			//calculate the delta to anchor on the middle top point
			final PrecisionPoint centerDelta = new PrecisionPoint(moveDelta);
			final PrecisionRectangle baseRectCenter = getSourceRectangle();
			baseRectCenter.setPreciseLocation(baseRectCenter.preciseX() + (baseRectCenter.preciseWidth() / 2), baseRectCenter.preciseY() + (baseRectCenter.preciseHeight() / 2));
			baseRectCenter.translate(moveDelta);
			getSnapToHelper().snapPoint(request, PositionConstants.HORIZONTAL | PositionConstants.VERTICAL, new PrecisionRectangle[]{ baseRectCenter, jointRect }, centerDelta);

			final Point ref = baseRectCenter.getTopLeft();
			distVSPoints.put(distance(ref, centerDelta), centerDelta);
		}

		return distVSPoints;
	}

	/**
	 * 
	 * @param pt1
	 *        a first point
	 * @param pt2
	 *        the second point
	 * @return
	 *         the distance between the two points
	 */
	protected final double distance(final Point pt1, final Point pt2) {
		double deltaX = pt1.preciseX() - pt2.preciseX();
		double deltaY = pt1.preciseY() - pt2.preciseY();
		return Math.hypot(deltaX, deltaY);
	}
}

Back to the top