Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7267a877d2068299f27f7f23b99a4191c517dca5 (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
/*****************************************************************************
 * Copyright (c) 2009, 2016 CEA LIST, Esterel Technologies SAS 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
 *  Alain Le Guennec (Esterel Technologies SAS) - bug 497391
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editpolicies;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
import org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker;
import org.eclipse.gmf.runtime.diagram.core.listener.NotificationListener;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.listenerservice.IPapyrusListener;
import org.eclipse.papyrus.infra.emf.appearance.helper.AppearanceHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.uml.diagram.common.figure.node.IPapyrusNodeNamedElementFigure;
import org.eclipse.papyrus.uml.diagram.common.figure.node.NodeNamedElementFigure;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * this edit policy has in charge to display the qualified name of an element.
 * To display it, the editpart must be a {@link IPapyrusEditPart} and the
 * associated figure has to be a {@link NodeNamedElementFigure}
 */
public class QualifiedNameDisplayEditPolicy extends GraphicalEditPolicy implements NotificationListener, IPapyrusListener {

	/** key for this edit policy */
	public static final String QUALIFIED_NAME_POLICY = "Qualified_name_editpolicy"; //$NON-NLS-1$

	/** host semantic element */
	protected NamedElement hostSemanticNamedElement;

	/**
	 * The parent listeners list
	 */
	protected List<Object> parentListeners;

	/**
	 * Creates a new QualifiedNameDisplayEditPolicy
	 */
	public QualifiedNameDisplayEditPolicy() {
		super();
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void activate() {
		// retrieve the view and the element managed by the edit part
		View view = (View) getHost().getModel();
		if (view == null) {
			return;
		}

		hostSemanticNamedElement = getNamedElement();
		if (hostSemanticNamedElement != null) {
			// adds a listener on the view and the element controlled by the
			// editpart
			getDiagramEventBroker().addNotificationListener(view, this);

			if (hostSemanticNamedElement == null) {
				return;
			}
			getDiagramEventBroker().addNotificationListener(hostSemanticNamedElement, this);

			refreshQualifiedNameDisplay();
		}
		addParentListeners();
	}

	/**
	 * refresh the qualified name
	 */
	protected void refreshQualifiedNameDisplay() {
		if (getHost() instanceof IPapyrusEditPart) {
			if (((IPapyrusEditPart) getHost()).getPrimaryShape() instanceof NodeNamedElementFigure) {
				NodeNamedElementFigure nodeNamedElementFigure = (NodeNamedElementFigure) ((IPapyrusEditPart) getHost()).getPrimaryShape();
				refreshQualifiedNameDepth(nodeNamedElementFigure);
				refreshQualifiedName(nodeNamedElementFigure);
			}
		}

	}

	/**
	 * set the qualified name to display
	 *
	 * @param nodeNamedElementFigure
	 *            the associated figure to the editpart
	 */
	protected void refreshQualifiedName(IPapyrusNodeNamedElementFigure nodeNamedElementFigure) {
		nodeNamedElementFigure.setQualifiedName(hostSemanticNamedElement.getQualifiedName());
	}

	/**
	 * refresh the editpart with a depth for the qualified name
	 *
	 * @param nodeNamedElementFigure
	 *            the associated figure to the editpart
	 */
	protected void refreshQualifiedNameDepth(NodeNamedElementFigure nodeNamedElementFigure) {
		nodeNamedElementFigure.setDepth(AppearanceHelper.getQualifiedNameDepth((View) getHost().getModel()));
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void deactivate() {
		// retrieve the view and the element managed by the edit part
		View view = (View) getHost().getModel();
		if (view == null) {
			return;
		}
		if (hostSemanticNamedElement != null) {
			// remove notification on element and view
			getDiagramEventBroker().removeNotificationListener(view, this);
			getDiagramEventBroker().removeNotificationListener(hostSemanticNamedElement, this);
		}

		// removes the reference to the semantic element
		hostSemanticNamedElement = null;
		removeParentListeners();
	}

	/**
	 *
	 * @return the associated named element to the editpart. it can return null
	 *         if this not a named element
	 */
	protected NamedElement getNamedElement() {
		View view = (View) getHost().getModel();
		if (view == null) {
			return null;
		}
		if (view.getElement() != null && view.getElement() instanceof NamedElement) {
			return (NamedElement) view.getElement();
		}
		return null;
	}

	/**
	 * Gets the diagram event broker from the editing domain.
	 *
	 * @return the diagram event broker
	 */
	protected DiagramEventBroker getDiagramEventBroker() {
		TransactionalEditingDomain theEditingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain();
		if (theEditingDomain != null) {
			return DiagramEventBroker.getInstance(theEditingDomain);
		}
		return null;
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void notifyChanged(Notification notification) {
		if (getNamedElement() == null) {
			return;
		}

		if (UMLPackage.eINSTANCE.getNamedElement_Name().equals(notification.getFeature()) || notification.getNotifier() instanceof EAnnotation) {
			refreshQualifiedNameDisplay();
		} else if (UMLPackage.eINSTANCE.getNamedElement_Name().equals(notification.getFeature())) {
			if (parentListeners.contains(notification.getNotifier()) || getNamedElement().equals(notification.getNotifier())) {
				refreshQualifiedNameDisplay();
			}
		} else if (notification.getFeature() instanceof EReference) {
			EReference ref = (EReference) notification.getFeature();
			if (ref.isContainment()) {
				if (parentListeners.contains(notification.getNotifier())) {
					removeParentListeners();
				}
				addParentListeners();
				refreshQualifiedNameDisplay();
			}
		}
	}

	/**
	 * Add listeners for all parents
	 */
	private void addParentListeners() {
		DiagramEventBroker diagramEventBroker = getDiagramEventBroker();
		if (diagramEventBroker != null) {
			if (parentListeners == null) {
				parentListeners = new ArrayList<Object>();
			}
			if (getNamedElement() != null) {
				EObject parentEOBject = getNamedElement().eContainer();
				while (parentEOBject != null) {
					diagramEventBroker.addNotificationListener(parentEOBject, this);
					parentListeners.add(parentEOBject);
					parentEOBject = parentEOBject.eContainer();
				}
			}
		}
	}

	/**
	 * Remove all parents listeners
	 */
	public void removeParentListeners() {
		if (parentListeners != null) {
			for (Object listener : parentListeners) {
				getDiagramEventBroker().removeNotificationListener((EObject) listener, this);
			}
			parentListeners.clear();
		}
	}
}

Back to the top