8418417261637c2f4d0cc4b72e7ef7a6c3c3da3c
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / ui / OverrideIndicatorLabelDecorator.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.core.Flags;
14 import net.sourceforge.phpdt.core.IMethod;
15 import net.sourceforge.phpdt.core.IType;
16 import net.sourceforge.phpdt.core.JavaModelException;
17 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
18 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
19 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageImageDescriptor;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.ui.WebUI;
22
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.viewers.IDecoration;
25 import org.eclipse.jface.viewers.ILabelDecorator;
26 import org.eclipse.jface.viewers.ILabelProviderListener;
27 import org.eclipse.jface.viewers.ILightweightLabelDecorator;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.graphics.Rectangle;
31
32 /**
33  * LabelDecorator that decorates an method's image with override or implements
34  * overlays. The viewer using this decorator is responsible for updating the
35  * images on element changes.
36  * 
37  * <p>
38  * This class may be instantiated; it is not intended to be subclassed.
39  * </p>
40  * 
41  * @since 2.0
42  */
43 public class OverrideIndicatorLabelDecorator implements ILabelDecorator,
44                 ILightweightLabelDecorator {
45
46         private ImageDescriptorRegistry fRegistry;
47
48         private boolean fUseNewRegistry = false;
49
50         /**
51          * Creates a decorator. The decorator creates an own image registry to cache
52          * images.
53          */
54 //      public OverrideIndicatorLabelDecorator() {
55 //              this(null);
56 //              fUseNewRegistry = true;
57 //      }
58
59         /*
60          * Creates decorator with a shared image registry.
61          * 
62          * @param registry The registry to use or <code>null</code> to use the
63          * Java plugin's image registry.
64          */
65         /**
66          * Note: This constructor is for internal use only. Clients should not call
67          * this constructor.
68          */
69         public OverrideIndicatorLabelDecorator(ImageDescriptorRegistry registry) {
70                 fRegistry = registry;
71         }
72
73         private ImageDescriptorRegistry getRegistry() {
74                 if (fRegistry == null) {
75                         fRegistry = fUseNewRegistry ? new ImageDescriptorRegistry()
76                                         : WebUI.getImageDescriptorRegistry();
77                 }
78                 return fRegistry;
79         }
80
81         /*
82          * (non-Javadoc)
83          * 
84          * @see ILabelDecorator#decorateText(String, Object)
85          */
86         public String decorateText(String text, Object element) {
87                 return text;
88         }
89
90         /*
91          * (non-Javadoc)
92          * 
93          * @see ILabelDecorator#decorateImage(Image, Object)
94          */
95         public Image decorateImage(Image image, Object element) {
96                 int adornmentFlags = computeAdornmentFlags(element);
97                 if (adornmentFlags != 0) {
98                         ImageDescriptor baseImage = new ImageImageDescriptor(image);
99                         Rectangle bounds = image.getBounds();
100                         return getRegistry().get(
101                                         new JavaElementImageDescriptor(baseImage, adornmentFlags,
102                                                         new Point(bounds.width, bounds.height)));
103                 }
104                 return image;
105         }
106
107         /**
108          * Note: This method is for internal use only. Clients should not call this
109          * method.
110          */
111         public int computeAdornmentFlags(Object element) {
112                 if (element instanceof IMethod) {
113                         if (!PreferenceConstants.getPreferenceStore().getBoolean(
114                                         PreferenceConstants.APPEARANCE_OVERRIDE_INDICATOR)) {
115                                 return 0;
116                         }
117
118                         try {
119                                 IMethod method = (IMethod) element;
120                                 // if (!method.getJavaProject().isOnClasspath(method)) {
121                                 // return 0;
122                                 // }
123
124                                 int flags = method.getFlags();
125                                 IType type = method.getDeclaringType();// jsurfer INSERT
126                                 if (type != null && type.isClass() && !method.isConstructor()
127                                                 && !Flags.isPrivate(flags) && !Flags.isStatic(flags)) {
128                                         return getOverrideIndicators(method);
129                                 }
130                         } catch (JavaModelException e) {
131                                 if (!e.isDoesNotExist()) {
132                                         PHPeclipsePlugin.log(e);
133                                 }
134                         }
135                 }
136                 return 0;
137         }
138
139         /**
140          * Note: This method is for internal use only. Clients should not call this
141          * method.
142          */
143         protected int getOverrideIndicators(IMethod method)
144                         throws JavaModelException {
145                 //IType type = method.getDeclaringType();
146                 // ITypeHierarchy hierarchy=
147                 // SuperTypeHierarchyCache.getTypeHierarchy(type);
148                 // if (hierarchy != null) {
149                 // return findInHierarchy(type, hierarchy, method.getElementName(),
150                 // method.getParameterTypes());
151                 // }
152                 return 0;
153         }
154
155         /**
156          * Note: This method is for internal use only. Clients should not call this
157          * method.
158          */
159         // protected int findInHierarchy(IType type, ITypeHierarchy hierarchy,
160         // String name, String[] paramTypes) throws JavaModelException {
161         // IMethod impl= JavaModelUtil.findMethodDeclarationInHierarchy(hierarchy,
162         // type, name, paramTypes, false);
163         // if (impl != null) {
164         // IMethod overridden=
165         // JavaModelUtil.findMethodImplementationInHierarchy(hierarchy, type, name,
166         // paramTypes, false);
167         // if (overridden != null) {
168         // return JavaElementImageDescriptor.OVERRIDES;
169         // } else {
170         // return JavaElementImageDescriptor.IMPLEMENTS;
171         // }
172         // }
173         // return 0;
174         // }
175         /*
176          * (non-Javadoc)
177          * 
178          * @see IBaseLabelProvider#addListener(ILabelProviderListener)
179          */
180         public void addListener(ILabelProviderListener listener) {
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see IBaseLabelProvider#dispose()
187          */
188         public void dispose() {
189                 if (fRegistry != null && fUseNewRegistry) {
190                         fRegistry.dispose();
191                 }
192         }
193
194         /*
195          * (non-Javadoc)
196          * 
197          * @see IBaseLabelProvider#isLabelProperty(Object, String)
198          */
199         public boolean isLabelProperty(Object element, String property) {
200                 return true;
201         }
202
203         /*
204          * (non-Javadoc)
205          * 
206          * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
207          */
208         public void removeListener(ILabelProviderListener listener) {
209         }
210
211         /*
212          * (non-Javadoc)
213          * 
214          * @see org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(java.lang.Object,
215          *      org.eclipse.jface.viewers.IDecoration)
216          */
217         public void decorate(Object element, IDecoration decoration) {
218                 int adornmentFlags = computeAdornmentFlags(element);
219                 if (adornmentFlags != 0) {
220                         decoration.addOverlay(PHPUiImages.DESC_OVR_OVERRIDES);
221                 }
222         }
223
224 }