Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5e5931cc0e40a152d842814e79cfd2ffc733da79 (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
/*****************************************************************************
 * Copyright (c) 2009 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
 *  Patrick Tessier (CEA LIST) - Initial API and implementation
 *  Celine JANSSENS (ALL4TEC) celine.janssens@all4tec.net - Bug 455311 Stereotype Display
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editpolicies;

import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusUMLElementFigure;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayConstant;

/**
 * This edit policy is used to display only applied stereotypes and properties
 * in a external node (that is a labelEditPart) In order to use it, the edit
 * part has to be {@link IPapyrusEditPart} and the associated figure has to be {@link IPapyrusUMLElementFigure}
 */
public class AppliedStereotypeExternalNodeEditPolicy extends AppliedStereotypeLabelDisplayEditPolicy {

	protected View parentView = null;

	public AppliedStereotypeExternalNodeEditPolicy() {
		super();
	}

	@Override
	public void activate() {
		// retrieve the view and the element managed by the edit part
		View view = getView();
		if (view == null) {
			return;
		}
		super.activate();
		// add a listener for TimeObservationEditPart
		// eContainer = getParent() , but here it's the ECore model
		EObject parent = view.eContainer();
		if (parent instanceof View) {
			parentView = (View) parent;
			getDiagramEventBroker().addNotificationListener(parentView, this);
		}



	}

	@Override
	public void deactivate() {
		if (parentView != null) {
			getDiagramEventBroker().removeNotificationListener(parentView, this);
		}

		super.deactivate();
	}

	/**
	 * Refresh the text of the stereotype
	 */
	@Override
	protected void refreshStereotypeDisplay() {
		IFigure figure;
		if (getHost() instanceof IPapyrusEditPart) {
			figure = ((IPapyrusEditPart) getHost()).getPrimaryShape();
		} else {
			figure = hostEditPart.getFigure();
		}

		if (figure instanceof IPapyrusUMLElementFigure) {// calculate text
			// and icon to display
			final String stereotypesToDisplay = stereotypesToDisplay();
			((IPapyrusUMLElementFigure) figure).setStereotypeDisplay(tag + (stereotypesToDisplay), null);

		}



	}

	/**
	 * Retrieve the List of the Stereotypes to Display
	 *
	 * @return the list of stereotypes to display with properties if there are
	 *         selected to be displayed
	 */
	@Override
	public String stereotypesToDisplay() {

		// retrieve all stereotypes to be displayed

		if (parentView == null) {
			return EMPTY_STRING;
		}

		// try to display stereotype properties
		final String stereotypesToDisplay = helper.getStereotypeTextToDisplay(parentView);
		final String stereotypesPropertiesToDisplay = helper.getStereotypePropertiesInBrace(parentView);

		String display = getStereotypeAndPropertiesTextToDisplay(stereotypesToDisplay, stereotypesPropertiesToDisplay);

		return display;

	}

	/**
	 * @param stereotypesToDisplay
	 * @param stereotypesPropertiesToDisplay
	 * @param display
	 * @return
	 */
	private String getStereotypeAndPropertiesTextToDisplay(final String stereotypesToDisplay, final String stereotypesPropertiesToDisplay) {
		StringBuilder display = new StringBuilder();
		if (stereotypesToDisplay != null && !stereotypesToDisplay.isEmpty()) {
			display.append(stereotypesToDisplay);
		}

		if (stereotypesPropertiesToDisplay != null && !stereotypesPropertiesToDisplay.isEmpty()) {
			if (display.length() > 0) {
				display.append(StereotypeDisplayConstant.STEREOTYPE_PROPERTY_SEPARATOR);
			}
			display.append(StereotypeDisplayConstant.BRACE_LEFT + stereotypesPropertiesToDisplay + StereotypeDisplayConstant.BRACE_RIGHT);
		}
		return display.toString();
	}

}

Back to the top