|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.ui.views.markers.internal; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Iterator; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
import org.eclipse.core.resources.IMarker; |
| 18 |
import org.eclipse.core.resources.IMarkerDelta; |
| 19 |
import org.eclipse.core.resources.IResource; |
| 20 |
import org.eclipse.core.resources.IResourceChangeEvent; |
| 21 |
import org.eclipse.core.resources.IResourceChangeListener; |
| 22 |
import org.eclipse.core.resources.IResourceDelta; |
| 23 |
import org.eclipse.core.resources.IWorkspaceRoot; |
| 24 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 25 |
import org.eclipse.core.runtime.CoreException; |
| 26 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 27 |
import org.eclipse.core.runtime.IStatus; |
| 28 |
import org.eclipse.core.runtime.Platform; |
| 29 |
import org.eclipse.core.runtime.Status; |
| 30 |
import org.eclipse.core.runtime.jobs.IJobManager; |
| 31 |
import org.eclipse.core.runtime.jobs.Job; |
| 32 |
import org.eclipse.jface.viewers.deferred.AbstractConcurrentContentProvider; |
| 33 |
import org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener; |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 3.1 |
| 37 |
*/ |
| 38 |
public class MarkerProvider extends AbstractConcurrentContentProvider { |
| 39 |
|
| 40 |
private static final String WAITING_FOR_WORKSPACE_CHANGES_TO_FINISH = Messages |
| 41 |
.getString("MarkerView.waiting_on_changes"); //$NON-NLS-1$ |
| 42 |
|
| 43 |
private static final String SEARCHING_FOR_MARKERS = Messages |
| 44 |
.getString("MarkerView.searching_for_markers"); //$NON-NLS-1$ |
| 45 |
|
| 46 |
private static final String REFRESHING_MARKER_COUNTS = Messages |
| 47 |
.getString("MarkerView.refreshing_counts"); //$NON-NLS-1$ |
| 48 |
|
| 49 |
private IWorkspaceRoot getWorkspaceRoot() { |
| 50 |
return ResourcesPlugin.getWorkspace().getRoot(); |
| 51 |
} |
| 52 |
|
| 53 |
private Job searchJob = new Job(SEARCHING_FOR_MARKERS) { |
| 54 |
/* (non-Javadoc) |
| 55 |
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) |
| 56 |
*/ |
| 57 |
protected IStatus run(IProgressMonitor monitor) { |
| 58 |
doRefresh(monitor); |
| 59 |
return Status.OK_STATUS; |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
private MarkerConverter converter = new MarkerConverter(); |
| 64 |
|
| 65 |
private int totalMarkers = 0; |
| 66 |
private boolean markerCountDirty = false; |
| 67 |
private MarkerList currentMarkers = new MarkerList(); |
| 68 |
private ArrayList awaitingRefresh = new ArrayList(); |
| 69 |
|
| 70 |
IResourceChangeListener resourceListener = new IResourceChangeListener() { |
| 71 |
public void resourceChanged(IResourceChangeEvent event) { |
| 72 |
MarkerProvider.this.resourceChanged(event); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
public MarkerProvider() { |
| 77 |
ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceListener); |
| 78 |
} |
| 79 |
|
| 80 |
public void resourceChanged(IResourceChangeEvent event) { |
| 81 |
String[] markerTypes = getMarkerTypes(); |
| 82 |
|
| 83 |
boolean refreshNeeded = false; |
| 84 |
|
| 85 |
for (int idx = 0; idx < markerTypes.length; idx++) { |
| 86 |
IMarkerDelta[] markerDeltas = event.findMarkerDeltas( |
| 87 |
markerTypes[idx], true); |
| 88 |
List changes = new ArrayList(markerDeltas.length / 3); |
| 89 |
List additions = new ArrayList(markerDeltas.length / 3); |
| 90 |
List removals = new ArrayList(markerDeltas.length / 3); |
| 91 |
|
| 92 |
examineDelta(markerDeltas, changes, additions, removals); |
| 93 |
|
| 94 |
fireRemove(removals.toArray()); |
| 95 |
fireAdd(additions.toArray()); |
| 96 |
fireUpdate(changes.toArray()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private void examineDelta(IMarkerDelta[] deltas, List changes, List additions, List removals) { |
| 101 |
for (int idx = 0; idx < deltas.length; idx++) { |
| 102 |
IMarkerDelta delta = deltas[idx]; |
| 103 |
int kind = delta.getKind(); |
| 104 |
|
| 105 |
if (kind == IResourceDelta.CHANGED) { |
| 106 |
changes.add(converter.getMarker(deltas[idx].getMarker())); |
| 107 |
} else if (kind == IResourceDelta.ADDED) { |
| 108 |
additions.add(converter.getMarker(deltas[idx].getMarker())); |
| 109 |
} else if (kind == IResourceDelta.REMOVED) { |
| 110 |
removals.add(converter.getMarker(deltas[idx].getMarker())); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public void dispose() { |
| 116 |
ResourcesPlugin.getWorkspace().removeResourceChangeListener(resourceListener); |
| 117 |
} |
| 118 |
|
| 119 |
public int getTotalMarkers() { |
| 120 |
return totalMarkers; |
| 121 |
} |
| 122 |
|
| 123 |
private String[] getMarkerTypes() { |
| 124 |
return new String[] {IMarker.MARKER}; |
| 125 |
} |
| 126 |
|
| 127 |
/* (non-Javadoc) |
| 128 |
* @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#getElements(org.eclipse.core.runtime.IProgressMonitor) |
| 129 |
*/ |
| 130 |
public void doRefresh(IProgressMonitor monitor) { |
| 131 |
IJobManager jobMan = Platform.getJobManager(); |
| 132 |
IWorkspaceRoot root = getWorkspaceRoot(); |
| 133 |
|
| 134 |
ArrayList result = new ArrayList(); |
| 135 |
|
| 136 |
try { |
| 137 |
monitor.subTask(WAITING_FOR_WORKSPACE_CHANGES_TO_FINISH); |
| 138 |
|
| 139 |
jobMan.beginRule(root, monitor); |
| 140 |
|
| 141 |
if (monitor.isCanceled()) { |
| 142 |
throw new InterruptedException(); |
| 143 |
} |
| 144 |
|
| 145 |
monitor.subTask(SEARCHING_FOR_MARKERS); |
| 146 |
IMarker[] markers = root.findMarkers(IMarker.MARKER, |
| 147 |
true, IResource.DEPTH_INFINITE); |
| 148 |
|
| 149 |
for (int i = 0; i < markers.length; i++) { |
| 150 |
IMarker marker = markers[i]; |
| 151 |
|
| 152 |
ConcreteMarker next = converter.getMarker(marker); |
| 153 |
|
| 154 |
next.refresh(); |
| 155 |
|
| 156 |
result.add(next); |
| 157 |
} |
| 158 |
} catch (CoreException e) { |
| 159 |
// TODO: SHOULD LOG THIS!!!!!!!!!!!! |
| 160 |
} catch (InterruptedException e) { |
| 161 |
} finally { |
| 162 |
jobMan.endRule(root); |
| 163 |
} |
| 164 |
|
| 165 |
synchronized(awaitingRefresh) { |
| 166 |
for (Iterator iter = awaitingRefresh.iterator(); iter.hasNext();) { |
| 167 |
IConcurrentContentProviderListener listener = (IConcurrentContentProviderListener) iter.next(); |
| 168 |
|
| 169 |
listener.setContents(result.toArray()); |
| 170 |
} |
| 171 |
|
| 172 |
awaitingRefresh.clear(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* (non-Javadoc) |
| 177 |
* @see org.eclipse.jface.viewers.deferred.IConcurrentContentProvider#requestUpdate(org.eclipse.jface.viewers.deferred.IConcurrentContentProviderListener) |
| 178 |
*/ |
| 179 |
public void requestUpdate(IConcurrentContentProviderListener listener) { |
| 180 |
synchronized(awaitingRefresh) { |
| 181 |
awaitingRefresh.add(listener); |
| 182 |
searchJob.schedule(); |
| 183 |
} |
| 184 |
} |
| 185 |
} |