e008eff5c91ffc92898270a1a2362e653a718b7f
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / JavaElementImageDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.ui;
12
13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
14
15 import org.eclipse.jface.resource.CompositeImageDescriptor;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.util.Assert;
18 import org.eclipse.swt.graphics.ImageData;
19 import org.eclipse.swt.graphics.Point;
20
21 /**
22  * A JavaImageDescriptor consists of a base image and several adornments. The
23  * adornments are computed according to the flags either passed during creation
24  * or set via the method <code>setAdornments</code>.
25  * 
26  * <p>
27  * This class may be instantiated; it is not intended to be subclassed.
28  * </p>
29  * 
30  * @since 2.0
31  */
32 public class JavaElementImageDescriptor extends CompositeImageDescriptor {
33
34         /** Flag to render the abstract adornment */
35         public final static int ABSTRACT = 0x001;
36
37         /** Flag to render the final adornment */
38         public final static int FINAL = 0x002;
39
40         /** Flag to render the synchronized adornment */
41         public final static int SYNCHRONIZED = 0x004;
42
43         /** Flag to render the static adornment */
44         public final static int STATIC = 0x008;
45
46         /** Flag to render the runnable adornment */
47         public final static int RUNNABLE = 0x010;
48
49         /** Flag to render the waring adornment */
50         public final static int WARNING = 0x020;
51
52         /** Flag to render the error adornment */
53         public final static int ERROR = 0x040;
54
55         /** Flag to render the 'override' adornment */
56         public final static int OVERRIDES = 0x080;
57
58         /** Flag to render the 'implements' adornment */
59         public final static int IMPLEMENTS = 0x100;
60
61         /** Flag to render the 'constructor' adornment */
62         public final static int CONSTRUCTOR = 0x200;
63
64         private ImageDescriptor fBaseImage;
65
66         private int fFlags;
67
68         private Point fSize;
69
70         /**
71          * Creates a new JavaElementImageDescriptor.
72          * 
73          * @param baseImage
74          *            an image descriptor used as the base image
75          * @param flags
76          *            flags indicating which adornments are to be rendered. See
77          *            <code>setAdornments</code> for valid values.
78          * @param size
79          *            the size of the resulting image
80          * @see #setAdornments(int)
81          */
82         public JavaElementImageDescriptor(ImageDescriptor baseImage, int flags,
83                         Point size) {
84                 fBaseImage = baseImage;
85                 Assert.isNotNull(fBaseImage);
86                 fFlags = flags;
87                 Assert.isTrue(fFlags >= 0);
88                 fSize = size;
89                 Assert.isNotNull(fSize);
90         }
91
92         /**
93          * Sets the descriptors adornments. Valid values are: <code>ABSTRACT</code>,
94          * <code>FINAL</code>, <code>SYNCHRONIZED</code>, </code>STATIC<code>,
95          * </code>RUNNABLE<code>, </code>WARNING<code>, </code>ERROR<code>,
96          * </code>OVERRIDDES<code>, <code>IMPLEMENTS</code>,
97          * <code>CONSTRUCTOR</code>, or any combination of those.
98          * 
99          * @param adornments
100          *            the image descritpors adornments
101          */
102         public void setAdornments(int adornments) {
103                 Assert.isTrue(adornments >= 0);
104                 fFlags = adornments;
105         }
106
107         /**
108          * Returns the current adornments.
109          * 
110          * @return the current adornments
111          */
112         public int getAdronments() {
113                 return fFlags;
114         }
115
116         /**
117          * Sets the size of the image created by calling <code>createImage()</code>.
118          * 
119          * @param size
120          *            the size of the image returned from calling
121          *            <code>createImage()</code>
122          * @see ImageDescriptor#createImage()
123          */
124         public void setImageSize(Point size) {
125                 Assert.isNotNull(size);
126                 Assert.isTrue(size.x >= 0 && size.y >= 0);
127                 fSize = size;
128         }
129
130         /**
131          * Returns the size of the image created by calling
132          * <code>createImage()</code>.
133          * 
134          * @return the size of the image created by calling
135          *         <code>createImage()</code>
136          * @see ImageDescriptor#createImage()
137          */
138         public Point getImageSize() {
139                 return new Point(fSize.x, fSize.y);
140         }
141
142         /*
143          * (non-Javadoc) Method declared in CompositeImageDescriptor
144          */
145         protected Point getSize() {
146                 return fSize;
147         }
148
149         /*
150          * (non-Javadoc) Method declared on Object.
151          */
152         public boolean equals(Object object) {
153                 if (object == null
154                                 || !JavaElementImageDescriptor.class.equals(object.getClass()))
155                         return false;
156
157                 JavaElementImageDescriptor other = (JavaElementImageDescriptor) object;
158                 return (fBaseImage.equals(other.fBaseImage) && fFlags == other.fFlags && fSize
159                                 .equals(other.fSize));
160         }
161
162         /*
163          * (non-Javadoc) Method declared on Object.
164          */
165         public int hashCode() {
166                 return fBaseImage.hashCode() | fFlags | fSize.hashCode();
167         }
168
169         /*
170          * (non-Javadoc) Method declared in CompositeImageDescriptor
171          */
172         protected void drawCompositeImage(int width, int height) {
173                 ImageData bg;
174                 if ((bg = fBaseImage.getImageData()) == null)
175                         bg = DEFAULT_IMAGE_DATA;
176
177                 drawImage(bg, 0, 0);
178                 drawTopRight();
179                 drawBottomRight();
180                 drawBottomLeft();
181         }
182
183         private void drawTopRight() {
184                 int x = getSize().x;
185                 ImageData data = null;
186                 if ((fFlags & ABSTRACT) != 0) {
187                         data = PHPUiImages.DESC_OVR_ABSTRACT.getImageData();
188                         x -= data.width;
189                         drawImage(data, x, 0);
190                 }
191                 if ((fFlags & CONSTRUCTOR) != 0) {
192                         data = PHPUiImages.DESC_OVR_CONSTRUCTOR.getImageData();
193                         x -= data.width;
194                         drawImage(data, x, 0);
195                 }
196                 if ((fFlags & FINAL) != 0) {
197                         data = PHPUiImages.DESC_OVR_FINAL.getImageData();
198                         x -= data.width;
199                         drawImage(data, x, 0);
200                 }
201                 if ((fFlags & STATIC) != 0) {
202                         data = PHPUiImages.DESC_OVR_STATIC.getImageData();
203                         x -= data.width;
204                         drawImage(data, x, 0);
205                 }
206         }
207
208         private void drawBottomRight() {
209                 Point size = getSize();
210                 int x = size.x;
211                 ImageData data = null;
212                 if ((fFlags & OVERRIDES) != 0) {
213                         data = PHPUiImages.DESC_OVR_OVERRIDES.getImageData();
214                         x -= data.width;
215                         drawImage(data, x, size.y - data.height);
216                 }
217                 if ((fFlags & IMPLEMENTS) != 0) {
218                         data = PHPUiImages.DESC_OVR_IMPLEMENTS.getImageData();
219                         x -= data.width;
220                         drawImage(data, x, size.y - data.height);
221                 }
222                 if ((fFlags & SYNCHRONIZED) != 0) {
223                         data = PHPUiImages.DESC_OVR_SYNCH.getImageData();
224                         x -= data.width;
225                         drawImage(data, x, size.y - data.height);
226                 }
227                 if ((fFlags & RUNNABLE) != 0) {
228                         data = PHPUiImages.DESC_OVR_RUN.getImageData();
229                         x -= data.width;
230                         drawImage(data, x, size.y - data.height);
231                 }
232         }
233
234         private void drawBottomLeft() {
235                 Point size = getSize();
236                 int x = 0;
237                 ImageData data = null;
238                 if ((fFlags & ERROR) != 0) {
239                         data = PHPUiImages.DESC_OVR_ERROR.getImageData();
240                         drawImage(data, x, size.y - data.height);
241                         x += data.width;
242                 }
243                 if ((fFlags & WARNING) != 0) {
244                         data = PHPUiImages.DESC_OVR_WARNING.getImageData();
245                         drawImage(data, x, size.y - data.height);
246                         x += data.width;
247                 }
248         }
249 }