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