KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.graphics.Device;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.graphics.ImageData;
16
17 /**
18  * @since 3.1
19  */

20 class ImageDataImageDescriptor extends ImageDescriptor {
21
22     private ImageData data;
23     
24     /**
25      * Original image being described, or null if this image is described
26      * completely using its ImageData
27      */

28     private Image originalImage = null;
29     
30     /**
31      * Creates an image descriptor, given an image and the device it was created on.
32      *
33      * @param originalImage
34      */

35     ImageDataImageDescriptor(Image originalImage) {
36         this(originalImage.getImageData());
37         this.originalImage = originalImage;
38     }
39     
40     /**
41      * Creates an image descriptor, given some image data.
42      *
43      * @param data describing the image
44      */

45
46     ImageDataImageDescriptor(ImageData data) {
47         this.data = data;
48     }
49     
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#create(org.eclipse.swt.graphics.Device)
52      */

53     public Object JavaDoc createResource(Device device) throws DeviceResourceException {
54
55         // If this descriptor is an existing font, then we can return the original font
56
// if this is the same device.
57
if (originalImage != null) {
58             // If we're allocating on the same device as the original font, return the original.
59
if (originalImage.getDevice() == device) {
60                 return originalImage;
61             }
62         }
63         
64         return super.createResource(device);
65     }
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.jface.resource.DeviceResourceDescriptor#destroy(java.lang.Object)
69      */

70     public void destroyResource(Object JavaDoc previouslyCreatedObject) {
71         if (previouslyCreatedObject == originalImage) {
72             return;
73         }
74         
75         super.destroyResource(previouslyCreatedObject);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.jface.resource.ImageDescriptor#getImageData()
80      */

81     public ImageData getImageData() {
82         return data;
83     }
84     
85     /* (non-Javadoc)
86      * @see Object#hashCode
87      */

88     public int hashCode() {
89         return data.hashCode();
90     }
91
92     /* (non-Javadoc)
93      * @see Object#equals
94      */

95     public boolean equals(Object JavaDoc obj) {
96         if (!(obj instanceof ImageDataImageDescriptor)) {
97             return false;
98         }
99         
100         ImageDataImageDescriptor imgWrap = (ImageDataImageDescriptor) obj;
101        
102         if (originalImage != null) {
103             return imgWrap.originalImage == originalImage;
104         }
105         
106         return (imgWrap.originalImage == null && data.equals(imgWrap.data));
107     }
108     
109 }
110
Popular Tags