Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fcc56d8c702cbf83304214921cc559e4844bbfa6 (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
/*****************************************************************************
 * Copyright (c) 2018 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.component.custom.edit.policies;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy;
import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.DefaultCreationEditPolicy;
import org.eclipse.uml2.uml.Component;

/**
 * This policy used to reparent in case when semantically child {@link Component} contained in parnet {@link Component}
 * but graphically child {@link Component} contained in another place.
 *
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=479981
 */
public class CompositeCompartmentCreationEditPolicy extends DefaultCreationEditPolicy {

	/**
	 * Reparent only view if child is {@link Component}
	 * if other case
	 *
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.DefaultCreationEditPolicy#getReparentCommand(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart)
	 */
	@Override
	protected ICommand getReparentCommand(IGraphicalEditPart gep) {
		if (gep.resolveSemanticElement() instanceof Component) {
			return getReparentViewCommand(gep);
		}

		return super.getReparentCommand(gep);
	}

	/**
	 * In case when element is {@link Component} don't check is newContext already contain element
	 *
	 * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.CreationEditPolicy#shouldReparent(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
	 */
	@Override
	protected boolean shouldReparent(EObject element, EObject newContext) {
		if (element instanceof Component) {
			return !(element == null || element == newContext || isContainedIn(element, newContext));
		}
		return super.shouldReparent(element, newContext);
	}

	/**
	 * pivate method isContainedIn({@link EObject} element, {@link EObject} newContext) from {@link CreationEditPolicy}
	 */
	private boolean isContainedIn(EObject element, EObject newContext) {
		EObject container = newContext.eContainer();
		while (container != null) {
			if (container.equals(element)) {
				return true;
			}
			container = container.eContainer();
		}
		return false;
	}
}

Back to the top