Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b225b9345b1b677f697354b12f9116c8edd66d9 (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
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 * 
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.modelelement;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFProperties;
import org.eclipse.emf.databinding.FeaturePath;
import org.eclipse.emf.ecore.EAttribute;
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.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.databinding.EMFObservableList;
import org.eclipse.papyrus.infra.emf.databinding.EMFObservableValue;
import org.eclipse.papyrus.infra.emf.providers.EMFContentProvider;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.widgets.creation.ReferenceValueFactory;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.views.properties.Activator;
import org.eclipse.papyrus.views.properties.creation.EcorePropertyEditorFactory;

/**
 * A ModelElement to manipulate EMF objects.
 * This ModelElement uses EMFProperties to retrieve Observables when there
 * is no Editing Domain, and {@link EMFObservableValue} / {@link EMFObservableList} when
 * an Editing domain is available
 * 
 * @author Camille Letavernier
 */
public class EMFModelElement extends AbstractModelElement {

	/**
	 * The EObject manipulated by this ModelElement
	 */
	protected EObject source;

	/**
	 * The Editing Domain of the EObject for this ModelElement
	 */
	protected EditingDomain domain;

	/**
	 * 
	 * Constructs a new EMFModelElement for the given EObject
	 * 
	 * @param source
	 */
	public EMFModelElement(EObject source) {
		this(source, null);
	}

	/**
	 * 
	 * Constructs a new EMFModelElement for the given EObject and Editing Domain
	 * 
	 * @param source
	 * @param domain
	 */
	public EMFModelElement(EObject source, EditingDomain domain) {
		this.source = source;
		this.domain = domain;
	}

	/**
	 * @return the EditingDomain for this ModelElement
	 */
	public EditingDomain getDomain() {
		return domain;
	}

	/**
	 * @return the EObject for this ModelElement
	 */
	public EObject getSource() {
		return source;
	}

	@Override
	protected IObservable doGetObservable(String propertyPath) {
		FeaturePath featurePath = getFeaturePath(propertyPath);
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return null;
		}

		if(feature.getUpperBound() != 1) {
			IObservableList list = domain == null ? EMFProperties.list(featurePath).observe(source) : new EMFObservableList(EMFProperties.list(featurePath).observe(source), domain, getSource(featurePath), feature);
			return list;
		}

		IObservableValue value = domain == null ? EMFProperties.value(featurePath).observe(source) : new EMFObservableValue(getSource(featurePath), feature, domain);
		return value;
	}

	/**
	 * Returns the last EObject by following the given featurePath from the {@link #source} EObject
	 * The last feature of the featurePath can be used to retrieve value from the returned EObject
	 * 
	 * @param featurePath
	 * @return the EObject found by resolving to the given FeaturePath
	 */
	public EObject getSource(FeaturePath featurePath) {
		EObject currentSource = source;
		EStructuralFeature[] features = featurePath.getFeaturePath();
		for(int i = 0; i < features.length - 1; i++) {
			currentSource = (EObject)currentSource.eGet(features[i]);
		}
		return currentSource;
	}

	/**
	 * Returns the feature represented by the given FeaturePath
	 * 
	 * @param featurePath
	 * @return
	 *         The last feature obtained by navigating the feature path
	 */
	public EStructuralFeature getFeature(FeaturePath featurePath) {
		EStructuralFeature[] features = featurePath.getFeaturePath();
		return features[features.length - 1];
	}

	/**
	 * Returns the feature represented by the given propertyPath.
	 * 
	 * @param propertyPath
	 *        The property path may contain one or more dots to navigate the properties (e.g. : feature1.feature2.feature3)
	 * @return
	 *         The last feature obtained by resolving the full property path
	 */
	public EStructuralFeature getFeature(String propertyPath) {
		FeaturePath featurePath = getFeaturePath(propertyPath);
		return getFeature(featurePath);
	}

	/**
	 * Returns the featurePath corresponding to the given propertyPath
	 * 
	 * @param propertyPath
	 *        The property path may contain one or more dots to navigate the properties (e.g. : feature1.feature2.feature3)
	 * @return
	 *         The featurePath corresponding to the given propertyPath
	 */
	public FeaturePath getFeaturePath(String propertyPath) {
		String[] featureNames = propertyPath.split("\\."); //$NON-NLS-1$
		EStructuralFeature[] features = new EStructuralFeature[featureNames.length];

		int i = 0;
		EClass currentClass = source.eClass();
		for(String featureName : featureNames) {
			EStructuralFeature feature = currentClass.getEStructuralFeature(featureName);
			features[i++] = feature;
			if(i < featureNames.length) {
				if(feature instanceof EReference) {
					EReference reference = (EReference)feature;
					EClassifier type = reference.getEType();
					if(type instanceof EClass) {
						currentClass = (EClass)type;
						continue;
					}
				}

				Activator.log.warn("Cannot find feature path " + propertyPath + " for EClass " + source.eClass()); //$NON-NLS-1$ //$NON-NLS-2$
				return null;
			}
		}

		return FeaturePath.fromList(features);
	}

	@Override
	public IStaticContentProvider getContentProvider(String propertyPath) {
		FeaturePath featurePath = getFeaturePath(propertyPath);
		EStructuralFeature feature = getFeature(featurePath);
		if(feature != null) {
			return new EMFContentProvider(getSource(featurePath), feature);
		}
		return super.getContentProvider(propertyPath);
	}

	@Override
	public ILabelProvider getLabelProvider(String propertyPath) {
		try {
			return ServiceUtilsForResource.getInstance().getServiceRegistry(source.eResource()).getService(LabelProviderService.class).getLabelProvider();
		} catch (ServiceException ex) {
			Activator.log.error(ex);
			return new LabelProvider();
		}
	}

	@Override
	public boolean isOrdered(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return true;
		}
		return feature.isOrdered();
	}

	@Override
	public boolean isUnique(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return false;
		}
		return feature.isUnique();
	}

	@Override
	public boolean isMandatory(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return false;
		}

		return EMFHelper.isRequired(feature);
	}

	@Override
	public boolean isEditable(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return false;
		}
		return feature.isChangeable() && !EMFHelper.isReadOnly(source);
	}

	@Override
	public boolean forceRefresh(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return false;
		}
		return feature.isDerived();
	}

	@Override
	public ReferenceValueFactory getValueFactory(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature != null) {
			if(feature instanceof EReference) {
				EReference reference = (EReference)feature;
				if(reference.isContainment()) {
					return new EcorePropertyEditorFactory(reference);
				}
			}
		}

		return super.getValueFactory(propertyPath);
	}

	@Override
	public Object getDefaultValue(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return null;
		}
		return feature.getDefaultValue();
	}

	@Override
	public boolean getDirectCreation(String propertyPath) {
		EStructuralFeature feature = getFeature(propertyPath);
		if(feature == null) {
			return false;
		}

		if(feature instanceof EAttribute) {
			return false;
		}

		return ((EReference)feature).isContainment();
	}
}

Back to the top