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