Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9a0f9a531243248386cf227bc40dd55c7f3ffc95 (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
/*****************************************************************************
 * Copyright (c) 2011, 2018 CEA LIST, Christian W. Damus, 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bugs 323802, 440108
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation
 *  Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *  Christian W. Damus - bug 507479
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.properties.modelelement;

import static org.eclipse.emf.ecore.util.EcoreUtil.isAncestor;
import static org.eclipse.uml2.uml.ParameterDirectionKind.INOUT_LITERAL;
import static org.eclipse.uml2.uml.ParameterDirectionKind.IN_LITERAL;
import static org.eclipse.uml2.uml.ParameterDirectionKind.OUT_LITERAL;
import static org.eclipse.uml2.uml.ParameterDirectionKind.RETURN_LITERAL;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.HistoryUtil;
import org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils;
import org.eclipse.papyrus.infra.internationalization.utils.utils.InternationalizationConstants;
import org.eclipse.papyrus.infra.properties.ui.modelelement.EMFModelElement;
import org.eclipse.papyrus.infra.properties.ui.providers.FeatureContentProvider;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFGraphicalContentProvider;
import org.eclipse.papyrus.infra.ui.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.infra.ui.emf.utils.ProviderHelper;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.providers.EmptyContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.util.INameResolutionHelper;
import org.eclipse.papyrus.infra.widgets.util.IPapyrusConverter;
import org.eclipse.papyrus.uml.properties.creation.ConnectorTypeEditorFactory;
import org.eclipse.papyrus.uml.properties.creation.MessageValueSpecificationFactory;
import org.eclipse.papyrus.uml.properties.creation.OwnedRuleCreationFactory;
import org.eclipse.papyrus.uml.properties.creation.UMLPropertyEditorFactory;
import org.eclipse.papyrus.uml.properties.databinding.KeywordObservableValue;
import org.eclipse.papyrus.uml.properties.databinding.UMLLabelObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.ExtensionRequiredObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableList;
import org.eclipse.papyrus.uml.tools.databinding.PapyrusObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.ProvidedInterfaceObservableList;
import org.eclipse.papyrus.uml.tools.databinding.RequiredInterfaceObservableList;
import org.eclipse.papyrus.uml.tools.databinding.UnsettableStringValue;
import org.eclipse.papyrus.uml.tools.providers.ConstrainedElementContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLContainerContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLContentProvider;
import org.eclipse.papyrus.uml.tools.providers.UMLFilteredLabelProvider;
import org.eclipse.papyrus.uml.tools.util.UMLReferenceConverter;
import org.eclipse.papyrus.uml.tools.utils.NameResolutionHelper;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * A ModelElement representing a UML Element
 *
 * @author Camille Letavernier
 *
 */
public class UMLModelElement extends EMFModelElement {

	/**
	 *
	 * Constructor.
	 *
	 * @param source
	 *            The EObject represented by this ModelElement
	 */
	public UMLModelElement(final EObject source) {
		super(source);
	}

	/**
	 *
	 * Constructor.
	 *
	 * @param source
	 *            The EObject represented by this ModelElement
	 * @param domain
	 *            The EditingDomain on which the commands will be executed
	 */
	public UMLModelElement(final EObject source, final EditingDomain domain) {
		super(source, domain);
	}

	@Override
	public IObservable doGetObservable(final String propertyPath) {
		IObservable value = null;
		if (InternationalizationConstants.LABEL_PROPERTY_PATH.equals(propertyPath) && InternationalizationPreferencesUtils.isInternationalizationNeedToBeLoaded()) {
			value = new UMLLabelObservableValue((NamedElement) source, domain);
		} else if (InternationalizationConstants.KEYWORD_PROPERTY_PATH.equals(propertyPath)) {
			value = new KeywordObservableValue((Stereotype) source, domain);
		} else {
			FeaturePath featurePath = getFeaturePath(propertyPath);
			EStructuralFeature feature = getFeature(propertyPath);

			if (feature == UMLPackage.eINSTANCE.getExtension_IsRequired()) {
				value = new ExtensionRequiredObservableValue((Extension) source, domain);
			} else if (feature == UMLPackage.eINSTANCE.getPort_Provided()) {
				value = new ProvidedInterfaceObservableList((Port) source, domain);
			} else if (feature == UMLPackage.eINSTANCE.getPort_Required()) {
				value = new RequiredInterfaceObservableList((Port) source, domain);
			} else if (feature == null) {
				value = null;
			} else if (feature.getUpperBound() != 1) {
				IObservableList list = domain == null ? EMFProperties.list(featurePath).observe(source) : new PapyrusObservableList(EMFProperties.list(featurePath).observe(source), domain, getSource(featurePath), feature);
				value = list;
			} else if ((feature == UMLPackage.Literals.NAMED_ELEMENT__NAME) && (domain != null)) {
				// Empty string as a name is not useful, so we unset instead
				value = new UnsettableStringValue(getSource(featurePath), feature, domain);
			} else {
				value = domain == null ? EMFProperties.value(featurePath).observe(source) : new PapyrusObservableValue(getSource(featurePath), feature, domain);
			}
		}
		return value;
	}

	@Override
	protected boolean isFeatureEditable(final String propertyPath) {
		boolean result = false;
		if (InternationalizationConstants.LABEL_PROPERTY_PATH.equals(propertyPath) || InternationalizationConstants.KEYWORD_PROPERTY_PATH.equals(propertyPath)) {
			result = true;
		} else {
			EStructuralFeature feature = getFeature(propertyPath);
			if (feature == UMLPackage.eINSTANCE.getMessage_Signature()) {
				result = true;
			} else if (feature == UMLPackage.eINSTANCE.getExtension_IsRequired()) {
				result = true;
			} else if (feature == UMLPackage.eINSTANCE.getPort_Provided() || feature == UMLPackage.eINSTANCE.getPort_Required() &&
					source instanceof Port) {
				result = ((Port) source).getType() != null;
			} else {
				result = super.isFeatureEditable(propertyPath);
			}
		}
		return result;
	}

	@Override
	public IStaticContentProvider getContentProvider(final String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);

		if (feature == null) {
			return EmptyContentProvider.instance;
		}

		// Workaround: the standard ContentProvider does not correctly hide the selected elements in ReferenceSelector.
		// With a ContainmentBasedBrowseStrategy, it works better (But we don't have the infinite tree in the selection dialog).
		if (feature == UMLPackage.Literals.CONSTRAINT__CONSTRAINED_ELEMENT) {
			return new ConstrainedElementContentProvider(source, feature);
		}

		ResourceSet resourceSet = domain == null ? null : domain.getResourceSet();
		Optional<Predicate<EObject>> isValid = Optional.empty();

		if (feature == UMLPackage.Literals.MESSAGE__SIGNATURE) {
			// according to the UML norm 2.5, section 17.4.3.1
			// The signature of a Message refers to either an Operation or a Signal.
			isValid = Optional.of(element -> element instanceof Operation || element instanceof Signal);
		} else if (feature == UMLPackage.Literals.INTERACTION_FRAGMENT__COVERED) {
			// Can only cover a lifeline in the same interaction. So, allow any object in the same
			// interaction or that contains the interaction (so that it may be reached in the tree)
			InteractionFragment self = (InteractionFragment) this.source;
			Interaction myInteraction = (Interaction) EMFCoreUtil.getContainer(self, UMLPackage.Literals.INTERACTION);
			isValid = Optional.ofNullable(myInteraction)
					.map(interaction -> element -> isAncestor(interaction, element));
		}

		return isValid.map(
				valid -> new UMLContentProvider(source, feature, null, resourceSet) {
					@Override
					public boolean isValidValue(Object element) {
						EObject eObject = EMFHelper.getEObject(element);
						return valid.test(eObject) && super.isValidValue(element);
					}
				})
				.orElseGet(() -> new UMLContentProvider(source, feature, null, resourceSet));
	}

	@Override
	public boolean isOrdered(final String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if (feature == UMLPackage.eINSTANCE.getStereotype_Icon()) {
			return true;
		}
		return super.isOrdered(propertyPath);
	}

	@Override
	public ReferenceValueFactory getValueFactory(final String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if (!(feature instanceof EReference)) {
			return null;
		}
		EReference reference = (EReference) feature;
		if (reference == UMLPackage.eINSTANCE.getMessage_Argument()) {
			if (source instanceof Message) {
				Set<ParameterDirectionKind> directions = new HashSet<ParameterDirectionKind>();
				switch (((Message) source).getMessageSort()) {
				case REPLY_LITERAL:
					directions.add(OUT_LITERAL);
					directions.add(INOUT_LITERAL);
					directions.add(RETURN_LITERAL);
					return new MessageValueSpecificationFactory(reference, (Message) source, directions);
				case SYNCH_CALL_LITERAL:
				case ASYNCH_CALL_LITERAL:
				case ASYNCH_SIGNAL_LITERAL:
					directions.add(IN_LITERAL);
					directions.add(INOUT_LITERAL);
					return new MessageValueSpecificationFactory(reference, (Message) source, directions);
				case DELETE_MESSAGE_LITERAL:
				case CREATE_MESSAGE_LITERAL:
				default:
					break;
				}
			}
		}

		boolean isOwnedRuleSubset = ownedRuleSubsets.contains(reference);

		if (isOwnedRuleSubset) {
			return new OwnedRuleCreationFactory(reference);
		}

		UMLPropertyEditorFactory factory;

		if (reference == UMLPackage.eINSTANCE.getConnector_Type() && source instanceof Connector) {
			factory = new ConnectorTypeEditorFactory(reference);
		} else if (reference == UMLPackage.eINSTANCE.getMessage_Signature()) {
			factory = new UMLPropertyEditorFactory(reference) {

				/**
				 * @see org.eclipse.papyrus.infra.properties.ui.creation.EcorePropertyEditorFactory#getAvailableEClasses()
				 *
				 * @return
				 */
				@Override
				protected List<EClass> getAvailableEClasses() {
					// according to the UML norm 2.5, section 17.4.3.1
					// The signature of a Message refers to either an Operation or a Signal.
					final List<EClass> eClasses = new ArrayList<EClass>();
					eClasses.add(UMLPackage.eINSTANCE.getOperation());
					eClasses.add(UMLPackage.eINSTANCE.getSignal());
					return eClasses;
				}
			};
		} else {
			factory = new UMLPropertyEditorFactory(reference);
		}

		EClass type = reference.getEReferenceType();

		factory.setContainerLabelProvider(new UMLFilteredLabelProvider());
		factory.setReferenceLabelProvider(new EMFLabelProvider());
		ITreeContentProvider contentProvider = new UMLContainerContentProvider(source, reference);

		ResourceSet rs = source == null ? null : source.eResource() == null ? null : source.eResource().getResourceSet();
		EMFGraphicalContentProvider provider = ProviderHelper.encapsulateProvider(contentProvider, rs, HistoryUtil.getHistoryID(source, feature, "container"));

		factory.setContainerContentProvider(provider);
		factory.setReferenceContentProvider(new FeatureContentProvider(type));

		return factory;
	}

	@Override
	public boolean isMandatory(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);

		// Avoid unsetting container references (i.e. reference to the parent)
		// (Which would result in detaching the edited object from the model)
		// See Bug 404445: [Constraint] Unsetting the context of a constraint destroys it (Semantically)
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=404445
		if (feature instanceof EReference) {
			EReference reference = (EReference) feature;
			if (reference.isContainer()) {
				return true;
			}
		}

		return super.isMandatory(propertyPath);
	}

	/**
	 * The set of all EStructuralFeature representing subsets of {@link Namespace#getOwnedRules()}
	 */
	public static final Set<EStructuralFeature> ownedRuleSubsets = new HashSet<EStructuralFeature>();

	static {
		// Behavior
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getBehavior_Precondition());
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getBehavior_Postcondition());

		// Operation
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getOperation_BodyCondition());
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getOperation_Precondition());
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getOperation_Postcondition());

		// ProtocolTransition
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getProtocolTransition_PreCondition());
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getProtocolTransition_PostCondition());

		// Transition
		ownedRuleSubsets.add(UMLPackage.eINSTANCE.getTransition_Guard());
	}

	@Override
	public IValidator getValidator(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if (feature == UMLPackage.eINSTANCE.getNamedElement_Name()) {

			return new org.eclipse.papyrus.uml.tools.databinding.NamedElementValidator(source);
		}

		// TODO improve if our model add his own constraint
		return null;
	}

	/**
	 * @see org.eclipse.papyrus.infra.properties.ui.modelelement.AbstractModelElement#getNameResolutionHelper(java.lang.String)
	 *
	 * @param propertyPath
	 * @return
	 */
	@Override
	public INameResolutionHelper getNameResolutionHelper(String propertyPath) {
		EObject source = getSource();
		Object feature = getFeature(propertyPath);
		if (feature instanceof EStructuralFeature && source instanceof Element) {
			EStructuralFeature f = (EStructuralFeature) feature;
			EClassifier etype = f.getEType();
			if (etype instanceof EClass) {
				return new NameResolutionHelper((Element) source, (EClass) etype);
			}
		}
		return super.getNameResolutionHelper(propertyPath);
	}

	/**
	 *
	 * @see org.eclipse.papyrus.infra.properties.ui.modelelement.AbstractModelElement#getPapyrusConverter(java.lang.String)
	 *
	 * @param propertyPath
	 * @return
	 */
	@Override
	public IPapyrusConverter getPapyrusConverter(String propertyPath) {
		INameResolutionHelper helper = getNameResolutionHelper(propertyPath);
		if (helper != null) {
			return new UMLReferenceConverter(helper, getFeature(propertyPath).isMany());
		}
		return null;
	}
}

Back to the top