Skip to main content
summaryrefslogtreecommitdiffstats
blob: b1e1c3160c6b1c91376c1f1c19f1ce0b6ef7dc6a (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
package org.eclipse.emf.editor.provider;

/**
 * <copyright> 
 *
 * Copyright (c) 2008 itemis AG 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: 
 *   itemis AG - Initial API and implementation
 *
 * </copyright>
 *
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.editor.EEPlugin;
import org.eclipse.emf.editor.extxpt.ExtXptFacade;
import org.eclipse.emf.editor.ui.ImageRegistry;

/**
 * @author Dennis Huebner
 * 
 */
public class ExtendedLabelProvider implements IItemLabelProvider {

	private static final String ICONS_FOLDER = "icons";
	private static final String ICON_EXTENSION_NAME = "icon";
	private static final String LABEL_EXTENSION_NAME = "label";
	private final ExtXptFacade facade;
	private IItemLabelProvider registryItemLabelProvider;

	public ExtendedLabelProvider(ExtXptFacade facade) {
		this.facade = facade;
		this.registryItemLabelProvider = new RegistryItemLabelProvider();
	}

	public Object getImage(Object element) {
		Object retVal = null;
		try {
			if (element instanceof EObject) {
				EObject eObject = (EObject) element;
				String iconName = evaluate(eObject, ICON_EXTENSION_NAME);
				Resource eResource = eObject.eClass().eResource();
				// TODO try instance scope
				// retVal = locateImage(iconName, eObject.eResource(), eObject);
				// if not found try metamodel scope
				if (retVal == null && iconName != null)
					retVal = locateImage(iconName, eResource, eObject);
			}
		} catch (Throwable ex) {
			EEPlugin.logError("ERROR", ex);
		}
		if (retVal != null)
			return retVal;
		// Fallback: Ask registry for image
		return registryItemLabelProvider.getImage(element);

	}

	/**
	 * @param iconName
	 * @param eResource
	 * @param eObject
	 * @return
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	private Object locateImage(String iconName, Resource eResource,
			EObject eObject) throws MalformedURLException, IOException {
		// TODO understand the requirements :)
		Object retVal = null;
		if (eResource != null) {
			URI metaModelURI = eResource.getURI();
			URI iconURI = createIconURI(eObject, iconName, metaModelURI);

			if (metaModelURI.isPlatform()) {
				// platform resource... good
				String platformString = metaModelURI.toPlatformString(true);

				IFile metaModelFile = ResourcesPlugin.getWorkspace().getRoot()
						.getFile(new Path(platformString));
				if (metaModelFile != null) {
					// using IFile to allow resource change listening
					IFile f = metaModelFile.getProject()
							.getFile(iconURI.path());
					if (f != null && f.exists()) {
						// ask registry
						retVal = ImageRegistry.getDefault().getImage(f);
					}
				}
			} else if (metaModelURI.isArchive()) {// archived
				// handle archived resources
				// return URI. Image will be stored in
				// ExtendedImageRegistry
				// which is not refreshable
				if (checkAccessable(iconURI))
					retVal = iconURI;
			} else {
				// eResource not present physically... bad
				URI classpathURI = URI
						.createURI(ClasspathUriResolver.CLASSPATH_SCHEME + ":"
								+ iconURI.path());
				URI iconNormalizedURI = new ClasspathUriResolver().resolve(
						facade.getProject(), classpathURI);
				// return URI. Image will be stored in
				// ExtendedImageRegistry
				// which is not refreshable
				if (!ClasspathUriResolver.isClassapthUri(iconNormalizedURI)
						&& checkAccessable(iconNormalizedURI))
					retVal = iconNormalizedURI;
			}
		}
		return retVal;
	}

	private boolean checkAccessable(URI iconURI) throws IOException {
		URL url = new URL(iconURI.toString());
		if (url != null) {
			try {
				InputStream in = url.openStream();// check readable
				in.close();
				return true;
			} catch (FileNotFoundException e) {
				// ignore no feedback to user
			}
		}
		return false;
	}

	/**
	 * @param element
	 * @param iconName
	 * @param metaModelURI
	 * @return
	 */
	private URI createIconURI(EObject eObject, String iconName, URI metaModelURI) {
		String packageName = packageName(eObject);
		return metaModelURI.trimSegments(metaModelURI.segmentCount())
				.appendSegment(ICONS_FOLDER).appendSegment(packageName)
				.appendSegment(iconName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.edit.provider.IItemLabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object element) {
		String text = evaluate(element, LABEL_EXTENSION_NAME);
		return text;
	}

	/**
	 * @param element
	 * @param retVal
	 * @return
	 */
	private String evaluate(Object element, String name) {
		String retVal = null;
		if (element instanceof EObject && facade != null) {
			EObject eO = (EObject) element;
			retVal = (String) facade.style(name, eO);
		}
		return retVal;
	}

	/**
	 * @param object
	 * @return
	 */
	private String packageName(EObject object) {
		return object.eClass().getEPackage().getName();
	}

}

Back to the top