Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47b30bd4cfddb432b132a7a921060fcad94f8ffd (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
/*****************************************************************************
 * 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.ecore.EClassifier;
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.IPropertyEditorDescriptor;
import org.eclipse.swt.widgets.Composite;


/**
 * Extract or inject values for a string-typed feature.
 */
public class StringEMFModelHandler extends EMFFeatureModelHandler {

	/** ID of this model handler */
	public static final String ID = "String"; //$NON-NLS-1$

	/**
	 * Creates a new StringEMFModelHandler.
	 * 
	 * @param featureName
	 *        the name of the feature to edit
	 */
	public StringEMFModelHandler(String featureName) {
		super(featureName);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setValueInModel(EObject objectToEdit, Object newValue) {
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return;
		}

		// check the type of the feature to set
		EClassifier type = featureToEdit.getEType();
		if(type == null) {
			Activator.log.debug("Impossible to find the type of the feature: " + getFeatureName()); //$NON-NLS-1$
		} else {
			if(String.class.isAssignableFrom(type.getInstanceClass())) {
				objectToEdit.eSet(featureToEdit, newValue);
			} else if(Integer.TYPE.isAssignableFrom(type.getInstanceClass())) {
				try {
					int value = Integer.parseInt(newValue.toString());
					objectToEdit.eSet(featureToEdit, value);
				} catch (NumberFormatException e) {
					Activator.log.debug(newValue + " can not be parsed as an integer"); //$NON-NLS-1$
				}
			} else {
				Activator.log.error("Feature: " + getFeatureName() + ". Impossible to understand the value for the type: " + featureToEdit.getEType().getInstanceClass(), null); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}

	}

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

	/**
	 * {@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 String Values"); //$NON-NLS-1$
		for(EObject objectToEdit : objectsToEdit) {
			EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
			if(featureToEdit == null) {
				return null;
			}
			//CreateStringValueOperation operation = new CreateStringValueOperation(editingDomain, "Create new String Value", objectToEdit);
			IUndoableOperation operation = getCreateStringValueCommand(editingDomain, "Create new String Value", objectToEdit); //$NON-NLS-1$
			if(operation != null) {
				command.add(operation);
			}
			//			if(indexOfNewValue == NEW_VALUE_NOT_SET_INDEX) { // try to update
			//				indexOfNewValue = operation.getIndexOfNewValue();
			//			}
		}

		operations.add(command.reduce());

		return operations;
	}

	/**
	 * Returns the command to create a string value
	 * 
	 * @param editingDomain
	 *        the editing domain
	 * @param string
	 *        the command's name
	 * @param objectToEdit
	 *        the object to edit
	 * @return
	 *         The command to create a string value
	 */
	protected IUndoableOperation getCreateStringValueCommand(TransactionalEditingDomain editingDomain, String string, EObject objectToEdit) {
		CompositeTransactionalCommand command = new CompositeTransactionalCommand(editingDomain, string);
		int indexOfNewValue = -2;
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return command;
		}
		if(featureToEdit.getUpperBound() == 1) {
			indexOfNewValue = -1;
		} else {
			@SuppressWarnings("unchecked")
			List<Object> values = new ArrayList<Object>((List<Object>)objectToEdit.eGet(featureToEdit));
			indexOfNewValue = values.size();
		}

		// 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>)objectToEdit.eGet(featureToEdit));
			values.add(indexOfNewValue, 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 String value for the controlled property
	//	 */
	//	protected class CreateStringValueOperation 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 CreateStringValueOperation(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>)objectToEdit.eGet(featureToEdit));
	//				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>)objectToEdit.eGet(featureToEdit));
	//				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 {
			for(int suffix = 0; suffix < 1000; suffix++) {
				String tmpName = featureName + suffix;
				boolean found = false;
				@SuppressWarnings("unchecked")
				List<Object> values = new ArrayList<Object>((List<Object>)objectToEdit.eGet(featureToEdit));
				for(Object value : values) {
					if(tmpName.equals(value)) {
						found = true;
					}
				}
				if(!found) {
					return tmpName;
				}
			}
			return featureName;
		}
	}

	/**
	 * 
	 * @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;
		}

		// check the type of the feature to set
		EClassifier type = featureToEdit.getEType();
		if(type == null) {
			Activator.log.debug("Impossible to find the type of the feature: " + getFeatureName()); //$NON-NLS-1$
			return null;
		} else {
			if(String.class.isAssignableFrom(type.getInstanceClass())) {
				return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, newValue) };
			} else if(Integer.TYPE.isAssignableFrom(type.getInstanceClass())) {
				try {
					int value = Integer.parseInt(newValue.toString());
					return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, value) };
				} catch (NumberFormatException e) {
					Activator.log.debug(newValue + " can not be parsed as an integer"); //$NON-NLS-1$
				}
			} else {
				Activator.log.error("Feature: " + getFeatureName() + ". Impossible to understand the value for the type: " + featureToEdit.getEType().getInstanceClass(), null); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
		return null;
	}
}

Back to the top