001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.util;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.util.ArrayList;
018: import java.util.Enumeration;
019: import java.util.Hashtable;
020: import java.util.zip.ZipEntry;
021: import java.util.zip.ZipFile;
022:
023: import org.eclipse.jface.resource.ImageDescriptor;
024: import org.eclipse.jface.viewers.ITableLabelProvider;
025: import org.eclipse.jface.viewers.LabelProvider;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.core.plugin.PluginRegistry;
028: import org.eclipse.pde.internal.ui.PDEPluginImages;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.swt.graphics.ImageData;
031: import org.eclipse.ui.plugin.AbstractUIPlugin;
032:
033: public class SharedLabelProvider extends LabelProvider implements
034: ITableLabelProvider {
035: public static final int F_ERROR = 1;
036: public static final int F_WARNING = 2;
037: public static final int F_EXPORT = 4;
038: public static final int F_EDIT = 8;
039: public static final int F_BINARY = 16;
040: public static final int F_EXTERNAL = 32;
041: public static final int F_JAVA = 64;
042: public static final int F_JAR = 128;
043: public static final int F_PROJECT = 256;
044: Hashtable images = new Hashtable();
045: ArrayList consumers = new ArrayList();
046: private Image fBlankImage;
047:
048: public SharedLabelProvider() {
049:
050: }
051:
052: public void connect(Object consumer) {
053: if (!consumers.contains(consumer))
054: consumers.add(consumer);
055: }
056:
057: public void disconnect(Object consumer) {
058: consumers.remove(consumer);
059: if (consumers.size() == 0) {
060: dispose();
061: }
062: }
063:
064: public void dispose() {
065: if (consumers.size() == 0) {
066: for (Enumeration elements = images.elements(); elements
067: .hasMoreElements();) {
068: ((Image) elements.nextElement()).dispose();
069: }
070: images.clear();
071: if (fBlankImage != null) {
072: fBlankImage.dispose();
073: fBlankImage = null;
074: }
075: }
076: }
077:
078: public Image get(ImageDescriptor desc) {
079: return get(desc, 0);
080: }
081:
082: public Image get(ImageDescriptor desc, int flags) {
083: Object key = desc;
084:
085: if (flags != 0) {
086: key = getKey(desc.hashCode(), flags);
087: }
088: Image image = (Image) images.get(key);
089: if (image == null) {
090: image = createImage(desc, flags);
091: images.put(key, image);
092: }
093: return image;
094: }
095:
096: public Image get(Image image, int flags) {
097: if (flags == 0)
098: return image;
099: String key = getKey(image.hashCode(), flags);
100: Image resultImage = (Image) images.get(key);
101: if (resultImage == null) {
102: resultImage = createImage(image, flags);
103: images.put(key, resultImage);
104: }
105: return resultImage;
106: }
107:
108: private String getKey(long hashCode, int flags) {
109: return ("" + hashCode) + ":" + flags; //$NON-NLS-1$ //$NON-NLS-2$
110: }
111:
112: private Image createImage(ImageDescriptor baseDesc, int flags) {
113: if (flags == 0) {
114: return baseDesc.createImage();
115: }
116: ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
117: ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
118: ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
119: ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
120: OverlayIcon compDesc = new OverlayIcon(baseDesc,
121: new ImageDescriptor[][] { upperRight, lowerRight,
122: lowerLeft, upperLeft });
123: return compDesc.createImage();
124: }
125:
126: private Image createImage(Image baseImage, int flags) {
127: if (flags == 0) {
128: return baseImage;
129: }
130: ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
131: ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
132: ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
133: ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
134: ImageOverlayIcon compDesc = new ImageOverlayIcon(baseImage,
135: new ImageDescriptor[][] { upperRight, lowerRight,
136: lowerLeft, upperLeft });
137: return compDesc.createImage();
138: }
139:
140: private ImageDescriptor[] getLowerLeftOverlays(int flags) {
141: if ((flags & F_ERROR) != 0)
142: return new ImageDescriptor[] { PDEPluginImages.DESC_ERROR_CO };
143: if ((flags & F_WARNING) != 0)
144: return new ImageDescriptor[] { PDEPluginImages.DESC_WARNING_CO };
145: return null;
146: }
147:
148: private ImageDescriptor[] getUpperRightOverlays(int flags) {
149: if ((flags & F_EXPORT) != 0)
150: return new ImageDescriptor[] { PDEPluginImages.DESC_EXPORT_CO };
151: if ((flags & F_EDIT) != 0)
152: return new ImageDescriptor[] { PDEPluginImages.DESC_DOC_CO };
153: if ((flags & F_JAVA) != 0)
154: return new ImageDescriptor[] { PDEPluginImages.DESC_JAVA_CO };
155: return null;
156: }
157:
158: private ImageDescriptor[] getLowerRightOverlays(int flags) {
159: if ((flags & F_JAR) != 0)
160: return new ImageDescriptor[] { PDEPluginImages.DESC_JAR_CO };
161: if ((flags & F_PROJECT) != 0)
162: return new ImageDescriptor[] { PDEPluginImages.DESC_PROJECT_CO };
163: return null;
164: }
165:
166: private ImageDescriptor[] getUpperLeftOverlays(int flags) {
167: if ((flags & F_EXTERNAL) != 0)
168: return new ImageDescriptor[] { PDEPluginImages.DESC_EXTERNAL_CO };
169: if ((flags & F_BINARY) != 0)
170: return new ImageDescriptor[] { PDEPluginImages.DESC_BINARY_CO };
171: return null;
172: }
173:
174: public String getColumnText(Object obj, int index) {
175: return getText(obj);
176: }
177:
178: public Image getColumnImage(Object obj, int index) {
179: return getImage(obj);
180: }
181:
182: public Image getImageFromPlugin(String bundleID, String path) {
183: ImageDescriptor desc = AbstractUIPlugin
184: .imageDescriptorFromPlugin(bundleID, path);
185: return (desc != null) ? get(desc) : getBlankImage();
186: }
187:
188: public Image getImageFromPlugin(IPluginModelBase model,
189: String relativePath) {
190: String platform = "platform:/plugin/"; //$NON-NLS-1$
191: if (relativePath.startsWith(platform)) {
192: relativePath = relativePath.substring(platform.length());
193: int index = relativePath.indexOf('/');
194: if (index == -1)
195: return null;
196: model = PluginRegistry.findModel(relativePath.substring(0,
197: index));
198: if (model == null)
199: return null;
200: relativePath = relativePath.substring(index + 1);
201: }
202:
203: String location = model.getInstallLocation();
204: if (location == null)
205: return null;
206:
207: File pluginLocation = new File(location);
208: InputStream stream = null;
209: ZipFile jarFile = null;
210: try {
211: if (pluginLocation.isDirectory()) {
212: File file = new File(pluginLocation, relativePath);
213: if (file.exists())
214: stream = new FileInputStream(file);
215: else if (relativePath.length() > 5
216: && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
217: file = new File(pluginLocation, relativePath
218: .substring(5));
219: if (file.exists())
220: stream = new FileInputStream(file);
221: }
222: } else {
223: jarFile = new ZipFile(pluginLocation, ZipFile.OPEN_READ);
224: ZipEntry manifestEntry = jarFile.getEntry(relativePath);
225: if (manifestEntry != null) {
226: stream = jarFile.getInputStream(manifestEntry);
227: } else if (relativePath.length() > 5
228: && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
229: manifestEntry = jarFile.getEntry(relativePath
230: .substring(5));
231: if (manifestEntry != null) {
232: stream = jarFile.getInputStream(manifestEntry);
233: }
234: }
235: }
236: if (stream != null) {
237: ImageDescriptor desc = ImageDescriptor
238: .createFromImageData(new ImageData(stream));
239: return get(desc);
240: }
241: } catch (FileNotFoundException e) {
242: } catch (IOException e) {
243: } finally {
244: try {
245: if (stream != null)
246: stream.close();
247: if (jarFile != null)
248: jarFile.close();
249: } catch (IOException e) {
250: }
251: }
252: return getBlankImage();
253: }
254:
255: public Image getBlankImage() {
256: if (fBlankImage == null)
257: fBlankImage = ImageDescriptor.getMissingImageDescriptor()
258: .createImage();
259: return fBlankImage;
260: }
261: }
|