Skip to main content
summaryrefslogtreecommitdiffstats
blob: ee4b787ec997ac03f811348192e2baa46f5defa4 (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
/*****************************************************************************
 * Copyright (c) 2013 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) - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.extendedtypes.applystereotypeactionconfiguration;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.papyrus.infra.extendedtypes.IActionEditHelperAdvice;
import org.eclipse.papyrus.infra.extendedtypes.emf.Activator;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.tools.utils.NamedElementUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * advice for the {@link ApplyStereotypeActionConfiguration}
 */
public class ApplyStereotypeActionEditHelperAdvice extends AbstractEditHelperAdvice implements IActionEditHelperAdvice<ApplyStereotypeActionConfiguration> {

	/** configuration for this edit helper advice */
	protected ApplyStereotypeActionConfiguration configuration;

	/**
	 * {@inheritDoc}
	 */
	public void init(ApplyStereotypeActionConfiguration configuration) {
		this.configuration = configuration;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean approveRequest(IEditCommandRequest request) {
		return super.approveRequest(request);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getBeforeConfigureCommand(ConfigureRequest request) {
		return super.getBeforeConfigureCommand(request);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getAfterConfigureCommand(ConfigureRequest request) {
		if(configuration==null) {
			return null;
		}
		ICommand resultCommand = null;
		// retrieve eobject 
		EObject elementToConfigure = request.getElementToConfigure();
		if(!(elementToConfigure instanceof Element)) {
			return null;
		}
		
		TransactionalEditingDomain editingDomain = request.getEditingDomain();
		if(editingDomain ==null) {
			return null;
		}
		// retrieve edit service to get features from configure command
		IElementEditService service = ElementEditServiceUtils.getCommandProvider(elementToConfigure);
		if(service == null) {
			Activator.log.error("Impossible to get edit service from element: " + elementToConfigure, null);
			return null;
		}
		
		// for each stereotype, apply stereotype and apply values
		for(StereotypeToApply stereotypeToApply : configuration.getStereotypesToApply()) {
			Stereotype stereotype = ((Element)elementToConfigure).getApplicableStereotype(stereotypeToApply.getStereotypeQualifiedName());
			
			if(stereotype !=null) {
				ApplyStereotypeCommand applyStereotypeCommand = new ApplyStereotypeCommand(editingDomain, (Element)elementToConfigure, stereotype, stereotypeToApply.isUpdateName());
				if(resultCommand == null) {
					resultCommand = applyStereotypeCommand;
				} else {
					resultCommand = resultCommand.compose(applyStereotypeCommand);
				}
				
				// apply values
				for(FeatureToSet featureToSet : stereotypeToApply.getFeaturesToSet()) {
					// retrieve feature value
					ICommand command = getSetStereotypeFeatureValueCommand((Element)elementToConfigure, stereotype, featureToSet.getFeatureName(), featureToSet.getValue(), service, request);
					if(command != null) {
						if(resultCommand == null) {
							resultCommand = command;
						} else {
							resultCommand = resultCommand.compose(command);
						}
					}
				}
			}
			
		}
	
		if(resultCommand != null) {
			return resultCommand.reduce();
		}
		return super.getAfterConfigureCommand(request);
	}

	/**
	 * @param elementToConfigure
	 *        the eobject to configure
	 * @param name
	 *        the name of the feature to set
	 * @param value
	 *        the new value of the feature
	 */
	protected ICommand getSetStereotypeFeatureValueCommand(Element elementToConfigure, Stereotype stereotype, String name, FeatureValue featureValue, IElementEditService service, ConfigureRequest configureRequest) {
		if(name == null) {
			Activator.log.debug("No feature name has been set.");
			return null;
		}
		if(elementToConfigure.eClass() == null) {
			Activator.log.error("Impossible to find EClass from EObject: " + elementToConfigure, null);
			return null;
		}
		
		if(configureRequest.getEditingDomain()==null) {
			return null;
		}
		
		// retrieve structural feature for the element to configure
		TypedElement typedElement = (TypedElement)stereotype.getMember(name, true, UMLPackage.eINSTANCE.getTypedElement());
		if(typedElement == null) {
			Activator.log.error("Impossible to find feature " + name + " for eobject " + elementToConfigure, null);
			return null;
		}
		Object value = getStereotypeValue(elementToConfigure, stereotype, typedElement.getType(), featureValue);
		
		return new SetStereotypeValueCommand(configureRequest.getEditingDomain(), elementToConfigure, stereotype, name, value);
		
	}
	
	
	/**
	 * @param elementToConfigure
	 * @param stereotype
	 * @param feature
	 * @param featureValue
	 * @return
	 */
	protected Object getStereotypeValue(Element elementToConfigure, Stereotype stereotype, Type type, FeatureValue featureValue) {
		return StereotypeFeatureValueUtils.getValue(elementToConfigure, stereotype, type, featureValue);
	}


	public static class ApplyStereotypeCommand extends AbstractTransactionalCommand {

		private Stereotype stereotype;
		
		private Element element;

		private boolean rename;
		
		/**
		 * @param domain editing domain to modify the element
		 * @param element the element on which stereotype is applied. Must not be <code>null</code>
		 * @param stereotype the stereotype to modify 
		 * @param rename 
		 */
		public ApplyStereotypeCommand(TransactionalEditingDomain domain, Element element, Stereotype stereotype, boolean rename) {
			super(domain, "Apply Stereotype "+stereotype.getLabel(), getWorkspaceFiles(element));
			this.element = element;
			this.stereotype = stereotype;
			this.rename = rename;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			try {
				Object stereotypeApplication = element.applyStereotype(stereotype);
				if(rename && element instanceof NamedElement) {
					if(((NamedElement)element).getNamespace()!=null) {
						String newName = NamedElementUtil.getDefaultNameWithIncrement(stereotype.getName(), element, ((NamedElement)element).getNamespace().getMembers());
						((NamedElement)element).setName(newName);
					}
				}
				return CommandResult.newOKCommandResult(stereotypeApplication);
			} catch (Throwable t) {
				Activator.log.error(t);
				return CommandResult.newErrorCommandResult(t.getMessage());
			}
		}
	}
	
	
	public static class SetStereotypeValueCommand extends AbstractTransactionalCommand {

		private Element element;
		private Stereotype stereotype;
		private String featureName;
		private Object featureValue;

		/**
		 * Default Constructor
		 * @param domain editing domain to modify the element
		 * @param element the element on which stereotype is applied. Must not be <code>null</code>
		 * @param stereotype the stereotype to modify 
		 * @param featureName name of the stereotype feature to modify 
		 * @param featureValue the new value for the stereotype feature value
		 */
		public SetStereotypeValueCommand(TransactionalEditingDomain domain, Element element, Stereotype stereotype, String featureName, Object featureValue) {
			super(domain, "Set stereotype  value "+featureName, getWorkspaceFiles(element.getStereotypeApplication(stereotype)));
			this.element = element;
			this.stereotype = stereotype;
			this.featureName = featureName;
			this.featureValue = featureValue;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			try {
				element.setValue(stereotype, featureName, featureValue);
			} catch (Throwable t) {
				Activator.log.error(t);
				return CommandResult.newErrorCommandResult(t.getMessage());
			}
			
			return CommandResult.newOKCommandResult(element.getValue(stereotype, featureName));
		}
		
	}
}

Back to the top