Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8e4ecaaab85c7ea12df3c14ff42ae6fc711042d (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
/*****************************************************************************
 * Copyright (c) 2010, 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:
 *   Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - Initial API and Implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editpolicies;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.helper.FloatingLabelHelper;
import org.eclipse.papyrus.uml.tools.utils.ICustomAppearance;
import org.eclipse.uml2.uml.MultiplicityElement;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Specific edit policy for label displaying stereotypes and their properties for edges representing
 * UML elements.
 */
public class MaskManagedFloatingLabelEditPolicy extends AbstractMaskManagedEditPolicy {

	/**
	 * Creates a new PropertyLabelEditPolicy.
	 */
	public MaskManagedFloatingLabelEditPolicy() {
		super();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addAdditionalListeners() {
		super.addAdditionalListeners();
		NamedElement umlElement = getUMLElement();
		// check host semantic element is not null
		if (umlElement == null) {
			Activator.log.error("No semantic element present when adding listeners in FloatingLabelEditPolicy", null);
			return;
		}
		// adds a listener to the element itself, and to linked elements, like Type
		if (umlElement instanceof TypedElement) {
			if (((TypedElement) umlElement).getType() != null) {
				getDiagramEventBroker().addNotificationListener(((TypedElement) umlElement).getType(), this);
			}
		}
		if (umlElement instanceof MultiplicityElement) {
			getDiagramEventBroker().addNotificationListener(((MultiplicityElement) umlElement).getUpperValue(), this);
			getDiagramEventBroker().addNotificationListener(((MultiplicityElement) umlElement).getLowerValue(), this);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Collection<String> getDefaultDisplayValue() {
		return ICustomAppearance.DEFAULT_UML_FLOATING_LABEL;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Map<String, String> getMasks() {
		return FloatingLabelHelper.getInstance().getMasks();
	}

	/**
	 * {@inheritedDoc}.
	 *
	 * @return the UML element
	 */
	@Override
	public NamedElement getUMLElement() {
		EObject element = super.getUMLElement();
		if (element instanceof NamedElement) {
			return (NamedElement) element;
		}
		return null;
	}

	/**
	 * {@inheritedDoc}.
	 *
	 * @param notification
	 *            the notification
	 */
	@Override
	public void notifyChanged(Notification notification) {
		super.notifyChanged(notification);
		// change the label of the figure managed by the host edit part (managed by the parent edit
		// part in general...)
		// it must be changed only if:
		// - the annotation corresponding to the display of the stereotype changes
		// - the stereotype application list has changed
		Object object = notification.getNotifier();
		NamedElement property = getUMLElement();
		Object feature = notification.getFeature();
		if (object == null || property == null || feature == null) {
			return;
		}

		if (feature == UMLPackage.eINSTANCE.getLiteralInteger_Value()) {
			refreshDisplay();
		} else if (feature == UMLPackage.eINSTANCE.getLiteralUnlimitedNatural_Value()) {
			refreshDisplay();
		}
		if (object.equals(property)) {
			notifyNamedElementChanged(property, notification);
		}
		if (isMaskManagedAnnotation(object)) {
			refreshDisplay();
		}
		if (isRemovedMaskManagedLabelAnnotation(object, notification)) {
			refreshDisplay();
		}
	}

	/**
	 * notifies that the named element has changed.
	 *
	 * @param namedElement
	 *            the property that has changed
	 * @param notification
	 *            the notification send when the element has been changed
	 */
	protected void notifyNamedElementChanged(NamedElement namedElement, Notification notification) {
		switch (notification.getFeatureID(NamedElement.class)) {
		case UMLPackage.NAMED_ELEMENT__NAME:
			refreshDisplay();
			break;
		case UMLPackage.NAMED_ELEMENT__VISIBILITY:
			refreshDisplay();
			break;
		default:
			// does nothing in other cases
			break;
		}
	}

	/**
	 * Returns the view controlled by the host edit part.
	 *
	 * @return the view controlled by the host edit part
	 */
	@Override
	protected View getView() {
		EditPart host = getHost();
		if (host == null) {
			return null;
		}

		Object hostView = host.getModel();
		if (hostView instanceof View) {
			Object parentView = ((View) hostView).eContainer();
			if (parentView instanceof View) {
				return (View) parentView;
			}
			return null;
		}
		return null;
	}


	/**
	 * Refreshes the display of the edit part.
	 */
	@Override
	public void refreshDisplay() {
		// calls the helper for this edit Part
		FloatingLabelHelper.getInstance().refreshEditPartDisplay((GraphicalEditPart) getHost());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void removeAdditionalListeners() {
		super.removeAdditionalListeners();
		NamedElement umlElement = getUMLElement();
		if (umlElement == null) {
			// check semantic element is not null and this is really an instance of NamedElement
			return;
		}
		// remove listener to the element itself, and to linked elements, like Type
		if (umlElement instanceof TypedElement) {
			if (((TypedElement) umlElement).getType() != null) {
				getDiagramEventBroker().removeNotificationListener(((TypedElement) umlElement).getType(), this);
			}
		}
		if (umlElement instanceof MultiplicityElement) {
			getDiagramEventBroker().removeNotificationListener(((MultiplicityElement) umlElement).getUpperValue(), this);
			getDiagramEventBroker().removeNotificationListener(((MultiplicityElement) umlElement).getLowerValue(), this);
		}
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractMaskManagedEditPolicy#getCurrentDisplayValue()
	 *
	 * @return
	 */
	@Override
	public Collection<String> getCurrentDisplayValue() {
		if (getView() == null) {
			return Collections.emptySet();
		}
		return super.getCurrentDisplayValue();
	}
}

Back to the top