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