1 11 package org.eclipse.jface.resource; 12 13 import java.net.URL ; 14 15 import org.eclipse.swt.SWTException; 16 import org.eclipse.swt.graphics.Device; 17 import org.eclipse.swt.graphics.Image; 18 import org.eclipse.swt.graphics.ImageData; 19 import org.eclipse.swt.graphics.PaletteData; 20 import org.eclipse.swt.graphics.RGB; 21 import org.eclipse.swt.widgets.Display; 22 23 52 public abstract class ImageDescriptor extends DeviceResourceDescriptor { 53 54 58 protected static final ImageData DEFAULT_IMAGE_DATA = new ImageData(6, 6, 59 1, new PaletteData(new RGB[] { new RGB(255, 0, 0) })); 60 61 64 protected ImageDescriptor() { 65 } 67 68 77 public static ImageDescriptor createFromFile(Class location, String filename) { 78 return new FileImageDescriptor(location, filename); 79 } 80 81 90 public static ImageDescriptor createFromImageData(ImageData data) { 91 return new ImageDataImageDescriptor(data); 92 } 93 94 103 public static ImageDescriptor createFromImage(Image img) { 104 return new ImageDataImageDescriptor(img); 105 } 106 107 126 public static ImageDescriptor createWithFlags(ImageDescriptor originalImage, int swtFlags) { 127 return new DerivedImageDescriptor(originalImage, swtFlags); 128 } 129 130 144 public static ImageDescriptor createFromImage(Image img, Device theDevice) { 145 return new ImageDataImageDescriptor(img); 146 } 147 148 154 public static ImageDescriptor createFromURL(URL url) { 155 if (url == null) { 156 return getMissingImageDescriptor(); 157 } 158 return new URLImageDescriptor(url); 159 } 160 161 164 public Object createResource(Device device) throws DeviceResourceException { 165 Image result = createImage(false, device); 166 if (result == null) { 167 throw new DeviceResourceException(this); 168 } 169 return result; 170 } 171 172 175 public void destroyResource(Object previouslyCreatedObject) { 176 ((Image)previouslyCreatedObject).dispose(); 177 } 178 179 204 public Image createImage() { 205 return createImage(true); 206 } 207 208 226 public Image createImage(boolean returnMissingImageOnError) { 227 return createImage(returnMissingImageOnError, Display.getCurrent()); 228 } 229 230 246 public Image createImage(Device device) { 247 return createImage(true, device); 248 } 249 250 271 public Image createImage(boolean returnMissingImageOnError, Device device) { 272 273 ImageData data = getImageData(); 274 if (data == null) { 275 if (!returnMissingImageOnError) { 276 return null; 277 } 278 data = DEFAULT_IMAGE_DATA; 279 } 280 281 285 286 try { 287 if (data.transparentPixel >= 0) { 288 ImageData maskData = data.getTransparencyMask(); 289 return new Image(device, data, maskData); 290 } 291 return new Image(device, data); 292 } catch (SWTException exception) { 293 if (returnMissingImageOnError) { 294 try { 295 return new Image(device, DEFAULT_IMAGE_DATA); 296 } catch (SWTException nextException) { 297 return null; 298 } 299 } 300 return null; 301 } 302 } 303 304 319 public abstract ImageData getImageData(); 320 321 326 public static ImageDescriptor getMissingImageDescriptor() { 327 return MissingImageDescriptor.getInstance(); 328 } 329 } 330 | Popular Tags |