Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2248005e4b92a9ee77dff2aedff24af4ae7c6f34 (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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*****************************************************************************
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.state.editor.xtext.ui.contributions;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.papyrus.core.utils.EditorUtils;
import org.eclipse.papyrus.extensionpoints.editors.ui.IPopupEditorHelper;
import org.eclipse.papyrus.state.editor.xtext.ui.internal.UmlStateActivator;
import org.eclipse.papyrus.state.editor.xtext.umlState.BehaviorKind;
import org.eclipse.papyrus.state.editor.xtext.umlState.StateRule;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.xtext.gmf.glue.PopupEditorConfiguration;
import org.eclipse.xtext.gmf.glue.edit.part.IXtextEMFReconciler;

import com.google.inject.Injector;

/**
 * @author CEA LIST
 * 
 *         This class is used for contribution to the Papyrus extension point DirectEditor. It is used for the integration
 *         of an xtext generated editor, for States of UML StateMachines.
 * 
 */
public class StatePopupEditorConfigurationContribution extends PopupEditorConfiguration {

	private State state = null;

	private String newStateName;

	private String newEntryName = null;

	private BehaviorKind newEntryKind;

	private String newDoName = null;

	private BehaviorKind newDoKind;

	private String newExitName = null;

	private BehaviorKind newExitKind;

	private enum BehaviorRole_Local {
		ENTRY, DO, EXIT
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.xtext.gmf.glue.PopupEditorConfiguration#createPopupEditorHelper(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart)
	 */
	@Override
	public IPopupEditorHelper createPopupEditorHelper(Object editPart) {

		// resolves the edit part, and the associated semantic element
		IGraphicalEditPart graphicalEditPart = null;
		if(!(editPart instanceof IGraphicalEditPart))
			return null;
		graphicalEditPart = (IGraphicalEditPart)editPart;

		if(!(graphicalEditPart.resolveSemanticElement() instanceof State))
			return null;
		state = (State)graphicalEditPart.resolveSemanticElement();

		// retrieves the XText injector
		Injector injector = UmlStateActivator.getInstance().getInjector("org.eclipse.papyrus.state.editor.xtext.UmlState");

		// builds the text content and extension for a temporary file, to be edited by the xtext editor
		String textToEdit = "" + this.getTextToEdit(graphicalEditPart.resolveSemanticElement());
		String fileExtension = "" + ".umlstate";

		// builds a new IXtextEMFReconciler.
		// Its purpose is to extract any relevant information from the textual specification,
		// and then merge it in the context UML model if necessary
		IXtextEMFReconciler reconciler = new IXtextEMFReconciler() {

			public void reconcile(EObject modelObject, EObject xtextObject) {
				// first: retrieves / determines if the xtextObject is a StateRule object
				EObject modifiedObject = xtextObject;
				if(!(modelObject instanceof State))
					return;
				while(xtextObject != null && !(xtextObject instanceof StateRule)) {
					modifiedObject = modifiedObject.eContainer();
				}
				if(modifiedObject == null)
					return;
				StateRule stateRuleObject = (StateRule)xtextObject;

				// Retrieves the information to be populated in modelObject
				newStateName = "" + stateRuleObject.getName();

				newEntryName = "";
				newDoName = "";
				newExitName = "";

				if(stateRuleObject.getEntry() != null) {
					newEntryKind = stateRuleObject.getEntry().getKind();
					if(stateRuleObject.getEntry().getBehaviorName() != null) {
						newEntryName = "" + stateRuleObject.getEntry().getBehaviorName();
					}
				}

				if(stateRuleObject.getDo() != null) {
					newDoKind = stateRuleObject.getDo().getKind();
					if(stateRuleObject.getDo().getBehaviorName() != null) {
						newDoName = stateRuleObject.getDo().getBehaviorName();
					}
				}

				if(stateRuleObject.getExit() != null) {
					newExitKind = stateRuleObject.getExit().getKind();
					if(stateRuleObject.getExit().getBehaviorName() != null) {
						newExitName = stateRuleObject.getExit().getBehaviorName();
					}
				}

				// Creates and executes the update command
				UpdateUMLStateCommand updateCommand = new UpdateUMLStateCommand(state);

				try {
					OperationHistoryFactory.getOperationHistory().execute(updateCommand, new NullProgressMonitor(), null);
				} catch (ExecutionException e) {
					org.eclipse.papyrus.properties.runtime.Activator.log.error(e);
				}
			}
		};
		return super.createPopupEditorHelper(graphicalEditPart, injector, reconciler, textToEdit, fileExtension);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.xtext.gmf.glue.PopupEditorConfiguration#getTextToEdit(java.lang.Object)
	 */
	@Override
	public String getTextToEdit(Object editedObject) {
		if(editedObject instanceof State) {
			State state = (State)editedObject;
			String textToEdit = "";

			// name
			textToEdit = textToEdit + state.getName();

			// entryActivity
			if(state.getEntry() != null) {
				String kind = behaviorKindAsString(state.getEntry());
				textToEdit = textToEdit + "\nentry " + kind + " " + state.getEntry().getName();
			}

			// doActivity
			if(state.getDoActivity() != null) {
				String kind = behaviorKindAsString(state.getDoActivity());
				textToEdit = textToEdit + "\ndo " + kind + " " + state.getDoActivity().getName();
			}

			// exitActivity
			if(state.getExit() != null) {
				String kind = behaviorKindAsString(state.getExit());
				textToEdit = textToEdit + "\nexit " + kind + " " + state.getExit().getName();
			}

			return textToEdit;
		}

		return "not a State";
	}

	/**
	 * @author CEA LIST
	 * 
	 *         A command for updating the context UML model
	 */
	protected class UpdateUMLStateCommand extends AbstractTransactionalCommand {

		private State state;

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor
		 * , org.eclipse.core.runtime.IAdaptable)
		 */
		@Override
		protected CommandResult doExecuteWithResult(IProgressMonitor arg0, IAdaptable arg1) throws ExecutionException {

			state.setName(newStateName);
			state.setEntry(updateOrCreateBehavior(BehaviorRole_Local.ENTRY, newEntryKind, newEntryName));
			state.setDoActivity(updateOrCreateBehavior(BehaviorRole_Local.DO, newDoKind, newDoName));
			state.setExit(updateOrCreateBehavior(BehaviorRole_Local.EXIT, newExitKind, newExitName));
			return CommandResult.newOKCommandResult(state);
		}

		public UpdateUMLStateCommand(State state) {
			super(EditorUtils.getTransactionalEditingDomain(), "State Update", getWorkspaceFiles(state));
			this.state = state;
		}
	}

	private String behaviorKindAsString(Behavior b) {
		if(b instanceof Activity)
			return "Activity";
		if(b instanceof StateMachine)
			return "StateMachine";
		if(b instanceof OpaqueBehavior)
			return "OpaqueBehavior";
		return "";
	}

	private BehaviorKind behaviorKindAsBehaviorKind(Behavior b) {
		if(b instanceof Activity)
			return BehaviorKind.ACTIVITY;
		if(b instanceof StateMachine)
			return BehaviorKind.STATE_MACHINE;
		if(b instanceof OpaqueBehavior)
			return BehaviorKind.OPAQUE_BEHAVIOR;

		return BehaviorKind.OPAQUE_BEHAVIOR;
	}

	private Behavior updateOrCreateBehavior(BehaviorRole_Local role, BehaviorKind kind, String behaviorName) {
		Behavior behavior = null;
		switch(role) {
		case DO:
			behavior = state.getDoActivity();
			if(behavior != null) {
				if(behaviorName.equals("")) {
					// behavior needs to be deleted
					state.setDoActivity(null);
					behavior.destroy();
					behavior = null;
				} else {
					if(behaviorKindAsBehaviorKind(behavior) != kind) {
						// behavior needs to deleted, and a new one needs to be created						
						state.setDoActivity(null);
						behavior.destroy();
						behavior = createBehavior(kind, behaviorName);
					} else {
						// Behavior simply needs to be renamed
						behavior.setName("" + behaviorName);
					}
				}
			} else {
				if(behaviorName.equals("")) {
					// nothing needs to be done
				} else {
					// behavior needs to be created
					behavior = createBehavior(kind, behaviorName);
				}
			}
			break;

		case ENTRY:
			behavior = state.getEntry();
			if(behavior != null) {
				if(behaviorName.equals("")) {
					// behavior needs to be deleted
					state.setEntry(null);
					behavior.destroy();
					behavior = null;
				} else {
					if(behaviorKindAsBehaviorKind(behavior) != kind) {
						// behavior needs to deleted, and a new one needs to be created
						state.setEntry(null);
						behavior.destroy();
						behavior = createBehavior(kind, behaviorName);
					} else {
						// Behavior simply needs to be renamed
						behavior.setName("" + behaviorName);
					}
				}
			} else {
				if(behaviorName.equals("")) {
					// nothing needs to be done
				} else {
					// behavior needs to be created
					behavior = createBehavior(kind, behaviorName);
				}
			}
			break;

		case EXIT:
			behavior = state.getExit();
			if(behavior != null) {
				if(behaviorName.equals("")) {
					// behavior needs to be deleted
					state.setExit(null);
					behavior.destroy();
					behavior = null;
				} else {
					if(behaviorKindAsBehaviorKind(behavior) != kind) {
						// behavior needs to deleted, and a new one needs to be created
						state.setExit(null);
						behavior.destroy();
						behavior = createBehavior(kind, behaviorName);
					} else {
						// Behavior simply needs to be renamed
						behavior.setName("" + behaviorName);
					}
				}
			} else {
				if(behaviorName.equals("")) {
					// nothing needs to be done
				} else {
					// behavior needs to be created
					behavior = createBehavior(kind, behaviorName);
				}
			}
			break;

		default:
			break;
		}

		return behavior;
	}

	private Behavior createBehavior(BehaviorKind kind, String name) {

		if(kind == null)
			return null;

		Behavior behavior = null;

		switch(kind) {
		case ACTIVITY:
			behavior = UMLFactory.eINSTANCE.createActivity();
			break;

		case OPAQUE_BEHAVIOR:
			behavior = UMLFactory.eINSTANCE.createOpaqueBehavior();
			break;

		case STATE_MACHINE:
			behavior = UMLFactory.eINSTANCE.createStateMachine();
			break;

		default:
			break;
		}

		behavior.setName("" + name);

		return behavior;
	}
}

Back to the top