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