Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36205f6bf5970f290513285f8d2300c2c107fb42 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*****************************************************************************
 * Copyright (c) 2012, 2014 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 323802
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.stereotype.edition.editpolicies;

import java.util.StringTokenizer;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafe;
import org.eclipse.papyrus.uml.appearance.helper.AppliedStereotypeHelper;
import org.eclipse.papyrus.uml.appearance.helper.UMLVisualInformationPapyrusConstant;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeNodeLabelDisplayEditPolicy;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.command.CreateAppliedStereotypePropertyViewCommand;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.editpart.AppliedStereotypePropertyEditPart;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * This editpolicy is used to add dynamically labels of stereotypes into nodes int the context of AppliedStereorypeCompartmentEditpart
 */
public class AppliedStereotypePropertiesEditPolicy extends AppliedStereotypeNodeLabelDisplayEditPolicy {


	public static String APPLIED_STEREOTYPE_VISIBILITY_COMPARTMENT = "AppliedStereotypeVisibilityEditPolicy";

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

	/**
	 * Returns the uml element controlled by the host edit part
	 * 
	 * @return the uml element controlled by the host edit part
	 */
	@Override
	protected Element getUMLElement() {
		return (Element)((View)getView().eContainer()).getElement();
	}


	/**
	 * 
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#getView()
	 * 
	 * @return
	 */
	@Override
	protected View getView() {
		return (View)((EObject)getHost().getModel()).eContainer();
	}


