Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5ee600a1b7907ac7879e83b0ca9c5857168ba44 (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
/*****************************************************************************
 * Copyright (c) 2008, 2015 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr
 *  Christian W. Damus - bug 433206
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.commands;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.ui.commands.DeferredCreateConnectionViewCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest.ConnectionViewDescriptor;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

/**
 * This class is used to create a connection view
 *
 * @author Patrick Tessier
 */
public class CommonDeferredCreateConnectionViewCommand extends DeferredCreateConnectionViewCommand implements ICommand {

	protected ICommand command;

	/** the command saved for undo and redo */
	protected Command createConnectionCmd;

	protected ConnectionViewDescriptor viewDescriptor;

	/**
	 * Constructor
	 *
	 * @param editingDomain
	 *            the editing domain
	 * @param element
	 *            the semantic element (may be null)
	 * @param sourceViewAdapter
	 *            the source of the link
	 * @param targetViewAdapter
	 *            the target of the link (may be null if the command is filled)
	 * @param viewer
	 *            the viewer
	 * @param preferencesHint
	 *            the preferencehint of the diagram
	 * @param command
	 *            the command in which we look for the result for the target
	 *            (may be null)
	 */
	public CommonDeferredCreateConnectionViewCommand(TransactionalEditingDomain editingDomain, EObject element, IAdaptable sourceViewAdapter, IAdaptable targetViewAdapter, EditPartViewer viewer, PreferencesHint preferencesHint, ICommand command) {
		super(editingDomain, element, sourceViewAdapter, targetViewAdapter, viewer, preferencesHint);
		this.command = command;
		setResult(CommandResult.newOKCommandResult());
	}

	/**
	 * Constructor
	 *
	 * @param editingDomain
	 *            the editing domain
	 * @param semanticHint
	 *            the semantic of the graphical element
	 * @param sourceViewAdapter
	 *            the source of the link
	 * @param targetViewAdapter
	 *            the target of the link (may be null if the command is filled)
	 * @param viewer
	 *            the viewer
	 * @param preferencesHint
	 *            the preferencehint of the diagram
	 * @param viewDescriptor
	 *            the view descriptor
	 * @param command
	 *            the command in which we look for the result for the target
	 *            (may be null)
	 */
	public CommonDeferredCreateConnectionViewCommand(TransactionalEditingDomain editingDomain, String semanticHint, IAdaptable sourceViewAdapter, IAdaptable targetViewAdapter, EditPartViewer viewer, PreferencesHint preferencesHint,
			ConnectionViewDescriptor viewDescriptor, ICommand command) {
		super(editingDomain, semanticHint, sourceViewAdapter, targetViewAdapter, viewer, preferencesHint);
		this.viewDescriptor = viewDescriptor;
		this.command = command;
		setResult(CommandResult.newOKCommandResult(viewDescriptor));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		Map<?, ?> epRegistry = viewer.getEditPartRegistry();
		IGraphicalEditPart sourceEP = (IGraphicalEditPart) epRegistry.get(sourceViewAdapter.getAdapter(View.class));
		IGraphicalEditPart targetEP = (IGraphicalEditPart) epRegistry.get(targetViewAdapter.getAdapter(View.class));

		/*
		 * when the source or the target of a link is an other link, the
		 * previous method returns something like ElementNameEditPart A such
		 * EditPart does not allow to get a Command for the creation of the link
		 */
		if (sourceEP instanceof LabelEditPart) {
			sourceEP = findEditPartForCreation(sourceViewAdapter.getAdapter(View.class));
		}

		if (targetEP instanceof LabelEditPart) {
			targetEP = findEditPartForCreation(targetViewAdapter.getAdapter(View.class));
		}

		return doExecuteWithResult(progressMonitor, info, sourceEP, targetEP);
	}

	/**
	 * Returns the EditPart for the creation
	 *
	 * @param adapter
	 *            the view of the element for which we are looking for a valid
	 *            EditPart
	 * @return the EditPart for the creation
	 */
	protected IGraphicalEditPart findEditPartForCreation(View view) {
		Iterator<?> editPartIterator = viewer.getEditPartRegistry().values().iterator();
		EObject semanticElement = view.getElement();
		while (editPartIterator.hasNext()) {
			EditPart currentEditPart = (EditPart) editPartIterator.next();
			if ((!(currentEditPart instanceof CompartmentEditPart)) && currentEditPart instanceof IGraphicalEditPart && semanticElement == (((IGraphicalEditPart) currentEditPart).resolveSemanticElement())) {
				// In the case of a diagram created inside an element and this element being dropped inside, there is a possibility (the order does not seem to be kept between creations)
				// that the editPart matching the semantic element is the diagram itself as it is contained by the element.
				// We cannot link a diagram Edit Part, it will return a null command, preventing the link creation.
				if (!(currentEditPart instanceof LabelEditPart || currentEditPart.getModel() instanceof Diagram)) {
					return (IGraphicalEditPart) currentEditPart;
				}
			}
		}
		return null;
	}

	/**
	 * Creates a connection view between the source and target.
	 *
	 * @throws ExecutionException
	 */
	protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info, IGraphicalEditPart sourceEditPart, IGraphicalEditPart targetEditPart) throws ExecutionException {

		// If these are null, then the diagram's editparts may not
		// have been refreshed yet.
		Assert.isNotNull(sourceEditPart);
		Assert.isNotNull(targetEditPart);

		// use the String semanticHint to create a view
		// modification in order to fix the bug
		CreateConnectionViewRequest createRequest = new CreateConnectionViewRequest(viewDescriptor);
		createConnectionCmd = CreateConnectionViewRequest.getCreateCommand(createRequest, sourceEditPart, targetEditPart);

		if (createConnectionCmd == null) {
			throw new ExecutionException("Connection creation command is null."); //$NON-NLS-1$
		}

		if (createConnectionCmd.canExecute()) {
			createConnectionCmd.execute();
		}

		if (element != null) {
			((View) (createRequest.getConnectionViewDescriptor().getAdapter(View.class))).setElement(element);
		}
		viewer = null;// for garbage collection
		return CommandResult.newOKCommandResult(viewDescriptor);
	}

	public void setElement(EObject element) {
		this.element = element;
	}
}

Back to the top