KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > ProblemMarkerManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.viewsupport;
12
13
14 import java.util.HashSet JavaDoc;
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 /**
40  * Listens to resource deltas and filters for marker changes of type IMarker.PROBLEM
41  * Viewers showing error ticks should register as listener to
42  * this type.
43  */

44 public class ProblemMarkerManager implements IResourceChangeListener, IAnnotationModelListener , IAnnotationModelListenerExtension {
45
46     /**
47      * Visitors used to look if the element change delta contains a marker change.
48      */

49     private static class ProjectErrorVisitor implements IResourceDeltaVisitor {
50
51         private HashSet JavaDoc fChangedElements;
52         
53         public ProjectErrorVisitor(HashSet JavaDoc 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                     // only track open Java projects
63
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                 // invalidate the resource and all parents
74
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     /*
107      * @see IResourceChangeListener#resourceChanged
108      */

109     public void resourceChanged(IResourceChangeEvent event) {
110         HashSet JavaDoc changedElements= new HashSet JavaDoc();
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     /* (non-Javadoc)
127      * @see IAnnotationModelListener#modelChanged(IAnnotationModel)
128      */

129     public void modelChanged(IAnnotationModel model) {
130         // no action
131
}
132
133     /* (non-Javadoc)
134      * @see IAnnotationModelListenerExtension#modelChanged(AnnotationModelEvent)
135      */

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     /**
148      * Adds a listener for problem marker changes.
149      */

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     /**
159      * Removes a <code>IProblemChangedListener</code>.
160      */

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 JavaDoc() {
173                 public void run() {
174                     Object JavaDoc[] 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