Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf09e8c0517b60087341a33dcd9685d200a85c5e (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
/*****************************************************************************
 * Copyright (c) 2013 CEA
 *
 *
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramEditPartsUtil;
import org.eclipse.uml2.uml.ExecutionOccurrenceSpecification;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.PartDecomposition;

/**
 * Ordering InteractionFragments of Interaction or InteractionOperand.
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=403233
 *
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class FragmentsOrderer {

	private static final float HALF_UNIT = 0.5f;

	private static final float CONVERT_UNIT = 1.0f;

	/**
	 * Interaction or InteractionOperand.
	 */
	private InteractionFragment fragmentRoot;

	private IGraphicalEditPart fragmentRootEditPart;

	private Map<InteractionFragment, Float> fragmentPositions;

	private Map<InteractionFragment, Integer> fragmentIndexes;

	private Map<View, Float[]> cachePositions;

	/**
	 * Constructor.
	 *
	 */
	public FragmentsOrderer(IGraphicalEditPart fragmentRootEditPart) {
		this.fragmentRootEditPart = fragmentRootEditPart;
	}

	public void addCachePosition(View view, Float[] position) {
		if (cachePositions == null) {
			cachePositions = new HashMap<>(1);
		}
		cachePositions.put(view, position);
	}

	private boolean prepared() {
		if (fragmentRootEditPart == null) {
			return false;
		}
		EObject element = fragmentRootEditPart.resolveSemanticElement();
		if (element instanceof Interaction || element instanceof InteractionOperand) {
			fragmentRoot = (InteractionFragment) element;
		} else {
			return false;
		}
		// compute fragment position;
		fragmentPositions = new HashMap<>();
		// compute new indexes.
		fragmentIndexes = new HashMap<>();
		EList<InteractionFragment> orderingFragments = getOrderingFragments();
		return orderingFragments != null && !orderingFragments.isEmpty();
	}

	/**
	 * Compute indexes with vertical position.
	 */
	private void computeNewIndexes() {
		fragmentIndexes.clear();
		computePositions();
		List<Entry<InteractionFragment, Float>> positionalEntries = new ArrayList<>(fragmentPositions.entrySet());
		Collections.sort(positionalEntries, new Comparator<Map.Entry<InteractionFragment, Float>>() {

			@Override
			public int compare(Entry<InteractionFragment, Float> o1, Entry<InteractionFragment, Float> o2) {
				Float v1 = o1.getValue();
				Float v2 = o2.getValue();
				if (v1 != null && v2 != null) {
					if (v1 < v2) {
						return -1;
					} else if (v1 > v2) {
						return 1;
					}
				}
				return 0;
			}
		});
		for (int i = 0; i < positionalEntries.size(); i++) {
			Entry<InteractionFragment, Float> entry = positionalEntries.get(i);
			fragmentIndexes.put(entry.getKey(), i);
		}
	}

	private EList<InteractionFragment> getOrderingFragments() {
		EList<InteractionFragment> fragments = null;
		if (fragmentRoot instanceof Interaction) {
			fragments = ((Interaction) fragmentRoot).getFragments();
		} else if (fragmentRoot instanceof InteractionOperand) {
			fragments = ((InteractionOperand) fragmentRoot).getFragments();
		}
		return fragments;
	}

	private View getGraphicalView(EObject eObj) {
		if (eObj instanceof ExecutionOccurrenceSpecification) {
			ExecutionSpecification execution = ((ExecutionOccurrenceSpecification) eObj).getExecution();
			return getGraphicalView(execution);
		} else if (eObj instanceof MessageOccurrenceSpecification) {
			return getGraphicalView(((MessageOccurrenceSpecification) eObj).getMessage());
		} else if (eObj instanceof PartDecomposition) {
			EList<Lifeline> covereds = ((PartDecomposition) eObj).getCovereds();
			if (covereds.size() == 1) {
				return getGraphicalView(covereds.get(0));
			}
		} else if (eObj != null) {
			List<?> views = DiagramEditPartsUtil.getEObjectViews(eObj);
			if (views.size() == 1) {
				return (View) views.get(0);
			}
			return null;
		}
		return null;
	}

	private Float getPosition(InteractionFragment fragment) {
		if (fragment == null) {
			return null;
		}
		View view = getGraphicalView(fragment);
		if (view == null) {
			return null;
		}
		Float[] preferPosition = cachePositions != null ? cachePositions.get(view) : null;
		EObject hostElement = ViewUtil.resolveSemanticElement(view);
		EditPart editPart = DiagramEditPartsUtil.getEditPartFromView(view, fragmentRootEditPart);
		if (fragment instanceof MessageOccurrenceSpecification && hostElement instanceof Message && editPart instanceof ConnectionNodeEditPart) {
			boolean isStart = fragment == ((Message) hostElement).getSendEvent();
			Point location = getAbsoluteEdgeExtremity((ConnectionNodeEditPart) editPart, isStart, preferPosition);
			if (location != null) {
				return isStart ? location.y - HALF_UNIT : location.y + HALF_UNIT;
			}
		} else if (fragment instanceof ExecutionOccurrenceSpecification && hostElement instanceof ExecutionSpecification && editPart instanceof IGraphicalEditPart) {
			Rectangle bounds = getAbsoluteBounds((IGraphicalEditPart) editPart, preferPosition);
			if (bounds != null) {
				if (bounds.height <= 0) {
					bounds.height = 50;// LifelineXYLayoutEditPolicy.EXECUTION_INIT_HEIGHT;
				}
				if (fragment == ((ExecutionSpecification) hostElement).getStart()) {
					return bounds.y - HALF_UNIT;
				} else {
					return bounds.bottom() + HALF_UNIT;
				}
			}
		} else if (view instanceof Shape && editPart instanceof IGraphicalEditPart) {
			Rectangle bounds = getAbsoluteBounds((IGraphicalEditPart) editPart, preferPosition);
			if (bounds != null) {
				return bounds.y * CONVERT_UNIT;
			}
		}
		return null;
	}

	private Point getAbsoluteEdgeExtremity(ConnectionNodeEditPart editPart, boolean isStart, Float[] preferPosition) {
		if (editPart == null) {
			return null;
		}
		PointList points = editPart.getConnectionFigure().getPoints().getCopy();
		if (points.size() == 2 && new Point(0, 0).equals(points.getFirstPoint()) && new Point(100, 100).equals(points.getLastPoint())) {
			// not display yet.
			if (preferPosition != null) {
				if (isStart && preferPosition[0] != null) {
					return new Point(0, preferPosition[0].intValue());
				} else if (!isStart && preferPosition[1] != null) {
					return new Point(0, preferPosition[1].intValue());
				}
			}
			return SequenceUtil.getAbsoluteEdgeExtremity(editPart, isStart, false);
		}
		return SequenceUtil.getAbsoluteEdgeExtremity(editPart, isStart, true);
	}

	private Rectangle getAbsoluteBounds(IGraphicalEditPart editPart, Float[] preferPosition) {
		if (editPart == null) {
			return null;
		}
		Rectangle rect = editPart.getFigure().getBounds().getCopy();
		if (rect.isEmpty() && rect.x == 0 && rect.y == 0) {// Not displayed yet.
			View view = editPart.getNotationView();
			if (view instanceof Node) {
				LayoutConstraint constraint = ((Node) view).getLayoutConstraint();
				if (constraint instanceof Bounds) {
					Bounds bounds = (Bounds) constraint;
					if (bounds.getX() > 0) {
						rect.x = bounds.getX();
					}
					if (bounds.getY() > 0) {
						rect.y = bounds.getY();
					} else if (preferPosition != null && preferPosition.length > 0 && preferPosition[0] != null) {
						rect.y = preferPosition[0].intValue();
					}
					if (bounds.getWidth() != -1) {
						rect.width = bounds.getWidth();
					}
					if (bounds.getHeight() != -1) {
						rect.height = bounds.getHeight();
					}
				}
			}
			Rectangle parentRect = getAbsoluteBounds((IGraphicalEditPart) editPart.getParent(), null);
			rect.x += parentRect.x;
			rect.y += parentRect.y;
		} else {
			rect = SequenceUtil.getAbsoluteBounds(editPart);
		}
		return rect;
	}

	public void ordering() {
		if (prepared()) {
			safelyChangeOrder();
		}
		clear();
	}

	/**
	 * @param orderingFragments
	 */
	protected void computePositions() {
		final EList<InteractionFragment> orderingFragments = getOrderingFragments();
		if (orderingFragments == null || orderingFragments.isEmpty()) {
			return;
		}
		for (InteractionFragment fragment : orderingFragments) {
			Float newPos = getPosition(fragment);
			fragmentPositions.put(fragment, newPos);
		}
	}

	private void clear() {
		if (fragmentIndexes != null) {
			fragmentIndexes.clear();
			fragmentIndexes = null;
		}
		if (fragmentPositions != null) {
			fragmentPositions.clear();
			fragmentPositions = null;
		}
		fragmentRoot = null;
	}

	/**
	 * This method should be called with write Transaction.
	 */
	protected void safelyChangeOrder() {
		EList<InteractionFragment> orderingFragments = getOrderingFragments();
		synchronized (orderingFragments) {
			computeNewIndexes();
			for (InteractionFragment fragment : new ArrayList<>(orderingFragments)) {
				int oldPos = orderingFragments.indexOf(fragment);
				if (oldPos == -1) {
					continue;
				}
				Integer newPos = fragmentIndexes.get(fragment);
				if (newPos == null || oldPos == newPos.intValue() || newPos.intValue() < 0 || newPos.intValue() >= orderingFragments.size()) {
					continue;
				}
				orderingFragments.move(newPos.intValue(), oldPos);
			}
		}
	}
}

Back to the top