Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b76858aad8f08fd6890ad31098f84613a51e46ff (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
/*****************************************************************************
 * Copyright (c) 2010 CEA
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Atos Origin - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.locator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.diagram.ui.internal.figures.BorderItemContainerFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.papyrus.uml.diagram.common.locator.AdvancedBorderItemLocator;
import org.eclipse.papyrus.uml.diagram.sequence.figures.DestructionEventFigure;

/**
 * This class is used to place all BorderItem node in the middle of the figure for the X but let the position Y
 *
 */
public class CenterLocator extends AdvancedBorderItemLocator {


	/**
	 * Constructor
	 *
	 * @param parentFigure
	 *            the parent figure
	 * @param location
	 *            ContinuationLocator.TOP or ContinuationLocator.BOTTOM
	 */
	public CenterLocator(IFigure parentFigure, int location) {
		super(parentFigure, location);
	}

	/**
	 * The DestructionEventFigure.
	 *
	 * It must be access through the method {@link #getDestructionEventFigure()}
	 */
	private DestructionEventFigure destructionEventFigure = null;

	/**
	 * The BorderItemContainerFigure. It must be access through the method {@link #getBorderItemContainerFigure()}
	 */
	private BorderItemContainerFigure borderItemContainerFigure = null;

	/**
	 * Get the DestructionEventFigure of the lifeline, if it is drawn.
	 *
	 * @return the DestructionEventFigure or null
	 */
	private DestructionEventFigure getDestructionEventFigure() {
		if (destructionEventFigure == null) {
			BorderItemContainerFigure borderItemContainerFigure = getBorderItemContainerFigure();
			if (borderItemContainerFigure != null) {
				for (Object child : borderItemContainerFigure.getChildren()) {
					if (child instanceof DefaultSizeNodeFigure) {
						for (Object figure : ((DefaultSizeNodeFigure) child).getChildren()) {
							if (figure instanceof DestructionEventFigure) {
								destructionEventFigure = (DestructionEventFigure) figure;
								return destructionEventFigure;
							}
						}
					}
				}
			}
		}
		return destructionEventFigure;
	}

	/**
	 * Get the BorderItemContainerFigure
	 *
	 * @return the borderItemContainerFigure or null
	 */
	private BorderItemContainerFigure getBorderItemContainerFigure() {
		if (borderItemContainerFigure == null) {
			IFigure figure = getParentFigure().getParent();
			for (Object object : figure.getChildren()) {
				if (object instanceof BorderItemContainerFigure) {
					borderItemContainerFigure = (BorderItemContainerFigure) object;
					return borderItemContainerFigure;
				}
			}
		}
		return borderItemContainerFigure;
	}

	/**
	 * Overridden :
	 * - the destructionEventFigure is always drawn at the end of the figure
	 *
	 * @see org.eclipse.papyrus.uml.diagram.common.locator.AdvancedBorderItemLocator#getValidLocation(org.eclipse.draw2d.geometry.Rectangle, org.eclipse.draw2d.IFigure)
	 */
	@Override
	public Rectangle getValidLocation(Rectangle proposedLocation, IFigure borderItem) {
		// The valid position for destruction event is always the bottom
		if (getDestructionEventFigure() != null) {
			if (borderItem.equals(getDestructionEventFigure().getParent())) {
				Rectangle realLocation = new Rectangle(proposedLocation);
				Point point = new Point(getParentBorder().getCenter().x - realLocation.getSize().width / 2, getParentBorder().y + getParentBorder().height - realLocation.height / 2);
				realLocation.setLocation(point);
				return realLocation;
			}
		}
		proposedLocation.setX(getParentFigure().getBounds().x + getParentFigure().getBounds().width / 2 - borderItem.getBounds().width() / 2);

		if (proposedLocation.y - proposedLocation.height / 2 <= getParentFigure().getBounds().y) {
			proposedLocation.setY(getParentFigure().getBounds().y);
		}
		if (proposedLocation.y - proposedLocation.height / 2 >= getParentFigure().getBounds().getBottomLeft().y) {
			proposedLocation.setY(getParentFigure().getBounds().getBottomLeft().y);
		}
		return super.getValidLocation(proposedLocation, borderItem);
	}


}

Back to the top