Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1f5077813d62518b9238020f46d2cbdd5ffe5953 (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagram.clazz.custom.helper.advice;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
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.DestroyDependentsRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.diagram.clazz.custom.utils.GeneralizationUtil;
import org.eclipse.papyrus.diagram.clazz.edit.parts.ModelEditPart;
import org.eclipse.papyrus.diagram.common.util.CrossReferencerUtil;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.NamedElement;

/**
 * <pre>
 * This HelperAdvice completes {@link Generalization} edit commands with diagram specific
 * commands in order to remove inconsistent views (e.g. inherited Port or Property
 * in case a {@link Generalization} is deleted or re-oriented.
 * </pre>
 */
public class GeneralizationHelperAdvice extends AbstractEditHelperAdvice {

	@Override
	protected ICommand getBeforeReorientRelationshipCommand(ReorientRelationshipRequest request) {

		// The list of member views becoming inconsistent after re-orient that should be deleted.
		Set<View> viewsToDestroy = new HashSet<View>();

		if(request.getRelationship() instanceof Generalization) {
			viewsToDestroy.addAll(getMemberViewsToDestroy((Generalization)request.getRelationship()));
		}

		//return the command to destroy all these views
		if(!viewsToDestroy.isEmpty()) {

			DestroyDependentsRequest ddr = new DestroyDependentsRequest(request.getEditingDomain(), request.getRelationship(), false);
			return ddr.getDestroyDependentsCommand(viewsToDestroy);

		}
		return null;
	}

	@Override
	protected ICommand getBeforeDestroyDependentsCommand(DestroyDependentsRequest request) {

		EObject destructee = request.getElementToDestroy();

		Set<View> viewsToDestroy = new HashSet<View>();

		if(destructee instanceof Generalization) {
			viewsToDestroy = getMemberViewsToDestroy((Generalization)destructee);
		}

		//return the command to destroy all these views
		if(!viewsToDestroy.isEmpty()) {
			return request.getDestroyDependentsCommand(viewsToDestroy);
		}
		return null;
	}

	/**
	 * This methods looks for inconsistent views to delete in case the generalization is deleted or
	 * re-oriented.
	 * 
	 * @param generalization
	 *        the modified generalization
	 * @return the list of {@link View} to delete
	 */
	protected Set<View> getMemberViewsToDestroy(Generalization generalization) {
		Set<View> viewsToDestroy = new HashSet<View>();

		Classifier general = ((Generalization)generalization).getGeneral();
		if(general != null) {
			// Parse members
			EList<NamedElement> members = general.getMembers();
			for(NamedElement member : members) {

				// Find Views in Composite Structure Diagram that are referencing current member
				Iterator<View> viewIt = CrossReferencerUtil.getCrossReferencingViews(member, ModelEditPart.MODEL_ID).iterator();
				while(viewIt.hasNext()) {
					View view = (View)viewIt.next();

					// Test if current view (member) is concerned by the deletion (re-orientation) of the generalization
					GeneralizationUtil util = new GeneralizationUtil();
					if(util.isConcernedByGeneralizationChanges(generalization, view)) {
						viewsToDestroy.add(view);
					}
				}
			}
		}

		return viewsToDestroy;
	}
}

Back to the top