1 11 package org.eclipse.pde.internal.ui.util; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileNotFoundException ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.ArrayList ; 19 import java.util.Enumeration ; 20 import java.util.Hashtable ; 21 import java.util.zip.ZipEntry ; 22 import java.util.zip.ZipFile ; 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 images = new Hashtable (); 47 ArrayList consumers = new ArrayList (); 48 private Image fBlankImage; 49 50 public SharedLabelProvider() { 51 52 } 53 public void connect(Object consumer) { 54 if (!consumers.contains(consumer)) 55 consumers.add(consumer); 56 } 57 public void disconnect(Object 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 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 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 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 getKey(long hashCode, int flags) { 106 return (""+hashCode) + ":"+flags; } 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 getColumnText(Object obj, int index) { 174 return getText(obj); 175 } 176 public Image getColumnImage(Object obj, int index) { 177 return getImage(obj); 178 } 179 180 public Image getImageFromPlugin(String bundleID, String 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 relativePath) { 187 String platform = "platform:/plugin/"; 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 location = model.getInstallLocation(); 200 if (location == null) 201 return null; 202 203 File pluginLocation = new File (location); 204 InputStream stream = null; 205 ZipFile jarFile = null; 206 try { 207 if (pluginLocation.isDirectory()) { 208 File file = new File (pluginLocation, relativePath); 209 if (file.exists()) 210 stream = new FileInputStream (file); 211 else if (relativePath.length() > 5 212 && relativePath.startsWith("$nl$/")) { file = new File (pluginLocation, relativePath.substring(5)); 214 if (file.exists()) 215 stream = new FileInputStream (file); 216 } 217 } else { 218 jarFile = new ZipFile (pluginLocation, ZipFile.OPEN_READ); 219 ZipEntry manifestEntry = jarFile.getEntry(relativePath); 220 if (manifestEntry != null) { 221 stream = jarFile.getInputStream(manifestEntry); 222 } else if (relativePath.length() > 5 223 && relativePath.startsWith("$nl$/")) { 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 e) { 235 } catch (IOException e) { 236 } finally { 237 try { 238 if (stream != null) 239 stream.close(); 240 if (jarFile != null) 241 jarFile.close(); 242 } catch (IOException 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 |