Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d532871b9f7299fdd4677688db80f3f01542b97b (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
/*****************************************************************************
 * Copyright (c) 2018 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.service.types.helper.advice;

import static org.eclipse.gmf.runtime.common.core.command.CompositeCommand.compose;

import java.util.Optional;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.edithelper.AbstractEditHelperAdvice;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.uml2.uml.GeneralOrdering;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;

/**
 * Edit-helper advice for elements that own {@link InteractionFragment}s, being
 * {@link Interaction}s and {@link InteractionOperand}s.
 * 
 * @since 4.0
 */
public class InteractionFragmentContainerEditHelperAdvice extends AbstractEditHelperAdvice {

	/**
	 * Initializes me.
	 */
	public InteractionFragmentContainerEditHelperAdvice() {
		super();
	}

	/**
	 * In the case of a contained {@link InteractionFragment} to be destroyed, we don't
	 * actually destroy it but just re-assign it to the containing {@link Interaction}
	 * or {@link InteractionOperand}, unless that container also is being destroyed, in
	 * which case we bump up the containment at most to the root {@link Interaction}.
	 * If that {@link Interaction} also is being destroyed, then we just destroy the
	 * fragment because the entire content of the interaction is being destroyed.
	 */
	@Override
	protected ICommand getBeforeDestroyElementCommand(DestroyElementRequest req) {
		ICommand superResult = super.getBeforeDestroyElementCommand(req);

		// If it's a combined fragment, record that it's being deleted
		InteractionContainerDeletionContext.deleting(req);

		EObject elementToDestroy = req.getElementToDestroy();

		Optional<ICommand> result = Optional.empty();
		if (elementToDestroy instanceof InteractionFragment) {
			InteractionFragment fragment = (InteractionFragment) elementToDestroy;
			Optional<InteractionContainerDeletionContext> context = InteractionContainerDeletionContext.get(req);
			result = context.map(ctx -> ctx.getDestroyCommand(fragment));
		} else if (elementToDestroy instanceof GeneralOrdering) {
			GeneralOrdering generalOrdering = (GeneralOrdering) elementToDestroy;
			Optional<InteractionContainerDeletionContext> context = InteractionContainerDeletionContext.get(req);
			result = context.map(ctx -> ctx.getDestroyCommand(generalOrdering));
		}

		// If we have an advice command, it should replace the 'instead' command
		result.ifPresent(advice -> instead(req, advice));

		return result.map(advice -> compose(superResult, advice)).orElse(superResult);
	}

	ICommand instead(IEditCommandRequest request, ICommand instead) {
		request.setParameter(IEditCommandRequest.REPLACE_DEFAULT_COMMAND, Boolean.TRUE);
		return instead;
	}

	@Override
	protected ICommand getAfterDestroyElementCommand(DestroyElementRequest request) {
		// In case I replaced the default command, don't propagate that parameter
		// to any destroy-dependents requests
		request.getParameters().remove(IEditCommandRequest.REPLACE_DEFAULT_COMMAND);

		return super.getAfterDestroyElementCommand(request);
	}
}

Back to the top