Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b78c10e1cae448e2e528243112efb7e25f3c4b9 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*****************************************************************************
 * 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
 *  Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.fr - Bug 393532
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.stereotype.edition.editpolicies;

import java.util.Iterator;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gmf.runtime.diagram.core.commands.DeleteCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.BorderedBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationUtils;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.GMFUnsafe;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeNodeLabelDisplayEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.stereotype.CreateAppliedStereotypeCommentViewCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.CreateAppliedStereotypePropertyViewCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.CreateAppliedStereotypeViewCommand;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeDisplayUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;

/**
 * This editpolicy has in charge to :
 * - create or destroy the appliedStereotypesCommentEditpart
 * - create notation view associated to this CommentEditPart
 * - refresh the Comment content based on the UML information
 * by using NamedStyle mechanism into Notation model
 * 
 *
 */
public class AppliedStereotypeCommentEditPolicy extends AppliedStereotypeNodeLabelDisplayEditPolicy {


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

	/** the comment node associated to the Host EditPart */
	private Node comment;


	@Override
	public void activate() {
		super.activate();
		getDiagramEventBroker().addNotificationListener(hostView.eContainer(), this);

	};

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#deactivate()
	 *
	 */
	@Override
	public void deactivate() {
		getDiagramEventBroker().removeNotificationListener(hostView.eContainer(), this);
		super.deactivate();
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeLabelDisplayEditPolicy#refreshNotationStructure()
	 *
	 */
	@Override
	public void refreshNotationStructure() {

		if (hostView != null) {

			removeUnappliedStereotypes(comment);


			if (!stereotypeList.isEmpty()) {
				refreshStereotypeCommentStructure();
			}
		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void notifyChanged(Notification notification) {
		super.notifyChanged(notification);
		if (comment != null) {

			int eventType = notification.getEventType();
			EObject object = NotationUtils.getEObjectValue(comment, StereotypeDisplayUtils.STEREOTYPE_COMMENT_RELATION_NAME, null);
			// If the reference object of the comment is removed, delete the Comment node itself.
			if (eventType == Notification.REMOVE && notification.getOldValue().equals(hostView) && object == null) {
				executeAppliedStereotypeCommentDeletion(hostEditPart.getEditingDomain(), comment);
			}
		}


	}


	/**
	 * Refresh the structure for the Stereotype List
	 * 
	 * @param stereotypeList
	 *            The list of Stereotype applied on the Element.
	 */
	public void refreshStereotypeCommentStructure() {

		if (comment != null) {
			removeUnappliedStereotypes(comment);
		}
		// rebuild the structure from the Stereotype List
		if (!stereotypeList.isEmpty()) {
			comment = createCommentNode();
			if (comment != null) {
				for (Stereotype stereotype : stereotypeList) {
					refreshStereotypeCompartmentStructure(stereotype);
					refreshStereotypeBraceStructure(stereotype);

				}
			}
		}

	}


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

		super.refreshStereotypeDisplay();
		// If no more Compartment, delete the Comment
		if (comment != null && getAppliedStereotypeCompartmentNumber(comment) == 0) {
			executeAppliedStereotypeCommentDeletion(hostEditPart.getEditingDomain(), comment);
		}
	}


	/**
	 * Get the number of Visible Compartments
	 * 
	 * @param view
	 *            The View where the number of visible Compartment are evaluated
	 * 
	 * @return the number of Visible Stereotype Compartment
	 */
	protected int getAppliedStereotypeCompartmentNumber(View view) {
		int nbVisibleCompartment = 0;
		Iterator<View> iteratorView = view.getChildren().iterator();
		while (iteratorView.hasNext()) {
			View subview = iteratorView.next();
			if (helper.isStereotypeBrace(subview) || helper.isStereotypeCompartment(subview)) {
				nbVisibleCompartment++;
			}
		}
		return nbVisibleCompartment;
	}


	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AppliedStereotypeLabelDisplayEditPolicy#refreshStereotypeCompartmentStructure(org.eclipse.uml2.uml.Stereotype)
	 *
	 * @param stereotype
	 */
	@Override
	public void refreshStereotypeBraceStructure(Stereotype stereotype) {

		BasicCompartment compartment = helper.getStereotypeCompartment(hostView, stereotype);
		if (compartment == null) { // No Label Exist for this Stereotype
			createAppliedStereotypeBraceCompartment(stereotype);
			createAppliedStereotypeBraceProperties(stereotype);

		}
	}

	/**
	 * Refresh The StereotypeCompartment notation structure.
	 * 
	 * @param stereotype
	 *            Stereotype related to the Compartment to created
	 */
	public void refreshStereotypeCompartmentStructure(Stereotype stereotype) {

		BasicCompartment compartment = helper.getStereotypeCompartment(comment, stereotype);
		if (compartment == null) { // 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, StereotypeDisplayUtils.STEREOTYPE_COMPARTMENT_TYPE);
		}
	}


	/**
	 * 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(comment, stereotype);
		if (compartment != null && stereotype != null) {

			EList<Property> properties = stereotype.allAttributes();
			for (Property property : properties) {
				createAppliedStereotypeProperty(compartment, property);
			}
		}
	}

	/**
	 * In Charge of PropertyView Creation
	 * 
	 * @param propertyType
	 *            Type of Property {@link StereotypeDisplayUtils#STEREOTYPE_PROPERTY_TYPE} or {@link StereotypeDisplayUtils#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 (property != null && !property.getName().startsWith(Extension.METACLASS_ROLE_PREFIX)) {
			if (!helper.isPropertyExist(compartment, property)) {
				// go through each stereotype property
				executeAppliedStereotypePropertyViewCreation(hostEditPart, compartment, property);

			}
		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#createAppliedStereotypeBraceCompartment(org.eclipse.uml2.uml.Stereotype)
	 *
	 * @param stereotype
	 *            Stereotype related to the Brace Compartment to created
	 */
	@Override
	protected void createAppliedStereotypeBraceCompartment(Stereotype stereotype) {
		final View node = hostEditPart.getNotationView();
		// doesn't exist already
		if (!helper.isCompartmentExist(node, stereotype)) {
			// Create Compartment
			executeAppliedStereotypeCompartmentCreation(hostEditPart, stereotype, StereotypeDisplayUtils.STEREOTYPE_BRACE_TYPE);

		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractAppliedStereotypeDisplayEditPolicy#createAppliedStereotypeBraceProperties(org.eclipse.uml2.uml.Stereotype)
	 *
	 * @param stereotype
	 *            Stereotype related to the Brace Properties to created
	 */
	@Override
	protected void createAppliedStereotypeBraceProperties(Stereotype stereotype) {
		Node compartment = helper.getStereotypeBraceCompartment(comment, stereotype);
		if (compartment != null && stereotype != null) {

			EList<Property> properties = stereotype.allAttributes();
			for (Property property : properties) {
				createAppliedStereotypeBraceProperty(compartment, property);
			}
		}
	}

	/**
	 * In charge of Comment Node creation.
	 * 
	 * @return The Comment Node newly created
	 */
	private Node createCommentNode() {
		if (helper.getStereotypeComment(hostView) == null) {
			// if Comment doesn't exist => Create it and copy the structure from the host

			if (getHost() instanceof ConnectionEditPart) {
				executeAppliedStereotypeCommentCreation(((ConnectionEditPart) getHost()).resolveSemanticElement());
			}
			if (getHost() instanceof GraphicalEditPart) {
				executeAppliedStereotypeCommentCreation(((GraphicalEditPart) getHost()).resolveSemanticElement());
			}

		}
		return helper.getStereotypeComment(hostView);

	}


	/**
	 * 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, final String type) {
		try {
			editPart.getEditingDomain().runExclusive(new Runnable() {

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


						public void run() {
							CreateAppliedStereotypeViewCommand command = new CreateAppliedStereotypeViewCommand(editPart.getEditingDomain(), comment, stereotype, type);

							// use to avoid to put it in the command stack
							try {
								GMFUnsafe.write(editPart.getEditingDomain(), command);
							} catch (Exception e) {
								Activator.log.error(e);
							}
						}
					});

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


	/**
	 * 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) {
		try {
			editPart.getEditingDomain().runExclusive(new Runnable() {

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

						public void run() {

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

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





	/**
	 * The goal of this method is to execute the a command to create a notation node for applied stereotype
	 * as "Comment" shape.
	 *
	 * @param appliedstereotype
	 *            the stereotype application
	 */
	protected void executeAppliedStereotypeCommentCreation(final EObject node) {

		final TransactionalEditingDomain domain = hostEditPart.getEditingDomain();
		Display.getCurrent().syncExec(new Runnable() {

			public void run() {
				int x = 200;
				int y = 100;
				if (hostEditPart.getModel() instanceof Node) {
					LayoutConstraint constraint = ((Node) hostEditPart.getModel()).getLayoutConstraint();
					if (constraint instanceof Bounds) {
						x = x + ((Bounds) constraint).getX();
						y = ((Bounds) constraint).getY();
					}

				}
				if (hostEditPart.getModel() instanceof Edge && ((((Edge) hostEditPart.getModel()).getSource()) instanceof Node)) {

					LayoutConstraint constraint = ((Node) ((Edge) hostEditPart.getModel()).getSource()).getLayoutConstraint();
					if (constraint instanceof Bounds) {
						x = x + ((Bounds) constraint).getX();
						y = ((Bounds) constraint).getY() - 100;
					}

				}
				boolean isBorderElement = false;
				if (hostEditPart instanceof BorderedBorderItemEditPart) {
					isBorderElement = true;
				}
				if (helper.getStereotypeComment((View) getHost().getModel()) == null) {
					CreateAppliedStereotypeCommentViewCommand command = new CreateAppliedStereotypeCommentViewCommand(domain, (View) hostEditPart.getModel(), x, y, node, isBorderElement);
					// use to avoid to put it in the command stack
					try {
						GMFUnsafe.write(domain, command);
					} catch (Exception e) {
						Activator.log.error(e);
					}
				}
			}

		});
	}

	/**
	 * In Charge to delete the Comment Node.
	 *
	 * @param domain
	 *            the transactional editing domain
	 * @param commentNode
	 *            the view that represent the comment of stereotype
	 */

	protected void executeAppliedStereotypeCommentDeletion(final TransactionalEditingDomain domain, final View commentNode) {
		Display.getCurrent().syncExec(new Runnable() {

			public void run() {
				// because it is asynchrony the comment node maybe become s null
				if (commentNode != null && TransactionUtil.getEditingDomain(commentNode) != null) {
					DeleteCommand command = new DeleteCommand(commentNode);
					// use to avoid to put it in the command stack
					try {
						GMFUnsafe.write(domain, command);
					} catch (Exception e) {
						Activator.log.error(e);
					}
				}
			}
		});
	}

	/**
	 * Get the Node (notation) corresponding to the Host editpart
	 */
	protected Node getNotationNode() {
		Node node = null;

		if (hostEditPart != null) {
			View view = hostEditPart.getNotationView();
			if (view instanceof Node) {
				node = (Node) view;
			}
		}
		return node;
	}


}

Back to the top