Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49714414a7d4fe5f01a77fba137f61145c47f405 (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
/*****************************************************************************
 * 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:
 *   CEA LIST - Initial API and implementation
 *   Vincent Lorenzo - bug 492522
 *   Mickaël ADAM (ALL4TEC) - mickael.adam@all4tec.net - Bug 517679
 *****************************************************************************/

package org.eclipse.papyrus.uml.service.types.helper.advice;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
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.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
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.DestroyReferenceRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.gmf.runtime.notation.Connector;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.types.core.utils.ElementTypeRegistryUtils;
import org.eclipse.papyrus.uml.service.types.utils.ElementUtil;
import org.eclipse.papyrus.uml.service.types.utils.RequestParameterConstants;
import org.eclipse.uml2.common.util.CacheAdapter;

/**
 * Abstract class used to remove referenced element when a link with no direct semantic element linked to it is destroyed
 *
 */
public abstract class AbstractReferenceDeleteRelationshipEditHelperAdvice extends AbstractEditHelperAdvice {

	/**
	 * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#configureRequest(org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest)
	 *
	 * @param request
	 */
	@Override
	public void configureRequest(IEditCommandRequest request) {
		super.configureRequest(request);
		if (request instanceof DestroyReferenceRequest && ((DestroyReferenceRequest) request).getContainingFeature() == null) {
			String visualId = (String) request.getParameter(RequestParameterConstants.VIEW_VISUAL_ID);
			((DestroyReferenceRequest) request).setContainingFeature(getFeature(visualId, request.getClientContext()));
		}
	}

	/**
	 * 
	 * @param visualId
	 *            the visual id
	 * @return
	 * 		the EReference represented by the view with this visual id
	 * @since 3.1
	 */
	protected EReference getFeature(String visualId, IClientContext context) {
		List<IElementType> elementTypes = ElementTypeRegistryUtils.getElementTypesBySemanticHint(visualId, context.getId());

		for (IElementType iElementType : elementTypes) {
			Map<String, EReference> featureElementTypeToEReferenceMap = getFeatureElementTypeToEReferenceMap();
			for (String featureElementType : featureElementTypeToEReferenceMap.keySet()) {
				List<ISpecializationType> subs = Arrays.asList(ElementTypeRegistry.getInstance().getSpecializationsOf(featureElementType));

				boolean typeOf = ElementUtil.isTypeOf(iElementType, ElementTypeRegistry.getInstance().getType(featureElementType));// Fix due to the miss of element in all context
				if (subs.contains(iElementType) || typeOf) {
					return featureElementTypeToEReferenceMap.get(featureElementType);
				}
			}
		}

		return null;
	}

	/**
	 * 
	 * @return
	 * 		the map linking an element type to a EReference
	 */
	protected abstract Map<String, EReference> getFeatureElementTypeToEReferenceMap();

	/**
	 * 
	 * @param eobject
	 *            an eobject
	 * @return
	 * 		all the views for the given element, the returned value is never <code>null</code>
	 */
	private List<View> getAllViewsFor(final EObject eobject) {
		final List<View> elementViews = new ArrayList<View>();
		if (null != eobject) {

			final Collection<Setting> settings = CacheAdapter.getInstance().getNonNavigableInverseReferences(eobject);
			// get all views representing the feature owner
			for (Setting ref : settings) {
				if (NotationPackage.eINSTANCE.getView_Element().equals(ref.getEStructuralFeature())) {
					View view = (View) ref.getEObject();
					if (view != null) {
						elementViews.add(view);
					}
				}
			}
		}
		return elementViews;
	}

	/**
	 * 
	 * @param request
	 *            the destroy reference request
	 * @return
	 * 		the list of connector to destroy for the request
	 * @since 3.1
	 */
	protected List<Connector> findConnectorsToDestroy(final DestroyReferenceRequest request) {
		List<Connector> connectorsToDestroy = new ArrayList<Connector>();
		EObject featureOwner = request.getContainer();
		if (null != featureOwner) {
			final List<View> featureOwnerViews = getAllViewsFor(featureOwner);
			if (checkSourceEdgeToFindConnectorToDestroy()) {
				connectorsToDestroy.addAll(findSourceConnectorsToDestroy(request, featureOwnerViews));
			}
			if (checkTargetEdgeToFindConnectorToDestroy()) {
				connectorsToDestroy.addAll(findTargetConnectorsToDestroy(request, featureOwnerViews));
			}
		}
		return connectorsToDestroy;
	}

	/**
	 * 
	 * @param request
	 *            the destroy reference request
	 * @param featureOwnerViews
	 *            the views representing the feature owner
	 * @return
	 * 		the list of connectors linked as source to the views of the feature owner to destroy
	 */
	private List<Connector> findSourceConnectorsToDestroy(final DestroyReferenceRequest request, final List<View> featureOwnerViews) {
		final List<Connector> viewsToDestroy = new ArrayList<Connector>();
		final EObject removedReference = request.getReferencedObject();
		final String visualId = (String) request.getParameter(RequestParameterConstants.VIEW_VISUAL_ID);
		if (visualId != null && !visualId.isEmpty() && removedReference != null)
			for (View currentView : featureOwnerViews) {
				List<?> sourceEdges = currentView.getSourceEdges();
				for (Object current : sourceEdges) {
					if (current instanceof Connector) {
						Connector conn = (Connector) current;
						if (visualId.equals(conn.getType())) {
							View target = conn.getTarget();
							EObject model = target.getElement();
							if (removedReference == model) {
								viewsToDestroy.add(conn);
							}
						}
					}
				}
			}
		return viewsToDestroy;
	}

	/**
	 * 
	 * @param request
	 *            the destroy reference request
	 * @param featureOwnerViews
	 *            the views representing the feature owner
	 * @return
	 * 		the list of connectors linked as target to the views of the feature owner to destroy
	 */
	private List<Connector> findTargetConnectorsToDestroy(final DestroyReferenceRequest request, final List<View> featureOwnerViews) {
		final List<Connector> viewsToDestroy = new ArrayList<Connector>();
		final EObject removedReference = request.getReferencedObject();
		final String visualId = (String) request.getParameter(RequestParameterConstants.VIEW_VISUAL_ID);
		if (visualId != null && !visualId.isEmpty() && removedReference != null)
			for (View currentView : featureOwnerViews) {
				final List<?> targetEdgets = currentView.getTargetEdges();
				for (Object current : targetEdgets) {
					if (current instanceof Connector) {
						Connector conn = (Connector) current;
						if (visualId.equals(conn.getType())) {
							final View target = conn.getTarget();
							EObject model = target.getElement();
							if (removedReference == model) {
								viewsToDestroy.add(conn);
							}
						}
					}
				}
			}
		return viewsToDestroy;
	}

	/**
	 * 
	 * @return
	 * 		<code>true</code> if we cross the connector whose the source is connected to the element
	 */
	protected abstract boolean checkSourceEdgeToFindConnectorToDestroy();

	/**
	 * 
	 * @return
	 * 		<code>true</code> if we cross the connector whose the target is connected to the element
	 */
	protected abstract boolean checkTargetEdgeToFindConnectorToDestroy();

	/**
	 * @see org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice#getBeforeDestroyReferenceCommand(org.eclipse.gmf.runtime.emf.type.core.requests.DestroyReferenceRequest)
	 *
	 * @param request
	 * @return
	 */
	@Override
	protected ICommand getBeforeDestroyReferenceCommand(final DestroyReferenceRequest request) {
		final CompositeCommand command = new CompositeCommand("Clear Connectors"); // $NON-NLS-1$

		// 1. get all connectors to destroy
		final Collection<Connector> connectorsToDestroy = findConnectorsToDestroy(request);

		if (!connectorsToDestroy.isEmpty()) {
			final TransactionalEditingDomain domain = request.getEditingDomain();
			for (View edge : connectorsToDestroy) {
				final DestroyElementRequest destroy = new DestroyElementRequest(domain, edge, false);
				final Object eHelperContext = destroy.getEditHelperContext();
				final IElementType context = ElementTypeRegistry.getInstance().getElementType(eHelperContext);
				if (context != null) {
					final ICommand result = context.getEditCommand(destroy);
					if (result != null) {
						command.add(result);
					}
				}
			}
		}

		if (command.isEmpty() || !command.canExecute()) {
			return null;
		}
		return command;
	}

}

Back to the top