Skip to main content
summaryrefslogtreecommitdiffstats
blob: 76a3aaf1bb98aed74683eeae99d78876955e1fda (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
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
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.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.papyrus.infra.emf.appearance.helper.VisualInformationPapyrusConstants;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractMaskManagedEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineNameEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.papyrus.uml.diagram.sequence.preferences.LifelinePreferencePage;
import org.eclipse.papyrus.uml.diagram.sequence.util.LifelineLabelHelper;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;


public class LifelineLabelEditPolicy  extends AbstractMaskManagedEditPolicy {

	private IPropertyChangeListener preferenceListener;

	@Override
	public void addAdditionalListeners() {
		super.addAdditionalListeners();
		
		if(preferenceListener == null){
			preferenceListener = new IPropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent event) {
					handlePreferenceChange(event);
				}				
			};
			IPreferenceStore store = UMLDiagramEditorPlugin.getInstance().getPreferenceStore();
			store.addPropertyChangeListener(this.preferenceListener);
		}

		Lifeline lifeline = getUMLElement();
		// check host semantic element is not null
		if(lifeline == null) {
			return;
		}
		// adds a listener to the element itself, and to linked elements, like Type
		getDiagramEventBroker().addNotificationListener(lifeline, this);
		
		ConnectableElement ce = lifeline.getRepresents();
		if(ce != null){
			getDiagramEventBroker().addNotificationListener(ce, this);
			if(ce.getType() != null)
				getDiagramEventBroker().addNotificationListener(ce.getType(), this);
		}
	}
	
	protected void handlePreferenceChange(PropertyChangeEvent event) {
		EditPart part = getHost();
		if(part == null || part.getParent() == null)
			return;
		
		String key = event.getProperty();
		if(key.equals(LifelinePreferencePage.LABEL_DISPLAY_PREFERENCE)){
			refreshDisplay();
		}
	}

	@Override
	protected void removeAdditionalListeners() {
		super.removeAdditionalListeners();
		Lifeline lifeline = getUMLElement();
		// check host semantic element is not null
		if(lifeline == null) {
			return;
		}
		getDiagramEventBroker().removeNotificationListener(lifeline, this);
		
		ConnectableElement ce = lifeline.getRepresents();
		if(ce != null){
			getDiagramEventBroker().removeNotificationListener(ce, this);
			if(ce.getType() != null)
				getDiagramEventBroker().removeNotificationListener(ce.getType(), this);
		}
	}
	
	public void notifyChanged(Notification notification) {
		super.notifyChanged(notification);

		Object object = notification.getNotifier();
		if(object == null || getUMLElement() == null) {
			return;
		}
		
		if(notification.getFeature().equals(UMLPackage.eINSTANCE.getNamedElement_Name())) {
			refreshDisplay();
		} else if(notification.getFeature().equals(UMLPackage.Literals.LIFELINE__REPRESENTS)) {
			// change represent element
			if(notification.getNewValue() instanceof ConnectableElement){
				ConnectableElement ce = (ConnectableElement )notification.getNewValue();
				getDiagramEventBroker().addNotificationListener(ce, this);
				if(ce.getType() != null)
					getDiagramEventBroker().addNotificationListener(ce.getType(), this);
			}
			
			if(notification.getOldValue() instanceof ConnectableElement){
				ConnectableElement ce = (ConnectableElement )notification.getOldValue();
				getDiagramEventBroker().removeNotificationListener(ce, this);
				
				if(ce.getType() != null)
					getDiagramEventBroker().removeNotificationListener(ce.getType(), this);
			}
			
			refreshDisplay();
		}else if(isMaskManagedAnnotation(object) || isRemovedMaskManagedLabelAnnotation(object, notification)) {
			refreshDisplay();
		} else if(object.equals(getUMLElement().getRepresents()) ){
			// change represent type
			if(notification.getNewValue() instanceof Type && notification.getNewValue() instanceof EObject){
				getDiagramEventBroker().addNotificationListener((EObject)notification.getNewValue(), this);
			}
			
			if(notification.getOldValue() instanceof Type && notification.getOldValue() instanceof EObject){
				getDiagramEventBroker().removeNotificationListener((EObject)notification.getOldValue(), this);
			}
			
			refreshDisplay();
		}
	}

	
	public void refreshDisplay() {
		// calls the helper for this edit Part
		LifelineEditPart lp = (LifelineEditPart)getHost();
		List children = lp.getChildren();
		for(Object p : children)
			if(p  instanceof LifelineNameEditPart)
				LifelineLabelHelper.getInstance().refreshEditPartDisplay((GraphicalEditPart)p);
	}	

	public int getCurrentDisplayValue() {
		EAnnotation customeDisplayAnnotation = ((View)getHost().getModel()).getEAnnotation(VisualInformationPapyrusConstants.CUSTOM_APPEARENCE_ANNOTATION);
		int displayValue = getDefaultDisplayValue();
		if(customeDisplayAnnotation != null && customeDisplayAnnotation.getDetails().get(VisualInformationPapyrusConstants.CUSTOM_APPEARANCE_MASK_VALUE) != null) {
			displayValue = Integer.parseInt(customeDisplayAnnotation.getDetails().get(VisualInformationPapyrusConstants.CUSTOM_APPEARANCE_MASK_VALUE));
		} else {
			// no specific information => look in preferences
			IPreferenceStore store = UMLDiagramEditorPlugin.getInstance().getPreferenceStore();
			int displayValueTemp = store.getInt(LifelinePreferencePage.LABEL_DISPLAY_PREFERENCE);
			if(displayValueTemp != 0) {
				displayValue = displayValueTemp;
			}
		}
		return displayValue;
	}

	public int getDefaultDisplayValue() {
		return LifelinePreferencePage.DEFAULT_LABEL_DISPLAY;
	}

	public String getMaskLabel(int value) {
		return LifelineLabelHelper.getInstance().getMaskLabel(value);
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection<String> getMaskLabels() {
		return LifelineLabelHelper.getInstance().getMaskLabels();
	}

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

	/**
	 * {@inheritDoc}
	 */
	public Collection<Integer> getMaskValues() {
		return LifelineLabelHelper.getInstance().getMaskValues();
	}

	/**
	 * {@inheritDoc}
	 */
	public String getPreferencePageID() {
		return "org.eclipse.papyrus.uml.diagram.sequence.preferences.LifelinePreferencePage";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Lifeline getUMLElement() {
		return (Lifeline)hostSemanticElement;
	}	
	
}

Back to the top