KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > SharedLabelProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.util;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.zip.ZipEntry JavaDoc;
22 import java.util.zip.ZipFile JavaDoc;
23
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.viewers.ITableLabelProvider;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.pde.core.plugin.IPluginModelBase;
28 import org.eclipse.pde.core.plugin.PluginRegistry;
29 import org.eclipse.pde.internal.ui.PDEPluginImages;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.graphics.ImageData;
32 import org.eclipse.ui.plugin.AbstractUIPlugin;
33
34 public class SharedLabelProvider
35     extends LabelProvider
36     implements ITableLabelProvider {
37     public static final int F_ERROR = 1;
38     public static final int F_WARNING = 2;
39     public static final int F_EXPORT = 4;
40     public static final int F_EDIT = 8;
41     public static final int F_BINARY = 16;
42     public static final int F_EXTERNAL = 32;
43     public static final int F_JAVA = 64;
44     public static final int F_JAR = 128;
45     public static final int F_PROJECT = 256;
46     Hashtable JavaDoc images = new Hashtable JavaDoc();
47     ArrayList JavaDoc consumers = new ArrayList JavaDoc();
48     private Image fBlankImage;
49
50     public SharedLabelProvider() {
51
52     }
53     public void connect(Object JavaDoc consumer) {
54         if (!consumers.contains(consumer))
55             consumers.add(consumer);
56     }
57     public void disconnect(Object JavaDoc consumer) {
58         consumers.remove(consumer);
59         if (consumers.size() == 0) {
60             dispose();
61         }
62     }
63     public void dispose() {
64         if (consumers.size() == 0) {
65             for (Enumeration JavaDoc elements = images.elements(); elements.hasMoreElements();) {
66                 ((Image)elements.nextElement()).dispose();
67             }
68             images.clear();
69             if (fBlankImage != null) {
70                 fBlankImage.dispose();
71                 fBlankImage = null;
72             }
73         }
74     }
75     
76     public Image get(ImageDescriptor desc) {
77         return get(desc, 0);
78     }
79
80     public Image get(ImageDescriptor desc, int flags) {
81         Object JavaDoc key = desc;
82
83         if (flags != 0) {
84             key = getKey(desc.hashCode(), flags);
85         }
86         Image image = (Image) images.get(key);
87         if (image == null) {
88             image = createImage(desc, flags);
89             images.put(key, image);
90         }
91         return image;
92     }
93     
94     public Image get(Image image, int flags) {
95         if (flags==0) return image;
96         String JavaDoc key = getKey(image.hashCode(), flags);
97         Image resultImage = (Image)images.get(key);
98         if (resultImage == null) {
99             resultImage = createImage(image, flags);
100             images.put(key, resultImage);
101         }
102         return resultImage;
103     }
104
105     private String JavaDoc getKey(long hashCode, int flags) {
106         return (""+hashCode) + ":"+flags; //$NON-NLS-1$ //$NON-NLS-2$
107
}
108
109     private Image createImage(ImageDescriptor baseDesc, int flags) {
110         if (flags == 0) {
111             return baseDesc.createImage();
112         }
113         ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
114         ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
115         ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
116         ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
117         OverlayIcon compDesc =
118             new OverlayIcon(
119                 baseDesc,
120                 new ImageDescriptor[][] { upperRight, lowerRight, lowerLeft, upperLeft });
121         return compDesc.createImage();
122     }
123     
124     private Image createImage(Image baseImage, int flags) {
125         if (flags == 0) {
126             return baseImage;
127         }
128         ImageDescriptor[] lowerLeft = getLowerLeftOverlays(flags);
129         ImageDescriptor[] upperRight = getUpperRightOverlays(flags);
130         ImageDescriptor[] lowerRight = getLowerRightOverlays(flags);
131         ImageDescriptor[] upperLeft = getUpperLeftOverlays(flags);
132         ImageOverlayIcon compDesc =
133             new ImageOverlayIcon(
134                 baseImage,
135                 new ImageDescriptor[][] { upperRight, lowerRight, lowerLeft, upperLeft });
136         return compDesc.createImage();
137     }
138
139     private ImageDescriptor[] getLowerLeftOverlays(int flags) {
140         if ((flags & F_ERROR) != 0)
141             return new ImageDescriptor[] { PDEPluginImages.DESC_ERROR_CO };
142         if ((flags & F_WARNING) != 0)
143             return new ImageDescriptor[] { PDEPluginImages.DESC_WARNING_CO };
144         return null;
145     }
146
147     private ImageDescriptor[] getUpperRightOverlays(int flags) {
148         if ((flags & F_EXPORT) != 0)
149             return new ImageDescriptor[] { PDEPluginImages.DESC_EXPORT_CO };
150         if ((flags & F_EDIT) != 0)
151             return new ImageDescriptor[] { PDEPluginImages.DESC_DOC_CO };
152         if ((flags & F_JAVA) != 0)
153             return new ImageDescriptor[] { PDEPluginImages.DESC_JAVA_CO };
154         return null;
155     }
156     
157     private ImageDescriptor[] getLowerRightOverlays(int flags) {
158         if ((flags & F_JAR) != 0)
159             return new ImageDescriptor[] { PDEPluginImages.DESC_JAR_CO };
160         if ((flags & F_PROJECT) != 0)
161             return new ImageDescriptor[] { PDEPluginImages.DESC_PROJECT_CO };
162         return null;
163     }
164     
165     private ImageDescriptor[] getUpperLeftOverlays(int flags) {
166         if ((flags & F_EXTERNAL) != 0)
167             return new ImageDescriptor[] { PDEPluginImages.DESC_EXTERNAL_CO };
168         if ((flags & F_BINARY) != 0)
169             return new ImageDescriptor[] { PDEPluginImages.DESC_BINARY_CO };
170         return null;
171     }
172
173     public String JavaDoc getColumnText(Object JavaDoc obj, int index) {
174         return getText(obj);
175     }
176     public Image getColumnImage(Object JavaDoc obj, int index) {
177         return getImage(obj);
178     }
179
180     public Image getImageFromPlugin(String JavaDoc bundleID, String JavaDoc path) {
181         ImageDescriptor desc = AbstractUIPlugin.imageDescriptorFromPlugin(bundleID, path);
182         return (desc != null) ? get(desc) : getBlankImage();
183     }
184     
185
186     public Image getImageFromPlugin(IPluginModelBase model, String JavaDoc relativePath) {
187         String JavaDoc platform = "platform:/plugin/"; //$NON-NLS-1$
188
if (relativePath.startsWith(platform)) {
189             relativePath = relativePath.substring(platform.length());
190             int index = relativePath.indexOf('/');
191             if (index == -1)
192                 return null;
193             model = PluginRegistry.findModel(relativePath.substring(0, index));
194             if (model == null)
195                 return null;
196             relativePath = relativePath.substring(index + 1);
197         }
198         
199         String JavaDoc location = model.getInstallLocation();
200         if (location == null)
201             return null;
202         
203         File JavaDoc pluginLocation = new File JavaDoc(location);
204         InputStream JavaDoc stream = null;
205         ZipFile JavaDoc jarFile = null;
206         try {
207             if (pluginLocation.isDirectory()) {
208                 File JavaDoc file = new File JavaDoc(pluginLocation, relativePath);
209                 if (file.exists())
210                     stream = new FileInputStream JavaDoc(file);
211                 else if (relativePath.length() > 5
212                         && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
213
file = new File JavaDoc(pluginLocation, relativePath.substring(5));
214                     if (file.exists())
215                         stream = new FileInputStream JavaDoc(file);
216                 }
217             } else {
218                     jarFile = new ZipFile JavaDoc(pluginLocation, ZipFile.OPEN_READ);
219                     ZipEntry JavaDoc manifestEntry = jarFile.getEntry(relativePath);
220                     if (manifestEntry != null) {
221                         stream = jarFile.getInputStream(manifestEntry);
222                     } else if (relativePath.length() > 5
223                         && relativePath.startsWith("$nl$/")) { //$NON-NLS-1$
224
manifestEntry = jarFile.getEntry(relativePath.substring(5));
225                     if (manifestEntry != null) {
226                         stream = jarFile.getInputStream(manifestEntry);
227                     }
228                 }
229             }
230             if (stream != null) {
231                 ImageDescriptor desc = ImageDescriptor.createFromImageData(new ImageData(stream));
232                 return get(desc);
233             }
234         } catch (FileNotFoundException JavaDoc e) {
235         } catch (IOException JavaDoc e) {
236         } finally {
237             try {
238                 if (stream != null)
239                     stream.close();
240                 if (jarFile != null)
241                     jarFile.close();
242             } catch (IOException JavaDoc e) {
243             }
244         }
245         return getBlankImage();
246     }
247
248     public Image getBlankImage() {
249         if (fBlankImage == null)
250             fBlankImage = ImageDescriptor.getMissingImageDescriptor().createImage();
251         return fBlankImage;
252     }
253 }
254
Popular Tags