4
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.resource.ImageDescriptor;
|
|
14
|
|
15 import dwtx.jface.resource.DeviceResourceDescriptor;
|
|
16 import dwtx.jface.resource.FileImageDescriptor;
|
|
17 import dwtx.jface.resource.URLImageDescriptor;
|
|
18 import dwtx.jface.resource.ImageDataImageDescriptor;
|
|
19 import dwtx.jface.resource.DerivedImageDescriptor;
|
|
20 import dwtx.jface.resource.DeviceResourceException;
|
|
21 import dwtx.jface.resource.MissingImageDescriptor;
|
|
22
|
|
23 import tango.net.Uri;
|
|
24
|
|
25 import dwt.DWTException;
|
|
26 import dwt.graphics.Device;
|
|
27 import dwt.graphics.Image;
|
|
28 import dwt.graphics.ImageData;
|
|
29 import dwt.graphics.PaletteData;
|
|
30 import dwt.graphics.RGB;
|
|
31 import dwt.widgets.Display;
|
|
32
|
|
33 import dwt.dwthelper.utils;
|
|
34
|
|
35 /**
|
|
36 * An image descriptor is an object that knows how to create
|
|
37 * an DWT image. It does not hold onto images or cache them,
|
|
38 * but rather just creates them on demand. An image descriptor
|
|
39 * is intended to be a lightweight representation of an image
|
|
40 * that can be manipulated even when no DWT display exists.
|
|
41 * <p>
|
|
42 * This package defines a concrete image descriptor implementation
|
|
43 * which reads an image from a file (<code>FileImageDescriptor</code>).
|
|
44 * It also provides abstract framework classes (this one and
|
|
45 * <code>CompositeImageDescriptor</code>) which may be subclassed to define
|
|
46 * news kinds of image descriptors.
|
|
47 * </p>
|
|
48 * <p>
|
|
49 * Using this abstract class involves defining a concrete subclass
|
|
50 * and providing an implementation for the <code>getImageData</code>
|
|
51 * method.
|
|
52 * </p>
|
|
53 * <p>
|
|
54 * There are two ways to get an Image from an ImageDescriptor. The method
|
|
55 * createImage will always return a new Image which must be disposed by
|
|
56 * the caller. Alternatively, createResource() returns a shared
|
|
57 * Image. When the caller is done with an image obtained from createResource,
|
|
58 * they must call destroyResource() rather than disposing the Image directly.
|
|
59 * The result of createResource() can be safely cast to an Image.
|
|
60 * </p>
|
|
61 *
|
|
62 * @see dwt.graphics.Image
|
|
63 */
|
|
64 public abstract class ImageDescriptor : DeviceResourceDescriptor {
|
|
65
|
|
66 /**
|
|
67 * A small red square used to warn that an image cannot be created.
|
|
68 * <p>
|
|
69 */
|
|
70 protected static const ImageData DEFAULT_IMAGE_DATA;
|
|
71
|
|
72 static this(){
|
|
73 DEFAULT_IMAGE_DATA = new ImageData(6, 6, 1, new PaletteData( [ new RGB(255, 0, 0) ] ));
|
|
74 }
|
|
75
|
|
76 /**
|
|
77 * Constructs an image descriptor.
|
|
78 */
|
|
79 protected this() {
|
|
80 // do nothing
|
|
81 }
|
|
82
|
|
83 /**
|
|
84 * Creates and returns a new image descriptor from a file.
|
|
85 * Convenience method for
|
|
86 * <code>new FileImageDescriptor(location,filename)</code>.
|
|
87 *
|
|
88 * @param location the class whose resource directory contain the file
|
|
89 * @param filename the file name
|
|
90 * @return a new image descriptor
|
|
91 */
|
|
92 public static ImageDescriptor createFromFile(ClassInfo location, String filename) {
|
|
93 return new FileImageDescriptor(location, filename);
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * Creates and returns a new image descriptor given ImageData
|
|
98 * describing the image.
|
|
99 *
|
|
100 * @since 3.1
|
|
101 *
|
|
102 * @param data contents of the image
|
|
103 * @return newly created image descriptor
|
|
104 */
|
|
105 public static ImageDescriptor createFromImageData(ImageData data) {
|
|
106 return new ImageDataImageDescriptor(data);
|
|
107 }
|
|
108
|
|
109 /**
|
|
110 * Creates and returns a new image descriptor for the given image. Note
|
|
111 * that disposing the original Image will cause the descriptor to become invalid.
|
|
112 *
|
|
113 * @since 3.1
|
|
114 *
|
|
115 * @param img image to create
|
|
116 * @return a newly created image descriptor
|
|
117 */
|
|
118 public static ImageDescriptor createFromImage(Image img) {
|
|
119 return new ImageDataImageDescriptor(img);
|
|
120 }
|
|
121
|
|
122 /**
|
|
123 * Creates an ImageDescriptor based on the given original descriptor, but with additional
|
|
124 * DWT flags.
|
|
125 *
|
|
126 * <p>
|
|
127 * Note that this sort of ImageDescriptor is slower and consumes more resources than
|
|
128 * a regular image descriptor. It will also never generate results that look as nice as
|
|
129 * a hand-drawn image. Clients are encouraged to supply their own disabled/grayed/etc. images
|
|
130 * rather than using a default image and transforming it.
|
|
131 * </p>
|
|
132 *
|
|
133 * @param originalImage image to transform
|
|
134 * @param swtFlags any flag that can be passed to the flags argument of Image#Image(Device, Image, int)
|
|
135 * @return an ImageDescriptor that creates new images by transforming the given image descriptor
|
|
136 *
|
|
137 * @see Image#Image(Device, Image, int)
|
|
138 * @since 3.1
|
|
139 *
|
|
140 */
|
|
141 public static ImageDescriptor createWithFlags(ImageDescriptor originalImage, int swtFlags) {
|
|
142 return new DerivedImageDescriptor(originalImage, swtFlags);
|
|
143 }
|
|
144
|
|
145 /**
|
|
146 * Creates and returns a new image descriptor for the given image. This
|
|
147 * method takes the Device that created the Image as an argument, allowing
|
|
148 * the original Image to be reused if the descriptor is asked for another
|
|
149 * Image on the same device. Note that disposing the original Image will
|
|
150 * cause the descriptor to become invalid.
|
|
151 *
|
|
152 * @deprecated use {@link ImageDescriptor#createFromImage(Image)}
|
|
153 * @since 3.1
|
|
154 *
|
|
155 * @param img image to create
|
|
156 * @param theDevice the device that was used to create the Image
|
|
157 * @return a newly created image descriptor
|
|
158 */
|
|
159 public static ImageDescriptor createFromImage(Image img, Device theDevice) {
|
|
160 return new ImageDataImageDescriptor(img);
|
|
161 }
|
|
162
|
|
163 /**
|
|
164 * Creates and returns a new image descriptor from a URL.
|
|
165 *
|
|
166 * @param url The URL of the image file.
|
|
167 * @return a new image descriptor
|
|
168 */
|
|
169 public static ImageDescriptor createFromURL(Uri url) {
|
|
170 if (url is null) {
|
|
171 return getMissingImageDescriptor();
|
|
172 }
|
|
173 return new URLImageDescriptor(url);
|
|
174 }
|
|
175
|
|
176 /* (non-Javadoc)
|
|
177 * @see dwtx.jface.resource.DeviceResourceDescriptor#createResource(dwt.graphics.Device)
|
|
178 */
|
|
179 public Object createResource(Device device) {
|
|
180 Image result = createImage(false, device);
|
|
181 if (result is null) {
|
|
182 throw new DeviceResourceException(this);
|
|
183 }
|
|
184 return result;
|
|
185 }
|
|
186
|
|
187 /* (non-Javadoc)
|
|
188 * @see dwtx.jface.resource.DeviceResourceDescriptor#destroyResource(Object)
|
|
189 */
|
|
190 public void destroyResource(Object previouslyCreatedObject) {
|
|
191 (cast(Image)previouslyCreatedObject).dispose();
|
|
192 }
|
|
193
|
|
194 /**
|
|
195 * Creates and returns a new DWT image for this image descriptor. Note that
|
|
196 * each call returns a new DWT image object. The returned image must be
|
|
197 * explicitly disposed using the image's dispose call. The image will not be
|
|
198 * automatically garbage collected. A default image is returned in the event
|
|
199 * of an error.
|
|
200 *
|
|
201 * <p>
|
|
202 * Note: this method differs from createResource(Device) in that the returned image
|
|
203 * must be disposed directly, whereas an image obtained from createResource(...)
|
|
204 * must be disposed by calling destroyResource(...). It is not possible to
|
|
205 * mix-and-match. If you obtained the Image from this method, you must not dispose
|
|
206 * it by calling destroyResource. Clients are encouraged to use
|
|
207 * create/destroyResource and downcast the result to Image rather than using
|
|
208 * createImage.
|
|
209 * </p>
|
|
210 *
|
|
211 * <p>
|
|
212 * Note: it is still possible for this method to return <code>null</code>
|
|
213 * in extreme cases, for example if DWT runs out of image handles.
|
|
214 * </p>
|
|
215 *
|
|
216 * @return a new image or <code>null</code> if the image could not be
|
|
217 * created
|
|
218 */
|
|
219 public Image createImage() {
|
|
220 return createImage(true);
|
|
221 }
|
|
222
|
|
223 /**
|
|
224 * Creates and returns a new DWT image for this image descriptor. The
|
|
225 * returned image must be explicitly disposed using the image's dispose
|
|
226 * call. The image will not be automatically garbage collected. In the event
|
|
227 * of an error, a default image is returned if
|
|
228 * <code>returnMissingImageOnError</code> is true, otherwise
|
|
229 * <code>null</code> is returned.
|
|
230 * <p>
|
|
231 * Note: Even if <code>returnMissingImageOnError</code> is true, it is
|
|
232 * still possible for this method to return <code>null</code> in extreme
|
|
233 * cases, for example if DWT runs out of image handles.
|
|
234 * </p>
|
|
235 *
|
|
236 * @param returnMissingImageOnError
|
|
237 * flag that determines if a default image is returned on error
|
|
238 * @return a new image or <code>null</code> if the image could not be
|
|
239 * created
|
|
240 */
|
|
241 public Image createImage(bool returnMissingImageOnError) {
|
|
242 return createImage(returnMissingImageOnError, Display.getCurrent());
|
|
243 }
|
|
244
|
|
245 /**
|
|
246 * Creates and returns a new DWT image for this image descriptor. The
|
|
247 * returned image must be explicitly disposed using the image's dispose
|
|
248 * call. The image will not be automatically garbage collected. A default
|
|
249 * image is returned in the event of an error.
|
|
250 * <p>
|
|
251 * Note: it is still possible for this method to return <code>null</code>
|
|
252 * in extreme cases, for example if DWT runs out of image handles.
|
|
253 * </p>
|
|
254 *
|
|
255 * @param device
|
|
256 * the device on which to create the image
|
|
257 * @return a new image or <code>null</code> if the image could not be
|
|
258 * created
|
|
259 * @since 2.0
|
|
260 */
|
|
261 public Image createImage(Device device) {
|
|
262 return createImage(true, device);
|
|
263 }
|
|
264
|
|
265 /**
|
|
266 * Creates and returns a new DWT image for this image descriptor. The
|
|
267 * returned image must be explicitly disposed using the image's dispose
|
|
268 * call. The image will not be automatically garbage collected. In the even
|
|
269 * of an error, a default image is returned if
|
|
270 * <code>returnMissingImageOnError</code> is true, otherwise
|
|
271 * <code>null</code> is returned.
|
|
272 * <p>
|
|
273 * Note: Even if <code>returnMissingImageOnError</code> is true, it is
|
|
274 * still possible for this method to return <code>null</code> in extreme
|
|
275 * cases, for example if DWT runs out of image handles.
|
|
276 * </p>
|
|
277 *
|
|
278 * @param returnMissingImageOnError
|
|
279 * flag that determines if a default image is returned on error
|
|
280 * @param device
|
|
281 * the device on which to create the image
|
|
282 * @return a new image or <code>null</code> if the image could not be
|
|
283 * created
|
|
284 * @since 2.0
|
|
285 */
|
|
286 public Image createImage(bool returnMissingImageOnError, Device device) {
|
|
287
|
|
288 ImageData data = getImageData();
|
|
289 if (data is null) {
|
|
290 if (!returnMissingImageOnError) {
|
|
291 return null;
|
|
292 }
|
|
293 data = DEFAULT_IMAGE_DATA;
|
|
294 }
|
|
295
|
|
296 /*
|
|
297 * Try to create the supplied image. If there is an DWT Exception try and create
|
|
298 * the default image if that was requested. Return null if this fails.
|
|
299 */
|
|
300
|
|
301 try {
|
|
302 if (data.transparentPixel >= 0) {
|
|
303 ImageData maskData = data.getTransparencyMask();
|
|
304 return new Image(device, data, maskData);
|
|
305 }
|
|
306 return new Image(device, data);
|
|
307 } catch (DWTException exception) {
|
|
308 if (returnMissingImageOnError) {
|
|
309 try {
|
|
310 return new Image(device, DEFAULT_IMAGE_DATA);
|
|
311 } catch (DWTException nextException) {
|
|
312 return null;
|
|
313 }
|
|
314 }
|
|
315 return null;
|
|
316 }
|
|
317 }
|
|
318
|
|
319 /**
|
|
320 * Creates and returns a new DWT <code>ImageData</code> object
|
|
321 * for this image descriptor.
|
|
322 * Note that each call returns a new DWT image data object.
|
|
323 * <p>
|
|
324 * This framework method is declared public so that it is
|
|
325 * possible to request an image descriptor's image data without
|
|
326 * creating an DWT image object.
|
|
327 * </p>
|
|
328 * <p>
|
|
329 * Returns <code>null</code> if the image data could not be created.
|
|
330 * </p>
|
|
331 *
|
|
332 * @return a new image data or <code>null</code>
|
|
333 */
|
|
334 public abstract ImageData getImageData();
|
|
335
|
|
336 /**
|
|
337 * Returns the shared image descriptor for a missing image.
|
|
338 *
|
|
339 * @return the missing image descriptor
|
|
340 */
|
|
341 public static ImageDescriptor getMissingImageDescriptor() {
|
|
342 return MissingImageDescriptor.getInstance();
|
|
343 }
|
|
344 }
|