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
|
/*****************************************************************************
* 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.properties.runtime.modelhandler.emf;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.CompositeTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.papyrus.properties.runtime.Activator;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IBoundedValuesPropertyEditorDescriptor;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.swt.widgets.Composite;
/**
* Model Handler for enumeration
*/
public class EnumerationEMFModelHandler extends EMFFeatureModelHandler {
/**
* Creates a new EnumerationEMFModelHandler.
*
* @param featureName
* the name of the feature to edit
*/
public EnumerationEMFModelHandler(String featureName) {
super(featureName);
}
/** id of this model handler */
public static final String ID = "Enumeration"; //$NON-NLS-1$
/**
* {@inheritDoc}
*/
@Override
public Object getValueToEdit(EObject objectToEdit) {
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit == null) {
return null;
}
Object value = objectToEdit.eGet(featureToEdit);
return (value instanceof Enumerator) ? ((Enumerator)value).getLiteral() : value;
}
/**
* {@inheritDoc}
*/
@Override
public void setValueInModel(EObject objectToEdit, Object newValue) {
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit == null) {
return;
}
// remove value if result of the editor is empty
if(newValue == null || newValue.equals("")) { //$NON-NLS-1$
objectToEdit.eUnset(featureToEdit);
} else {
// retrieve the real value for the enumeration
if(newValue instanceof String) {
EEnum type = (EEnum)featureToEdit.getEType();
EEnumLiteral literal = type.getEEnumLiteral((String)newValue);
Enumerator instance = literal.getInstance();
objectToEdit.eSet(featureToEdit, instance);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void completeEditorDescriptor(IPropertyEditorDescriptor descriptor, List<? extends EObject> objectToEdit) {
if(objectToEdit.size() < 1) {
return;
}
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit.get(0));
if(featureToEdit == null) {
return;
}
// test enumeration, reference, etc.
List<String> values = new ArrayList<String>();
// check if there is an empty string at the beginning. there is one if the lower bound of the feature to edit equal 0
if(featureToEdit.getLowerBound() == 0) {
values.add(""); //$NON-NLS-1$
}
if(featureToEdit.getEType() instanceof EEnum) {
EEnum type = ((EEnum)featureToEdit.getEType());
for(EEnumLiteral literal : type.getELiterals()) {
values.add(literal.getLiteral());
}
}
if(descriptor instanceof IBoundedValuesPropertyEditorDescriptor) {
((IBoundedValuesPropertyEditorDescriptor)descriptor).setAvailableValues(values);
} else {
Activator.log.warn("Warning: " + descriptor + "could not be completed."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
/**
* {@inheritDoc}
*/
public String getId() {
return ID;
}
/**
* {@inheritDoc}
*/
@Override
public List<IUndoableOperation> getCreateValueOperations(List<? extends EObject> objectsToEdit, Composite parent) {
List<IUndoableOperation> operations = new ArrayList<IUndoableOperation>(1);
TransactionalEditingDomain editingDomain = EMFUtils.getTransactionalEditingDomain(objectsToEdit);
if(editingDomain == null) {
Activator.log.error("Impossible during creation operation to find the editing domain for objects: " + objectsToEdit, null); //$NON-NLS-1$
return null;
}
int indexOfNewValue = NEW_VALUE_NOT_SET_INDEX;
CompositeTransactionalCommand command = new CompositeTransactionalCommand(editingDomain, "Create new Enumeration Value"); //$NON-NLS-1$
for(EObject objectToEdit : objectsToEdit) {
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit == null) {
return null;
}
// CreateEnumerationValueOperation operation = new CreateEnumerationValueOperation(editingDomain, "Create new Enumeration Value", objectToEdit);
IUndoableOperation operation = getCreateEnumerationValueOperation(editingDomain, "Create new Enumeration Value", objectToEdit); //$NON-NLS-1$
if(operation != null) {
command.add(operation);
}
if(indexOfNewValue == NEW_VALUE_NOT_SET_INDEX) { // try to update
}
}
operations.add(command.reduce());
return operations;
}
/**
* Returns the command to edit the object
*
* @param editingDomain
* the editing domain
* @param string
* the name of the command
* @param objectToEdit
* the object to edit
* @return
* the command to edit the object
*/
protected IUndoableOperation getCreateEnumerationValueOperation(TransactionalEditingDomain editingDomain, String string, EObject objectToEdit) {
CompositeTransactionalCommand command = new CompositeTransactionalCommand(editingDomain, string);
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit == null) {
Activator.log.error("Impossible to find the feature " + getFeatureName(), null); //$NON-NLS-1$
return command;
}
// create the value and add it to the feature
// if feature = simple valued => set
// if feature = multi valued => add at the end
Object newValue;
if(featureToEdit.getUpperBound() == 1) {
newValue = getInitialValue(objectToEdit);
} else {
@SuppressWarnings("unchecked")
List<Object> values = new ArrayList<Object>((List<Object>)getValueToEdit(objectToEdit));
values.add(values.size(), getInitialValue(objectToEdit));
newValue = values;
}
SetRequest[] requests = getSetRequest(editingDomain, objectToEdit, newValue);
if(requests != null) {
org.eclipse.papyrus.service.edit.service.IElementEditService provider = org.eclipse.papyrus.service.edit.service.ElementEditServiceUtils.getCommandProvider(objectToEdit);
if(provider != null) {
ICommand editCommand = null;
for(SetRequest current : requests) {
editCommand = provider.getEditCommand(current);
if(editCommand != null && editCommand.canExecute()) {
command.add(editCommand);
}
}
}
}
return command;
}
// /**
// * Operation to create a Enumeration value for the controlled property
// */
// protected class CreateEnumerationValueOperation extends AbstractTransactionalCommand {
//
// /** object to edit */
// protected final EObject objectToEdit;
//
// /** index of the value to add (-1 in case of single valued property) */
// private int indexOfNewValue = -2;
//
// /**
// * Initializes me with the editing domain, a label, transaction options, and
// * a list of {@link IFile}s that anticipate modifying when I am executed,
// * undone or redone.
// *
// * @param domain
// * the editing domain used to modify the model
// * @param label
// * my user-readable label, should never be <code>null</code>.
// * @param objectToEdit
// * the {@link EObject} to edit
// */
// public CreateEnumerationValueOperation(TransactionalEditingDomain domain, String label, EObject objectToEdit) {
// super(domain, (label == null) ? "" : label, null);
// this.objectToEdit = objectToEdit;
//
// EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
// if(featureToEdit.getUpperBound() == 1) {
// indexOfNewValue = -1;
// } else {
// @SuppressWarnings("unchecked")
// List<Object> values = new ArrayList<Object>((List<Object>)getValueToEdit(objectToEdit));
// indexOfNewValue = values.size();
// }
// }
//
// /**
// * Returns the index at which the value will be added
// *
// * @return the index at which the value will be added
// */
// public int getIndexOfNewValue() {
// return indexOfNewValue;
// }
//
// /**
// * {@inheritDoc}
// */
// @Override
// protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
// // create the value and add it to the feature
// // if feature = simple valued => set
// // if feature = multi valued => add at the end
// EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
// if(featureToEdit == null) {
// return CommandResult.newErrorCommandResult("Impossible to find the feature " + getFeatureName());
// }
// Object newValue;
// if(featureToEdit.getUpperBound() == 1) {
// newValue = getInitialValue(objectToEdit);
// } else {
// @SuppressWarnings("unchecked")
// List<Object> values = new ArrayList<Object>((List<Object>)getValueToEdit(objectToEdit));
// values.add(indexOfNewValue, getInitialValue(objectToEdit));
// newValue = values;
// }
// setValueInModel(objectToEdit, newValue);
// return CommandResult.newOKCommandResult();
// }
// }
/**
* Returns the initial value for a new value of the feature
*
* @param objectToEdit
* the object to edit
*
* @return the initial value, not <code>null</code>
*/
public Object getInitialValue(EObject objectToEdit) {
String featureName = getFeatureName();
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit.getUpperBound() == 1) {
return featureName;
} else {
EClassifier eClassifier = featureToEdit.getEType();
if(eClassifier instanceof EEnum) {
return ((EEnum)eClassifier).getDefaultValue();
} else if(Boolean.TYPE.isAssignableFrom(eClassifier.getInstanceClass())) {
return Boolean.TRUE;
}
Activator.log.error("Impossible to find a default value", null); //$NON-NLS-1$
return null;
}
}
/**
*
* @see org.eclipse.papyrus.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) {
EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
if(featureToEdit == null) {
return null;
}
// remove value if result of the editor is empty
if(newValue == null || newValue.equals("")) { //$NON-NLS-1$
return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, featureToEdit.getDefaultValue()) };
} else {
// retrieve the real value for the enumeration
if(newValue instanceof String) {
EEnum type = (EEnum)featureToEdit.getEType();
EEnumLiteral literal = type.getEEnumLiteral((String)newValue);
Enumerator instance = literal.getInstance();
return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, instance) };
}
}
return null;
}
}
|