Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c2c060482d08317b4dd167bc6f0132d1ee1b264c (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
/*****************************************************************************
 * Copyright (c) 2011 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.infra.gmfdiag.common.commands;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.ui.commands.CreateCommand;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;

/**
 * A replacement for CreateCommand that avoids that takes into account the incorrect
 * generation of ViewProvider by GMFTooling and modifies SemanticAdapter in call to {@link ViewService#provides(Class, org.eclipse.core.runtime.IAdaptable, View, String, int, boolean, org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint)} .
 */
public class CreateViewCommand extends CreateCommand {

	/** Constructor */
	public CreateViewCommand(TransactionalEditingDomain editingDomain, ViewDescriptor viewDescriptor, View containerView) {
		super(editingDomain, viewDescriptor, containerView);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canExecute() {
		// Warning the element adapter can possibly be null (see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=353129)
		if (viewDescriptor.getElementAdapter() == null) {
			return false;
		}

		// Try to adapt the descriptor ElementAdapter in EObject
		EObject element = EMFHelper.getEObject(viewDescriptor.getElementAdapter());
		IElementType elementType = viewDescriptor.getElementAdapter().getAdapter(IElementType.class);

		SemanticElementAdapter semanticAdapter = new SemanticElementAdapter(element, elementType);

		// Use the semanticAdapter instead of view descriptor element adapter to avoid the use of provides(ViewForKind) method
		// from ViewProvider which is incorrectly implemented in GMF Tooling generated editors (other editors may have undesired side-effect on each-other).

		return ViewService.getInstance().provides(viewDescriptor.getViewKind(), semanticAdapter, containerView, viewDescriptor.getSemanticHint(), viewDescriptor.getIndex(), viewDescriptor.isPersisted(), viewDescriptor.getPreferencesHint());
	}



}

Back to the top