Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47bf88300c4b3564b9d8815b8cd7d43f07b71892 (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
/*****************************************************************************
 * Copyright (c) 2010 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;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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.Edge;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.uml2.common.util.CacheAdapter;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.DurationObservation;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.TimeObservation;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class SequenceViewDependentsAdvice extends AbstractEditHelperAdvice {

	@SuppressWarnings("rawtypes")
	@Override
	protected ICommand getBeforeSetCommand(SetRequest request) {
		EObject element = request.getElementToEdit();
		View view = findView(element);
		if (view != null) {
			// Clear edge's when the UML model has been changed.
			EStructuralFeature feature = request.getFeature();
			Object value = request.getValue();
			if (!(value instanceof List<?>)) {
				return null;
			}
			List<Edge> destroyEdges = new ArrayList<Edge>();
			List<Element> targetObjects = new ArrayList<Element>();
			if (element instanceof Comment && UMLPackage.Literals.COMMENT__ANNOTATED_ELEMENT == feature) {
				targetObjects.addAll(((Comment) element).getAnnotatedElements());
			} else if (element instanceof DurationObservation && UMLPackage.Literals.DURATION_OBSERVATION__EVENT == feature) {
				targetObjects.addAll(((DurationObservation) element).getEvents());
			} else if (element instanceof TimeObservation && UMLPackage.Literals.TIME_OBSERVATION__EVENT == feature) {
				targetObjects.add(((TimeObservation) element).getEvent());
			} else if (element instanceof Constraint && UMLPackage.Literals.CONSTRAINT__CONSTRAINED_ELEMENT == feature) {
				targetObjects.addAll(((Constraint) element).getConstrainedElements());
			}
			for (Element object : targetObjects) {
				if (value instanceof List && ((List) value).contains(object)) {
					continue;
				}
				View target = findView(object);
				List sourceConnections = ViewUtil.getSourceConnections(view);
				for (Object connector : sourceConnections) {
					if (!(connector instanceof Edge)) {
						continue;
					}
					Edge edge = (Edge) connector;
					if (target == edge.getTarget()) {
						destroyEdges.add(edge);
					}
				}
			}
			if (!destroyEdges.isEmpty()) {
				TransactionalEditingDomain editingDomain = request.getEditingDomain();
				CompositeCommand command = new CompositeCommand("Clear Connectors");
				for (Edge edge : destroyEdges) {
					DestroyElementRequest destroy = new DestroyElementRequest(editingDomain, edge, 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 super.getBeforeSetCommand(request);
	}

	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