Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f8ee3dff130f773889b54363dd8e9b24badc0ad (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*****************************************************************************
 * Copyright (c) 2008, 2016 CEA LIST, Christian W. Damus, and others.
 *
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - only calculate affected files for workspace resources (CDO)
 *  Laurent Wouters (CEA) - laurent.wouters@cea.fr - Refactoring for viewpoints
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common;

import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.commands.CheckedOperationHistory;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.commands.OpenDiagramCommand;
import org.eclipse.papyrus.infra.core.language.ILanguageService;
import org.eclipse.papyrus.infra.core.resource.IEMFModel;
import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler2;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ReadOnlyAxis;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModelUtils;
import org.eclipse.papyrus.infra.core.sashwindows.di.service.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.readonly.ReadOnlyManager;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForResource;
import org.eclipse.papyrus.infra.gmfdiag.common.messages.Messages;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramUtils;
import org.eclipse.papyrus.infra.services.edit.internal.context.TypeContext;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.viewpoints.configuration.ModelAutoCreate;
import org.eclipse.papyrus.infra.viewpoints.configuration.OwningRule;
import org.eclipse.papyrus.infra.viewpoints.configuration.RootAutoSelect;
import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
import org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * Command creating a new GMF diagram in Papyrus. This command is intended to be used in eclipse
 * extensions.
 *
 * Commands to create a GMF Diagram can subclass this class. There is two kinds of commands: -
 * Eclipse handlers issuing commands (toolbar, menu, ...). This commands can find the active editor
 * by using the Worbench.getActivePArt(). The entry point is {@link #execute(ExecutionEvent)}. -
 * Commands called during editor initializing (like wizard). This commands require the diResourceSet
 * to work. The entry point is {@link #createDiagram(Resource, EObject, String)}
 *
 * @author cedric dumoulin
 * @author <a href="mailto:jerome.benois@obeo.fr">Jerome Benois</a>
 */
public abstract class AbstractPapyrusGmfCreateDiagramCommandHandler extends AbstractHandler implements IHandler, ICreationCommand {

	/**
	 * Inner class for the creation of diagrams
	 *
	 * @author Laurent Wouters
	 *
	 */
	private class Creator {

		private ModelSet modelSet;

		private EObject owner;

		private EObject element;

		private ViewPrototype prototype;

		private OwningRule rule;

		private String name;

		private IElementEditService service;

		private IClientContext clientContext;

		public Creator(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name) {
			this.modelSet = modelSet;
			this.owner = owner;
			this.element = element;
			this.prototype = prototype;
			this.name = name;
		}

		private CommandResult createDiagram() throws ServiceException {
			Resource notationResource = getNotationResource(modelSet, owner, element);
			if (notationResource == null) {
				return CommandResult.newErrorCommandResult("Cannot create a diagram on the selected element (ReadOnly?)");
			}
			Resource diResource = DiModelUtils.getDiResource(modelSet);

			if (owner == null) {
				Resource modelResource = ILanguageService.getLanguageModels(modelSet).stream()
						.filter(IEMFModel.class::isInstance)
						.map(IEMFModel.class::cast)
						.findAny()
						.map(IEMFModel::getResource)
						.orElse(null);
				owner = getRootElement(modelResource);
				attachModelToResource(owner, modelResource);
			}

			service = ElementEditServiceUtils.getCommandProvider(owner);
			if (service == null) {
				// Something isn't right ...
				return null;
			}

			try {
				clientContext = TypeContext.getContext();
			} catch (ServiceException e) {
				Activator.log.error(e);
			}
			if (clientContext == null) {
				// Something isn't right ...
				return null;
			}

			rule = PolicyChecker.getCurrent().getOwningRuleFor(prototype, owner);
			if (rule == null) {
				// Something isn't right ...
				return null;
			}

			element = owner;
			if (rule.getNewModelPath() != null) {
				// We have a path for the root auto-creation
				for (ModelAutoCreate auto : rule.getNewModelPath()) {
					EReference ref = auto.getFeature();
					String type = auto.getCreationType();
					if (ref.isMany()) {
						element = create(element, ref, type);
					} else {
						EObject temp = (EObject) element.eGet(ref);
						if (temp != null) {
							element = temp;
						} else {
							element = create(element, ref, type);
						}
					}
				}
			}

			if (rule.getSelectDiagramRoot() != null) {
				// We have a path for the root auto-selection
				for (RootAutoSelect auto : rule.getSelectDiagramRoot()) {
					EReference ref = auto.getFeature();
					element = (EObject) element.eGet(ref);
				}
			}

			CommandResult result = doEditDiagramName(prototype, name);
			if (!result.getStatus().isOK()) {
				return result;
			} else {
				name = (result.getReturnValue() != null) ? result.getReturnValue().toString() : null;
			}

			Diagram diagram = doCreateDiagram(notationResource, owner, element, prototype, name);

			if (diagram != null) {
				IPageManager pageManager = ServiceUtilsForResource.getInstance().getService(IPageManager.class, diResource);
				pageManager.addPage(diagram);
				return CommandResult.newOKCommandResult(diagram);
			}

			return CommandResult.newCancelledCommandResult();
		}

		private EObject create(EObject origin, EReference reference, String typeID) {
			IElementType itype = ElementTypeRegistry.getInstance().getType(typeID);
			CreateElementRequest request = new CreateElementRequest(origin, itype, reference);
			ICommand command = service.getEditCommand(request);
			IStatus status = null;
			try {
				status = command.execute(null, null);
			} catch (ExecutionException e) {
				return null;
			}
			if (!status.isOK()) {
				return null;
			}
			CommandResult result = command.getCommandResult();
			if (result == null) {
				return null;
			}
			return (EObject) result.getReturnValue();
		}
	}

	protected Resource getNotationResource(ModelSet modelSet, EObject owner, EObject element) {
		if (element == null) { // If the element is null, the root element of the main model will be used. Return the main notation resource
			return NotationUtils.getNotationResource(modelSet);
		}
		URI uriWithoutExtension = element.eResource().getURI().trimFileExtension();

		URI notationURI = uriWithoutExtension.appendFileExtension(NotationModel.NOTATION_FILE_EXTENSION);
		Resource notationResource = modelSet.getResource(notationURI, false);

		// The resource doesn't exist. Maybe we're trying to create a
		// diagram on a pure-UML library. Try to create a new resource
		if (notationResource == null) {
			notationResource = modelSet.createResource(notationURI);
			if (notationResource == null) {
				modelSet.getResources().remove(notationResource);
				return null;
			}

			EditingDomain editingDomain = EMFHelper.resolveEditingDomain(element);

			if (EMFHelper.isReadOnly(notationResource, editingDomain)) {
				// Check whether the resource can be made writable
				IReadOnlyHandler2 roHandler = ReadOnlyManager.getReadOnlyHandler(editingDomain);
				if (roHandler.canMakeWritable(ReadOnlyAxis.anyAxis(), new URI[] { notationResource.getURI() }).or(false)) {
					return notationResource; // The read-only manager will eventually ask for a user confirmation
				} else {
					modelSet.getResources().remove(notationResource);
					return null; // The resource can't be made writable; don't go further
				}
			}
		}

		return notationResource;
	}


	/**
	 * @return
	 */
	protected CommandResult doEditDiagramName(ViewPrototype prototype, String name) {

		if (name == null) {
			name = openDiagramNameDialog(prototype.isNatural() ? getDefaultDiagramName() : "New" + prototype.getLabel().replace(" ", ""));
		}
		// canceled
		if (name == null) {
			return CommandResult.newCancelledCommandResult();
		}
		return CommandResult.newOKCommandResult(name);
	}


	/**
	 * Get the root element associated with canvas.
	 */
	private EObject getRootElement(Resource modelResource) {
		EObject rootElement = null;
		if (modelResource != null && modelResource.getContents() != null && modelResource.getContents().size() > 0) {
			Object root = modelResource.getContents().get(0);
			if (root instanceof EObject) {
				rootElement = (EObject) root;
			}
		}

		return rootElement;
	}

	/**
	 * Store model element in the resource.
	 */
	private void attachModelToResource(EObject root, Resource resource) {
		resource.getContents().add(root);
	}

	/**
	 * Open popup to enter the new diagram name
	 *
	 * @param defaultValue
	 *            the default value
	 * @return the entered diagram name
	 */
	private String openDiagramNameDialog(String defaultValue) {
		if (defaultValue == null) {
			defaultValue = "";
		}

		InputDialog inputDialog = new InputDialog(Display.getCurrent().getActiveShell(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_SelectNewDiagramName, Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_NewDiagramName, defaultValue, null);
		int result = inputDialog.open();

		if (result == Window.OK) {
			String name = inputDialog.getValue();
			if (name == null || name.length() == 0) {
				name = defaultValue;
			}
			return name;
		}
		return null;
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#createDiagram(org.eclipse.emf.ecore.resource.Resource, org.eclipse.emf.ecore.EObject,
	 * java.lang.String)
	 */
	@Override
	public final Diagram createDiagram(ModelSet modelSet, EObject owner, String name) {
		ViewPrototype proto = ViewPrototype.get(getCreatedDiagramType(), owner, owner);
		if (proto == null) {
			return null;
		}
		return createDiagram(modelSet, owner, owner, proto, name);
	}


	public final Diagram createDiagram(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name, boolean openDiagram) {
		ICommand createCmd = getCreateDiagramCommand(modelSet, owner, element, prototype, name);
		TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain();
		CompositeCommand cmd = new CompositeCommand("Create diagram");
		cmd.add(createCmd);
		if (openDiagram) {
			cmd.add(new OpenDiagramCommand(dom, createCmd));
		}

		try {

			IStatus status = CheckedOperationHistory.getInstance().execute(cmd, new NullProgressMonitor(), null);
			if (status.isOK()) {
				CommandResult result = cmd.getCommandResult();
				Object returnValue = result.getReturnValue();

				// CompositeCommands should always return a collection
				if (returnValue instanceof Collection<?>) {
					for (Object returnElement : (Collection<?>) returnValue) {
						if (returnElement instanceof Diagram) {
							return (Diagram) returnElement;
						}
					}
				}
			} else if (status.getSeverity() != IStatus.CANCEL) {
				StatusManager.getManager().handle(status, StatusManager.SHOW);
			}
		} catch (ExecutionException ex) {
			Activator.log.error(ex);
		}

		return null;
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#createDiagram(org.eclipse.emf.ecore.resource.Resource, org.eclipse.emf.ecore.EObject,
	 * org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype, java.lang.String)
	 */
	@Override
	public final Diagram createDiagram(ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name) {
		return createDiagram(modelSet, owner, element, prototype, name, true);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet,
	 * org.eclipse.emf.ecore.EObject, java.lang.String)
	 */
	@Override
	public final ICommand getCreateDiagramCommand(ModelSet modelSet, EObject owner, String name) {
		ViewPrototype proto = ViewPrototype.get(getCreatedDiagramType(), owner, owner);
		if (proto == null) {
			return null;
		}
		return getCreateDiagramCommand(modelSet, owner, owner, proto, name);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet,
	 * org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype,
	 * java.lang.String)
	 */
	@Override
	public final ICommand getCreateDiagramCommand(final ModelSet modelSet, final EObject owner, final EObject element, final ViewPrototype prototype, final String name) {
		// Diagram creation should not change the semantic resource
		final Resource notationResource = NotationUtils.getNotationResource(modelSet);
		final Resource diResource = DiModelUtils.getDiResource(modelSet);

		ArrayList<IFile> modifiedFiles = new ArrayList<IFile>();
		if (notationResource.getURI().isPlatformResource()) {
			modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(notationResource.getURI().toPlatformString(true))));
		}
		if (diResource.getURI().isPlatformResource()) {
			modifiedFiles.add(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(diResource.getURI().toPlatformString(true))));
		}

		return new AbstractTransactionalCommand(modelSet.getTransactionalEditingDomain(), Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_CreateDiagramCommandLabel, modifiedFiles) {

			private Diagram diagram = null;

			private EObject diagramElement = null;

			private EObject diagramOwner = null;

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				Creator creator = new Creator(modelSet, owner, element, prototype, name);
				try {
					CommandResult commandResult = creator.createDiagram();
					if (!commandResult.getStatus().isOK()) {
						return commandResult;
					}

					diagram = (Diagram) commandResult.getReturnValue();
					diagramElement = diagram.getElement();
					diagramOwner = DiagramUtils.getOwner(diagram);
					return commandResult;
				} catch (ServiceException e) {
					Activator.log.error(e);
				}
				return CommandResult.newErrorCommandResult("Error during diagram creation");
			}

			@Override
			protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				// the undo corresponds to a destroy diagram command
				// during diagram creation no adapters are set to the diagram so the setElement is not registered
				// to remove the cross reference using the element reference it is better to use the destroy element command
				// DestroyElementPapyrusCommand depc = (diagram != null) ? new DestroyElementPapyrusCommand(new DestroyElementRequest(diagram, false)) : null;
				IStatus status = super.doUndo(monitor, info);
				diagram.setElement(null);
				return status;
			}

			@Override
			protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				diagram.setElement(diagramElement);
				DiagramUtils.setOwner(diagram, diagramOwner);
				IStatus status = super.doRedo(monitor, info);
				return status;
			}
		};
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#getCreatedDiagramType()
	 */
	@Override
	public String getCreatedDiagramType() {
		return getDiagramNotationID();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.papyrus.commands.ICreationCommand#isParentReassignable()
	 */
	@Override
	public boolean isParentReassignable() {
		// yes by default
		return true;
	}



	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// This method should not be called, use the execute(ExecutionEvent, ViewPrototype, String) method.
		throw new UnsupportedOperationException();
	}


	protected abstract String getDiagramNotationID();

	protected abstract PreferencesHint getPreferenceHint();

	protected abstract String getDefaultDiagramName();

	/**
	 * Overridable method that effectively create the diagram with the given validated parameters
	 *
	 * @param diagramResource
	 *            the diagram resource
	 * @param owner
	 *            the diagram's owner
	 * @param element
	 *            the diagram's model element
	 * @param prototype
	 *            the diagram's prototype
	 * @param name
	 *            the diagram's name
	 * @return the created diagram, or <code>null</code> if the creation failed
	 */
	protected Diagram doCreateDiagram(Resource diagramResource, EObject owner, EObject element, ViewPrototype prototype, String name) {
		// create diagram
		Diagram diagram = ViewService.createDiagram(element, getDiagramNotationID(), getPreferenceHint());
		if (diagram != null) {
			diagram.setName(name);
			diagram.setElement(element);
			DiagramUtils.setOwner(diagram, owner);
			if (!prototype.isNatural()) {
				DiagramUtils.setPrototype(diagram, prototype);
			}
			diagramResource.getContents().add(diagram);
			initializeDiagram(diagram);

		}
		return diagram;
	}

	/**
	 * Overridable method for the initialization of create diagrams
	 *
	 * @param diagram
	 *            the created diagram
	 */
	protected void initializeDiagram(EObject diagram) {
		// Subclasses may override
	}
}

Back to the top