Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b658f7f35a4ff7bf4e08f005c30ee10a4d12e26 (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
/*******************************************************************************
 *  Copyright (c) 2000, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.ui.util;

import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;
import org.eclipse.pde.internal.ui.PDEPluginImages;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class SharedLabelProvider extends LabelProvider implements ITableLabelProvider {
	public static final int F_ERROR = 1;
	public static final int F_WARNING = 2;
	public static final int F_EXPORT = 4;
	public static final int F_EDIT = 8;
	public static final int F_BINARY = 16;
	public static final int F_EXTERNAL = 32;
	public static final int F_JAVA = 64;
	public static final int F_JAR = 128;
	public static final int F_PROJECT = 256;
	public static final int F_OPTIONAL = 512;
	public static final int F_INTERNAL = 1024;
	public static final int F_FRIEND = 2048;
	Hashtable<Object, Image> images = new Hashtable<Object, Image>();
	ArrayList<Object> consumers = new ArrayList<Object>();
	private Image fBlankImage;

	public SharedLabelProvider() {

	}

	public void connect(Object consumer) {
		if (!consumers.contains(consumer))
			consumers.add(consumer);
	}

	public void disconnect(Object consumer) {
		consumers.remove(consumer);
		if (consumers.size() == 0) {
			dispose();
		}
	}

	public void dispose() {
		if (consumers.size() == 0) {
			for (Enumeration<Image> elements = images.elements(); elements.hasMoreElements();) {
				elements.nextElement().dispose();
			}
			images.clear();
			if (fBlankImage != null) {
				fBlankImage.dispose();
				fBlankImage = null;
			}
		}
	}

	public Image get(ImageDescriptor desc) {
		return get(desc, 0);
	}

	public Image get(ImageDescriptor desc, int flags) {
		Object key = desc;

		if (flags != 0) {
			key = getKey(desc.hashCode(), flags);
		}
		Image image = images.get(key);
		if (image == null) {
			image = createImage(desc, flags);
			images.put(key, image);
		}
		return image;
	}

	public Image get(Image image, int flags) {
		if (flags == 0)
			return image;
		String key = getKey(image.hashCode(), flags);
		Image resultImage = images.get(key);
		if (resultImage == null) {
			resultImage = createImage(image, flags);
			images.put(key, resultImage);
		}
		return resultImage;
	}

	private String getKey(long hashCode, int flags) {
		return ("" + hashCode) + ":" + flags; //$NON-NLS-1$ //$NON-NLS-2$
	}

	private Image createImage(ImageDescriptor baseDesc, int flags) {
		if (flags == 0) {
			return baseDesc.createImage();
		}
		ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
		ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
		ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
		ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
		OverlayIcon compDesc = new OverlayIcon(baseDesc, new ImageDescriptor[][] {upperRight, lowerRight, lowerLeft, upperLeft});
		return compDesc.createImage();
	}

	private Image createImage(Image baseImage, int flags) {
		if (flags == 0) {
			return baseImage;
		}
		ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
		ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
		ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
		ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
		ImageOverlayIcon compDesc = new ImageOverlayIcon(baseImage, new ImageDescriptor[][] {upperRight, lowerRight, lowerLeft, upperLeft});
		return compDesc.createImage();
	}

	private ImageDescriptor[] getLowerLeftOverlays(int flags) {
		if ((flags & F_ERROR) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_ERROR_CO};
		if ((flags & F_WARNING) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_WARNING_CO};
		return null;
	}

	private ImageDescriptor[] getUpperRightOverlays(int flags) {
		if ((flags & F_EXPORT) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_EXPORT_CO};
		if ((flags & F_EDIT) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_DOC_CO};
		if ((flags & F_JAVA) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_JAVA_CO};
		return null;
	}

	private ImageDescriptor[] getLowerRightOverlays(int flags) {
		if ((flags & F_JAR) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_JAR_CO};
		if ((flags & F_PROJECT) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_PROJECT_CO};
		if ((flags & F_OPTIONAL) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_OPTIONAL_CO};
		if ((flags & F_INTERNAL) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_INTERNAL_CO};
		if ((flags & F_FRIEND) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_FRIEND_CO};
		return null;
	}

	private ImageDescriptor[] getUpperLeftOverlays(int flags) {
		if ((flags & F_EXTERNAL) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_EXTERNAL_CO};
		if ((flags & F_BINARY) != 0)
			return new ImageDescriptor[] {PDEPluginImages.DESC_BINARY_CO};
		return null;
	}

	public String getColumnText(Object obj, int index) {
		return getText(obj);
	}

	public Image getColumnImage(Object obj, int index) {
		return getImage(obj);
	}

	public Image getImageFromPlugin(String bundleID, String path) {
		ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(bundleID, path);
		return (desc != null) ? get(desc) : getBlankImage();
	}

	public Image getImageFromPlugin(IPluginModelBase model, String relativePath) {
		String platform = "platform:/plugin/"; //$NON-NLS-1$
		if (relativePath.startsWith(platform)) {
			relativePath = relativePath.substring(platform.length());
			int index = relativePath.indexOf('/');
			if (index == -1)
				return null;
			model = PluginRegistry.findModel(relativePath.substring(0, index));
			if (model == null)
				return null;
			relativePath = relativePath.substring(index + 1);
		}

		String location = model.getInstallLocation();
		if (location == null)
			return null;

		File pluginLocation = new File(location);
		InputStream stream = null;
		ZipFile jarFile = null;
		try {
			if (pluginLocation.isDirectory()) {
				File file = new File(pluginLocation, relativePath);
				if (file.exists())
					stream = new FileInputStream(file);
				else if (relativePath.length() > 5 && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
					file = new File(pluginLocation, relativePath.substring(5));
					if (file.exists())
						stream = new FileInputStream(file);
				}
			} else {
				jarFile = new ZipFile(pluginLocation, ZipFile.OPEN_READ);
				ZipEntry manifestEntry = jarFile.getEntry(relativePath);
				if (manifestEntry != null) {
					stream = jarFile.getInputStream(manifestEntry);
				} else if (relativePath.length() > 5 && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
					manifestEntry = jarFile.getEntry(relativePath.substring(5));
					if (manifestEntry != null) {
						stream = jarFile.getInputStream(manifestEntry);
					}
				}
			}
			if (stream != null) {
				ImageDescriptor desc = ImageDescriptor.createFromImageData(new ImageData(stream));
				return get(desc);
			}
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} finally {
			try {
				if (stream != null)
					stream.close();
				if (jarFile != null)
					jarFile.close();
			} catch (IOException e) {
			}
		}
		return getBlankImage();
	}

	public Image getBlankImage() {
		if (fBlankImage == null)
			fBlankImage = ImageDescriptor.getMissingImageDescriptor().createImage();
		return fBlankImage;
	}
}

Back to the top