KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > OverrideIndicatorLabelDecorator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui;
12
13 import org.eclipse.swt.graphics.Image;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.graphics.Rectangle;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.viewers.IDecoration;
19 import org.eclipse.jface.viewers.ILabelDecorator;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ILightweightLabelDecorator;
22
23 import org.eclipse.jdt.core.Flags;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMethod;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.ITypeHierarchy;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.dom.ASTNode;
30 import org.eclipse.jdt.core.dom.CompilationUnit;
31 import org.eclipse.jdt.core.dom.IMethodBinding;
32 import org.eclipse.jdt.core.dom.MethodDeclaration;
33 import org.eclipse.jdt.core.dom.SimpleName;
34
35 import org.eclipse.jdt.internal.corext.dom.Bindings;
36 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
38 import org.eclipse.jdt.internal.corext.util.JdtFlags;
39 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
40 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
41
42 import org.eclipse.jdt.internal.ui.JavaPlugin;
43 import org.eclipse.jdt.internal.ui.JavaPluginImages;
44 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
45 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
46 import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor;
47
48 /**
49  * LabelDecorator that decorates an method's image with override or implements overlays.
50  * The viewer using this decorator is responsible for updating the images on element changes.
51  *
52  * <p>
53  * This class may be instantiated; it is not intended to be subclassed.
54  * </p>
55  *
56  * @since 2.0
57  */

58 public class OverrideIndicatorLabelDecorator implements ILabelDecorator, ILightweightLabelDecorator {
59
60     private ImageDescriptorRegistry fRegistry;
61     private boolean fUseNewRegistry= false;
62
63     /**
64      * Creates a decorator. The decorator creates an own image registry to cache
65      * images.
66      */

67     public OverrideIndicatorLabelDecorator() {
68         this(null);
69         fUseNewRegistry= true;
70     }
71
72     /*
73      * Creates decorator with a shared image registry.
74      *
75      * @param registry The registry to use or <code>null</code> to use the Java plugin's
76      * image registry.
77      */

78     /**
79      * Note: This constructor is for internal use only. Clients should not call this constructor.
80      * @param registry The registry to use.
81      */

82     public OverrideIndicatorLabelDecorator(ImageDescriptorRegistry registry) {
83         fRegistry= registry;
84     }
85     
86     private ImageDescriptorRegistry getRegistry() {
87         if (fRegistry == null) {
88             fRegistry= fUseNewRegistry ? new ImageDescriptorRegistry() : JavaPlugin.getImageDescriptorRegistry();
89         }
90         return fRegistry;
91     }
92     
93     
94     /* (non-Javadoc)
95      * @see ILabelDecorator#decorateText(String, Object)
96      */

97     public String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element) {
98         return text;
99     }
100
101     /* (non-Javadoc)
102      * @see ILabelDecorator#decorateImage(Image, Object)
103      */

104     public Image decorateImage(Image image, Object JavaDoc element) {
105         int adornmentFlags= computeAdornmentFlags(element);
106         if (adornmentFlags != 0) {
107             ImageDescriptor baseImage= new ImageImageDescriptor(image);
108             Rectangle bounds= image.getBounds();
109             return getRegistry().get(new JavaElementImageDescriptor(baseImage, adornmentFlags, new Point(bounds.width, bounds.height)));
110         }
111         return image;
112     }
113     
114     /**
115      * Note: This method is for internal use only. Clients should not call this method.
116      * @param element The element to decorate
117      * @return Resulting decorations (combination of JavaElementImageDescriptor.IMPLEMENTS
118      * and JavaElementImageDescriptor.OVERRIDES)
119      */

120     public int computeAdornmentFlags(Object JavaDoc element) {
121         if (element instanceof IMethod) {
122             try {
123                 IMethod method= (IMethod) element;
124                 if (!method.getJavaProject().isOnClasspath(method)) {
125                     return 0;
126                 }
127                 int flags= method.getFlags();
128                 if (!method.isConstructor() && !Flags.isPrivate(flags) && !Flags.isStatic(flags)) {
129                     int res= getOverrideIndicators(method);
130                     if (res != 0 && Flags.isSynchronized(flags)) {
131                         return res | JavaElementImageDescriptor.SYNCHRONIZED;
132                     }
133                     return res;
134                 }
135             } catch (JavaModelException e) {
136                 if (!e.isDoesNotExist()) {
137                     JavaPlugin.log(e);
138                 }
139             }
140         }
141         return 0;
142     }
143     
144     /**
145      * Note: This method is for internal use only. Clients should not call this method.
146      * @param method The element to decorate
147      * @return Resulting decorations (combination of JavaElementImageDescriptor.IMPLEMENTS
148      * and JavaElementImageDescriptor.OVERRIDES)
149      * @throws JavaModelException
150      */

