Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e1f4dfc28461f3287aa852a4c83aaeff12eb49c (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
/*****************************************************************************
 * 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
 *  Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Bug 496905
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.clazz.custom.policies;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.InformationFlowConveyedLabelEditPart;
import org.eclipse.papyrus.uml.internationalization.utils.utils.UMLLabelInternationalization;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.InformationFlow;

/**
 * This class manages the display of the conveyed classifiers by an {@linkInformationFlow}
 *
 *
 */
public class InformationFlowCustomLabelEditPolicy extends AbstractCustomLabelEditPolicy {

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

	public static final int ADD_CONVEYED_CLASSIFIER = Notification.ADD;

	public static final int REMOVE_CONVEYED_CLASSIFER = Notification.REMOVE;

	public static final int CHANGE_NAME_OF_A_CONVOYED_CLASSIFIER = Notification.SET;

	public InformationFlowCustomLabelEditPolicy() {
		super();
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	protected void addAdditionalListeners() {
		// adds a listener to the element itself, and to linked elements, like Type
		if (getUMLElement() instanceof InformationFlow) {
			// adds listener to each Convoyed Classifier
			EList<Classifier> conveyedClassifiers = ((InformationFlow) getUMLElement()).getConveyeds();
			for (int i = 0; i < conveyedClassifiers.size(); i++) {
				getDiagramEventBroker().addNotificationListener(conveyedClassifiers.get(i), this);
			}
		}
	}

	@Override
	public void refreshDisplay() {
		refreshNameDisplay();
	}

	/**
	 *
	 * {@inheritDoc}
	 */
	@Override
	protected void removeAdditionalListeners() {
		if (getUMLElement() instanceof InformationFlow) {
			EList<Classifier> conveyedClassifiers = ((InformationFlow) getUMLElement()).getConveyeds();
			for (int i = 0; i < conveyedClassifiers.size(); i++) {
				getDiagramEventBroker().removeNotificationListener(conveyedClassifiers.get(i), this);
			}
		}
	}

	/**
	 * Refresh the name in the Label
	 *
	 */
	protected void refreshNameDisplay() {
		if (getHost() instanceof InformationFlowConveyedLabelEditPart) {
			((InformationFlowConveyedLabelEditPart) getHost()).setLabelText(nameToDisplay());
		}
	}

	/**
	 *
	 * {@inheritedDoc}
	 */
	@Override
	public void notifyChanged(Notification 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:
		// - a convoyed classifier's name have changed
		// - add or remove of a convoyed classifier
		final int eventType = notification.getEventType();
		if (notification.getNotifier() instanceof InformationFlow) {
			switch (eventType) {
			case ADD_CONVEYED_CLASSIFIER:
				getDiagramEventBroker().addNotificationListener((EObject) notification.getNewValue(), this);
				refreshDisplay();
				break;
			case REMOVE_CONVEYED_CLASSIFER:
				getDiagramEventBroker().removeNotificationListener((EObject) notification.getOldValue(), this);
				refreshDisplay();
				break;
			default:
				// Nothing to do
			}
		}
		if (eventType == CHANGE_NAME_OF_A_CONVOYED_CLASSIFIER) {
			refreshDisplay();
		}
	}

	/**
	 *
	 * @return the name to display
	 */
	public String nameToDisplay() {
		String name = "";
		if (getUMLElement() instanceof InformationFlow) {
			EList<Classifier> classes = ((InformationFlow) getUMLElement()).getConveyeds();
			for (int i = 0; i < classes.size(); i++) {
				name += UMLLabelInternationalization.getInstance().getLabel(classes.get(i));
				if (i != classes.size() - 1) {
					name += ", ";
				}
			}
		}
		return name;
	}
}

Back to the top