Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba5b0142c3f878569946a2f024bc40a28a9755b3 (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
/*****************************************************************************
 * Copyright (c) 2016 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.figures;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderedNodeFigure;
import org.eclipse.papyrus.infra.gmfdiag.common.figure.node.ScalableCompartmentFigure;
import org.eclipse.papyrus.infra.gmfdiag.common.figure.node.SelectableBorderedNodeFigure;
import org.eclipse.papyrus.uml.diagram.common.figure.node.AutomaticCompartmentLayoutManager;

/**
 * @since 3.0
 *        This class manage as {@link AutomaticCompartmentLayoutManager} but layout figure as XY layout if there are {@link ILifelineInternalFigure}
 */
public class LifeLineLayoutManager extends AutomaticCompartmentLayoutManager {
	private int bottomHeaderY = 0;
	/** The layout contraints */
	protected Map<IFigure, Rectangle> constraints = new HashMap<IFigure, Rectangle>();

	public int getBottomHeader() {
		return bottomHeaderY;
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.figure.node.AutomaticCompartmentLayoutManager#layout(org.eclipse.draw2d.IFigure)
	 *
	 * @param container
	 */
	@Override
	public void layout(IFigure container) {
		super.layout(container);
		layoutXYFigure(container);
	}

	/*
	 * Fills the given bound data as a non-compartment child
	 *
	 * @param container
	 * The container to layout
	 * 
	 * @param bound
	 * The bound to fill
	 * 
	 * @param previous
	 * The previously filled bound
	 */
	protected void fillBoundsForOther(IFigure container, Rectangle bound, Rectangle previous) {
		bound.x = container.getBounds().x + 1; // +1, see bug 490318, restore +1 to fix shift from Papyrus Luna to Papyrus Mars
		bound.width = container.getBounds().width;
		if (previous == null) {
			bound.y = container.getBounds().y + 3;
		} else {
			bound.y = previous.getBottomLeft().y + 1;
		}
	}

	/**
	 * @see org.eclipse.draw2d.AbstractLayout#setConstraint(org.eclipse.draw2d.IFigure, java.lang.Object)
	 *
	 * @param child
	 * @param constraint
	 */
	@Override
	public void setConstraint(IFigure child, Object constraint) {
		if (child instanceof SelectableBorderedNodeFigure) {
			if (((SelectableBorderedNodeFigure) child).getMainFigure() instanceof ILifelineInternalFigure) {
				if (constraint instanceof Rectangle) {
					constraints.put(child, (Rectangle) constraint);
				}
			}
		}
		super.setConstraint(child, constraint);
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.figure.node.AutomaticCompartmentLayoutManager#fillBoundsForCompartment(org.eclipse.draw2d.IFigure, org.eclipse.draw2d.geometry.Rectangle, org.eclipse.draw2d.geometry.Rectangle, double)
	 *
	 * @param container
	 * @param bound
	 * @param previous
	 * @param ratio
	 */
	@Override
	protected void fillBoundsForCompartment(IFigure container, Rectangle bound, Rectangle previous, double ratio) {
		fillBoundsForOther(container, bound, previous);
		// bound.height = (int) (bound.height / ratio);
		if (previous == null) {
			bound.y = container.getBounds().y;
		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.figure.node.AutomaticCompartmentLayoutManager#layoutDefault(org.eclipse.draw2d.IFigure)
	 *
	 * @param container
	 */
	@Override
	protected void layoutDefault(IFigure container) {
		ScalableCompartmentFigure symbolFigure = null;
		int i = 0;
		while (symbolFigure == null && i < container.getChildren().size()) {
			if (container.getChildren().get(i) instanceof ScalableCompartmentFigure) {
				symbolFigure = (ScalableCompartmentFigure) container.getChildren().get(i);
			}
			i++;
		}
		super.layoutDefault(container);

		// change coordinate to set the symbol at the top
		if (symbolFigure != null) {
			symbolFigure.getBounds().setY(container.getBounds().getTop().y);
		}
		Rectangle containerBounds = container.getBounds();
		IFigure previous = null;
		for (IFigure child : visibleOthers) {
			Rectangle bound = new Rectangle();
			if (previous != null) {
				if (child.equals(symbolFigure)) {
					bound.y = containerBounds.y + 3;
				} else {
					bound.y = previous.getBounds().getBottomLeft().y + 1;
					bound.x = containerBounds.x + 1;
					bound.width = containerBounds.width;
					bound.height = child.getBounds().height;
					bottomHeaderY = bound.y + bound.height;
				}
			} else {
				bound.x = containerBounds.x + 1;
				// here the symbo may be present
				if (symbolFigure != null) {
					bound.y = containerBounds.y + 3 + symbolFigure.getBounds().height;
				} else {
					bound.y = containerBounds.y + 3;
				}

				bound.width = containerBounds.width;
				bound.height = child.getBounds().height;
			}
			child.setBounds(bound);
			previous = child;
		}


	}


	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.figure.node.AutomaticCompartmentLayoutManager#layoutOthers(org.eclipse.draw2d.geometry.Rectangle)
	 *
	 * @param container
	 */
	@Override
	protected void layoutOthers(Rectangle container) {
		super.layoutOthers(container);

		IFigure previous = null;
		for (IFigure child : visibleOthers) {
			Rectangle bound = new Rectangle();
			if (previous != null) {
				bound.y = previous.getBounds().getBottomLeft().y + 1;
				bound.x = container.x + 1;
				bound.width = container.width;
				bound.height = child.getBounds().height;
				bottomHeaderY = bound.y + bound.height;
			} else {
				bound.x = container.x + 1;
				// in the case where the content is grater than the container
				// it is forbidden to change the y coordinate
				bound.y = container.y + 3;
				bound.width = container.width;
				bound.height = child.getBounds().height;
				bottomHeaderY = bound.y + bound.height;
			}
			child.setBounds(bound);
			previous = child;
		}
	}


	/**
	 * all {@link ILifelineInternalFigure} must be managed as XY layout by respecting their position and size
	 * 
	 * @param container
	 */
	protected void layoutXYFigure(IFigure container) {
		for (Object child : container.getChildren()) {
			if (child instanceof BorderedNodeFigure) {
				if (((BorderedNodeFigure) child).getMainFigure() instanceof ILifelineInternalFigure) {
					Rectangle theConstraint = constraints.get(child).getCopy();
					if (theConstraint != null) {
						theConstraint.translate(container.getBounds().getTopLeft());
						((BorderedNodeFigure) child).setBounds(theConstraint);
					}
				}
			}
		}
	}
}

Back to the top