Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ced407cc8a6369c38746b0033e2c38fb831b743 (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
236
/*****************************************************************************
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Nizar GUEDIDI (CEA LIST) - update getImage() : test if element is null
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.helper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.papyrus.infra.emf.appearance.helper.AppearanceHelper;
import org.eclipse.papyrus.uml.appearance.helper.AppliedStereotypeHelper;
import org.eclipse.papyrus.uml.appearance.helper.UMLVisualInformationPapyrusConstant;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeDisplayHelper;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeDisplayUtils;
import org.eclipse.papyrus.uml.tools.utils.UMLUtil;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Helper class for labels for elements that can have stereotypes
 */
public abstract class StereotypedElementLabelHelper {

	private final static String EMPTY_STRING = "";

	/**
	 * {@inheritDoc}
	 */
	public Element getUMLElement(GraphicalEditPart editPart) {
		return UMLUtil.resolveUMLElement(editPart);
	}

	/**
	 * Parses the string containing the complete definition of properties to be
	 * displayed, and generates a map.
	 *
	 * @param editPart
	 *            the edit part for which the label is edited
	 * @param stereotypesToDisplay
	 *            the list of stereotypes to display
	 * @param stereotypesPropertiesToDisplay
	 *            the properties of stereotypes to display
	 * @return a map. The keys are the name of displayed stereotypes, the
	 *         corresponding data is a collection of its properties to be
	 *         displayed
	 */
	protected Map<String, List<String>> parseStereotypeProperties(GraphicalEditPart editPart, String stereotypesToDisplay, String stereotypesPropertiesToDisplay) {
		Map<String, List<String>> propertiesMap = new HashMap<String, List<String>>();
		if (stereotypesPropertiesToDisplay != null) {
			StringTokenizer stringTokenizer = new StringTokenizer(stereotypesPropertiesToDisplay, UMLVisualInformationPapyrusConstant.STEREOTYPE_PROPERTIES_LIST_SEPARATOR);
			while (stringTokenizer.hasMoreTokens()) {
				String propertyName = stringTokenizer.nextToken();
				// retrieve the name of the stereotype for this property
				String stereotypeName = propertyName.substring(0, propertyName.lastIndexOf(".")); // stereotypequalifiedName.propertyname
				if (!propertiesMap.containsKey(stereotypeName)) {
					List<String> propertiesForStereotype = new ArrayList<String>();
					propertiesMap.put(stereotypeName, propertiesForStereotype);
				}
				propertiesMap.get(stereotypeName).add(propertyName.substring(propertyName.lastIndexOf(".") + 1, propertyName.length()));
			}
		}
		return propertiesMap;
	}

	/**
	 * Returns the image to be displayed for the applied stereotypes.
	 *
	 * @return the image that represents the first applied stereotype or <code>null</code> if no image has to be displayed
	 */
	public Collection<Image> stereotypeIconsToDisplay(GraphicalEditPart editPart) {
		String stereotypespresentationKind = AppliedStereotypeHelper.getAppliedStereotypePresentationKind((View) editPart.getModel());
		if (stereotypespresentationKind == null) {
			return null;
		}
		if (stereotypespresentationKind.equals(UMLVisualInformationPapyrusConstant.ICON_STEREOTYPE_PRESENTATION) || stereotypespresentationKind.equals(UMLVisualInformationPapyrusConstant.TEXT_ICON_STEREOTYPE_PRESENTATION)) {
			// retrieve the first stereotype in the list of displayed stereotype
			String stereotypesToDisplay = AppliedStereotypeHelper.getStereotypesToDisplay((View) editPart.getModel());
			Collection<Stereotype> stereotypes = new ArrayList<Stereotype>();
			StringTokenizer tokenizer = new StringTokenizer(stereotypesToDisplay, ",");
			while (tokenizer.hasMoreTokens()) {
				String firstStereotypeName = tokenizer.nextToken();
				stereotypes.add(getUMLElement(editPart).getAppliedStereotype(firstStereotypeName));
			}
			return Activator.getIconElements(getUMLElement(editPart), stereotypes, false);
		}
		return new ArrayList<Image>();
	}

	/**
	 * get Stereotype String to display
	 *
	 * @return the list of stereotypes to display
	 */
	public String stereotypesToDisplay(GraphicalEditPart editPart) {
		View view = (View) editPart.getModel();
		// retrieve all stereotypes to be displayed
		// try to display stereotype properties
		String stereotypesToDisplay = StereotypeDisplayHelper.getInstance().getStereotypeTextToDisplay(view);
		String stereotypesPropertiesToDisplay = StereotypeDisplayHelper.getInstance().getStereotypePropertiesInBrace(view);


		String display = EMPTY_STRING;

		if (stereotypesToDisplay != null && !stereotypesToDisplay.equals(EMPTY_STRING)) {
			display += stereotypesToDisplay + StereotypeDisplayUtils.STEREOTYPE_PROPERTIES_SEPARATOR;
		}
		if (stereotypesPropertiesToDisplay != null && !stereotypesPropertiesToDisplay.equals(EMPTY_STRING)) {

			display += StereotypeDisplayUtils.BRACE_LEFT + stereotypesPropertiesToDisplay + StereotypeDisplayUtils.BRACE_RIGHT;
		}

		return display;

	}


	/**
	 * Refreshes the label of the figure associated to the specified edit part
	 *
	 * @param editPart
	 *            the edit part managing the refreshed figure
	 */
	public void refreshEditPartDisplay(GraphicalEditPart editPart) {
		IFigure figure = editPart.getFigure();
		// computes the icon to be displayed
		final Collection<Image> imageToDisplay = stereotypeIconsToDisplay(editPart);
		// should check if edit part has to display the element icon or not
		if (AppearanceHelper.showElementIcon((View) editPart.getModel())) {
			imageToDisplay.add(getImage(editPart));
		}
		// for each element in the list of stereotype icon, adds it to the icons
		// list of the
		// wrapping label
		// problem (RS - CEA LIST): more icons were displayed before refresh:
		// has to clean
		// problem 2 (RS - CEA LIST): no method to know how many icons were
		// displayed => should fix
		// a max number ?!
		// solution: set all images to null, and then add the correct icons
		int i = 0;
		if (figure instanceof WrappingLabel) {
			while (((WrappingLabel) figure).getIcon(i) != null) {
				((WrappingLabel) figure).setIcon(null, i);
				i++;
			}
			i = 0;
			for (Image image : imageToDisplay) {
				((WrappingLabel) figure).setIcon(image, i);
				i++;
			}
			((WrappingLabel) figure).setText(labelToDisplay(editPart));
		}
	}

	/**
	 * Computes the label to be displayed for the property
	 */
	protected String labelToDisplay(GraphicalEditPart editPart) {
		StringBuffer buffer = new StringBuffer();
		// computes the label for the stereotype (horizontal presentation)
		buffer.append(stereotypesToDisplay(editPart));
		// computes the string label to be displayed
		buffer.append(elementLabel(editPart));
		// buffer.append(PropertyUtil.getCustomLabel(getUMLElement(), 0));
		return buffer.toString();
	}

	/**
	 * Computes the label corresponding to the semantic element
	 *
	 * @param editPart
	 *            the graphical part managing the semantic element
	 * @return the string corresponding to the display of the semantic element
	 */
	protected abstract String elementLabel(GraphicalEditPart editPart);

	/**
	 * Returns the image for the element
	 *
	 * @param editPart
	 *            the edit part that displays the element
	 * @return the image
	 */
	public Image getImage(GraphicalEditPart editPart) {
		Element element = getUMLElement(editPart);
		String key = "";
		if (element instanceof NamedElement) {
			key = ((NamedElement) element).getName() + "::" + ((NamedElement) element).getVisibility();
		} else if (element != null) {
			key = element.getClass().getName();
		} else {
			return null;
		}
		ImageRegistry imageRegistry = Activator.getDefault().getImageRegistry();
		Image image = imageRegistry.get(key);
		ImageDescriptor descriptor = null;
		if (image == null) {
			AdapterFactory factory = Activator.getDefault().getItemProvidersAdapterFactory();
			IItemLabelProvider labelProvider = (IItemLabelProvider) factory.adapt(getUMLElement(editPart), IItemLabelProvider.class);
			if (labelProvider != null) {
				descriptor = ExtendedImageRegistry.getInstance().getImageDescriptor(labelProvider.getImage(getUMLElement(editPart)));
			}
			if (descriptor == null) {
				descriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			imageRegistry.put(key, descriptor);
			image = imageRegistry.get(key);
		}
		return image;
	}
}

Back to the top