Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5a1ef9a54f1db51ab6cbe7bb518868b01d50dfe (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
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Atos Origin - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.edit.policies;

import java.util.Collections;
import java.util.List;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineShape;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.requests.AlignmentRequest;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.SetBoundsCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.NonResizableEditPolicyEx;
import org.eclipse.gmf.runtime.diagram.ui.figures.IBorderItemLocator;
import org.eclipse.gmf.runtime.diagram.ui.internal.figures.LabelHelper;
import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;

public class BehaviorPropertyNodeEditPolicy extends NonResizableEditPolicyEx {

	private PolylineShape linkFeedback = null;

	/**
	 * @see org.eclipse.gef.editpolicies.NonResizableEditPolicy#createSelectionHandles()
	 */
	protected List createSelectionHandles() {
		MoveHandle mh = new MoveHandle((GraphicalEditPart)getHost());
		mh.setBorder(null);
		return Collections.singletonList(mh);
	}

	/**
	 * Calls other methods as appropriate.
	 */
	public void eraseSourceFeedback(Request request) {
		if((REQ_MOVE.equals(request.getType()) && isDragAllowed()) || REQ_CLONE.equals(request.getType()) || REQ_ADD.equals(request.getType()) || RequestConstants.REQ_DROP.equals(request.getType()))
			eraseChangeBoundsFeedback((ChangeBoundsRequest)request);
	}

	/**
	 * Calls other methods as appropriate.
	 */
	public void showSourceFeedback(Request request) {
		if((REQ_MOVE.equals(request.getType()) && isDragAllowed()) || REQ_ADD.equals(request.getType()) || REQ_CLONE.equals(request.getType()) || RequestConstants.REQ_DROP.equals(request.getType()))
			showChangeBoundsFeedback((ChangeBoundsRequest)request);
	}

	/**
	 * Returns the command contribution to a change bounds request.
	 * 
	 * @param request
	 *        the change bounds requesgt
	 * @return the command contribution to the request
	 */
	protected Command getMoveCommand(ChangeBoundsRequest request) {

		// translate the feedback figure
		PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
		getHostFigure().translateToAbsolute(rect);
		rect.translate(request.getMoveDelta());
		rect.resize(request.getSizeDelta());
		getHostFigure().translateToRelative(rect);

		if(getHost() instanceof IBorderItemEditPart) {
			IBorderItemEditPart borderItemEP = (IBorderItemEditPart)getHost();
			IBorderItemLocator borderItemLocator = borderItemEP.getBorderItemLocator();
			if(borderItemLocator != null) {
				Rectangle realLocation = borderItemLocator.getValidLocation(rect.getCopy(), borderItemEP.getFigure());
				Point parentOrigin = borderItemEP.getFigure().getParent().getBounds().getTopLeft();
				Dimension d = realLocation.getTopLeft().getDifference(parentOrigin);
				Point location = new Point(d.width, d.height);
				ICommand moveCommand = new SetBoundsCommand(borderItemEP.getEditingDomain(), DiagramUIMessages.Commands_MoveElement, new EObjectAdapter((View)getHost().getModel()), location);
				return new ICommandProxy(moveCommand);
			}
		} else if(getHost() instanceof LabelEditPart) {
			LabelEditPart editPart = (LabelEditPart)getHost();
			Point refPoint = editPart.getReferencePoint();
			Point normalPoint = LabelHelper.offsetFromRelativeCoordinate(getHostFigure(), rect, refPoint);
			ICommand moveCommand = new SetBoundsCommand(editPart.getEditingDomain(), DiagramUIMessages.Commands_MoveElement, new EObjectAdapter((View)editPart.getModel()), normalPoint);
			return new ICommandProxy(moveCommand);
		}
		return null;
	}

	/** Return <tt>null</tt> to avoid handling the request. */
	protected Command getAlignCommand(AlignmentRequest request) {
		return null;
	}

	/**
	 * Erase the feedback link figure
	 * 
	 * @see org.eclipse.gef.editpolicies.NonResizableEditPolicy#eraseChangeBoundsFeedback(org.eclipse.gef.requests.ChangeBoundsRequest)
	 */
	protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request) {
		super.eraseChangeBoundsFeedback(request);
		if(linkFeedback != null)
			removeFeedback(linkFeedback);
		linkFeedback = null;
	}

	/**
	 * Create the feedback link figure
	 * 
	 * @see org.eclipse.gef.editpolicies.NonResizableEditPolicy#createDragSourceFeedbackFigure()
	 */
	protected IFigure createDragSourceFeedbackFigure() {
		IFigure feedback = super.createDragSourceFeedbackFigure();
		linkFeedback = new PolylineShape();
		linkFeedback.setLineWidth(1);
		linkFeedback.setLineStyle(Graphics.LINE_DASHDOT);
		linkFeedback.setForegroundColor(((IGraphicalEditPart)getHost()).getFigure().getForegroundColor());
		addFeedback(linkFeedback);
		return feedback;
	}

	/**
	 * Show link feedback
	 * 
	 * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.BorderItemSelectionEditPolicy#showChangeBoundsFeedback(org.eclipse.gef.requests.ChangeBoundsRequest)
	 */
	protected void showChangeBoundsFeedback(org.eclipse.gef.requests.ChangeBoundsRequest request) {
		if(getHost() instanceof IBorderItemEditPart) {
			IBorderItemEditPart borderItemEP = (IBorderItemEditPart)getHost();
			IBorderItemLocator borderItemLocator = borderItemEP.getBorderItemLocator();

			if(borderItemLocator != null) {
				IFigure feedback = getDragSourceFeedbackFigure();
				PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
				getHostFigure().translateToAbsolute(rect);
				rect.translate(request.getMoveDelta());
				rect.resize(request.getSizeDelta());
				getHostFigure().translateToRelative(rect);
				Rectangle realLocation = borderItemLocator.getValidLocation(rect.getCopy(), borderItemEP.getFigure());
				getHostFigure().translateToAbsolute(realLocation);
				feedback.translateToRelative(realLocation);
				feedback.setBounds(realLocation);
			}
		} else {
			super.showChangeBoundsFeedback(request);
		}

		// translate the feedback figure
		IFigure p = getDragSourceFeedbackFigure();

		PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
		getHostFigure().translateToAbsolute(rect);
		rect.translate(request.getMoveDelta());
		rect.resize(request.getSizeDelta());

		p.translateToRelative(rect);
		p.setBounds(rect);

		Point referencePoint = getReferencePoint(request);
		Point end = getLinkEndPoint(request, referencePoint);
		linkFeedback.setEnd(end);
		Point start = getLinkStartPoint(request, referencePoint, end);
		if(start != null) {
			linkFeedback.setStart(start);
		} else {
			linkFeedback.setStart(referencePoint);
		}
	};

	/**
	 * Get the reference point at the center of the parent figure
	 * 
	 * @param request
	 *        change bounds request
	 * @return point
	 */
	private Point getReferencePoint(ChangeBoundsRequest request) {

		Point refPoint = ((LabelEditPart)getHost()).getReferencePoint();

		Rectangle centerMain = null;
		if(((IGraphicalEditPart)getHost().getParent()).getFigure() instanceof Connection) {
			centerMain = new Rectangle(refPoint.x, refPoint.y, 0, 0);
		} else {
			centerMain = ((IGraphicalEditPart)getHost().getParent()).getFigure().getBounds().getCopy();
			centerMain.translate(centerMain.width / 2, centerMain.height / 2);
		}

		PrecisionRectangle ref = new PrecisionRectangle(centerMain);
		getHostFigure().translateToAbsolute(ref);
		getDragSourceFeedbackFigure().translateToRelative(ref);
		return ref.getLocation();
	}

	/**
	 * Get the point for starting the link
	 * 
	 * @param request
	 *        change bounds request
	 * @param referencePoint
	 *        the reference point at the center of the start figure
	 * @param endPoint
	 *        the end point at the border of the end figure
	 * @return point where to start the link
	 */
	private Point getLinkStartPoint(ChangeBoundsRequest request, Point referencePoint, Point endPoint) {
		Point u1 = referencePoint;
		Point u2 = endPoint;
		if(getHost().getParent() instanceof AbstractBorderedShapeEditPart) {
			// the parent figure is a node : choose a point on its border
			AbstractBorderedShapeEditPart parentPart = (AbstractBorderedShapeEditPart)getHost().getParent();
			IFigure fig = parentPart.getMainFigure();
			if(fig instanceof NodeFigure) {
				PointList pointsList = ((NodeFigure)fig).getPolygonPoints().getCopy();
				// translate points relatively to drag feedback
				fig.translateToAbsolute(pointsList);
				getDragSourceFeedbackFigure().translateToRelative(pointsList);
				return getIntersectionPoint(pointsList, u1, u2);
			}
		}
		// either computation failed or the parent figure is a link
		return referencePoint;
	}

	/**
	 * Get the point for ending the link
	 * 
	 * @param request
	 *        change bounds request
	 * @param referencePoint
	 *        the reference point at the center of the start figure
	 * @return point
	 */
	private Point getLinkEndPoint(ChangeBoundsRequest request, Point referencePoint) {
		Rectangle rect = getDragSourceFeedbackFigure().getBounds();
		Point endPoint = getAppropriateBorderPoint(referencePoint, rect);
		return endPoint;
	}

	/**
	 * Get the point on the border intersection the segment
	 * 
	 * @param polygonalBounds
	 *        the list of points tracing the border
	 * @param insidePoint
	 *        first segment extremity
	 * @param outsideExtremity
	 *        second segment extremity
	 * @return the intersection point or null if none
	 */
	public static Point getIntersectionPoint(PointList polygonalBounds, Point insidePoint, Point outsideExtremity) {
		Point intersection = null;
		int i = 0;
		while(intersection == null && i < polygonalBounds.size() - 1) {
			Point v1 = polygonalBounds.getPoint(i).getCopy();
			Point v2 = polygonalBounds.getPoint(i + 1).getCopy();
			intersection = getIntersection(insidePoint, outsideExtremity, v1, v2);
			i++;
		}
		return intersection;
	}

	/**
	 * Get the point of the border which is the more appropriated to link to the
	 * reference point
	 * 
	 * @param referencePoint
	 *        point to refer to, which the link is directed to (not
	 *        necessary an end of the link)
	 * @param border
	 *        the border on which an anchoring point must be chosen
	 * @return a point of the border to use as link end
	 */
	public static Point getAppropriateBorderPoint(Point referencePoint, Rectangle border) {
		Point midTop = new Point(border.x + border.width / 2, border.y);
		Point midBottom = new Point(border.x + border.width / 2, border.y + border.height);
		Point midLeft = new Point(border.x, border.y + border.height / 2);
		Point midRight = new Point(border.x + border.width, border.y + border.height / 2);

		Dimension diffTop = referencePoint.getDifference(midTop);
		Dimension diffBottom = referencePoint.getDifference(midBottom);
		Dimension diffRight = referencePoint.getDifference(midRight);
		Dimension diffLeft = referencePoint.getDifference(midLeft);

		Point startPoint = midBottom;
		if(diffBottom.height > 0) {
			// check if right or left is more appropriated
			if(diffLeft.width < 0 && diffBottom.height < -diffLeft.width) {
				startPoint = midLeft;
			} else if(diffRight.width > 0 && diffBottom.height < diffRight.width) {
				startPoint = midRight;
			} else {
				startPoint = midBottom;
			}
		} else if(diffTop.height < 0) {
			// check if right or left is more appropriated
			if(diffLeft.width < 0 && -diffTop.height < -diffLeft.width) {
				startPoint = midLeft;
			} else if(diffRight.width > 0 && -diffTop.height < diffRight.width) {
				startPoint = midRight;
			} else {
				startPoint = midTop;
			}
		} else if(diffLeft.width < 0) {
			startPoint = midLeft;
		} else if(diffRight.width > 0) {
			startPoint = midRight;
		}
		return startPoint;
	}

	/**
	 * Get the intersection between segments joining the points
	 * 
	 * @param u1
	 *        the first extremity of the u segment
	 * @param u2
	 *        the second extremity of the u segment
	 * @param v1
	 *        the first extremity of the v segment
	 * @param v2
	 *        the second extremity of the v segment
	 * @return the intersection point or null if none
	 */
	public static Point getIntersection(Point u1, Point u2, Point v1, Point v2) {
		float denom = ((u2.y - u1.y) * (v2.x - v1.x)) - ((u2.x - u1.x) * (v2.y - v1.y));
		float nume_a = ((u2.x - u1.x) * (v1.y - u1.y)) - ((u2.y - u1.y) * (v1.x - u1.x));
		float nume_b = ((v2.x - v1.x) * (v1.y - u1.y)) - ((v2.y - v1.y) * (v1.x - u1.x));

		if(denom == 0.0f) {
			return null;
		}
		float ua = nume_a / denom;
		float ub = nume_b / denom;

		if(ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f) {
			// Get the intersection point.
			Float x = v1.x + ua * (v2.x - v1.x);
			Float y = v1.y + ua * (v2.y - v1.y);
			return new Point(x.intValue(), y.intValue());
		}
		return null;
	}
}

Back to the top