Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2600026dc9ce5c3db34cd47c14737fd28d1ab2ea (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 *
 * 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:
 *  Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *  Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - Text alignment implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.locator;



import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.GEFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.infra.gmfdiag.common.locator.IPapyrusBorderItemLocator;

/**
 * This class is used to constrain the position of ExternalNodeLabel. The
 * locator let the external node label be freely located by used anywhere around
 * the parent figure.
 */
public class ExternalLabelPositionLocator implements IPapyrusBorderItemLocator {

	/** the figure around which this label appears. */
	protected IFigure parentFigure = null;

	/** the position constraint. */
	protected Rectangle constraint = new Rectangle(0, 0, 0, 0);

	/** The position. */
	int position = PositionConstants.EAST;

	/** The offset. */
	private Dimension offset = new Dimension();

	/** The constrained. */
	private boolean isConstrained = false;

	/** The text alignment. */
	private int textAlignment;

	/** The view. */
	private View view;

	/** The Edit Part. */
	private EditPart editPart;

	/**
	 * @param editpart
	 *            the edit part to set
	 */
	@Override
	public void setEditpart(EditPart editPart) {
		this.editPart = editPart;
	}

	private boolean cachedIsConstrained = false;

	private Point margin;

	/**
	 * Sets the view.
	 *
	 * @param view
	 *            the view to set
	 */
	@Override
	public void setView(View view) {
		this.view = view;
	}

	/**
	 * Gets the position.
	 *
	 * @return the position
	 */
	@Override
	public int getPosition() {
		return position;
	}

	/**
	 * Sets the position.
	 *
	 * @param position
	 *            the position to set
	 */
	@Override
	public void setPosition(int position) {
		this.position = position;
	}

	/**
	 * Gets the offset.
	 *
	 * @return the offset
	 */
	@Override
	public Dimension getOffset() {
		return offset;
	}

	/**
	 * Sets the offset.
	 *
	 * @param offset
	 *            the offset to set
	 */
	@Override
	public void setOffset(Dimension offset) {
		this.offset = offset;
	}

	/**
	 * Checks if is constrained.
	 *
	 * @return the constrained
	 */
	@Override
	public boolean isConstrained() {
		return isConstrained;
	}

	/**
	 * Sets the constrained.
	 *
	 * @param constrained
	 *            the constrained to set
	 */
	@Override
	public void setConstrained(boolean constrained) {
		this.isConstrained = constrained;
	}

	/**
	 * get the location constraint.
	 *
	 * @return the constraint
	 */
	public Rectangle getConstraint() {
		return constraint;
	}

	/**
	 * Constructor *.
	 *
	 * @param parentFigure
	 *            the parent figure
	 */
	public ExternalLabelPositionLocator(IFigure parentFigure) {
		this.parentFigure = parentFigure;
	}

	/**
	 * Gets the valid location.
	 *
	 * @param proposedLocation
	 *            the proposed location
	 * @param borderItem
	 *            the border item
	 * @return the valid location
	 * @see org.eclipse.gmf.runtime.draw2d.ui.figures.IBorderItemLocator#getValidLocation(org.eclipse.draw2d.geometry.Rectangle, org.eclipse.draw2d.IFigure)
	 */
	@Override
	public Rectangle getValidLocation(Rectangle proposedLocation, IFigure borderItem) {
		return new Rectangle(proposedLocation);
	}

	/**
	 * Sets the constraint.
	 *
	 * @param constraint
	 *            the new constraint
	 * @see org.eclipse.gmf.runtime.draw2d.ui.figures.IBorderItemLocator#setConstraint(org.eclipse.draw2d.geometry.Rectangle)
	 */
	@Override
	public void setConstraint(Rectangle constraint) {
		this.constraint = constraint;
	}

	/**
	 * Gets the current side of parent.
	 *
	 * @return current side of parent
	 * @see org.eclipse.gmf.runtime.draw2d.ui.figures.IBorderItemLocator#getCurrentSideOfParent()
	 */
	@Override
	public int getCurrentSideOfParent() {
		return position;
	}

	/**
	 * Relocate.
	 *
	 * @param target
	 *            the target
	 * @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
	 */
	@Override
	public void relocate(IFigure target) {
		Rectangle proposedBounds = constraint.getCopy();

		proposedBounds.setLocation(constraint.getLocation().translate(parentFigure.getBounds().getTopLeft()));
		proposedBounds.setSize(target.getPreferredSize());

		if (isConstrained) { // TO_FIX if there is more than one label
			Point newconstraint;
			Point newOffset;
			// Set the translation when alignment is auto
			switch (getPositionOnParent()) {
			case PositionConstants.WEST:
				// alignRight:
				newconstraint = new Point(-proposedBounds.width - offset.width, offset.height);
				newOffset = new Point(newconstraint.x + proposedBounds.width, newconstraint.y);
				break;
			case PositionConstants.EAST:
				// alignLeft
				newconstraint = new Point(parentFigure.getBounds().width + offset.width, offset.height);
				newOffset = new Point(newconstraint.x, newconstraint.y);
				break;
			case PositionConstants.NORTH:
				// alignLeft center to the north
				newconstraint = new Point(-proposedBounds.width / 2 + offset.width, -parentFigure.getBounds().height - offset.height);
				newOffset = new Point(newconstraint.x, newconstraint.y);
				break;
			case PositionConstants.SOUTH:
				// alignLeft center to the south
				newconstraint = new Point(-proposedBounds.width / 2 + offset.width, parentFigure.getBounds().height + offset.height);
				newOffset = new Point(newconstraint.x, newconstraint.y);
				break;
			default:
				// the default is like the EAST
				newconstraint = new Point(proposedBounds.width + offset.width, offset.height);
				newOffset = new Point(newconstraint.x, newconstraint.y);
				break;
			}
			proposedBounds.setLocation(newconstraint.translate(parentFigure.getBounds().getTopLeft()));

			// Save new location only on change
			final Point offset = newOffset;
			ChangeBoundsRequest req = new ChangeBoundsRequest(RequestConstants.REQ_MOVE);
			req.setEditParts(editPart);
			req.setLocation(offset);
			Command command = editPart.getCommand(req);
			if (command != null && command.canExecute()) {
				TransactionUtil.getEditingDomain(view).getCommandStack().execute(GEFtoEMFCommandWrapper.wrap(command));
			}

		} else {
			int x;
			// Set Location
			switch (textAlignment) {
			case PositionConstants.LEFT:
				x = 0;
				break;
			case PositionConstants.RIGHT:
				x = -proposedBounds.width;
				break;
			case PositionConstants.CENTER:
				x = -proposedBounds.width / 2;
				break;
			default:
				x = 0;
				break;
			}
			proposedBounds.translate(x, 0);
		}

		target.setBounds(proposedBounds);
		cachedIsConstrained = isConstrained;
	}

	/**
	 * Gets the position on parent.
	 *
	 * @return the position on parent
	 */
	public int getPositionOnParent() {
		Rectangle portBounds = null;
		Rectangle parentBounds = null;
		int position = this.position;

		// Get the port figure
		if (editPart != null) {
			EditPart parent = editPart.getParent();
			if (parent instanceof IPapyrusEditPart) {
				IFigure portPrimaryShape = ((IPapyrusEditPart) parent).getPrimaryShape();
				portBounds = portPrimaryShape.getBounds();

				// Get the port's parent figure
				// if it's a papyrus edit part and the figure is paint(width !=0)
				if (parent.getParent() instanceof IPapyrusEditPart && portBounds.width != 0) {
					IFigure parentPrimaryShape = ((IPapyrusEditPart) parent.getParent()).getPrimaryShape();
					parentBounds = parentPrimaryShape.getBounds();

					if (portBounds.x + portBounds.width / 2 == parentBounds.x) {
						// West position
						position = PositionConstants.WEST;
					} else if (portBounds.x + portBounds.width / 2 == parentBounds.getBottomRight().x) {
						// East Position
						position = PositionConstants.EAST;
					} else if (portBounds.y + portBounds.height / 2 == parentBounds.y) {
						position = PositionConstants.NORTH;
					} else if (portBounds.y + portBounds.height / 2 == parentBounds.getBottomRight().y) {
						position = PositionConstants.SOUTH;
					}
				}
			}
		}

		return position;
	}


	/**
	 * Gets the text alignment.
	 *
	 * @return the textAlignment
	 */
	public int getTextAlignment() {
		return textAlignment;
	}

	/**
	 * Sets the text alignment.
	 *
	 * @param textAlignment
	 *            the textAlignment to set
	 */
	@Override
	public void setTextAlignment(int textAlignment) {
		this.textAlignment = textAlignment;
	}

	/**
	 * Checks if is initialize.
	 *
	 * @return true, if is initialize
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.locator.IPapyrusBorderItemLocator#isInitialize()
	 */
	@Override
	public boolean isInitialize() {
		return view != null;
	}

	/**
	 * Sets the margin.
	 *
	 * @param margin
	 *            the margin to set
	 */
	@Override
	public void setMargin(Point margin) {
		this.margin = margin;
	}
}

Back to the top