Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed1e170e541efccf8db91dee258048f6860c3b9f (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
/*****************************************************************************
 * Copyright (c) 2018 EclipseSource 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:
 *   EclipseSource - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.helpers.advice;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
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.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyDependentsRequest;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Size;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.adapter.NotationAndTypeAdapter;
import org.eclipse.papyrus.uml.diagram.sequence.command.SetResizeAndLocationCommand;
import org.eclipse.papyrus.uml.diagram.sequence.command.SetResizeCommand;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.InteractionOperandEditPart;
import org.eclipse.uml2.uml.InteractionOperand;

/**
 * <p>
 * Advice for {@link InteractionOperand} View (InteractionOperand_Shape), responsible
 * for resize sibling operands during deletion.
 * </p>
 * <p>
 * This advice must be bound before the default GMF's notationDepdendents advice,
 * to make sure we can propagate the size of the deleted view before it is actually
 * deleted.
 * </p>
 */
public class InteractionOperandViewAdvice extends AbstractEditHelperAdvice {

	@Override
	protected ICommand getBeforeDestroyDependentsCommand(DestroyDependentsRequest request) {
		ICommand beforeDestroyDependentsCommand = super.getBeforeDestroyDependentsCommand(request);

		if (request.getElementToDestroy() instanceof InteractionOperand) {
			Set<View> operandViews = findOperandViews((InteractionOperand) request.getElementToDestroy());
			return CompositeCommand.compose(beforeDestroyDependentsCommand,
					operandViews.stream()
							.map(view -> getSizeCommandFor(view, request.getEditingDomain()))
							.reduce(null, CompositeCommand::compose));
		}

		return beforeDestroyDependentsCommand;
	}

	private Set<View> findOperandViews(InteractionOperand operand) {
		return EMFHelper.getUsages(operand).stream()
				.filter(setting -> setting.getEStructuralFeature() == NotationPackage.Literals.VIEW__ELEMENT)
				.map(Setting::getEObject)
				.filter(View.class::isInstance)
				.map(View.class::cast)
				.filter(view -> view.getElement() == operand)
				.filter(view -> InteractionOperandEditPart.VISUAL_ID.equals(view.getType()))
				.collect(Collectors.toSet());
	}

	/**
	 * Return the command used to propagate the size of the given <code>viewToDestroy</code>
	 * to one of the sibling operands (The previous one, or the next one if the destroyed view
	 * if the first in the CF).
	 *
	 * The actual resize commands are created on-the-fly during command execution, so
	 * the commands can be properly chained in case of multi-deletion.
	 *
	 * @param viewToDestroy
	 * @param editingDomain
	 * @return
	 */
	private ICommand getSizeCommandFor(View viewToDestroy, TransactionalEditingDomain editingDomain) {
		// Do the actual work in the command (At execution time), so we can propagate size changes
		// in case of multi-selection (So when deleting A, B and C, A will resize B, then B will resize C, then C will resize D)
		return new AbstractTransactionalCommand(editingDomain, "Resize sibling operand", null) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
				Dimension deletedSize = getSize(viewToDestroy);
				if (deletedSize == null) {
					return null;
				}
				View viewBefore = findViewBefore(viewToDestroy);
				int height = deletedSize.height();
				if (viewBefore != null) {
					Dimension sizeOfViewBefore = getSize(viewBefore);
					sizeOfViewBefore.expand(0, height);
					NotationAndTypeAdapter adapter = new NotationAndTypeAdapter(viewBefore.getElement(), viewBefore);
					ICommand result = new SetResizeCommand(editingDomain, "Expand previous operand", adapter, sizeOfViewBefore);
					result.execute(monitor, info);
				} else {
					View viewAfter = findViewAfter(viewToDestroy);
					if (viewAfter != null) {
						Rectangle boundsOfViewAfter = getBounds(viewAfter);
						boundsOfViewAfter.height += height; // Do not use expand, because it would also translate the rectangle
						boundsOfViewAfter.y -= height; // Shift the following operand up (Used by the Grid policy to compute coverage)
						NotationAndTypeAdapter adapter = new NotationAndTypeAdapter(viewAfter.getElement(), viewAfter);
						ICommand result = new SetResizeAndLocationCommand(editingDomain, "Expand previous operand", adapter, boundsOfViewAfter);
						result.execute(monitor, info);
					}
				}
				return CommandResult.newOKCommandResult();
			}
		};

	}

	/**
	 * @param view
	 * @return
	 * 		The sibling view immediately following the given view, or <code>null</code> if there is no such view
	 */
	private static View findViewAfter(View view) {
		if (false == view.eContainer() instanceof View) {
			return null;
		}

		@SuppressWarnings("unchecked") // GMF is Java 1.4
		List<View> viewSiblings = ((View) view.eContainer()).getPersistedChildren();

		int index = viewSiblings.indexOf(view);
		int indexAfter = index + 1;
		if (indexAfter < viewSiblings.size()) {
			return viewSiblings.get(indexAfter);
		}
		return null;
	}

	/**
	 * @param view
	 * @return
	 * 		The sibling view immediately preceding the given view, or <code>null</code> if there is no such view
	 */
	private static View findViewBefore(View view) {
		if (false == view.eContainer() instanceof View) {
			return null;
		}

		@SuppressWarnings("unchecked") // GMF is Java 1.4
		List<View> viewSiblings = ((View) view.eContainer()).getPersistedChildren();

		int index = viewSiblings.indexOf(view) - 1;

		if (index >= 0) {
			return viewSiblings.get(index);
		}
		return null;
	}

	/**
	 * @param view
	 * @return
	 * 		A new {@link Rectangle} instance representing the model bounds of the {@link View},
	 *         or <code>null</code> if the View doesn't have a size.
	 */
	private Rectangle getBounds(View view) {
		if (view instanceof Node) {
			Node node = (Node) view;
			LayoutConstraint constraint = node.getLayoutConstraint();
			Rectangle bounds = new Rectangle();
			if (constraint instanceof Size) {
				Size size = (Size) constraint;
				bounds.setSize(size.getWidth(), size.getHeight());
			}
			if (constraint instanceof Location) {
				Location location = (Location) constraint;
				bounds.setLocation(location.getX(), location.getY());
			}
			return bounds;
		}
		return null;
	}



	private Dimension getSize(View viewToDestroy) {
		Rectangle bounds = getBounds(viewToDestroy);
		return bounds == null ? null : bounds.getSize();
	}
}

Back to the top