Package: ProblemMarkerManager$ProjectErrorVisitor

ProblemMarkerManager$ProjectErrorVisitor

nameinstructionbranchcomplexitylinemethod
ProblemMarkerManager.ProjectErrorVisitor(HashSet)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
checkInvalidate(IResourceDelta, IResource)
M: 30 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
isErrorDelta(IResourceDelta)
M: 57 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
visit(IResourceDelta)
M: 24 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.internal.viewsupport;
23:
24: import java.util.HashSet;
25: import java.util.Set;
26:
27: import org.eclipse.core.resources.IMarker;
28: import org.eclipse.core.resources.IMarkerDelta;
29: import org.eclipse.core.resources.IProject;
30: import org.eclipse.core.resources.IResource;
31: import org.eclipse.core.resources.IResourceChangeEvent;
32: import org.eclipse.core.resources.IResourceChangeListener;
33: import org.eclipse.core.resources.IResourceDelta;
34: import org.eclipse.core.resources.IResourceDeltaVisitor;
35: import org.eclipse.core.runtime.CoreException;
36: import org.eclipse.core.runtime.IProgressMonitor;
37: import org.eclipse.core.runtime.IStatus;
38: import org.eclipse.core.runtime.ListenerList;
39: import org.eclipse.core.runtime.Status;
40: import org.eclipse.jface.text.source.AnnotationModelEvent;
41: import org.eclipse.jface.text.source.IAnnotationModel;
42: import org.eclipse.jface.text.source.IAnnotationModelListener;
43: import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
44: import org.eclipse.swt.widgets.Display;
45: import org.eclipse.ui.progress.UIJob;
46: import org.overture.ide.ui.VdmUIPlugin;
47: import org.overture.ide.ui.internal.util.SWTUtil;
48:
49: /**
50: * Listens to resource deltas and filters for marker changes of type IMarker.PROBLEM
51: * Viewers showing error ticks should register as listener to
52: * this type.
53: */
54: public class ProblemMarkerManager implements IResourceChangeListener, IAnnotationModelListener , IAnnotationModelListenerExtension {
55:
56:         /**
57:          * Visitors used to look if the element change delta contains a marker change.
58:          */
59:         private static class ProjectErrorVisitor implements IResourceDeltaVisitor {
60:
61:                 private HashSet<IResource> fChangedElements;
62:
63:                 public ProjectErrorVisitor(HashSet<IResource> changedElements) {
64:                         fChangedElements= changedElements;
65:                 }
66:
67:                 public boolean visit(IResourceDelta delta) throws CoreException {
68:                         IResource res= delta.getResource();
69:•                        if (res instanceof IProject && delta.getKind() == IResourceDelta.CHANGED) {
70:                                 IProject project= (IProject) res;
71:•                                if (!project.isAccessible()) {
72:                                         // only track open Java projects
73:                                         return false;
74:                                 }
75:                         }
76:                         checkInvalidate(delta, res);
77:                         return true;
78:                 }
79:
80:                 private void checkInvalidate(IResourceDelta delta, IResource resource) {
81:                         int kind= delta.getKind();
82:•                        if (kind == IResourceDelta.REMOVED || kind == IResourceDelta.ADDED || (kind == IResourceDelta.CHANGED && isErrorDelta(delta))) {
83:                                 // invalidate the resource and all parents
84:•                                while (resource.getType() != IResource.ROOT && fChangedElements.add(resource)) {
85:                                         resource= resource.getParent();
86:                                 }
87:                         }
88:                 }
89:
90:                 private boolean isErrorDelta(IResourceDelta delta) {
91:•                        if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) {
92:                                 IMarkerDelta[] markerDeltas= delta.getMarkerDeltas();
93:•                                for (int i= 0; i < markerDeltas.length; i++) {
94:•                                        if (markerDeltas[i].isSubtypeOf(IMarker.PROBLEM)) {
95:                                                 int kind= markerDeltas[i].getKind();
96:•                                                if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED)
97:                                                         return true;
98:                                                 int severity= markerDeltas[i].getAttribute(IMarker.SEVERITY, -1);
99:                                                 int newSeverity= markerDeltas[i].getMarker().getAttribute(IMarker.SEVERITY, -1);
100:•                                                if (newSeverity != severity)
101:                                                         return true;
102:                                         }
103:                                 }
104:                         }
105:                         return false;
106:                 }
107:         }
108:
109:         private ListenerList fListeners;
110:
111:         private Set<IResource> fResourcesWithMarkerChanges;
112:         private Set<Object> fResourcesWithAnnotationChanges;
113:
114:         private UIJob fNotifierJob;
115:
116:         public ProblemMarkerManager() {
117:                 fListeners= new ListenerList();
118:                 fResourcesWithMarkerChanges= new HashSet<IResource>();
119:                 fResourcesWithAnnotationChanges= new HashSet<Object>();
120:         }
121:
122:         /*
123:          * @see IResourceChangeListener#resourceChanged
124:          */
125:         public void resourceChanged(IResourceChangeEvent event) {
126:                 HashSet<IResource> changedElements= new HashSet<IResource>();
127:
128:                 try {
129:                         IResourceDelta delta= event.getDelta();
130:                         if (delta != null)
131:                                 delta.accept(new ProjectErrorVisitor(changedElements));
132:                 } catch (CoreException e) {
133:                         VdmUIPlugin.printe(e);
134:                 }
135:
136:                 if (!changedElements.isEmpty()) {
137:                         boolean hasChanges= false;
138:                         synchronized (this) {
139:                                 if (fResourcesWithMarkerChanges.isEmpty()) {
140:                                         fResourcesWithMarkerChanges= changedElements;
141:                                         hasChanges= true;
142:                                 } else {
143:                                         hasChanges= fResourcesWithMarkerChanges.addAll(changedElements);
144:                                 }
145:                         }
146:                         if (hasChanges) {
147:                                 fireChanges();
148:                         }
149:                 }
150:         }
151:
152:         /* (non-Javadoc)
153:          * @see IAnnotationModelListener#modelChanged(IAnnotationModel)
154:          */
155:         public void modelChanged(IAnnotationModel model) {
156:                 // no action
157:         }
158:
159:         /* (non-Javadoc)
160:          * @see IAnnotationModelListenerExtension#modelChanged(AnnotationModelEvent)
161:          */
162:         public void modelChanged(AnnotationModelEvent event) {
163: //                if (event instanceof CompilationUnitAnnotationModelEvent) {
164: //                        CompilationUnitAnnotationModelEvent cuEvent= (CompilationUnitAnnotationModelEvent) event;
165: //                        if (cuEvent.includesProblemMarkerAnnotationChanges()) {
166: //                                boolean hasChanges= false;
167: //                                synchronized (this) {
168: //                                        IResource changedResource= cuEvent.getUnderlyingResource();
169: //                                        hasChanges= fResourcesWithAnnotationChanges.add(changedResource);
170: //                                }
171: //                                if (hasChanges) {
172: //                                        fireChanges();
173: //                                }
174: //                        }
175: //                }
176:         }
177:
178:
179:         /**
180:          * Adds a listener for problem marker changes.
181:          * @param listener the listener to add
182:          */
183:         public void addListener(IProblemChangedListener listener) {
184:                 if (fListeners.isEmpty()) {
185:                         VdmUIPlugin.getWorkspace().addResourceChangeListener(this);
186:                         //VdmUIPlugin.getDefault().getCompilationUnitDocumentProvider().addGlobalAnnotationModelListener(this);
187:                 }
188:                 fListeners.add(listener);
189:         }
190:
191:         /**
192:          * Removes a <code>IProblemChangedListener</code>.
193:          * @param listener the listener to remove
194:          */
195:         public void removeListener(IProblemChangedListener listener) {
196:                 fListeners.remove(listener);
197:                 if (fListeners.isEmpty()) {
198:                         VdmUIPlugin.getWorkspace().removeResourceChangeListener(this);
199: //                        JavaPlugin.getDefault().getCompilationUnitDocumentProvider().removeGlobalAnnotationModelListener(this);
200:                 }
201:         }
202:
203:         private void fireChanges() {
204:                 Display display= SWTUtil.getStandardDisplay();
205:                 if (display != null && !display.isDisposed()) {
206:                         postAsyncUpdate(display);
207:                 }
208:         }
209:
210:         private void postAsyncUpdate(final Display display) {
211:                 if (fNotifierJob == null) {
212:                         fNotifierJob= new UIJob(display, "VdmUIMessages.ProblemMarkerManager_problem_marker_update_job_description") {
213:                                 public IStatus runInUIThread(IProgressMonitor monitor) {
214:                                         runPendingUpdates();
215:                                         return Status.OK_STATUS;
216:                                 }
217:                         };
218:                         fNotifierJob.setSystem(true);
219:                 }
220:                 fNotifierJob.schedule();
221:         }
222:
223:         /**
224:          * Notify all IProblemChangedListener. Must be called in the display thread.
225:          */
226:         private void runPendingUpdates() {
227:                 IResource[] markerResources= null;
228:                 IResource[] annotationResources= null;
229:                 synchronized (this) {
230:                         if (!fResourcesWithMarkerChanges.isEmpty()) {
231:                                 markerResources= (IResource[]) fResourcesWithMarkerChanges.toArray(new IResource[fResourcesWithMarkerChanges.size()]);
232:                                 fResourcesWithMarkerChanges.clear();
233:                         }
234:                         if (!fResourcesWithAnnotationChanges.isEmpty()) {
235:                                 annotationResources= (IResource[]) fResourcesWithAnnotationChanges.toArray(new IResource[fResourcesWithAnnotationChanges.size()]);
236:                                 fResourcesWithAnnotationChanges.clear();
237:                         }
238:                 }
239:                 Object[] listeners= fListeners.getListeners();
240:                 for (int i= 0; i < listeners.length; i++) {
241:                         IProblemChangedListener curr= (IProblemChangedListener) listeners[i];
242:                         if (markerResources != null) {
243:                                 curr.problemsChanged(markerResources, true);
244:                         }
245:                         if (annotationResources != null) {
246:                                 curr.problemsChanged(annotationResources, false);
247:                         }
248:                 }
249:         }
250:
251: }