Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5ab28b07b504ef462c23d956976ff8e429ecb8ff (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
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr
 *****************************************************************************/
package org.eclipse.papyrus.views.properties.runtime.modelhandler.emf;

import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.views.properties.runtime.Activator;
import org.eclipse.papyrus.views.properties.runtime.controller.EMFPropertyEditorController;
import org.eclipse.papyrus.views.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.StructuralFeature;


/**
 * Abstract Model handler for stereotype properties
 */
public abstract class EMFStereotypeFeatureModelHandler extends EMFFeatureModelHandler {

	/** name of the stereotype to edit */
	private final String stereotypeName;

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addListenersToModel(List<? extends EObject> objectsToEdit, EMFPropertyEditorController controller) {
		super.addListenersToModel(objectsToEdit, controller);
		for(EObject object : objectsToEdit) {
			if(object instanceof Element) {
				Iterator<EObject> it = ((Element)object).getStereotypeApplications().iterator();
				while(it.hasNext()) {
					it.next().eAdapters().add(controller);
				}
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void removeListenersFromModel(List<? extends EObject> objectsToEdit, EMFPropertyEditorController controller) {
		super.removeListenersFromModel(objectsToEdit, controller);
		for(EObject object : objectsToEdit) {
			if(object instanceof Element) {
				Iterator<EObject> it = ((Element)object).getStereotypeApplications().iterator();
				while(it.hasNext()) {
					it.next().eAdapters().remove(controller);
				}
			}
		}
	}

	/**
	 * Creates a new EMFStereotypeFeatureModelHandler.
	 * 
	 * @param stereotypeName
	 *        name of the stereotype to which this feature belongs
	 * 
	 * @param featureName
	 *        he name of the feature to edit
	 */
	public EMFStereotypeFeatureModelHandler(String stereotypeName, String featureName) {
		super(featureName);
		this.stereotypeName = stereotypeName;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public EStructuralFeature getFeatureByName(EObject objectToEdit) {
		if(objectToEdit instanceof Element) {
			return EMFUtils.getStereotypeFeatureByName((Element)objectToEdit, retrieveStereotype((Element)objectToEdit), getFeatureName());
		}
		Activator.log.error("Impossible to cast into UML element: " + objectToEdit, null); //$NON-NLS-1$
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getAvailableValues(EObject eObject) {
		EClass eClass = eObject.eClass();
		if(eClass == null) {
			Activator.log.debug("problems during initialization, looking for availables values"); //$NON-NLS-1$
			return null;
		}
		EStructuralFeature feature = getFeatureByName(eObject);
		if(!(feature instanceof EReference)) {
			Activator.log.debug("feature is not a reference, looking for availables values: " + feature); //$NON-NLS-1$
			return null;
		}

		IItemPropertySource itemPropertySource = (IItemPropertySource)factory.adapt(((Element)eObject).getStereotypeApplication(retrieveStereotype((Element)eObject)), IItemPropertySource.class);
		if(itemPropertySource == null) {
			Activator.log.debug("impossible to find item Property source for " + retrieveStereotype((Element)eObject)); //$NON-NLS-1$
			return null;
		}
		IItemPropertyDescriptor itemPropertyDescriptor = itemPropertySource.getPropertyDescriptor(retrieveStereotype((Element)eObject), feature);
		if(itemPropertyDescriptor == null) {
			Activator.log.debug("impossible to find item Property descriptor for " + retrieveStereotype((Element)eObject) + " and " + feature); //$NON-NLS-1$ //$NON-NLS-2$
			return null;
		}
		return itemPropertyDescriptor.getChoiceOfValues(eObject);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object getValueToEdit(EObject objectToEdit) {
		if(!(objectToEdit instanceof Element)) {
			Activator.log.warn("the object to edit is not a UML2 Element: " + objectToEdit); //$NON-NLS-1$
			return null;
		}
		Element elementToEdit = (Element)objectToEdit;
		Stereotype stereotype = retrieveStereotype(elementToEdit);
		if(stereotype != null) {
			return getValueForElement(elementToEdit, stereotype);
		} else {
			Activator.log.warn("Impossible to get the stereotype: " + stereotypeName + " on the element: " + elementToEdit + " for feature " + getFeatureName()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		}
		return null;
	}

	/**
	 * Returns the value for the given element
	 * 
	 * @param elementToEdit
	 *        the element being edited
	 * @param stereotype
	 *        the stereotype to edit
	 * @return the value of the stereotype feature
	 */
	protected Object getValueForElement(Element elementToEdit, Stereotype stereotype) {
		return elementToEdit.getValue(stereotype, getFeatureName());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setValueInModel(EObject objectToEdit, Object newValue) {
		if(!(objectToEdit instanceof Element)) {
			Activator.log.warn("the object to edit is not a UML2 Element: " + objectToEdit); //$NON-NLS-1$
			return;
		}
		Element elementToEdit = (Element)objectToEdit;
		Stereotype stereotype = retrieveStereotype(elementToEdit);
		if(stereotype != null) {
			setValueForElement(elementToEdit, stereotype, newValue);
		} else {
			Activator.log.warn("Impossible to set value to the stereotype: " + stereotypeName + " on the element: " + elementToEdit); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * Sets the given value to the stereotype of the element
	 * 
	 * @param elementToEdit
	 *        the element to modify
	 * @param stereotype
	 *        the stereotype that contains the feature to modify
	 * @param newValue
	 *        the new value for the feature
	 */
	protected void setValueForElement(Element elementToEdit, Stereotype stereotype, Object newValue) {
		elementToEdit.setValue(stereotype, getFeatureName(), newValue);
	}

	/**
	 * Retrieve the stereotype given its qualified name
	 * 
	 * @param elementToEdit
	 *        the element being edited
	 * @return the stereotype found or <code>null</code> if no stereotype was found
	 */
	protected Stereotype retrieveStereotype(Element elementToEdit) {
		return elementToEdit.getAppliedStereotype(getStereotypeName());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void completeEditorDescriptor(IPropertyEditorDescriptor descriptor, List<? extends EObject> objectToEdit) {

	}

	/**
	 * Returns the name of the stereotype to edit
	 * 
	 * @return the name of the stereotype to edit
	 */
	public String getStereotypeName() {
		return stereotypeName;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isChangeable(List<? extends EObject> objectsToEdit) {
		if(objectsToEdit.size() < 1) {
			return false;
		}

		Element element = retrieveElement(objectsToEdit);
		if(element == null) {
			return false;
		}

		// retrieve the feature
		Stereotype stereotype = retrieveStereotype(element);
		if(stereotype == null) {
			return false;
		}

		StructuralFeature feature = retrieveStructuralFeature(element, stereotype);
		return (feature != null) ? !feature.isReadOnly() : false;
	}

	/**
	 * Retrieve the element to edit
	 * 
	 * @param objectToEdit
	 *        the list of objects selected
	 * @return the element or null
	 */
	protected Element retrieveElement(List<? extends EObject> objectToEdit) {
		if(objectToEdit.size() < 1) {
			return null;
		}

		EObject firstObject = objectToEdit.get(0);
		if(!(firstObject instanceof Element)) {
			Activator.log.warn("Object to edit should be a UML2 element: " + firstObject); //$NON-NLS-1$
			return null;
		}

		Element elementToEdit = (Element)firstObject;
		return elementToEdit;
	}

	/**
	 * Retrieves the structural feature
	 * 
	 * @param elementToEdit
	 *        the element for wich the structural feature should be retrieved
	 * @param stereotype
	 *        the stereotype for which the structural feature should be retrieved
	 * 
	 * @return the structural feature
	 */
	protected StructuralFeature retrieveStructuralFeature(Element elementToEdit, Stereotype stereotype) {
		for(Property property : stereotype.getAllAttributes()) {
			if(getFeatureName().equals(property.getName())) {
				return property;
			}
		}
		Activator.log.warn("No feature fond with name:" + getFeatureName() + " for stereotype " + stereotypeName); //$NON-NLS-1$ //$NON-NLS-2$
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public IEMFModelHandlerState createState(boolean readOnly) {
		return new EMFStereotypeFeatureModelHandlerState(this, readOnly);
	}

	/**
	 * 
	 * @see org.eclipse.papyrus.views.properties.runtime.modelhandler.emf.IEMFModelHandler#getSetRequest(org.eclipse.emf.transaction.TransactionalEditingDomain,
	 *      org.eclipse.emf.ecore.EObject, java.lang.Object)
	 * 
	 * @param domain
	 * @param objectToEdit
	 * @param newValue
	 * @return
	 */
	public SetRequest[] getSetRequest(TransactionalEditingDomain domain, EObject objectToEdit, Object newValue) {
		return null; //TODO
	}

}

Back to the top