1 11 package org.eclipse.ui.views.markers.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.eclipse.core.resources.IMarker; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.ResourcesPlugin; 25 import org.eclipse.core.runtime.CoreException; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 28 31 public class MarkerList { 32 33 private int[] markerCounts = null; 34 35 private ConcreteMarker[] markers; 36 37 41 private Map markerTable; 42 43 46 public MarkerList() { 47 this(new ConcreteMarker[0]); 48 } 49 50 public MarkerList(Collection markers) { 51 this((ConcreteMarker[]) markers.toArray(new ConcreteMarker[markers 52 .size()])); 53 } 54 55 60 public MarkerList(ConcreteMarker[] markers) { 61 this.markers = markers; 62 } 63 64 68 public void clearCache() { 69 for (int i = 0; i < markers.length; i++) { 70 ConcreteMarker marker = markers[i]; 71 72 marker.clearCache(); 73 } 74 75 markerTable = null; 76 } 77 78 84 private Map getMarkerMap() { 85 if (markerTable == null) { 86 markerTable = new HashMap (); 87 88 for (int idx = 0; idx < markers.length; idx++) { 89 ConcreteMarker marker = markers[idx]; 90 markerTable.put(marker.getMarker(), marker); 91 } 92 } 93 94 return markerTable; 95 } 96 97 105 public ConcreteMarker getMarker(IMarker toFind) { 106 return (ConcreteMarker) getMarkerMap().get(toFind); 107 } 108 109 113 public IMarker[] getIMarkers(){ 114 IMarker[] iMarkers = new IMarker[markers.length]; 115 for (int i = 0; i < markers.length; i++) { 116 iMarkers[i] = markers[i].getMarker(); 117 } 118 return iMarkers; 119 } 120 121 public static ConcreteMarker createMarker(IMarker marker) 122 throws CoreException { 123 if (marker.isSubtypeOf(IMarker.TASK)) { 124 return new TaskMarker(marker); 125 } else if (marker.isSubtypeOf(IMarker.BOOKMARK)) { 126 return new BookmarkMarker(marker); 127 } else if (marker.isSubtypeOf(IMarker.PROBLEM)) { 128 return new ProblemMarker(marker); 129 } else { 130 return new ConcreteMarker(marker); 131 } 132 } 133 134 public void refresh() { 135 for (int markerIdx = 0; markerIdx < markers.length; markerIdx++) { 136 ConcreteMarker next = markers[markerIdx]; 137 next.refresh(); 138 } 139 } 140 141 public List asList() { 142 return Arrays.asList(markers); 143 } 144 145 public MarkerList findMarkers(Collection ofIMarker) { 146 List result = new ArrayList (ofIMarker.size()); 147 148 Iterator iter = ofIMarker.iterator(); 149 while (iter.hasNext()) { 150 IMarker next = (IMarker) iter.next(); 151 152 ConcreteMarker marker = getMarker(next); 153 if (marker != null) { 154 result.add(marker); 155 } 156 } 157 158 return new MarkerList(result); 159 } 160 161 public static ConcreteMarker[] createMarkers(IMarker[] source) 162 throws CoreException { 163 ConcreteMarker[] result = new ConcreteMarker[source.length]; 164 165 for (int idx = 0; idx < source.length; idx++) { 166 result[idx] = createMarker(source[idx]); 167 } 168 169 return result; 170 } 171 172 184 public static MarkerList compute(MarkerFilter[] filters, 185 IProgressMonitor mon, boolean ignoreExceptions) 186 throws CoreException { 187 188 Collection returnMarkers = new HashSet (); 190 for (int i = 0; i < filters.length; i++) { 191 returnMarkers.addAll(filters[i].findMarkers(mon, ignoreExceptions)); 192 } 193 return new MarkerList(returnMarkers); 194 } 195 196 204 public static IMarker[] compute(String [] types) throws CoreException { 205 206 ArrayList result = new ArrayList (); 207 IResource input = ResourcesPlugin.getWorkspace().getRoot(); 208 209 for (int i = 0; i < types.length; i++) { 210 IMarker[] newMarkers = input.findMarkers(types[i], true, 211 IResource.DEPTH_INFINITE); 212 result.addAll(Arrays.asList(newMarkers)); 213 } 214 215 return (IMarker[]) result.toArray(new IMarker[result.size()]); 216 } 217 218 223 public ConcreteMarker[] toArray() { 224 return markers; 225 } 226 227 232 240 public int getItemCount() { 241 return markers.length; 242 } 243 244 249 public int getErrors() { 250 return getMarkerCounts()[IMarker.SEVERITY_ERROR]; 251 } 252 253 258 public int getInfos() { 259 return getMarkerCounts()[IMarker.SEVERITY_INFO]; 260 } 261 262 267 public int getWarnings() { 268 return getMarkerCounts()[IMarker.SEVERITY_WARNING]; 269 } 270 271 277 private int[] getMarkerCounts() { 278 if (markerCounts == null) { 279 markerCounts = new int[] { 0, 0, 0 }; 280 281 for (int idx = 0; idx < markers.length; idx++) { 282 ConcreteMarker marker = markers[idx]; 283 284 if (marker instanceof ProblemMarker) { 285 int severity = ((ProblemMarker) markers[idx]).getSeverity(); 286 if (severity >= 0 && severity <= 2) { 287 markerCounts[severity]++; 288 } 289 } 290 291 } 292 } 293 return markerCounts; 294 } 295 296 302 public Object [] getArray() { 303 return markers; 304 } 305 306 311 public int getSize() { 312 return getArray().length; 313 } 314 315 321 public ConcreteMarker getMarker(int index) { 322 return markers[index]; 323 } 324 325 330 public void updateMarkers(Collection addedMarkers,Collection removedMarkers) { 331 List list = new ArrayList (asList()); 332 list.addAll(addedMarkers); 333 list.removeAll(removedMarkers); 334 markers = new ConcreteMarker[list.size()]; 335 list.toArray(markers); 336 } 337 338 341 public void refreshAll() { 342 for (int i = 0; i < markers.length; i++) { 343 markers[i].refresh(); 344 } 345 } 346 347 350 public void clearGroups() { 351 for (int i = 0; i < markers.length; i++) { 352 markers[i].setGroup(null); 353 } 354 355 } 356 } 357 | Popular Tags |