1 11 package org.eclipse.jdt.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.ListenerList; 17 18 import org.eclipse.swt.graphics.Image; 19 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.viewers.IDecoration; 22 import org.eclipse.jface.viewers.ILabelDecorator; 23 import org.eclipse.jface.viewers.ILabelProviderListener; 24 import org.eclipse.jface.viewers.ILightweightLabelDecorator; 25 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 26 27 import org.eclipse.jdt.core.ElementChangedEvent; 28 import org.eclipse.jdt.core.Flags; 29 import org.eclipse.jdt.core.IClassFile; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.IElementChangedListener; 32 import org.eclipse.jdt.core.IJavaElement; 33 import org.eclipse.jdt.core.IJavaElementDelta; 34 import org.eclipse.jdt.core.IType; 35 import org.eclipse.jdt.core.ITypeRoot; 36 import org.eclipse.jdt.core.JavaCore; 37 import org.eclipse.jdt.core.JavaModelException; 38 import org.eclipse.jdt.core.search.IJavaSearchConstants; 39 import org.eclipse.jdt.core.search.IJavaSearchScope; 40 import org.eclipse.jdt.core.search.SearchEngine; 41 import org.eclipse.jdt.core.search.SearchPattern; 42 import org.eclipse.jdt.core.search.TypeNameRequestor; 43 44 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 45 46 public class InterfaceIndicatorLabelDecorator implements ILabelDecorator, ILightweightLabelDecorator { 47 48 private class IntefaceIndicatorChangeListener implements IElementChangedListener { 49 50 53 public void elementChanged(ElementChangedEvent event) { 54 List changed= new ArrayList (); 55 processDelta(event.getDelta(), changed); 56 if (changed.size() == 0) 57 return; 58 59 fireChange((IJavaElement[])changed.toArray(new IJavaElement[changed.size()])); 60 } 61 62 } 63 64 private ListenerList fListeners; 65 private IElementChangedListener fChangeListener; 66 67 public InterfaceIndicatorLabelDecorator() { 68 } 69 70 73 public Image decorateImage(Image image, Object element) { 74 return null; 75 } 76 77 80 public String decorateText(String text, Object element) { 81 return text; 82 } 83 84 87 public void addListener(ILabelProviderListener listener) { 88 if (fChangeListener == null) { 89 fChangeListener= new IntefaceIndicatorChangeListener(); 90 JavaCore.addElementChangedListener(fChangeListener); 91 } 92 93 if (fListeners == null) { 94 fListeners= new ListenerList(); 95 } 96 97 fListeners.add(listener); 98 } 99 100 103 public void dispose() { 104 if (fChangeListener != null) { 105 JavaCore.removeElementChangedListener(fChangeListener); 106 fChangeListener= null; 107 } 108 if (fListeners != null) { 109 Object [] listeners= fListeners.getListeners(); 110 for (int i= 0; i < listeners.length; i++) { 111 fListeners.remove(listeners[i]); 112 } 113 fListeners= null; 114 } 115 } 116 117 120 public boolean isLabelProperty(Object element, String property) { 121 return false; 122 } 123 124 127 public void removeListener(ILabelProviderListener listener) { 128 if (fListeners == null) 129 return; 130 131 fListeners.remove(listener); 132 133 if (fListeners.isEmpty() && fChangeListener != null) { 134 JavaCore.removeElementChangedListener(fChangeListener); 135 fChangeListener= null; 136 } 137 } 138 139 142 public void decorate(Object element, IDecoration decoration) { 143 try { 144 ImageDescriptor overlay= getOverlay(element); 145 if (overlay == null) 146 return; 147 148 decoration.addOverlay(overlay, IDecoration.TOP_RIGHT); 149 } catch (JavaModelException e) { 150 return; 151 } 152 } 153 154 private ImageDescriptor getOverlay(Object element) throws JavaModelException { 155 if (element instanceof ICompilationUnit) { 156 ICompilationUnit unit= (ICompilationUnit) element; 157 if (unit.isOpen()) { 158 IType mainType= unit.findPrimaryType(); 159 if (mainType != null) { 160 return getOverlayFromFlags(mainType.getFlags()); 161 } 162 return null; 163 } 164 String typeName= JavaCore.removeJavaLikeExtension(unit.getElementName()); 165 return getOverlayWithSearchEngine(unit, typeName); 166 } else if (element instanceof IClassFile) { 167 IClassFile classFile= (IClassFile) element; 168 if (classFile.isOpen()) { 169 return getOverlayFromFlags(classFile.getType().getFlags()); 170 } 171 String typeName= classFile.getType().getElementName(); 172 return getOverlayWithSearchEngine(classFile, typeName); 173 } 174 return null; 175 } 176 177 private ImageDescriptor getOverlayWithSearchEngine(ITypeRoot element, String typeName) { 178 SearchEngine engine= new SearchEngine(); 179 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }); 180 181 class Result extends RuntimeException { 182 private static final long serialVersionUID= 1L; 183 int modifiers; 184 public Result(int modifiers) { 185 this.modifiers= modifiers; 186 } 187 } 188 189 TypeNameRequestor requestor= new TypeNameRequestor() { 190 public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { 191 if (enclosingTypeNames.length == 0 && Flags.isPublic(modifiers)) { 192 throw new Result(modifiers); 193 } 194 } 195 }; 196 197 try { 198 String packName = element.getParent().getElementName(); 199 int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE; 200 engine.searchAllTypeNames(packName.toCharArray(), matchRule, typeName.toCharArray(), matchRule, IJavaSearchConstants.TYPE, scope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH , null); 201 } catch (Result e) { 202 return getOverlayFromFlags(e.modifiers); 203 } catch (JavaModelException e) { 204 JavaPlugin.log(e); 205 } 206 return null; 207 208 } 209 210 private ImageDescriptor getOverlayFromFlags(int flags) { 211 if (Flags.isAnnotation(flags)) { 212 return JavaPluginImages.DESC_OVR_ANNOTATION; 213 } else if (Flags.isEnum(flags)) { 214 return JavaPluginImages.DESC_OVR_ENUM; 215 } else if (Flags.isInterface(flags)) { 216 return JavaPluginImages.DESC_OVR_INTERFACE; 217 } else if ( Flags.isAbstract(flags)) { 218 return JavaPluginImages.DESC_OVR_ABSTRACT_CLASS; 219 } 220 return null; 221 } 222 223 private void fireChange(IJavaElement[] elements) { 224 if (fListeners != null && !fListeners.isEmpty()) { 225 LabelProviderChangedEvent event= new LabelProviderChangedEvent(this, elements); 226 Object [] listeners= fListeners.getListeners(); 227 for (int i= 0; i < listeners.length; i++) { 228 ((ILabelProviderListener) listeners[i]).labelProviderChanged(event); 229 } 230 } 231 } 232 233 private void processDelta(IJavaElementDelta delta, List result) { 234 IJavaElement elem= delta.getElement(); 235 236 boolean isChanged= delta.getKind() == IJavaElementDelta.CHANGED; 237 boolean isRemoved= delta.getKind() == IJavaElementDelta.REMOVED; 238 int flags= delta.getFlags(); 239 240 switch (elem.getElementType()) { 241 case IJavaElement.JAVA_PROJECT: 242 if (isRemoved || (isChanged && 243 (flags & IJavaElementDelta.F_CLOSED) != 0)) { 244 return; 245 } 246 processChildrenDelta(delta, result); 247 return; 248 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 249 if (isRemoved || (isChanged && ( 250 (flags & IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED) != 0 || 251 (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0))) { 252 return; 253 } 254 processChildrenDelta(delta, result); 255 return; 256 case IJavaElement.PACKAGE_FRAGMENT: 257 if (isRemoved) 258 return; 259 processChildrenDelta(delta, result); 260 return; 261 case IJavaElement.TYPE: 262 case IJavaElement.CLASS_FILE: 263 return; 264 case IJavaElement.JAVA_MODEL: 265 processChildrenDelta(delta, result); 266 return; 267 case IJavaElement.COMPILATION_UNIT: 268 if (!JavaModelUtil.isPrimary((ICompilationUnit) elem)) { 270 return; 271 } 272 273 if (isChanged && ((flags & IJavaElementDelta.F_CONTENT) != 0 || (flags & IJavaElementDelta.F_FINE_GRAINED) != 0)) { 274 if (delta.getAffectedChildren().length == 0) 275 return; 276 277 result.add(elem); 278 } 279 return; 280 default: 281 return; 283 } 284 } 285 286 private boolean processChildrenDelta(IJavaElementDelta delta, List result) { 287 IJavaElementDelta[] children= delta.getAffectedChildren(); 288 for (int i= 0; i < children.length; i++) { 289 processDelta(children[i], result); 290 } 291 return false; 292 } 293 294 } 295
| Popular Tags
|