Skip to main content
summaryrefslogtreecommitdiffstats
blob: da6cb86fb9ca10105521e861cb532b215439a0b2 (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
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST 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:
 *   Vincent LORENZO (CEA-LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.nattable.properties.providers;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.ISpecializationType;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration;
import org.eclipse.papyrus.infra.types.ElementTypeSetConfiguration;
import org.eclipse.papyrus.infra.types.IconEntry;
import org.eclipse.papyrus.infra.types.MetamodelTypeConfiguration;
import org.eclipse.papyrus.infra.types.SpecializationTypeConfiguration;
import org.eclipse.papyrus.uml.nattable.properties.Activator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;

/**
 * Custom Label provider for {@link ElementTypeConfiguration}.
 * This label provider has been created to hide that manipulated object are not UML Element or Profile
 *
 */
public class GenericRelationshipMatrixElementTypeLabelProvider implements ILabelProvider {

	/**
	 * 2 of the possibles contants returned by {@link GenericRelationshipMatrixElementTypeContentProvider} as root of the tree
	 */
	private final String UML = "UML"; //$NON-NLS-1$

	private final String SYSML = "SysML"; //$NON-NLS-1$

	/**
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
	 *
	 * @param listener
	 */
	@Override
	public void addListener(final ILabelProviderListener listener) {
		// nothing to do
	}

	/**
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
	 *
	 */
	@Override
	public void dispose() {
		// nothing to do
	}

	/**
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
	 *
	 * @param element
	 * @param property
	 * @return
	 */
	@Override
	public boolean isLabelProperty(final Object element, final String property) {
		// nothing to do
		return false;
	}

	/**
	 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
	 *
	 * @param listener
	 */
	@Override
	public void removeListener(final ILabelProviderListener listener) {
		// nothing to do
	}

	/**
	 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
	 *
	 * @param element
	 * @return
	 */
	@Override
	public Image getImage(final Object element) {
		URL url = null;
		if (element instanceof String) {// currently we have 2 levels in the displayed tree and the first one is String
			if (UML.equals(element)) {
				Bundle bundle = Platform.getBundle("org.eclipse.papyrus.uml.architecture"); //$NON-NLS-1$
				
				//TODO get the icons defined for the EPackage if exist ????

				url = bundle.getEntry("icons/uml.gif"); //$NON-NLS-1$
			} else if (SYSML.equals(element)) {
				Bundle bundle = Platform.getBundle("org.eclipse.papyrus.sysml.architecture"); //$NON-NLS-1$
				
				//TODO get the icons defined for the EPackage if exist ????
				url = bundle.getEntry("icons/sysml.gif"); //$NON-NLS-1$
			}
		} else if (element instanceof ElementTypeConfiguration) {
			url = getIconURL((ElementTypeConfiguration) element);
			if (null == url) {
				IElementType elementType = null;

				if (element instanceof MetamodelTypeConfiguration) {
					elementType = ElementTypeRegistry.getInstance().getType(((MetamodelTypeConfiguration) element).getIdentifier());
				} else if (element instanceof SpecializationTypeConfiguration) {
					elementType = ElementTypeRegistry.getInstance().getType(((SpecializationTypeConfiguration) element).getIdentifier());
				}
				if (null != elementType) {
					url = getIconURL(elementType);
				}
			}
		}
		if (null != url) {
			final Image im = ExtendedImageRegistry.INSTANCE.getImage(url); // $NON-NLS-1$
			if (null == im) {
				Activator.log.warn(NLS.bind("The image located at {0} as not been found", url)); //$NON-NLS-1$
			}
			return im;
		} else if (element instanceof ElementTypeConfiguration && !(element instanceof String)) {// we don't have a clean way currently to define icon for string excepted hard-coding
			Activator.log.warn(NLS.bind("No icon defined for ", element)); //$NON-NLS-1$
		}
		return null;
	}

	/**
	 * Duplicated code from org.eclipse.papyrus.infra.gmfdiag.common.providers.ElementTypeIconProvider
	 * 
	 * @param elementType
	 *            an element type
	 * @return
	 * 		the url of the icon to {@link Display} or <code>null</code> if not found
	 */
	private URL getIconURL(final IElementType elementType) {
		URL result = elementType.getIconURL();

		if ((result == null) && (elementType instanceof ISpecializationType)) {
			ISpecializationType subtype = (ISpecializationType) elementType;
			IElementType[] supertypes = subtype.getSpecializedTypes();
			if (supertypes != null) {
				for (int i = 0; (result == null) && (i < supertypes.length); i++) {
					result = getIconURL(supertypes[i]);
				}
			}
		}

		return result;
	}

	/**
	 * Duplicated code from AbstractElementTypeConfigurationFactory
	 * 
	 * @param elementTypeConfiguration
	 *            an element type configuration
	 * @return
	 * 		the url of the icon to use or <code>null</code> when not found
	 */
	private URL getIconURL(final ElementTypeConfiguration elementTypeConfiguration) {
		// icon associated to the elementType (GUI)
		IconEntry entry = elementTypeConfiguration.getIconEntry();
		URL iconURL = null;
		if (entry != null) {
			iconURL = getURLFromEntry(entry);
		}
		return iconURL;
	}

	/**
	 * Duplicated code from AbstractElementTypeConfigurationFactory
	 * 
	 * @param entry
	 *            an icon entry
	 * @return
	 * 		the url of the icon or <code>null</code> if not found
	 * 
	 */
	private URL getURLFromEntry(final IconEntry entry) {
		Bundle bundle = Platform.getBundle(entry.getBundleId());
		if (bundle == null) {
			Activator.log.warn(NLS.bind("Bundle {0} doesn't exist. I cannot found the expected icon {1}.", entry.getBundleId(), entry.getIconPath())); //$NON-NLS-1$
			return null;
		}
		URL result = bundle.getEntry(entry.getIconPath());
		if (result == null) {
			try {
				result = new URL(entry.getIconPath());
			} catch (MalformedURLException e) {
				Activator.log.error(NLS.bind("The icon path {0} seems invalid, I can't found this icon in the bundle {1}.", entry.getIconPath(), entry.getBundleId()), e); //$NON-NLS-1$
				result = null;
			}
		}
		return result;
	}


	/**
	 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
	 *
	 * @param element
	 * @return
	 */
	@Override
	public String getText(final Object element) {
		String returnedName = ""; //$NON-NLS-1$
		if (null != element) {
			returnedName = element.toString();
			if (element instanceof ElementTypeSetConfiguration) {
				returnedName = ((ElementTypeSetConfiguration) element).getName();
			}
			if (element instanceof ElementTypeConfiguration) {
				returnedName = ProviderUtils.getNameToDisplay((ElementTypeConfiguration) element);
			}
		}
		return returnedName;
	}

}

Back to the top