KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > DerivedImageDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.SWTException;
15 import org.eclipse.swt.graphics.Device;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.graphics.ImageData;
18 import org.eclipse.swt.widgets.Display;
19
20 /**
21  * An image descriptor which creates images based on another ImageDescriptor, but with
22  * additional SWT flags. Note that this is only intended for compatibility.
23  *
24  * @since 3.1
25  */

26 final class DerivedImageDescriptor extends ImageDescriptor {
27
28     private ImageDescriptor original;
29     private int flags;
30     
31     /**
32      * Create a new image descriptor
33      * @param original the original one
34      * @param swtFlags flags to be used when image is created {@link Image#Image(Device, Image, int)}
35      * @see SWT#IMAGE_COPY
36      * @see SWT#IMAGE_DISABLE
37      * @see SWT#IMAGE_GRAY
38      */

39     public DerivedImageDescriptor(ImageDescriptor original, int swtFlags) {
40         this.original = original;
41         flags = swtFlags;
42     }
43     
44     public Object JavaDoc createResource(Device device) throws DeviceResourceException {
45         try {
46             return internalCreateImage(device);
47         } catch (SWTException e) {
48             throw new DeviceResourceException(this, e);
49         }
50     }
51     
52     public Image createImage(Device device) {
53         return internalCreateImage(device);
54     }
55     
56     public int hashCode() {
57         return original.hashCode() + flags;
58     }
59     
60     public boolean equals(Object JavaDoc arg0) {
61         if (arg0 instanceof DerivedImageDescriptor) {
62             DerivedImageDescriptor desc = (DerivedImageDescriptor)arg0;
63             
64             return desc.original == original && flags == desc.flags;
65         }
66         
67         return false;
68     }
69     
70     /**
71      * Creates a new Image on the given device. Note that we defined a new
72      * method rather than overloading createImage since this needs to be
73      * called by getImageData(), and we want to be absolutely certain not
74      * to cause infinite recursion if the base class gets refactored.
75      *
76      * @param device device to create the image on
77      * @return a newly allocated Image. Must be disposed by calling image.dispose().
78      */

79     private final Image internalCreateImage(Device device) {
80         Image originalImage = original.createImage(device);
81         Image result = new Image(device, originalImage, flags);
82         original.destroyResource(originalImage);
83         return result;
84     }
85     
86     public ImageData getImageData() {
87         Image image = internalCreateImage(Display.getCurrent());
88         ImageData result = image.getImageData();
89         image.dispose();
90         return result;
91     }
92
93 }
94
Popular Tags