	/**
	 * this method is used to create a notation node for the property of the stereotype
	 * 
	 * @param editPart
	 *        the editpart container
	 * @param stereotypesPropertiesToDisplay
	 *        list of applied stereotype properties to display
	 * @param node
	 *        the comaprtment node that contains all properties
	 * @param stereotype
	 *        the stereotype associated to compartment node
	 */
	protected void executeAppliedStereotypePropertytCreation(final GraphicalEditPart editPart, final String stereotypesPropertiesToDisplay, final View node, final Stereotype stereotype) {
		try {
			editPart.getEditingDomain().runExclusive(new Runnable() {

				public void run() {
					Display.getCurrent().syncExec(new Runnable() {

						public void run() {

							StringTokenizer propStringTokenizer = new StringTokenizer(stereotypesPropertiesToDisplay, ",");
							while(propStringTokenizer.hasMoreElements()) {
								// extract property to display
								String propertyQN = propStringTokenizer.nextToken();
								//get a property that is interesting for us
								if(propertyQN.startsWith(stereotype.getQualifiedName())) {

									String propertyName = propertyQN.substring(propertyQN.lastIndexOf(".") + 1);
									Property stereotypeProperty = StereotypeUtil.getPropertyByName(stereotype, propertyName);

									if(stereotypeProperty == null) {
										return;
									}

									Node sterotypePropertyNode = null;
									int i = 0;
									//we go through all sub nodes to get sub node that is link to this property
									while(i < node.getChildren().size() && sterotypePropertyNode == null) {
										if((node.getChildren().get(i)) instanceof Node) {
											final Node currentNode = (Node)(node.getChildren().get(i));
											if(AppliedStereotypePropertyEditPart.ID.equals(currentNode.getType())) {
												if(stereotypeProperty.equals(currentNode.getElement())) {
													sterotypePropertyNode = currentNode;
												}
											}
										}
										i++;
									}

									if(sterotypePropertyNode == null) {
										//use to avoid to put it in the command stack
										CreateAppliedStereotypePropertyViewCommand command = new CreateAppliedStereotypePropertyViewCommand(editPart.getEditingDomain(), editPart.getNotationView(), stereotypeProperty);
										try {
											GMFUnsafe.write(editPart.getEditingDomain(), command);
										} catch (Exception e) {
											Activator.log.error(e);
										}
									}
								}
							}
						}
					});
				}
			});

		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	/**
	 * 
	 * {@inheritedDoc}
	 */
	@Override
	public void notifyChanged(Notification notification) {
		// refresh obly when the EAnnotation about stereotype is added or remove
		// to update only property of stereotype application


		// if element that has changed is a stereotype => refresh the label.
		if(notification.getNotifier() instanceof Node && (notification.getEventType() == Notification.ADD) && (notification.getNewValue() instanceof EAnnotation)) {
			if(UMLVisualInformationPapyrusConstant.STEREOTYPE_ANNOTATION == ((EAnnotation)notification.getNewValue()).getSource()) {
				// stereotype annotation has changed => refresh label display
				refreshDisplay();
			}
		}
	}


	/**
	 * maybe to remove
	 */
	protected void refreshEAnnotation() {
		final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
		String presentationKind = AppliedStereotypeHelper.getAppliedStereotypePresentationKind((View)editPart.getNotationView().eContainer());
		EObject stereotypeApplication = editPart.resolveSemanticElement();
		Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
		final RecordingCommand command = AppliedStereotypeHelper.getAddAppliedStereotypeCommand(editPart.getEditingDomain(), editPart.getNotationView(), stereotype.getQualifiedName(), presentationKind);
		try {

			editPart.getEditingDomain().runExclusive(new Runnable() {

				public void run() {
					Display.getCurrent().syncExec(new Runnable() {

						public void run() {
							editPart.getEditingDomain().getCommandStack().execute(command);
						}
					});
				}
			});

		} catch (Exception e) {
			Activator.log.error(e);
		}
	}

	@Override
	protected void refreshStereotypeDisplay() {
	}


	/**
	 * Refreshes the stereotype application property
	 */
	@Override
	public void refreshDisplay() {
		if(((View)getHost().getModel()).eContainer() != null) {
			String stereotypesPropertiesToDisplay = AppliedStereotypeHelper.getAppliedStereotypesPropertiesToDisplay((View)((View)getHost().getModel()).eContainer());
			final GraphicalEditPart editPart = (GraphicalEditPart)getHost();
			final View node = editPart.getNotationView();

			//1. Manage adding of application stereotype properties
			EObject stereotypeApplication = editPart.resolveSemanticElement();
			Stereotype stereotype = UMLUtil.getStereotype(stereotypeApplication);
			//if stereotype is null all property of stereotype has to be removed!
			if(stereotype != null) {
				//go through each stereotype property
				executeAppliedStereotypePropertytCreation(editPart, stereotypesPropertiesToDisplay, node, stereotype);

				//Manage removing of Stereotype application properties
				manageRemovingPropertiesNodes(stereotypesPropertiesToDisplay, editPart, node, stereotype);


			} else {
				try {
					editPart.getEditingDomain().runExclusive(new Runnable() {

						public void run() {
							Display.getCurrent().syncExec(new Runnable() {

								public void run() {
									DeleteCommand command = new DeleteCommand((View)getHost().getModel());
									//use to avoid to put it in the command stack
									try {
										GMFUnsafe.write(editPart.getEditingDomain(), command);
									} catch (Exception e) {
										System.err.println(e);
									}
								}

							});
						}
					});
				} catch (Exception e) {
					System.err.println(e);
				}

			}
		}
	}



	/**
	 * this method is used to remove nodes of property that are not in a given list
	 * 
	 * @param stereotypesPropertiesToDisplay
	 *        a list of qualified name of properties
	 * @param editPart
	 *        the graphical editpart that is the container
	 * @param node
	 *        the notation node that is the container
	 * @param stereotype
	 *        the stereotype that is display in this container
	 */
	protected void manageRemovingPropertiesNodes(String stereotypesPropertiesToDisplay, final GraphicalEditPart editPart, final View node, Stereotype stereotype) {
		int i = 0;
		while(i < node.getChildren().size()) {
			if((node.getChildren().get(i)) instanceof Node) {
				final Node currentNode = (Node)(node.getChildren().get(i));
				if(currentNode.getType().equals(AppliedStereotypePropertyEditPart.ID)) {
					Property property = (Property)currentNode.getElement();
					final String propertyQN = stereotype.getQualifiedName() + "." + property.getName();
					if(!stereotypesPropertiesToDisplay.contains(propertyQN)) {
						try {
							//yes, Execution of the Deletion command
							editPart.getEditingDomain().runExclusive(new Runnable() {

								public void run() {
									Display.getCurrent().syncExec(new Runnable() {

										public void run() {
											if(currentNode.eContainer() != null) {

												DeleteCommand command = new DeleteCommand(currentNode);
												//use to avoid to put it in the command stack
												try {
													GMFUnsafe.write(editPart.getEditingDomain(), command);
												} catch (Exception e) {
													System.err.println(e);
												}
											}

										}
									});
								}
							});
						} catch (Exception e) {
							System.err.println(e);
						}
					}

				}
			}
			i++;
		}
	}

}

Back to the top