1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 14 import java.util.HashSet ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.ListenerList; 18 19 import org.eclipse.core.resources.IMarker; 20 import org.eclipse.core.resources.IMarkerDelta; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IResourceChangeEvent; 24 import org.eclipse.core.resources.IResourceChangeListener; 25 import org.eclipse.core.resources.IResourceDelta; 26 import org.eclipse.core.resources.IResourceDeltaVisitor; 27 28 import org.eclipse.swt.widgets.Display; 29 30 import org.eclipse.jface.text.source.AnnotationModelEvent; 31 import org.eclipse.jface.text.source.IAnnotationModel; 32 import org.eclipse.jface.text.source.IAnnotationModelListener; 33 import org.eclipse.jface.text.source.IAnnotationModelListenerExtension; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitAnnotationModelEvent; 37 import org.eclipse.jdt.internal.ui.util.SWTUtil; 38 39 44 public class ProblemMarkerManager implements IResourceChangeListener, IAnnotationModelListener , IAnnotationModelListenerExtension { 45 46 49 private static class ProjectErrorVisitor implements IResourceDeltaVisitor { 50 51 private HashSet fChangedElements; 52 53 public ProjectErrorVisitor(HashSet changedElements) { 54 fChangedElements= changedElements; 55 } 56 57 public boolean visit(IResourceDelta delta) throws CoreException { 58 IResource res= delta.getResource(); 59 if (res instanceof IProject && delta.getKind() == IResourceDelta.CHANGED) { 60 IProject project= (IProject) res; 61 if (!project.isAccessible()) { 62 return false; 64 } 65 } 66 checkInvalidate(delta, res); 67 return true; 68 } 69 70 private void checkInvalidate(IResourceDelta delta, IResource resource) { 71 int kind= delta.getKind(); 72 if (kind == IResourceDelta.REMOVED || kind == IResourceDelta.ADDED || (kind == IResourceDelta.CHANGED && isErrorDelta(delta))) { 73 while (resource.getType() != IResource.ROOT && fChangedElements.add(resource)) { 75 resource= resource.getParent(); 76 } 77 } 78 } 79 80 private boolean isErrorDelta(IResourceDelta delta) { 81 if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) { 82 IMarkerDelta[] markerDeltas= delta.getMarkerDeltas(); 83 for (int i= 0; i < markerDeltas.length; i++) { 84 if (markerDeltas[i].isSubtypeOf(IMarker.PROBLEM)) { 85 int kind= markerDeltas[i].getKind(); 86 if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED) 87 return true; 88 int severity= markerDeltas[i].getAttribute(IMarker.SEVERITY, -1); 89 int newSeverity= markerDeltas[i].getMarker().getAttribute(IMarker.SEVERITY, -1); 90 if (newSeverity != severity) 91 return true; 92 } 93 } 94 } 95 return false; 96 } 97 } 98 99 private ListenerList fListeners; 100 101 102 public ProblemMarkerManager() { 103 fListeners= new ListenerList(); 104 } 105 106 109 public void resourceChanged(IResourceChangeEvent event) { 110 HashSet changedElements= new HashSet (); 111 112 try { 113 IResourceDelta delta= event.getDelta(); 114 if (delta != null) 115 delta.accept(new ProjectErrorVisitor(changedElements)); 116 } catch (CoreException e) { 117 JavaPlugin.log(e.getStatus()); 118 } 119 120 if (!changedElements.isEmpty()) { 121 IResource[] changes= (IResource[]) changedElements.toArray(new IResource[changedElements.size()]); 122 fireChanges(changes, true); 123 } 124 } 125 126 129 public void modelChanged(IAnnotationModel model) { 130 } 132 133 136 public void modelChanged(AnnotationModelEvent event) { 137 if (event instanceof CompilationUnitAnnotationModelEvent) { 138 CompilationUnitAnnotationModelEvent cuEvent= (CompilationUnitAnnotationModelEvent) event; 139 if (cuEvent.includesProblemMarkerAnnotationChanges()) { 140 IResource[] changes= new IResource[] { cuEvent.getUnderlyingResource() }; 141 fireChanges(changes, false); 142 } 143 } 144 } 145 146 147 150 public void addListener(IProblemChangedListener listener) { 151 if (fListeners.isEmpty()) { 152 JavaPlugin.getWorkspace().addResourceChangeListener(this); 153 JavaPlugin.getDefault().getCompilationUnitDocumentProvider().addGlobalAnnotationModelListener(this); 154 } 155 fListeners.add(listener); 156 } 157 158 161 public void removeListener(IProblemChangedListener listener) { 162 fListeners.remove(listener); 163 if (fListeners.isEmpty()) { 164 JavaPlugin.getWorkspace().removeResourceChangeListener(this); 165 JavaPlugin.getDefault().getCompilationUnitDocumentProvider().removeGlobalAnnotationModelListener(this); 166 } 167 } 168 169 private void fireChanges(final IResource[] changes, final boolean isMarkerChange) { 170 Display display= SWTUtil.getStandardDisplay(); 171 if (display != null && !display.isDisposed()) { 172 display.asyncExec(new Runnable () { 173 public void run() { 174 Object [] listeners= fListeners.getListeners(); 175 for (int i= 0; i < listeners.length; i++) { 176 IProblemChangedListener curr= (IProblemChangedListener) listeners[i]; 177 curr.problemsChanged(changes, isMarkerChange); 178 } 179 } 180 }); 181 } 182 } 183 184 } 185 | Popular Tags |