Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b8f7f20f196359e67172ba6b523db5170869d2e (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
/*****************************************************************************
 * 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.Arrays;
import java.util.List;

import org.eclipse.core.commands.operations.IUndoableOperation;
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.ui.celleditor.FeatureEditorDialog;
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.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.papyrus.core.services.ServiceException;
import org.eclipse.papyrus.core.utils.DisplayUtils;
import org.eclipse.papyrus.properties.runtime.Activator;
import org.eclipse.papyrus.properties.runtime.Messages;
import org.eclipse.papyrus.properties.runtime.propertyeditor.descriptor.IPropertyEditorDescriptor;
import org.eclipse.papyrus.service.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.service.edit.service.IElementEditService;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;


/**
 * Model Handler for References
 */
public class ReferenceEMFModelHandler extends EMFFeatureModelHandler {

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

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setValueInModel(EObject objectToEdit, Object newValue) {
		EStructuralFeature featureToEdit = getFeatureByName(objectToEdit);
		if(featureToEdit == null) {
			return;
		}
		if(newValue instanceof EObject || newValue == null) {
			objectToEdit.eSet(featureToEdit, newValue);
		} else if(newValue instanceof List<?>) {
			objectToEdit.eSet(featureToEdit, newValue);
		} else {
			Activator.log.error("impossible to set the new value", null); //$NON-NLS-1$
		}
	}

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

	/**
	 * {@inheritDoc}
	 */
	public String getId() {
		return ID;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<IUndoableOperation> getCreateValueOperations(final List<? extends EObject> objectsToEdit, Composite parent) {
		// checks the containment feature
		EObject eObject = objectsToEdit.get(0);
		EStructuralFeature feature = getFeatureByName(eObject);
		if(!(feature instanceof EReference)) {
			return new ArrayList<IUndoableOperation>();
		}
		// check containment feature

		boolean isContainment = ((EReference)feature).isContainment();
		if(isContainment) {
			// retrieve the Eclass of the elements to edit
			List<IUndoableOperation> undoableOperations = new ArrayList<IUndoableOperation>();
			EClass eClass = eObject.eClass();
			if(eClass == null) {
				return undoableOperations;
			}
			// Use edit service and look for element types that are possible types of the selected EReference
			List<IElementEditService> featureTypeServices;
			try {
				featureTypeServices = ElementEditServiceUtils.getEditServiceProvider().getContainedTypeEditServices(eObject, (EReference)feature);
			} catch (ServiceException e) {
				e.printStackTrace();
				return undoableOperations;
			}

			for(IElementEditService featureTypeService : featureTypeServices) {
				CreateElementRequest request = new CreateElementRequest(EMFUtils.getTransactionalEditingDomain(objectsToEdit), eObject, (IElementType)featureTypeService.getAdapter(IElementType.class), (EReference)feature);
				request.setLabel(Messages.bind(Messages.EMFTEReferenceController_CreationOperationMenuLabel, featureTypeService.getDisplayName()));
				ICommand command = featureTypeService.getEditCommand(request);
				if(command.canExecute()) {
					// adds it to the list of command that can be
					// executed
					undoableOperations.add(command);
				}
			}

			return undoableOperations;
		} else {

			//			IUndoableOperation operation = new FindReferenceCommand(EMFUtils.getTransactionalEditingDomain(objectsToEdit), "Find and Add references", objectsToEdit);
			IUndoableOperation operation = getFindReferenceCommand(EMFUtils.getTransactionalEditingDomain(objectsToEdit), "Find and Add references", objectsToEdit); //$NON-NLS-1$
			return Arrays.asList(operation);
		}
	}

	/**
	 * Returns the command to find the reference
	 * 
	 * @param transactionalEditingDomain
	 *        the editing domain
	 * @param string
	 *        the name of the command
	 * @param objectsToEdit
	 *        the lists of objects to edit
	 * @return
	 *         the command to find the reference
	 */
	public IUndoableOperation getFindReferenceCommand(TransactionalEditingDomain transactionalEditingDomain, String string, List<? extends EObject> objectsToEdit) {
		CompositeTransactionalCommand command = new CompositeTransactionalCommand(transactionalEditingDomain, string);
		// checks the containment feature
		EObject eObject = objectsToEdit.get(0);
		EStructuralFeature feature = getFeatureByName(eObject);
		if(!(feature instanceof EReference)) {
			return command;
		}

		// pops up a window to ask for a new reference
		Display display = Display.getCurrent();
		if(display == null && PlatformUI.isWorkbenchRunning()) {
			display = PlatformUI.getWorkbench().getDisplay();
		}
		display = (display != null) ? display : Display.getDefault();
		EClassifier eclassifier = feature.getEType();
		Object availableValues = getAvailableValues(eObject);
		List<?> values = new ArrayList<Object>();
		if(availableValues instanceof List<?>) {
			values = (List<?>)availableValues;
		} else if(availableValues instanceof Object[]) {
			values = Arrays.asList(availableValues);
		} else {
			values = Arrays.asList(availableValues);
		}

		@SuppressWarnings("unchecked")
		FeatureEditorDialog dialog = new FeatureEditorDialog(display.getActiveShell(), DisplayUtils.getLabelProvider(), eObject, eclassifier, (List<EObject>)eObject.eGet(feature), Messages.ReferenceEMFModelHandler_Select_Values, values, false, feature.isOrdered(), feature.isUnique());
		// should select the current value by default
		if(Dialog.OK == dialog.open()) {
			@SuppressWarnings("unchecked")
			List<EObject> currentValues = (List<EObject>)dialog.getResult();
			SetRequest[] requests = getSetRequest(transactionalEditingDomain, eObject, currentValues);
			if(requests != null) {

				org.eclipse.papyrus.service.edit.service.IElementEditService provider = org.eclipse.papyrus.service.edit.service.ElementEditServiceUtils.getCommandProvider(eObject);
				if(provider != null) {

					ICommand editCommand = null;
					for(SetRequest current : requests) {
						editCommand = provider.getEditCommand(current);

						if(editCommand != null && editCommand.canExecute()) {
							command.add(editCommand);
						}
					}
				}
			}
			return command;
		}

		return command;
	}

	//	/**
	//	 * Command to create references.
	//	 * Two cases:
	//	 * - This is a containment reference, should create element.
	//	 * - This is a simple reference, should only browse the model to get other elements
	//	 */
	//	protected class FindReferenceCommand extends AbstractTransactionalCommand {
	//
	//		/** list of objects to edit */
	//		protected final List<? extends EObject> objectsToEdit;
	//
	//		/**
	//		 * Creates a new FindReferenceCommand.
	//		 * 
	//		 * @param domain
	//		 *        editing domain used to create or manipulate elements
	//		 * @param label
	//		 *        the label of the command, used by UI.
	//		 * @param objectsToEdit
	//		 *        the list of objects to edit
	//		 */
	//		public FindReferenceCommand(TransactionalEditingDomain domain, String label, List<? extends EObject> objectsToEdit) {
	//			super(domain, label, null);
	//			this.objectsToEdit = objectsToEdit;
	//		}
	//
	//		/**
	//		 * {@inheritDoc}
	//		 */
	//		@Override
	//		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
	//
	//			// checks the containment feature
	//			EObject eObject = objectsToEdit.get(0);
	//			EStructuralFeature feature = getFeatureByName(eObject);
	//			if(!(feature instanceof EReference)) {
	//				return CommandResult.newErrorCommandResult("the feature " + getFeatureName() + " is not a reference");
	//			}
	//
	//			// pops up a window to ask for a new reference
	//			Display display = Display.getCurrent();
	//			if(display == null && PlatformUI.isWorkbenchRunning()) {
	//				display = PlatformUI.getWorkbench().getDisplay();
	//			}
	//			display = (display != null) ? display : Display.getDefault();
	//			EClassifier eclassifier = feature.getEType();
	//			Object availableValues = getAvailableValues(eObject);
	//			List<?> values = new ArrayList<Object>();
	//			if(availableValues instanceof List<?>) {
	//				values = (List<?>)availableValues;
	//			} else if(availableValues instanceof Object[]) {
	//				values = Arrays.asList(availableValues);
	//			} else {
	//				values = Arrays.asList(availableValues);
	//			}
	//
	//			@SuppressWarnings("unchecked")
	//			FeatureEditorDialog dialog = new FeatureEditorDialog(display.getActiveShell(), DisplayUtils.getLabelProvider(), eObject, eclassifier, (List<EObject>)eObject.eGet(feature), "Select Values", values, false, feature.isOrdered(), feature.isUnique());
	//			// should select the current value by default
	//			if(Dialog.OK == dialog.open()) {
	//				@SuppressWarnings("unchecked")
	//				List<EObject> currentValues = (List<EObject>)dialog.getResult();
	//				eObject.eSet(feature, currentValues);
	//			}
	//			return CommandResult.newOKCommandResult();
	//		}
	//	}

	/**
	 * 
	 * @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;
		}
		if(newValue instanceof EObject || newValue == null) {
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, newValue) };
		} else if(newValue instanceof List<?>) {
			return new SetRequest[]{ new SetRequest(domain, objectToEdit, featureToEdit, newValue) };
		} else {
			Activator.log.error("impossible to set the new value", null); //$NON-NLS-1$
		}
		return null;
	}
}

Back to the top