KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > JavaElementImageDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.ui;
12
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.swt.graphics.ImageData;
17 import org.eclipse.swt.graphics.Point;
18
19 import org.eclipse.jface.resource.CompositeImageDescriptor;
20 import org.eclipse.jface.resource.ImageDescriptor;
21
22 import org.eclipse.jdt.internal.ui.JavaPlugin;
23 import org.eclipse.jdt.internal.ui.JavaPluginImages;
24
25 /**
26  * A {@link JavaElementImageDescriptor} consists of a base image and several adornments. The adornments
27  * are computed according to the flags either passed during creation or set via the method
28  *{@link #setAdornments(int)}.
29  *
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  *
34  * @since 2.0
35  */

36 public class JavaElementImageDescriptor extends CompositeImageDescriptor {
37     
38     /** Flag to render the abstract adornment. */
39     public final static int ABSTRACT= 0x001;
40     
41     /** Flag to render the final adornment. */
42     public final static int FINAL= 0x002;
43     
44     /** Flag to render the synchronized adornment. */
45     public final static int SYNCHRONIZED= 0x004;
46     
47     /** Flag to render the static adornment. */
48     public final static int STATIC= 0x008;
49     
50     /** Flag to render the runnable adornment. */
51     public final static int RUNNABLE= 0x010;
52     
53     /** Flag to render the warning adornment. */
54     public final static int WARNING= 0x020;
55     
56     /** Flag to render the error adornment. */
57     public final static int ERROR= 0x040;
58     
59     /** Flag to render the 'override' adornment. */
60     public final static int OVERRIDES= 0x080;
61     
62     /** Flag to render the 'implements' adornment. */
63     public final static int IMPLEMENTS= 0x100;
64     
65     /** Flag to render the 'constructor' adornment. */
66     public final static int CONSTRUCTOR= 0x200;
67     
68     /**
69      * Flag to render the 'deprecated' adornment.
70      * @since 3.0
71      */

72     public final static int DEPRECATED= 0x400;
73     
74     /**
75      * Flag to render the 'volatile' adornment.
76      * @since 3.3
77      */

78     public final static int VOLATILE= 0x800;
79     
80     /**
81      * Flag to render the 'transient' adornment.
82      * @since 3.3
83      */

84     public final static int TRANSIENT= 0x1000;
85     
86
87     private ImageDescriptor fBaseImage;
88     private int fFlags;
89     private Point fSize;
90
91     /**
92      * Creates a new JavaElementImageDescriptor.
93      *
94      * @param baseImage an image descriptor used as the base image
95      * @param flags flags indicating which adornments are to be rendered. See {@link #setAdornments(int)}
96      * for valid values.
97      * @param size the size of the resulting image
98      */

99     public JavaElementImageDescriptor(ImageDescriptor baseImage, int flags, Point size) {
100         fBaseImage= baseImage;
101         Assert.isNotNull(fBaseImage);
102         fFlags= flags;
103         Assert.isTrue(fFlags >= 0);
104         fSize= size;
105         Assert.isNotNull(fSize);
106     }
107     
108     /**
109      * Sets the descriptors adornments. Valid values are: {@link #ABSTRACT}, {@link #FINAL},
110      * {@link #SYNCHRONIZED}, {@link #STATIC}, {@link #RUNNABLE}, {@link #WARNING},
111      * {@link #ERROR}, {@link #OVERRIDES}, {@link #IMPLEMENTS}, {@link #CONSTRUCTOR},
112      * {@link #DEPRECATED}, {@link #VOLATILE}, {@link #TRANSIENT} or any combination of those.
113      *
114      * @param adornments the image descriptors adornments
115      */

116     public void setAdornments(int adornments) {
117         Assert.isTrue(adornments >= 0);
118         fFlags= adornments;
119     }
120
121     /**
122      * Returns the current adornments.
123      *
124      * @return the current adornments
125      */

126     public int getAdronments() {
127         return fFlags;
128     }
129
130     /**
131      * Sets the size of the image created by calling {@link #createImage()}.
132      *
133      * @param size the size of the image returned from calling {@link #createImage()}
134      */

135     public void setImageSize(Point size) {
136         Assert.isNotNull(size);
137         Assert.isTrue(size.x >= 0 && size.y >= 0);
138         fSize= size;
139     }
140     
141     /**
142      * Returns the size of the image created by calling {@link #createImage()}.
143      *
144      * @return the size of the image created by calling {@link #createImage()}
145      */

146     public Point getImageSize() {
147         return new Point(fSize.x, fSize.y);
148     }
149     
150     /* (non-Javadoc)
151      * Method declared in CompositeImageDescriptor
152      */

153     protected Point getSize() {
154         return fSize;
155     }
156     
157     /* (non-Javadoc)
158      * Method declared on Object.
159      */

160     public boolean equals(Object JavaDoc object) {
161         if (object == null || !JavaElementImageDescriptor.class.equals(object.getClass()))
162             return false;
163             
164         JavaElementImageDescriptor other= (JavaElementImageDescriptor)object;
165         return (fBaseImage.equals(other.fBaseImage) && fFlags == other.fFlags && fSize.equals(other.fSize));
166     }
167     
168     /* (non-Javadoc)
169      * Method declared on Object.
170      */

171     public int hashCode() {
172         return fBaseImage.hashCode() | fFlags | fSize.hashCode();
173     }
174     
175     /* (non-Javadoc)
176      * Method declared in CompositeImageDescriptor
177      */

178     protected void drawCompositeImage(int width, int height) {
179         ImageData bg= getImageData(fBaseImage);
180             
181         if ((fFlags & DEPRECATED) != 0) { // draw *behind* the full image
182
Point size= getSize();
183             ImageData data= getImageData(JavaPluginImages.DESC_OVR_DEPRECATED);
184             drawImage(data, 0, size.y - data.height);
185         }
186         drawImage(bg, 0, 0);
187                 
188         drawTopRight();
189         drawBottomRight();
190         drawBottomLeft();
191         
192
193     }
194     
195     private ImageData getImageData(ImageDescriptor descriptor) {
196         ImageData data= descriptor.getImageData(); // see bug 51965: getImageData can return null
197
if (data == null) {
198             data= DEFAULT_IMAGE_DATA;
199             JavaPlugin.logErrorMessage("Image data not available: " + descriptor.toString()); //$NON-NLS-1$
200
}
201         return data;
202     }
203     
204     private void addTopRightImage(ImageDescriptor desc, Point pos) {
205         ImageData data= getImageData(desc);
206         int x= pos.x - data.width;
207         if (x >= 0) {
208             drawImage(data, x, pos.y);
209             pos.x= x;
210         }
211     }
212     
213     private void addBottomRightImage(ImageDescriptor desc, Point pos) {
214         ImageData data= getImageData(desc);
215         int x= pos.x - data.width;
216         int y= pos.y - data.height;
217         if (x >= 0 && y >= 0) {
218             drawImage(data, x, y);
219             pos.x= x;
220         }
221     }
222     
223     private void addBottomLeftImage(ImageDescriptor desc, Point pos) {
224         ImageData data= getImageData(desc);
225         int x= pos.x;
226         int y= pos.y - data.height;
227         if (x + data.width < getSize().x && y >= 0) {
228             drawImage(data, x, y);
229             pos.x= x + data.width;
230         }
231     }
232     
233     
234     private void drawTopRight() {
235         Point pos= new Point(getSize().x, 0);
236         if ((fFlags & ABSTRACT) != 0) {
237             addTopRightImage(JavaPluginImages.DESC_OVR_ABSTRACT, pos);
238         }
239         if ((fFlags & CONSTRUCTOR) != 0) {
240             addTopRightImage(JavaPluginImages.DESC_OVR_CONSTRUCTOR, pos);
241         }
242         if ((fFlags & FINAL) != 0) {
243             addTopRightImage(JavaPluginImages.DESC_OVR_FINAL, pos);
244         }
245         if ((fFlags & VOLATILE) != 0) {
246             addTopRightImage(JavaPluginImages.DESC_OVR_VOLATILE, pos);
247         }
248         if ((fFlags & STATIC) != 0) {
249             addTopRightImage(JavaPluginImages.DESC_OVR_STATIC, pos);
250         }
251
252     }
253     
254     private void drawBottomRight() {
255         Point size= getSize();
256         Point pos= new Point(size.x, size.y);
257
258         int flags= fFlags;
259         
260         int syncAndOver= SYNCHRONIZED | OVERRIDES;
261         int syncAndImpl= SYNCHRONIZED | IMPLEMENTS;
262         
263         if ((flags & syncAndOver) == syncAndOver) { // both flags set: merged overlay image
264
addBottomRightImage(JavaPluginImages.DESC_OVR_SYNCH_AND_OVERRIDES, pos);
265             flags &= ~syncAndOver; // clear to not render again
266
} else if ((flags & syncAndImpl) == syncAndImpl) { // both flags set: merged overlay image
267
addBottomRightImage(JavaPluginImages.DESC_OVR_SYNCH_AND_IMPLEMENTS, pos);
268             flags &= ~syncAndImpl; // clear to not render again
269
}
270         if ((flags & OVERRIDES) != 0) {
271             addBottomRightImage(JavaPluginImages.DESC_OVR_OVERRIDES, pos);
272         }
273         if ((flags & IMPLEMENTS) != 0) {
274             addBottomRightImage(JavaPluginImages.DESC_OVR_IMPLEMENTS, pos);
275         }
276         if ((flags & SYNCHRONIZED) != 0) {
277             addBottomRightImage(JavaPluginImages.DESC_OVR_SYNCH, pos);
278         }
279         if ((flags & RUNNABLE) != 0) {
280             addBottomRightImage(JavaPluginImages.DESC_OVR_RUN, pos);
281         }
282         if ((flags & TRANSIENT) != 0) {
283             addBottomRightImage(JavaPluginImages.DESC_OVR_TRANSIENT, pos);
284         }
285     }
286     
287     private void drawBottomLeft() {
288         Point pos= new Point(0, getSize().y);
289         if ((fFlags & ERROR) != 0) {
290             addBottomLeftImage(JavaPluginImages.DESC_OVR_ERROR, pos);
291         }
292         if ((fFlags & WARNING) != 0) {
293             addBottomLeftImage(JavaPluginImages.DESC_OVR_WARNING, pos);
294         }
295
296     }
297 }
298
Popular Tags