Skip to main content
summaryrefslogtreecommitdiffstats
blob: b625cf5c1aced98edc7a5b74bb6a97c41dcb5ea3 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
 * Copyright (c) 2014 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.uml2.internal.provider.decorator;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.provider.ExtendedItemProviderDecorator;
import org.eclipse.emf.compare.uml2.internal.provider.UMLCompareEditPlugin;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.IChangeNotifier;
import org.eclipse.emf.edit.provider.IChildCreationExtender;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemColorProvider;
import org.eclipse.emf.edit.provider.IItemFontProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IItemProviderDecorator;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ReflectiveItemProvider;
import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Item provider decorator for stereotyped {@link Element}s.
 * 
 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
 */
public class StereotypedElementItemProviderDecorator extends ExtendedItemProviderDecorator implements IEditingDomainItemProvider, ITreeItemContentProvider, IItemLabelProvider, IItemPropertySource, IItemColorProvider, IItemFontProvider {

	/**
	 * Constructor.
	 * 
	 * @param adapterFactory
	 *            {@link ComposeableAdapterFactory}.
	 */
	public StereotypedElementItemProviderDecorator(ComposeableAdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	@Override
	public String getText(Object object) {
		if (object instanceof Element) {
			Element elem = (Element)object;
			EList<Stereotype> appliedStereotypes = elem.getAppliedStereotypes();
			if (!appliedStereotypes.isEmpty()) {
				return getStereotypedElementLabel((Element)object, appliedStereotypes);
			}
		}
		return super.getText(object);
	}

	/**
	 * Gets a label for a stereotyped {@link Element}.
	 * 
	 * @param element
	 *            stereotyped {@link Element}.
	 * @param appliedStereotypes
	 *            List of stereotypes to display.
	 * @return the label of the stereotyped element.
	 */
	private String getStereotypedElementLabel(Element element, EList<Stereotype> appliedStereotypes) {
		StringBuilder labelBuilder = new StringBuilder();
		String stereotypes = Joiner.on(',')
				.join(Iterables.transform(appliedStereotypes, new Function<Stereotype, String>() {

					public String apply(Stereotype input) {
						return input.getName();
					}
				}));

		labelBuilder.append('<').append(stereotypes).append("> "); //$NON-NLS-1$
		if (element instanceof NamedElement) {
			NamedElement namedElement = (NamedElement)element;
			labelBuilder.append(namedElement.getName());
		}
		return labelBuilder.toString();
	}

	@Override
	public Object getImage(Object object) {
		if (object instanceof Element && !((Element)object).getAppliedStereotypes().isEmpty()) {
			return new ComposedImage(getStereotypeIcons((Element)object, super.getImage(object)));
		}
		return super.getImage(object);
	}

	/**
	 * Gets all icons from the stereotypes applied on the element.
	 * <p>
	 * The icons for a stereotype are computed this way:
	 * </p>
	 * <ol>
	 * <li>IF:There is {@link org.eclipse.uml2.uml.Image} in the profile model then computes the icon from its
	 * location. (warning: currently the feature "content" of a {@link org.eclipse.uml2.uml.Image} is not used
	 * to get a icon)</li>
	 * <li>ELSEIF: There is ItemProvider registered into the platform for the stereotype application use it.
	 * </li>
	 * <li>ELSE: Uses the base element icon.</li>
	 * </ol>
	 * <p>
	 * This method has been inspired from the implementation of
	 * {@link org.eclipse.uml2.uml.edit.providers.ElementItemProvider#overlayImage(Object object, Object image)}
	 * </p>
	 * 
	 * @param element
	 *            base {@link Element}.
	 * @param baseElementIcon
	 *            Base element icon.
	 * @return List of icon to use
	 */
	protected List<Object> getStereotypeIcons(Element element, Object baseElementIcon) {
		List<Object> images = null;
		Iterator<Stereotype> stereotypeIterator = element.getAppliedStereotypes().iterator();
		while (stereotypeIterator.hasNext() && images == null) {
			Stereotype appliedStereotype = stereotypeIterator.next();
			if (!appliedStereotype.getIcons().isEmpty()) {
				images = getStereotypeIconsFromProfile(appliedStereotype);
			} else {
				Object img = getStereotypeIconFromItemProvider(appliedStereotype, element);
				if (img != null) {
					images = Collections.singletonList(img);
				}
			}
		}
		if (images == null) {
			// If there is no icon for any stereotypes then uses the base element icon.
			images = Collections.singletonList(baseElementIcon);
		}

		return images;
	}

	/**
	 * Get the icon of a stereotype using registered item providers.
	 * 
	 * @param appliedStereotype
	 *            Applied stereotype from which you want to retrieve an icon.
	 * @param element
	 *            Base UML element.
	 * @return The icon of the stereotype or <code>null</code> if none.
	 */
	private Object getStereotypeIconFromItemProvider(Stereotype appliedStereotype, Element element) {
		EObject steretotypeApplication = element.getStereotypeApplication(appliedStereotype);
		return getStereotypeIconFromItemProvider(steretotypeApplication);
	}

	/**
	 * Gets the icons for a stereotype using the UML profile model.
	 * 
	 * @param appliedStereotype
	 *            Applied stereotyped from which you want to retrieve icons.
	 * @return List of icons to use for this stereotype or <code>null</code> if no icon has been found.
	 * @see Stereotype#getIcons()
	 */
	protected List<Object> getStereotypeIconsFromProfile(Stereotype appliedStereotype) {
		List<Object> images = new ArrayList<Object>();

		for (org.eclipse.uml2.uml.Image icon : appliedStereotype.getIcons()) {
			String location = icon.getLocation();

			if (!UML2Util.isEmpty(location)) {
				Object img = getIconFromLocation(icon.eResource(), location);
				if (img != null) {
					images.add(img);
				}
			} else {
				// TODO handle icons defined by the "content" feature...
			}
		}
		if (images.isEmpty()) {
			images = null;
		}
		return images;
	}

	/**
	 * Gets an icon from its location.
	 * 
	 * @param eResource
	 *            Resource holding the {@link org.eclipse.uml2.uml.Image} element.
	 * @param location
	 *            Location of the icon
	 * @return an icon or <code>null</code> otherwise.
	 */
	protected Object getIconFromLocation(Resource eResource, String location) {
		Object img = null;
		if (eResource != null) {
			ResourceSet resourceSet = eResource.getResourceSet();

			if (resourceSet != null) {
				URIConverter uriConverter = resourceSet.getURIConverter();
				URI normalizedURI = uriConverter.normalize(eResource.getURI());

				URI uri = URI.createURI(location).resolve(normalizedURI);

				URL url;
				try {
					url = new URL(uriConverter.normalize(uri).toString());
					url.openStream().close();
					img = url;
				} catch (IOException e) {
					UMLCompareEditPlugin.getPlugin().getLog()
							.log(new Status(IStatus.WARNING, "org.eclipse.emf.compare.uml2.edit", //$NON-NLS-1$
									UMLCompareEditPlugin.INSTANCE.getString(
											"Unable_To_Retreive_Icon_Error_Message", //$NON-NLS-1$
											new Object[] {location }),
									e));
				}
			}
		}
		return img;
	}

	/**
	 * Retrieves the icon from registered item providers.
	 * <p>
	 * This implementation remove the {@link ReflectiveItemProvider} to prevent getting the basic EMF icon.
	 * </p>
	 * </p>
	 * 
	 * @param object
	 *            Input.
	 * @return The icon of the input object or <code>null</code> if no icon has been found.
	 */
	public Object getStereotypeIconFromItemProvider(Object object) {
		/*
		 * Use the root adapter factory to retrieve the adapter factory of the static profiles. This only
		 * works if the EMF.edit code has been generated.
		 */
		ComposeableAdapterFactory rootAdapterFactory = ((ComposeableAdapterFactory)adapterFactory)
				.getRootAdapterFactory();
		IItemLabelProvider itemLabelProvider = (IItemLabelProvider)rootAdapterFactory.adapt(object,
				IItemLabelProvider.class);
		if (itemLabelProvider instanceof IItemProviderDecorator) {
			IChangeNotifier cNotifier = ((IItemProviderDecorator)itemLabelProvider)
					.getDecoratedItemProvider();
			if (cNotifier instanceof IItemLabelProvider) {
				itemLabelProvider = (IItemLabelProvider)cNotifier;
			}

		}
		if (itemLabelProvider == null || itemLabelProvider instanceof ReflectiveItemProvider) {
			return null;
		} else {
			return itemLabelProvider.getImage(object);
		}

	}

	public ResourceLocator getResourceLocator() {
		return ((IChildCreationExtender)adapterFactory).getResourceLocator();
	}

}

Back to the top