Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e811072fe19b86b8fd9d60dbc7cdaeb2640cd6cc (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*****************************************************************************
 * Copyright (c) 2017 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:
 *   Patrick Tessier (CEA LIST) - Initial API and implementation
 *   Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - Bug 521312
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IPrimaryEditPart;
import org.eclipse.papyrus.infra.gmfdiag.common.SemanticFromGMFElement;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AbstractExecutionSpecificationEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AbstractMessageEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.papyrus.uml.diagram.sequence.util.LogOptions;
import org.eclipse.papyrus.uml.diagram.sequence.util.RedirectionCommandStackListener;
import org.eclipse.papyrus.uml.diagram.sequence.util.RedirectionContentAdapter;
import org.eclipse.papyrus.uml.diagram.sequence.util.RedirectionOperationListener;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ExecutionSpecification;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
import org.eclipse.uml2.uml.OccurrenceSpecification;

/**
 * this class is used to manage strong and weak references to editPart in the sequence : all execution specification and all messages
 * for example: a message connected to an executionSpecification means that a strong reference exists between editpart of the message
 * and the editpart of the execution specification.
 * Two consecutive execution specifications on the same life-line means that a weak reference exists from the top exec to the bottom lifeline.
 * 
 * @since 4.0
 */
public class SequenceReferenceEditPolicy extends GraphicalEditPolicy implements NotificationListener {

	public static final String SEQUENCE_REFERENCE = "SEQUENCE_REFERENCE"; //$NON-NLS-1$
	public static final String NO_ROLE = "NO_ROLE"; //$NON-NLS-1$
	public static final String ROLE_START = "START"; //$NON-NLS-1$
	public static final String ROLE_FINISH = "FINISH"; //$NON-NLS-1$
	public static final String ROLE_SOURCE = "SOURCE"; //$NON-NLS-1$
	public static final String ROLE_TARGET = "TARGET"; //$NON-NLS-1$

	protected HashMap<EditPart, String> weakReferences = new HashMap<EditPart, String>();
	protected HashMap<EditPart, String> strongReferences = new HashMap<EditPart, String>();
	protected RedirectionContentAdapter redirectionContentAdapter;
	protected RedirectionCommandStackListener redirectionCommandStackListener;
	protected RedirectionOperationListener redirectionOperationListener;

	/**
	 * @return the weakReferences
	 */
	public HashMap<EditPart, String> getWeakReferences() {
		return weakReferences;
	}

	/**
	 * @return the strongReferences
	 */
	public HashMap<EditPart, String> getStrongReferences() {
		return strongReferences;
	}

	/**
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#activate()
	 *
	 */
	@Override
	public void activate() {
		super.activate();
		// add a listener to update weak and string references
		redirectionOperationListener = new RedirectionOperationListener(this);
		OperationHistoryFactory.getOperationHistory().addOperationHistoryListener(redirectionOperationListener);
		
		updateStrongAndWeakReferences();
	}

	/**
	 * @see org.eclipse.gef.editpolicies.AbstractEditPolicy#deactivate()
	 *
	 */
	@Override
	public void deactivate() {
		OperationHistoryFactory.getOperationHistory().removeOperationHistoryListener(redirectionOperationListener);
		super.deactivate();
	}

	/**
	 * compute strong a weak reference
	 */
	public void updateStrongAndWeakReferences() {
		UMLDiagramEditorPlugin.log.trace(LogOptions.SEQUENCE_DEBUG, "+ Update Strong and weak ref " + getHost().getClass().getName());//$NON-NLS-1$ //$NON-NLS-2$

		strongReferences.clear();
		weakReferences.clear();

		// management of execution specification
		if (getHost() instanceof AbstractExecutionSpecificationEditPart && (((AbstractExecutionSpecificationEditPart) getHost()).resolveSemanticElement() instanceof ExecutionSpecification)) {

			ExecutionSpecification exec = (ExecutionSpecification) ((AbstractExecutionSpecificationEditPart) getHost()).resolveSemanticElement();
			// manage Strong references
			if (exec.getStart() instanceof MessageEnd) {
				MessageEnd messageEnd = (MessageEnd) exec.getStart();
				addMessageIntoReferences(messageEnd, strongReferences, ROLE_START);
			}
			if (exec.getFinish() instanceof MessageEnd) {
				MessageEnd messageEnd = (MessageEnd) exec.getFinish();
				addMessageIntoReferences(messageEnd, strongReferences, ROLE_FINISH);
			}
			// manage weak references
			// the weak reference is the next element associated to next event after the finish event.
			if (exec.getCovereds().size() == 1) {
				Lifeline currentLifeline = exec.getCovereds().get(0);
				fillWeakReference((OccurrenceSpecification) exec.getFinish(), currentLifeline);
			}
		}
		// management of messages
		if (getHost() instanceof AbstractMessageEditPart && (((AbstractMessageEditPart) getHost()).resolveSemanticElement() instanceof Message)) {
			Message aMessage = (Message) ((AbstractMessageEditPart) getHost()).resolveSemanticElement();

			// manage Strong references
			// fill about source
			MessageEnd sourceEvent = aMessage.getSendEvent();
			if (null != sourceEvent) {
				addExecutionSpecIntoReferences((OccurrenceSpecification) sourceEvent, strongReferences, ROLE_SOURCE);
			}
			// fill about target
			MessageEnd targetEvent = aMessage.getReceiveEvent();
			if (null != targetEvent) {
				addExecutionSpecIntoReferences((OccurrenceSpecification) targetEvent, strongReferences, ROLE_TARGET);
			}

			// manage weakReferences
			// source
			if (null != sourceEvent && ((OccurrenceSpecification) sourceEvent).getCovereds().size() == 1) {
				Lifeline currentLifeline = ((OccurrenceSpecification) sourceEvent).getCovereds().get(0);
				fillWeakReference(((OccurrenceSpecification) sourceEvent), currentLifeline);
			}
			// target
			if (null != targetEvent && ((OccurrenceSpecification) targetEvent).getCovereds().size() == 1) {
				Lifeline currentLifeline = ((OccurrenceSpecification) targetEvent).getCovereds().get(0);
				fillWeakReference(((OccurrenceSpecification) targetEvent), currentLifeline);
			}
		}


	}

	/**
	 * this method is used to add a weak reference from the next event after the given event
	 * 
	 * @param sourceEvent
	 *            the given event the next is maybe an element to add in the weakreference
	 * @param currentLifeline,
	 *            the lifeline where we look for the next event
	 */
	protected void fillWeakReference(OccurrenceSpecification sourceEvent, Lifeline currentLifeline) {
		Element nextEvent = getNextEventFromLifeline(currentLifeline, sourceEvent);
		if (!isCoveredByStrinReference(nextEvent)) {
			if (nextEvent instanceof MessageOccurrenceSpecification && isOnlyMessageEnd((MessageOccurrenceSpecification) nextEvent)) {
				addMessageIntoReferences((MessageEnd) nextEvent, weakReferences, NO_ROLE);
			} else if (nextEvent instanceof OccurrenceSpecification) {
				addExecutionSpecIntoReferences((OccurrenceSpecification) nextEvent, weakReferences, NO_ROLE);
			}
		}
	}

	/**
	 * this method is used to get the execution specification associated to the message End
	 * 
	 * @param event
	 *            the given event where we look for a executionSpecification, must be never null
	 * @return the associated execution specification or null if there is no association
	 */
	protected ExecutionSpecification getExecutionSpecificationAssociatedToEvent(OccurrenceSpecification event) {
		ExecutionSpecification exec = null;
		if (null != event && !event.getCovereds().isEmpty()) {
			Lifeline currentLifeline = event.getCovereds().get(0);
			int index = 0;
			while (exec == null && index < currentLifeline.getCoveredBys().size()) {
				if (currentLifeline.getCoveredBys().get(index) instanceof ExecutionSpecification) {
					ExecutionSpecification currentExec = (ExecutionSpecification) currentLifeline.getCoveredBys().get(index);
					if (event.equals(currentExec.getStart())) {
						exec = currentExec;
					}
					if (event.equals(currentExec.getFinish())) {
						exec = currentExec;
					}
				}
				index++;
			}
		}
		return exec;
	}

	/**
	 * this method returns the next events after the given event
	 * 
	 * @param event
	 *            we look for the next event after this one.
	 * @param currentLifeline
	 *            the current lifeline where we look for the covered element
	 * @return null if there is not event after the given event or the next.
	 */
	protected Element getNextEventFromLifeline(Lifeline currentLifeline, Object event) {
		int index = currentLifeline.getCoveredBys().indexOf(event);
		Element nextEvent = null;
		if (index != -1) {
			// we look for the next event
			index = index + 1;
			while (nextEvent == null && (index < currentLifeline.getCoveredBys().size())) {
				if (currentLifeline.getCoveredBys().get(index) instanceof MessageEnd ||
						currentLifeline.getCoveredBys().get(index) instanceof OccurrenceSpecification) {
					nextEvent = currentLifeline.getCoveredBys().get(index);
				}
				index++;
			}
		}
		return nextEvent;
	}


	public boolean isCoveredByStrinReference(Element event) {
		for (Iterator<EditPart> iterator = getStrongReferences().keySet().iterator(); iterator.hasNext();) {
			EditPart editPart = (EditPart) iterator.next();
			if (editPart instanceof AbstractMessageEditPart) {
				Message message = (Message) (((AbstractMessageEditPart) editPart)).resolveSemanticElement();
				if (message.getSendEvent().equals(event)) {
					return true;
				}
				if (message.getReceiveEvent().equals(event)) {
					return true;
				}
			}
			if (editPart instanceof AbstractExecutionSpecificationEditPart) {
				ExecutionSpecification exec = (ExecutionSpecification) (((AbstractExecutionSpecificationEditPart) editPart)).resolveSemanticElement();
				if (exec.getStart().equals(event)) {
					return true;
				}
				if (exec.getFinish().equals(event)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * given a messageEnd, the corresponding editPart to a message is adding to the references list
	 * 
	 * @param messageEnd
	 *            a messageEnd
	 * @param referenceList
	 *            the list of references
	 */
	protected void addMessageIntoReferences(MessageEnd messageEnd, HashMap<EditPart, String> referenceList, String role) {
		if (messageEnd.getMessage() != null) {
			IGraphicalEditPart resultedEditPart = getEditPartFromSemantic(messageEnd.getMessage());
			if (resultedEditPart != null) {
				if (referenceList.equals(strongReferences)) {
					referenceList.put(resultedEditPart, role);
				} else if (referenceList.equals(weakReferences) && !(strongReferences.containsKey(resultedEditPart))) {
					referenceList.put(resultedEditPart, role);
				}
			}
		}
	}


	/**
	 * test if this event is only use of a message not also for an executionSpecification
	 * 
	 * @param messageEnd
	 * @return true if the message End is only used by a message
	 */
	protected boolean isOnlyMessageEnd(MessageOccurrenceSpecification messageEnd) {
		InteractionFragment owner = (InteractionFragment) messageEnd.getOwner();
		if (owner != null) {
			for (Element fragment : owner.getOwnedElements()) {
				if (fragment instanceof ExecutionSpecification) {
					if (messageEnd.equals(((ExecutionSpecification) (fragment)).getStart())) {
						return false;
					}
					if (messageEnd.equals(((ExecutionSpecification) (fragment)).getFinish())) {
						return false;
					}
				}
			}
		}
		return true;
	}

	/**
	 * given a messageEnd, the corresponding editPart to a ExecutionSpec is added to the references list
	 * 
	 * @param messageEnd
	 *            a messageEnd
	 * @param referenceList
	 *            the list of references
	 */
	protected void addExecutionSpecIntoReferences(OccurrenceSpecification sourceEvent, HashMap<EditPart, String> referenceList, String role) {
		ExecutionSpecification executionSpec = getExecutionSpecificationAssociatedToEvent(sourceEvent);
		if (executionSpec != null) {
			IGraphicalEditPart resultedEditPart = getEditPartFromSemantic(executionSpec);
			if (referenceList.equals(strongReferences)) {
				if (resultedEditPart != null) {
					if (executionSpec.getStart().equals(sourceEvent)) {
						referenceList.put(resultedEditPart, ROLE_START);
					} else {
						referenceList.put(resultedEditPart, ROLE_FINISH);
					}
				}
			} else if (referenceList.equals(weakReferences) && !(strongReferences.containsKey(resultedEditPart))) {
				if (resultedEditPart != null) {
					if (executionSpec.getStart().equals(sourceEvent)) {
						referenceList.put(resultedEditPart, ROLE_START);
					} else {
						referenceList.put(resultedEditPart, ROLE_FINISH);
					}
				}
			}
		}

	}

	/**
	 * this method return the controller attached to the semantic element
	 * the complexity of this algorithm is N (N the number of controller in the opened sequence diagram)
	 * 
	 * @param semanticElement
	 *            must be different from null
	 * @return the reference to the controller or null.
	 */
	protected IGraphicalEditPart getEditPartFromSemantic(Object semanticElement) {
		IGraphicalEditPart researchedEditPart = null;
		SemanticFromGMFElement semanticFromGMFElement = new SemanticFromGMFElement();
		EditPartViewer editPartViewer = getHost().getViewer();
		if (editPartViewer != null) {
			// look for all edit part if the semantic is contained in the list
			Iterator<?> iter = editPartViewer.getEditPartRegistry().values().iterator();

			while (iter.hasNext() && researchedEditPart == null) {
				Object currentEditPart = iter.next();
				// look only amidst IPrimary editpart to avoid compartment and labels of links
				if (currentEditPart instanceof IPrimaryEditPart) {
					Object currentElement = semanticFromGMFElement.getSemanticElement(currentEditPart);
					if (semanticElement.equals(currentElement)) {
						researchedEditPart = ((IGraphicalEditPart) currentEditPart);
					}
				}
			}
		}
		return researchedEditPart;
	}



	/**
	 * @see org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void notifyChanged(Notification notification) {
		updateStrongAndWeakReferences();
	}


	/**
	 * get the current Interaction from the editpart
	 * 
	 * @return null or the interaction
	 */
	protected Interaction getInteraction() {
		if (getHost() instanceof AbstractExecutionSpecificationEditPart && (((AbstractExecutionSpecificationEditPart) getHost()).resolveSemanticElement() instanceof ExecutionSpecification)) {

			ExecutionSpecification exec = (ExecutionSpecification) ((AbstractExecutionSpecificationEditPart) getHost()).resolveSemanticElement();
			return exec.getEnclosingInteraction();
		}
		if (getHost() instanceof AbstractMessageEditPart && (((AbstractMessageEditPart) getHost()).resolveSemanticElement() instanceof Message)) {
			Message aMessage = (Message) ((AbstractMessageEditPart) getHost()).resolveSemanticElement();
			return aMessage.getInteraction();
		}
		return null;
	}
}

Back to the top