Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28a8dc6b1815db161344cfc11fd1d69b367c731e (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*****************************************************************************
 * Copyright (c) 2012, 2016 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 323802
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.fr - Bug 393532
 *  Celine Janssens (ALL4TEC) celine.janssens@all4tec.net - Bug 455311 : Refactor Stereotypes Display
 *  Christian W. Damus - bug 492482
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.stereotype.edition.editpolicies;

import org.eclipse.emf.common.util.EList;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeNodeLabelDisplayEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusNodeUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.command.CreateAppliedStereotypeCompartmentCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.command.CreateAppliedStereotypePropertyViewCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayConstant;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * this edit policy can be apply only on {@link IPapyrusEditPart} in order to
 * access to primary figure. the primary figure has to be a {@link IPapyrusNodeUMLElementFigure}
 *
 */

public class AppliedStereotypeCompartmentEditPolicy extends AppliedStereotypeNodeLabelDisplayEditPolicy {

	/** constant for this edit policy role */
	public final static String STEREOTYPE_COMPARTMENT_POLICY = "AppliedStereotypeCompartmentEditPolicy"; //$NON-NLS-1$

	/**
	 * Creates a new AppliedStereotype display edit policy
	 */
	public AppliedStereotypeCompartmentEditPolicy() {
		super();
	}


	/**
	 * Refresh the NotationStructure of the Node for this Stereotype
	 *
	 * @param notationView
	 * @param stereo
	 */
	@Override
	public void refreshStereotypeStructure() {

		// Build the structure from the Stereotype List
		if (!stereotypeList.isEmpty()) {
			for (Stereotype stereotype : stereotypeList) {
				refreshStereotypeCompartmentStructure(stereotype);
				subscribe(helper.getStereotypeCompartment(hostView, stereotype));

			}
		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeNodeLabelDisplayEditPolicy#refreshStereotypeDisplay()
	 *
	 */
	@Override
	protected void refreshStereotypeDisplay() {
		// Nothing to refresh
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#removeListener()
	 *
	 */
	@Override
	public void removeListener() {
		// Remove Listener for Compartment and properties
		if (!stereotypeList.isEmpty()) {
			for (Stereotype stereotype : stereotypeList) {
				Node compartment = helper.getStereotypeCompartment(hostView, stereotype);

				if (null != compartment && null != stereotype) {
					unsubscribe(compartment);
					EList<Property> properties = stereotype.allAttributes();
					for (Property property : properties) {
						unsubscribe(helper.getStereotypeProperty(hostView, stereotype, property));
					}


				}
			}
		}
	}

	/**
	 * Refresh The StereotypeCompartment notation structure.
	 *
	 * @param view
	 * @param stereotype
	 */

	protected void refreshStereotypeCompartmentStructure(Stereotype stereotype) {

		BasicCompartment compartment = helper.getStereotypeCompartment(hostView, stereotype);
		if (null == compartment) { // No Compartment Exist for this Stereotype
			createAppliedStereotypeCompartment(stereotype);
		}
		createAppliedStereotypeProperties(stereotype);
	}

	/**
	 * This method creates a node for the compartment of stereotype if it does not exist.
	 *
	 * @param stereotypeApplication
	 *            the stereotype application
	 */
	protected void createAppliedStereotypeCompartment(final Stereotype stereotype) {
		final View node = hostEditPart.getNotationView();
		// doesn't exist already
		if (!helper.isCompartmentExist(node, stereotype)) {
			// Create Compartment
			executeAppliedStereotypeCompartmentCreation(hostEditPart, stereotype);

		}
	}


	/**
	 * In charge of properties view creation
	 *
	 * @param eObject
	 *            The Edit Part of which the Properties should be created
	 */

	protected void createAppliedStereotypeProperties(final Stereotype stereotype) {

		Node compartment = helper.getStereotypeCompartment(hostEditPart.getNotationView(), stereotype);
		if (null != compartment && null != stereotype) {

			EList<Property> properties = stereotype.allAttributes();
			for (Property property : properties) {
				createAppliedStereotypeProperty(compartment, property);
				subscribe(helper.getStereotypeProperty(hostView, stereotype, property));

			}


		}
	}

	/**
	 * In Charge of PropertyView Creation
	 *
	 * @param propertyType
	 *            Type of Property {@link StereotypeDisplayConstant#STEREOTYPE_PROPERTY_TYPE} or {@link StereotypeDisplayConstant#STEREOTYPE_PROPERTY_BRACE_TYPE}
	 * @param compartment
	 *            The Compartment owner of the Property that will be created
	 * @param property
	 *            The UML Property of the View to create
	 */

	protected void createAppliedStereotypeProperty(Node compartment, Property property) {
		// if stereotype is null all property of stereotype has to be removed!
		if (null != property && !property.getName().startsWith(Extension.METACLASS_ROLE_PREFIX)) {
			if (!helper.isPropertyExist(compartment, property)) {
				// go through each stereotype property
				executeAppliedStereotypePropertyViewCreation(hostEditPart, compartment, property);

			}
		}
	}


	/**
	 * the goal of this method is to execute the a command to create a notation node for a compartment of stereotype
	 *
	 * @param editPart
	 *            the editPart owner of the new compartment
	 * @param appliedstereotype
	 *            the stereotype application
	 */
	protected void executeAppliedStereotypeCompartmentCreation(final IGraphicalEditPart editPart, final Stereotype stereotype) {
		CreateAppliedStereotypeCompartmentCommand command = new CreateAppliedStereotypeCompartmentCommand(editPart.getEditingDomain(), editPart.getNotationView(), stereotype, StereotypeDisplayConstant.STEREOTYPE_COMPARTMENT_TYPE);

		// Record for undo if possible, otherwise unprotected
		execute(command);
	}


	/**
	 * this method is used to create a notation node for the property of the stereotype
	 *
	 * @param editPart
	 *            the editPart container
	 * @param compartment
	 * @param stereotypeApplication
	 * @param stereotype
	 *            the stereotype associated to compartment node
	 */
	protected void executeAppliedStereotypePropertyViewCreation(final IGraphicalEditPart editPart, final Node compartment, final Property stereotypeProperty) {
		CreateAppliedStereotypePropertyViewCommand command = new CreateAppliedStereotypePropertyViewCommand(editPart.getEditingDomain(), compartment, stereotypeProperty, StereotypeDisplayConstant.STEREOTYPE_PROPERTY_TYPE);

		// Record for undo if possible, otherwise unprotected
		execute(command);
	}


}

Back to the top