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