151     protected int getOverrideIndicators(IMethod method) throws JavaModelException {
152         CompilationUnit astRoot= JavaPlugin.getDefault().getASTProvider().getAST((IJavaElement) method.getOpenable(), ASTProvider.WAIT_ACTIVE_ONLY, null);
153         if (astRoot != null) {
154             int res= findInHierarchyWithAST(astRoot, method);
155             if (res != -1) {
156                 return res;
157             }
158         }
159         
160         IType type= method.getDeclaringType();
161         
162         MethodOverrideTester methodOverrideTester= SuperTypeHierarchyCache.getMethodOverrideTester(type);
163         IMethod defining= methodOverrideTester.findOverriddenMethod(method, true);
164         if (defining != null) {
165             if (JdtFlags.isAbstract(defining)) {
166                 return JavaElementImageDescriptor.IMPLEMENTS;
167             } else {
168                 return JavaElementImageDescriptor.OVERRIDES;
169             }
170         }
171         return 0;
172     }
173     
174     private int findInHierarchyWithAST(CompilationUnit astRoot, IMethod method) throws JavaModelException {
175         ASTNode node= NodeFinder.perform(astRoot, method.getNameRange());
176         if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) {
177             IMethodBinding binding= ((MethodDeclaration) node.getParent()).resolveBinding();
178             if (binding != null) {
179                 IMethodBinding defining= Bindings.findOverriddenMethod(binding, true);
180                 if (defining != null) {
181                     if (JdtFlags.isAbstract(defining)) {
182                         return JavaElementImageDescriptor.IMPLEMENTS;
183                     } else {
184                         return JavaElementImageDescriptor.OVERRIDES;
185                     }
186                 }
187                 return 0;
188             }
189         }
190         return -1;
191     }
192
193     /**
194      * Note: This method is for internal use only. Clients should not call this method.
195      * @param type The declaring type of the method to decorate.
196      * @param hierarchy The type hierarchy of the declaring type.
197      * @param name The name of the method to find.
198      * @param paramTypes The parameter types of the method to find.
199      * @return The resulting decoration.
200      * @throws JavaModelException
201      * @deprecated Not used anymore. This method is not accurate for methods in generic types.
202      */

203     protected int findInHierarchy(IType type, ITypeHierarchy hierarchy, String JavaDoc name, String JavaDoc[] paramTypes) throws JavaModelException {
204         IType superClass= hierarchy.getSuperclass(type);
205         if (superClass != null) {
206             IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, superClass, name, paramTypes, false);
207             if (res != null && !Flags.isPrivate(res.getFlags()) && JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) {
208                 if (JdtFlags.isAbstract(res)) {
209                     return JavaElementImageDescriptor.IMPLEMENTS;
210                 } else {
211                     return JavaElementImageDescriptor.OVERRIDES;
212                 }
213             }
214         }
215         IType[] interfaces= hierarchy.getSuperInterfaces(type);
216         for (int i= 0; i < interfaces.length; i++) {
217             IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, interfaces[i], name, paramTypes, false);
218             if (res != null) {
219                 if (JdtFlags.isAbstract(res)) {
220                     return JavaElementImageDescriptor.IMPLEMENTS;
221                 } else {
222                     return JavaElementImageDescriptor.OVERRIDES;
223                 }
224             }
225         }
226         return 0;
227     }
228     
229     /* (non-Javadoc)
230      * @see IBaseLabelProvider#addListener(ILabelProviderListener)
231      */

232     public void addListener(ILabelProviderListener listener) {
233     }
234
235     /* (non-Javadoc)
236      * @see IBaseLabelProvider#dispose()
237      */

238     public void dispose() {
239         if (fRegistry != null && fUseNewRegistry) {
240             fRegistry.dispose();
241         }
242     }
243
244     /* (non-Javadoc)
245      * @see IBaseLabelProvider#isLabelProperty(Object, String)
246      */

247     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
248         return true;
249     }
250
251     /* (non-Javadoc)
252      * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
253      */

254     public void removeListener(ILabelProviderListener listener) {
255     }
256     
257     /* (non-Javadoc)
258      * @see org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(java.lang.Object, org.eclipse.jface.viewers.IDecoration)
259      */

260     public void decorate(Object JavaDoc element, IDecoration decoration) {
261         int adornmentFlags= computeAdornmentFlags(element);
262         if ((adornmentFlags & JavaElementImageDescriptor.IMPLEMENTS) != 0) {
263             if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) {
264                 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_IMPLEMENTS);
265             } else {
266                 decoration.addOverlay(JavaPluginImages.DESC_OVR_IMPLEMENTS);
267             }
268         } else if ((adornmentFlags & JavaElementImageDescriptor.OVERRIDES) != 0) {
269             if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) {
270                 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_OVERRIDES);
271             } else {
272                 decoration.addOverlay(JavaPluginImages.DESC_OVR_OVERRIDES);
273             }
274         }
275     }
276
277 }
278
Popular Tags