Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f990b3cfebe4d136ff74703773aa739fae87ac9f (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*******************************************************************************
 * Copyright (c) 2013 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 		Jayant Gupta (Added Fix methods)
 * 
 *******************************************************************************/

package org.eclipse.etrice.ui.behavior.quickfix;

import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.abstractexec.behavior.AbstractExecutionValidator;
import org.eclipse.etrice.core.naming.RoomNameProvider;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.Message;
import org.eclipse.etrice.core.room.MessageFromIf;
import org.eclipse.etrice.core.room.RoomFactory;
import org.eclipse.etrice.core.room.State;
import org.eclipse.etrice.core.room.StateGraph;
import org.eclipse.etrice.core.room.Transition;
import org.eclipse.etrice.core.room.TransitionTerminal;
import org.eclipse.etrice.core.room.Trigger;
import org.eclipse.etrice.core.room.TriggeredTransition;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.ui.behavior.support.StateSupport.FeatureProvider.PropertyFeature;
import org.eclipse.etrice.ui.behavior.support.SupportUtil;
import org.eclipse.etrice.ui.behavior.support.TransitionSupport;
import org.eclipse.etrice.ui.common.quickfix.AbstractQuickfixProvider;
import org.eclipse.etrice.ui.common.quickfix.IDiagramModification;
import org.eclipse.etrice.ui.common.quickfix.IssueResolutionAcceptor;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.context.ICustomContext;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
import org.eclipse.graphiti.features.context.impl.CustomContext;
import org.eclipse.graphiti.features.context.impl.RemoveContext;
import org.eclipse.graphiti.features.context.impl.UpdateContext;
import org.eclipse.graphiti.features.custom.ICustomFeature;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.xtext.ui.editor.quickfix.Fix;
import org.eclipse.xtext.validation.FeatureBasedDiagnostic;

/**
 * @author Henrik Rentz-Reichert
 * @author jayant
 * 
 */
public class BehaviorQuickfixProvider extends AbstractQuickfixProvider {

	private static final String ADD_IMG = "icons/quickfix/add.gif";
	private static final String SUB_IMG = "icons/quickfix/add.gif";

	@Fix(AbstractExecutionValidator.DIAG_CODE_MISSING_TRIGGER)
	public void fixMissingTrigger(final FeatureBasedDiagnostic issue,
			IssueResolutionAcceptor acceptor) {
		final String msgName = issue.getIssueData()[1];
		final String ifItemName = issue.getIssueData()[2];
		acceptor.accept(issue, "Add the missing trigger",
				"Adds a new self-transition with trigger to handle message "
						+ msgName + " from port " + ifItemName, ADD_IMG,
				new IDiagramModification() {

					@Override
					public boolean apply(Diagram diagram, IFeatureProvider fp)
							throws Exception {
						// make a new self-transition & add a Trigger to handle
						// message

						// check preconditions & get pictogram element
						EObject obj = issue.getSourceEObject();
						if (!(obj instanceof StateGraph))
							return false;

						State state = ((StateGraph) obj).getStates().get(
								issue.getIndex());
						List<PictogramElement> pes = Graphiti.getLinkService()
								.getPictogramElements(diagram, state);
						if (pes.size() != 1
								|| !(pes.get(0) instanceof ContainerShape))
							// unexpected
							return false;

						// find matching interface item and message
						ActorClass ac = SupportUtil.getActorClass(diagram);

						// it is crucial that we consider all emitters of
						// messages (port, saps, spps)
						// including base classes
						List<InterfaceItem> items = RoomHelpers
								.getAllInterfaceItems(ac);
						for (InterfaceItem item : items) {
							if (item.getName().equals(ifItemName)) {
								List<Message> msgs = RoomHelpers
										.getIncoming(item);
								for (Message msg : msgs) {
									if (msg.getName().equals(msgName)) {
										// create triggered transition with our
										// trigger and add it to the state graph
										TriggeredTransition trans = RoomFactory.eINSTANCE
												.createTriggeredTransition();
										trans.setName(RoomNameProvider
												.getUniqueTransitionName((StateGraph) state
														.eContainer()));
										Trigger tri = RoomFactory.eINSTANCE
												.createTrigger();
										trans.getTriggers().add(tri);
										MessageFromIf mif = RoomFactory.eINSTANCE
												.createMessageFromIf();
										mif.setFrom(item);
										mif.setMessage(msg);
										tri.getMsgFromIfPairs().add(mif);
										((StateGraph) obj).getTransitions()
												.add(trans);

										// connect both ends to our state
										// caution: the returned
										// TransitionTerminal is created by this
										// call and will be
										// owned by the transition. Therefore we
										// have to do it twice
										ContainerShape shape = (ContainerShape) pes
												.get(0);
										Anchor anchor = shape.getAnchors().get(
												0);
										TransitionTerminal src = SupportUtil
												.getTransitionTerminal(anchor,
														fp);
										TransitionTerminal tgt = SupportUtil
												.getTransitionTerminal(anchor,
														fp);
										trans.setFrom(src);
										trans.setTo(tgt);

										// prepare a context for Graphiti
										AddConnectionContext addContext = new AddConnectionContext(
												anchor, anchor);
										addContext.setNewObject(trans);

										// and let the feature provider delegate
										// to the correct feature
										return fp.addIfPossible(addContext) != null;
									}
								}
							}
						}
						return false;
					}
				});
	}

	@Fix(AbstractExecutionValidator.DIAG_CODE_MISSING_MESSAGESEND)
	public void fixMissingMessageSend(final FeatureBasedDiagnostic issue,
			IssueResolutionAcceptor acceptor) {
		final String msgName = issue.getIssueData()[1];
		final String ifItemName = issue.getIssueData()[2];
		final String codeString = ifItemName + '.' + msgName;
		acceptor.accept(issue, "Add Entry Code to send message",
				"Adds Entry Code to send " + msgName + " to port " + ifItemName
						+ " in Edit State Dialog", ADD_IMG,
				new IDiagramModification() {
					@Override
					public boolean apply(Diagram diagram, IFeatureProvider fp)
							throws Exception {
						// open the State Property Dialog & add detail code to
						// send message to the port

						// check preconditions & get pictogram element
						EObject obj = issue.getSourceEObject();
						if (!(obj instanceof StateGraph))
							return false;

						State state = ((StateGraph) obj).getStates().get(
								issue.getIndex());
						List<PictogramElement> pes = Graphiti.getLinkService()
								.getPictogramElements(diagram, state);
						if (pes.size() != 1
								|| !(pes.get(0) instanceof ContainerShape))
							// unexpected
							return false;

						// Open StatePropertyDialog, add line, select added
						// line
						PropertyFeature propertyFeature = null;
						ICustomContext customContext = new CustomContext(pes
								.toArray(new PictogramElement[pes.size()]));
						ICustomFeature features[] = fp
								.getCustomFeatures(customContext);
						for (ICustomFeature feature : features) {
							if (feature instanceof PropertyFeature) {
								propertyFeature = (PropertyFeature) feature;
								break;
							}
						}
						if (propertyFeature != null) {
							propertyFeature
									.setEntryCodeSelectionString(codeString);
							propertyFeature.setAddCode(true);
							propertyFeature
									.setMessageDialogContents(
											"Please complete the code with the newly inserted and highlighted fragment.",
											"Quick Fix");
							propertyFeature.execute(customContext);
							return propertyFeature.hasDoneChanges();
						}

						return false;
					}
				});

		acceptor.accept(issue, "Add Exit Code to send message",
				"Adds Exit Code to send " + msgName + " to port " + ifItemName
						+ " in Edit State Dialog", ADD_IMG,
				new IDiagramModification() {
					@Override
					public boolean apply(Diagram diagram, IFeatureProvider fp)
							throws Exception {
						// add new detail code lines to send message to the exit
						// code section of the state and open the
						// StatePropertyDialog.

						// check preconditions & get pictogram element
						EObject obj = issue.getSourceEObject();
						if (!(obj instanceof StateGraph))
							return false;

						State state = ((StateGraph) obj).getStates().get(
								issue.getIndex());
						List<PictogramElement> pes = Graphiti.getLinkService()
								.getPictogramElements(diagram, state);
						if (pes.size() != 1
								|| !(pes.get(0) instanceof ContainerShape))
							// unexpected
							return false;

						// Open StatePropertyDialog, add line & select added
						// line
						PropertyFeature propertyFeature = null;
						ICustomContext customContext = new CustomContext(pes
								.toArray(new PictogramElement[pes.size()]));
						ICustomFeature features[] = fp
								.getCustomFeatures(customContext);
						for (ICustomFeature feature : features) {
							if (feature instanceof PropertyFeature) {
								propertyFeature = (PropertyFeature) feature;
								break;
							}
						}
						if (propertyFeature != null) {
							propertyFeature
									.setExitCodeSelectionString(codeString);
							propertyFeature.setAddCode(true);
							propertyFeature
									.setMessageDialogContents(
											"Please complete the code with the newly inserted and highlighted fragment.",
											"Quick Fix");
							propertyFeature.execute(customContext);
							return propertyFeature.hasDoneChanges();
						}

						return false;
					}
				});
	}

	@Fix(AbstractExecutionValidator.DIAG_CODE_VIOLATION_TRIGGER)
	public void fixViolationTrigger(final FeatureBasedDiagnostic issue,
			IssueResolutionAcceptor acceptor) {
		final String msgName = issue.getIssueData()[1];
		final String ifItemName = issue.getIssueData()[2];
		acceptor.accept(
				issue,
				"Remove the Violating Messgae from Trigger",
				"Remove the handled message "
						+ msgName
						+ " from port "
						+ ifItemName
						+ " from the trigger.\n"
						+ " If there are no more Messages handled by the trigger, the trigger would be removed.\n"
						+ " If there are no more triggers, the transition would be removed.",
				SUB_IMG, new IDiagramModification() {
					@Override
					public boolean apply(Diagram diagram, IFeatureProvider fp)
							throws Exception {

						// Get the pictogram element
						EObject obj = issue.getSourceEObject();
						if (!(obj instanceof Trigger))
							return false;

						MessageFromIf mifToDelete = null;
						Trigger trig = (Trigger) obj;
						TriggeredTransition trans = (TriggeredTransition) trig
								.eContainer();

						List<PictogramElement> pes = Graphiti.getLinkService()
								.getPictogramElements(diagram, trans);
						if (pes.size() != 1)
							// unexpected
							return false;

						// Test Remove the message from the trigger
						for (MessageFromIf mif : trig.getMsgFromIfPairs()) {
							if (mif.getMessage().getName() == msgName
									&& mif.getFrom().getName() == ifItemName) {
								mifToDelete = mif;
								break;
							}
						}

						if (mifToDelete != null) {

							if (trig.getMsgFromIfPairs().size() - 1 == 0) {
								// the trigger would be deleted

								if (trans.getTriggers().size() - 1 == 0) {
									// The transition would be deleted

									if (trans.getAction() != null) {
										Shell shell = PlatformUI.getWorkbench()
												.getActiveWorkbenchWindow()
												.getShell();
										if (!(MessageDialog
												.openQuestion(shell, "Warning",
														"Transition being removed contains Action Code. Do you want to continue?")))
											return false;
									}

									IRemoveContext rc = new RemoveContext(pes
											.get(0));
									IRemoveFeature removeFeature = fp
											.getRemoveFeature(rc);
									if (removeFeature != null) {
										removeFeature.remove(rc);
									}
									EcoreUtil.delete(trans);
									return true;

								} else
									trans.getTriggers().remove(trig);

							} else
								trig.getMsgFromIfPairs().remove(mifToDelete);

							UpdateContext updateContext = new UpdateContext(pes
									.get(0));
							return fp.updateIfPossible(updateContext) != null;
						}
						return false;
					}
				});
	}

	@Fix(AbstractExecutionValidator.DIAG_CODE_VIOLATION_MESSAGESEND)
	public void fixViolationMessageSend(final FeatureBasedDiagnostic issue,
			IssueResolutionAcceptor acceptor) {
		final String msgName = issue.getIssueData()[0];
		final String ifItemName = issue.getIssueData()[1];
		final String codeString = ifItemName + '.' + msgName;
		acceptor.accept(issue, "Inspect & remove the offending message",
				"Removes the message " + msgName + " sent to the port "
						+ ifItemName + " in Edit State Dialog.", ADD_IMG,
				new IDiagramModification() {

					@Override
					public boolean apply(Diagram diagram, IFeatureProvider fp)
							throws Exception {
						// Select the offending piece of action code in Property
						// Dialogs.

						EObject obj = issue.getSourceEObject();
						if (obj instanceof State || obj instanceof StateGraph) {

							// get the pictogram element for State
							State state;
							if (obj instanceof StateGraph)
								state = ((StateGraph) obj).getStates().get(
										issue.getIndex());
							else
								state = (State) obj;

							List<PictogramElement> pes = Graphiti
									.getLinkService().getPictogramElements(
											diagram, state);
							if (pes.size() != 1
									|| !(pes.get(0) instanceof ContainerShape))
								// unexpected
								return false;

							// Open StatePropertyDialog & select the offending
							// code
							PropertyFeature propertyFeature = null;
							ICustomContext customContext = new CustomContext(
									pes.toArray(new PictogramElement[pes.size()]));
							ICustomFeature features[] = fp
									.getCustomFeatures(customContext);
							for (ICustomFeature feature : features) {
								if (feature instanceof PropertyFeature) {
									propertyFeature = (PropertyFeature) feature;
									break;
								}
							}
							if (propertyFeature != null) {
								propertyFeature
										.setEntryCodeSelectionString(codeString);
								propertyFeature
										.setExitCodeSelectionString(codeString);
								propertyFeature
										.setDoCodeSelectionString(codeString);
								propertyFeature
										.setMessageDialogContents(
												"Please remove the highlighted code after inspection.",
												"Quick Fix");
								propertyFeature.execute(customContext);
								return propertyFeature.hasDoneChanges();
							}

						} else if (obj instanceof Transition
								|| obj instanceof Trigger) {
							// Get pictogram element for Transition
							Transition transition;
							if (obj instanceof Trigger)
								transition = (Transition) obj.eContainer();
							else
								transition = (Transition) obj;

							List<PictogramElement> pes = Graphiti
									.getLinkService().getPictogramElements(
											diagram, transition);
							if (pes.size() != 1)
								// unexpected
								return false;

							// Open TransitionPropertyDialog & select the
							// offending piece of code
							TransitionSupport.FeatureProvider.PropertyFeature propertyFeature = null;
							ICustomContext customContext = new CustomContext(
									pes.toArray(new PictogramElement[pes.size()]));
							ICustomFeature features[] = fp
									.getCustomFeatures(customContext);
							for (ICustomFeature feature : features) {
								if (feature instanceof TransitionSupport.FeatureProvider.PropertyFeature) {
									propertyFeature = (TransitionSupport.FeatureProvider.PropertyFeature) feature;
									break;
								}
							}
							if (propertyFeature != null) {
								propertyFeature
										.setActionCodeSelectionString(codeString);
								propertyFeature
										.setMessageDialogContents(
												"Please remove the highlighted code after inspection.",
												"Quick Fix");
								propertyFeature.execute(customContext);
								return propertyFeature.hasDoneChanges();
							}
						}
						return false;
					}
				});
	}
}

Back to the top