Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 77d929c6081bc83e7a7801489c5a0d02cbe55003 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 * 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:
 *  Boutheina Bannour (CEA LIST) boutheina.bannour@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.helpers.advice;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Connector;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.ContextLinkEditPart;
import org.eclipse.uml2.common.util.CacheAdapter;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * <pre>
 * This HelperAdvice completes {@contextLink Constraint} edit commands with diagram specific
 * commands in order to remove context link
 * in case a {@context Constraint} is modified.
 * </pre>
 */
public class ConstraintHelperAdvice extends AbstractEditHelperAdvice {

	@Override
	protected ICommand getAfterSetCommand(SetRequest request) {
		EObject element = request.getElementToEdit();
		if (element instanceof Constraint) {
			View view = findView(element);
			if (view != null) {
				EStructuralFeature feature = request.getFeature();
				final Object value = request.getValue();
				Vector<Edge> edgesToDestroy = new Vector<>();
				Element targetContextElement = null;
				if (UMLPackage.eINSTANCE.getConstraint_Context().equals(feature)) {
					Namespace constraintContext = ((Constraint) element).getContext();
					if (constraintContext == value) {
						// if the new value to set is the already context value
						// then do noting
						return null;
					}
					targetContextElement = constraintContext;
					if (targetContextElement != null) {
						View target = findView(targetContextElement);
						List sourceConnections = ViewUtil.getSourceConnections(view);
						for (Object connector : sourceConnections) {
							if (!(connector instanceof Connector)) {
								continue;
							}
							Edge edge = (Edge) connector;
							if (("" + ContextLinkEditPart.VISUAL_ID).equals(edge.getType())) {
								if (target.getElement() == edge.getTarget().getElement()) {
									edgesToDestroy.add(edge);
								}
							}
						}
					}
					if (!edgesToDestroy.isEmpty()) {
						CompositeCommand command = new CompositeCommand("Clear context links");
						for (Iterator iterator = edgesToDestroy.iterator(); iterator.hasNext();) {
							Edge edgeToDestroy = (Edge) iterator.next();
							TransactionalEditingDomain editingDomain = request.getEditingDomain();
							DestroyElementRequest destroy = new DestroyElementRequest(editingDomain, edgeToDestroy, false);
							Object eHelperContext = destroy.getEditHelperContext();
							IElementType context = ElementTypeRegistry.getInstance().getElementType(eHelperContext);
							if (context != null) {
								ICommand result = context.getEditCommand(destroy);
								if (result != null) {
									command.add(result);
								}
							}
						}
						return command;
					}
				}
			}
		}
		return null;
	}

	private View findView(EObject element) {
		if (element == null) {
			return null;
		}
		Collection<Setting> settings = CacheAdapter.getInstance().getNonNavigableInverseReferences(element);
		for (Setting ref : settings) {
			if (NotationPackage.eINSTANCE.getView_Element().equals(ref.getEStructuralFeature())) {
				View view = (View) ref.getEObject();
				if (view != null) {
					return view;
				}
			}
		}
		return null;
	}
}

Back to the top