]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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.internal.ui; | |
12 | ||
13 | import java.util.List; | |
14 | ||
15 | import org.eclipse.swt.graphics.ImageData; | |
16 | import org.eclipse.swt.graphics.Point; | |
17 | ||
18 | import org.eclipse.jface.resource.CompositeImageDescriptor; | |
19 | import org.eclipse.jface.resource.ImageDescriptor; | |
20 | import org.eclipse.jface.viewers.IDecoration; | |
21 | ||
22 | import org.eclipse.jdt.core.Flags; | |
23 | import org.eclipse.jdt.core.IClassFile; | |
24 | import org.eclipse.jdt.core.ICompilationUnit; | |
25 | import org.eclipse.jdt.core.IJavaElement; | |
26 | import org.eclipse.jdt.core.IJavaElementDelta; | |
27 | import org.eclipse.jdt.core.IType; | |
28 | import org.eclipse.jdt.core.ITypeRoot; | |
29 | import org.eclipse.jdt.core.JavaCore; | |
30 | import org.eclipse.jdt.core.JavaModelException; | |
31 | import org.eclipse.jdt.core.search.IJavaSearchConstants; | |
32 | import org.eclipse.jdt.core.search.IJavaSearchScope; | |
33 | import org.eclipse.jdt.core.search.SearchEngine; | |
34 | import org.eclipse.jdt.core.search.SearchPattern; | |
35 | import org.eclipse.jdt.core.search.TypeNameRequestor; | |
36 | ||
37 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; | |
38 | ||
39 | public class InterfaceIndicatorLabelDecorator extends AbstractJavaElementLabelDecorator { | |
40 | ||
41 | private static class TypeIndicatorOverlay extends CompositeImageDescriptor { | |
42 | private static Point fgSize; | |
43 | ||
44 | private final ImageDescriptor fType; | |
45 | private final boolean fDeprecated; | |
46 | private final boolean fPackageDefault; | |
47 | ||
48 | public TypeIndicatorOverlay(ImageDescriptor type, boolean deprecated, boolean packageDefault) { | |
49 | fType= type; | |
50 | fDeprecated= deprecated; | |
51 | fPackageDefault= packageDefault; | |
52 | } | |
53 | ||
54 | /* | |
55 | * @see org.eclipse.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int) | |
56 | */ | |
57 | @Override | |
58 | protected void drawCompositeImage(int width, int height) { | |
59 | if (fDeprecated) { | |
60 | ImageData imageData= JavaPluginImages.DESC_OVR_DEPRECATED.getImageData(); | |
61 | drawImage(imageData, -1, 1); // looks better, esp. together with interface indicator | |
62 | } | |
63 | if (fType != null) { // on top of deprecated indicator | |
64 | ImageData imageData= fType.getImageData(); | |
65 | drawImage(imageData, width - imageData.width, 0); | |
66 | } | |
67 | if (fPackageDefault) { | |
68 | ImageData imageData= JavaPluginImages.DESC_OVR_DEFAULT.getImageData(); | |
69 | drawImage(imageData, width - imageData.width, height - imageData.height); | |
70 | } | |
71 | } | |
72 | ||
73 | /* | |
74 | * @see org.eclipse.jface.resource.CompositeImageDescriptor#getSize() | |
75 | */ | |
76 | @Override | |
77 | protected Point getSize() { | |
78 | if (fgSize == null) { | |
79 | ImageData imageData= JavaPluginImages.DESC_OVR_DEPRECATED.getImageData(); | |
80 | fgSize= new Point(imageData.width, imageData.height); | |
81 | } | |
82 | return fgSize; | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * {@inheritDoc} | |
88 | */ | |
89 | @Override | |
90 | public void decorate(Object element, IDecoration decoration) { | |
91 | try { | |
92 | addOverlays(element, decoration); | |
93 | } catch (JavaModelException e) { | |
94 | return; | |
95 | } | |
96 | } | |
97 | ||
98 | private void addOverlays(Object element, IDecoration decoration) throws JavaModelException { | |
99 | if (element instanceof ICompilationUnit) { | |
100 | ICompilationUnit unit= (ICompilationUnit) element; | |
101 | if (unit.isOpen()) { | |
102 | IType mainType= unit.findPrimaryType(); | |
103 | if (mainType != null) { | |
104 | addOverlaysFromFlags(mainType.getFlags(), decoration); | |
105 | } | |
106 | return; | |
107 | } | |
108 | String typeName= JavaCore.removeJavaLikeExtension(unit.getElementName()); | |
109 | addOverlaysWithSearchEngine(unit, typeName, decoration); | |
110 | ||
111 | } else if (element instanceof IClassFile) { | |
112 | IClassFile classFile= (IClassFile) element; | |
113 | if (classFile.isOpen()) { | |
114 | addOverlaysFromFlags(classFile.getType().getFlags(), decoration); | |
115 | } else { | |
116 | String typeName= classFile.getType().getElementName(); | |
117 | addOverlaysWithSearchEngine(classFile, typeName, decoration); | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | private void addOverlaysWithSearchEngine(ITypeRoot element, String typeName, IDecoration decoration) { | |
123 | SearchEngine engine= new SearchEngine(); | |
124 | IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }); | |
125 | ||
126 | class Result extends RuntimeException { | |
127 | private static final long serialVersionUID= 1L; | |
128 | int modifiers; | |
129 | public Result(int modifiers) { | |
130 | this.modifiers= modifiers; | |
131 | } | |
132 | } | |
133 | ||
134 | TypeNameRequestor requestor= new TypeNameRequestor() { | |
135 | @Override | |
136 | public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { | |
137 | if (enclosingTypeNames.length == 0 /*&& Flags.isPublic(modifiers)*/) { | |
138 | throw new Result(modifiers); | |
139 | } | |
140 | } | |
141 | }; | |
142 | ||
143 | try { | |
144 | String packName = element.getParent().getElementName(); | |
145 | int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE; | |
146 | engine.searchAllTypeNames(packName.toCharArray(), matchRule, typeName.toCharArray(), matchRule, IJavaSearchConstants.TYPE, scope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH , null); | |
147 | } catch (Result e) { | |
148 | addOverlaysFromFlags(e.modifiers, decoration); | |
149 | } catch (JavaModelException e) { | |
150 | JavaPlugin.log(e); | |
151 | } | |
152 | ||
153 | } | |
154 | ||
155 | private void addOverlaysFromFlags(int flags, IDecoration decoration) { | |
156 | ImageDescriptor type; | |
157 | if (Flags.isAnnotation(flags)) { | |
158 | type= JavaPluginImages.DESC_OVR_ANNOTATION; | |
159 | } else if (Flags.isEnum(flags)) { | |
160 | type= JavaPluginImages.DESC_OVR_ENUM; | |
161 | } else if (Flags.isInterface(flags)) { | |
162 | type= JavaPluginImages.DESC_OVR_INTERFACE; | |
163 | } else if (/* is class */ Flags.isAbstract(flags)) { | |
164 | type= JavaPluginImages.DESC_OVR_ABSTRACT_CLASS; | |
165 | } else { | |
166 | type= null; | |
167 | } | |
168 | ||
169 | boolean deprecated= Flags.isDeprecated(flags); | |
170 | boolean packageDefault= Flags.isPackageDefault(flags); | |
171 | ||
172 | /* Each decoration position can only be used once. Since we don't want to take all positions | |
173 | * away from other decorators, we confine ourselves to only use the top right position. */ | |
174 | ||
175 | if (type != null && !deprecated && !packageDefault) { | |
176 | decoration.addOverlay(type, IDecoration.TOP_RIGHT); | |
177 | ||
178 | } else if (type == null && deprecated && !packageDefault) { | |
179 | decoration.addOverlay(JavaPluginImages.DESC_OVR_DEPRECATED, IDecoration.TOP_RIGHT); | |
180 | ||
181 | } else if (type != null || deprecated || packageDefault) { | |
182 | decoration.addOverlay(new TypeIndicatorOverlay(type, deprecated, packageDefault), IDecoration.TOP_RIGHT); | |
183 | } | |
184 | } | |
185 | ||
186 | @Override | |
187 | protected void processDelta(IJavaElementDelta delta, List<IJavaElement> result) { | |
188 | IJavaElement elem= delta.getElement(); | |
189 | ||
190 | boolean isChanged= delta.getKind() == IJavaElementDelta.CHANGED; | |
191 | boolean isRemoved= delta.getKind() == IJavaElementDelta.REMOVED; | |
192 | int flags= delta.getFlags(); | |
193 | ||
194 | switch (elem.getElementType()) { | |
195 | case IJavaElement.JAVA_PROJECT: | |
196 | if (isRemoved || (isChanged && | |
197 | (flags & IJavaElementDelta.F_CLOSED) != 0)) { | |
198 | return; | |
199 | } | |
200 | processChildrenDelta(delta, result); | |
201 | return; | |
202 | case IJavaElement.PACKAGE_FRAGMENT_ROOT: | |
203 | if (isRemoved || (isChanged && ( | |
204 | (flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0 || | |
205 | (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0))) { | |
206 | return; | |
207 | } | |
208 | processChildrenDelta(delta, result); | |
209 | return; | |
210 | case IJavaElement.PACKAGE_FRAGMENT: | |
211 | if (isRemoved) | |
212 | return; | |
213 | processChildrenDelta(delta, result); | |
214 | return; | |
215 | case IJavaElement.TYPE: | |
216 | case IJavaElement.CLASS_FILE: | |
217 | return; | |
218 | case IJavaElement.JAVA_MODEL: | |
219 | processChildrenDelta(delta, result); | |
220 | return; | |
221 | case IJavaElement.COMPILATION_UNIT: | |
222 | // Not the primary compilation unit. Ignore it | |
223 | if (!JavaModelUtil.isPrimary((ICompilationUnit) elem)) { | |
224 | return; | |
225 | } | |
226 | ||
227 | if (isChanged && ((flags & IJavaElementDelta.F_CONTENT) != 0 || (flags & IJavaElementDelta.F_FINE_GRAINED) != 0)) { | |
228 | if (delta.getAffectedChildren().length == 0) | |
229 | return; | |
230 | ||
231 | result.add(elem); | |
232 | } | |
233 | return; | |
234 | default: | |
235 | // fields, methods, imports ect | |
236 | return; | |
237 | } | |
238 | } | |
239 | ||
240 | } |