1 11 12 package org.eclipse.ui.views.tasklist; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.core.resources.IMarker; 22 import org.eclipse.core.resources.IMarkerDelta; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IResourceChangeEvent; 25 import org.eclipse.core.resources.IResourceChangeListener; 26 import org.eclipse.core.resources.IResourceDelta; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.jface.viewers.IStructuredContentProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.TableViewer; 31 import org.eclipse.jface.viewers.Viewer; 32 import org.eclipse.osgi.util.NLS; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.ui.internal.views.tasklist.TaskListMessages; 35 36 42 class TaskListContentProvider implements IStructuredContentProvider, 43 IResourceChangeListener { 44 45 private static final int TASKS = 0; 46 47 private static final int ERRORS = 1; 48 49 private static final int WARNINGS = 2; 50 51 private static final int INFOS = 3; 52 53 private TaskList taskList; 54 55 private TableViewer viewer; 56 57 private IResource input; 58 59 61 private int[] visibleMarkerCounts = null; 62 63 65 private int totalMarkerCount = -1; 66 67 70 public TaskListContentProvider(TaskList taskList) { 71 this.taskList = taskList; 72 this.viewer = taskList.getTableViewer(); 73 } 74 75 private boolean getFilterOnMarkerLimit() { 76 return taskList.getFilter().getFilterOnMarkerLimit(); 77 } 78 79 private int getMarkerLimit() { 80 return taskList.getFilter().getMarkerLimit(); 81 } 82 83 private boolean isMarkerLimitExceeded() { 84 return taskList.isMarkerLimitExceeded(); 85 } 86 87 private void setMarkerLimitExceeded(boolean markerLimitExceeded) { 88 taskList.setMarkerLimitExceeded(markerLimitExceeded); 89 } 90 91 95 public String getStatusSummaryVisible() { 96 if (visibleMarkerCounts == null) { 97 return ""; } 99 100 return NLS.bind(TaskListMessages.TaskList_statusSummaryVisible,new Integer (sum(visibleMarkerCounts)), 101 getStatusSummaryBreakdown(visibleMarkerCounts)); 102 } 103 104 110 public String getStatusSummarySelected(IStructuredSelection selection) { 111 int[] selectedMarkerCounts = getMarkerCounts(selection.toList()); 112 return NLS.bind(TaskListMessages.TaskList_statusSummarySelected, new Integer (sum(selectedMarkerCounts)), 113 getStatusSummaryBreakdown(selectedMarkerCounts) ); 114 } 115 116 120 private String getStatusSummaryBreakdown(int[] counts) { 121 return NLS.bind( 122 TaskListMessages.TaskList_statusSummaryBreakdown, 123 new Object []{ 124 new Integer (counts[TASKS]), 125 new Integer (counts[ERRORS]), 126 new Integer (counts[WARNINGS]), 127 new Integer (counts[INFOS])}); 128 } 129 130 134 public String getTitleSummary() { 135 if (visibleMarkerCounts == null) { 136 return ""; } 138 139 int visibleMarkerCount = sum(visibleMarkerCounts); 140 TasksFilter filter = taskList.getFilter(); 141 142 if (filter.isShowingAll()) { 143 return NLS.bind(TaskListMessages.TaskList_titleSummaryUnfiltered, new Integer (visibleMarkerCount)); 144 } else { 145 return NLS.bind(TaskListMessages.TaskList_titleSummaryFiltered, new Integer (visibleMarkerCount), 146 new Integer (getTotalMarkerCount())); 147 } 148 } 149 150 153 private int sum(int[] counts) { 154 int sum = 0; 155 156 for (int i = 0, l = counts.length; i < l; ++i) { 157 sum += counts[i]; 158 } 159 160 return sum; 161 } 162 163 168 private int getTotalMarkerCount() { 169 if (totalMarkerCount == -1) { 170 totalMarkerCount = 0; 171 172 try { 173 IResource root = taskList.getWorkspace().getRoot(); 174 IMarker[] markers = root.findMarkers(null, true, 175 IResource.DEPTH_INFINITE); 176 177 for (int i = 0; i < markers.length; ++i) { 178 if (isRootType(markers[i])) { 179 ++totalMarkerCount; 180 } 181 } 182 } catch (CoreException e) { 183 } 185 } 186 187 return totalMarkerCount; 188 } 189 190 193 private boolean isRootType(IMarker marker) { 194 String [] rootTypes = TasksFilter.ROOT_TYPES; 195 196 for (int i = 0, l = rootTypes.length; i < l; ++i) { 197 if (MarkerUtil.isMarkerType(marker, rootTypes[i])) { 198 return true; 199 } 200 } 201 202 return false; 203 } 204 205 208 private IMarker[] getMarkers() throws CoreException { 209 IResource[] resources = taskList.getResources(); 210 int l = resources.length; 211 IResource resource; 212 boolean bExists = false; 213 214 for (int i = 0; i < l; i++) { 215 resource = resources[i]; 216 217 if (resource != null && resource.exists()) { 218 bExists = true; 219 break; 220 } 221 } 222 223 if (!bExists) { 224 return new IMarker[0]; 225 } 226 227 if (taskList.showOwnerProject()) { 228 IResource[] projectResources = new IResource[l]; 229 IResource project; 230 231 for (int i = 0; i < l; i++) { 232 resource = resources[i]; 233 234 if (resource != null) { 235 project = resource.getProject(); 236 237 if (project != null) { 238 projectResources[i] = project; 239 } else { 240 projectResources[i] = resource; 241 } 242 } 243 } 244 245 resources = projectResources; 246 } 247 248 int depth = taskList.getResourceDepth(); 249 TasksFilter filter = taskList.getFilter(); 250 Set set = new HashSet (); 251 252 for (int i = 0; i < l; i++) { 253 resource = resources[i]; 254 255 if (resource != null) { 256 IMarker[] markers = resource.findMarkers(null, true, depth); 257 258 for (int j = 0; j < markers.length; ++j) { 259 IMarker marker = markers[j]; 260 261 if (filter.select(marker)) { 262 set.add(marker); 263 } 264 } 265 } 266 } 267 268 IMarker[] result = new IMarker[set.size()]; 269 set.toArray(result); 270 return result; 271 } 272 273 277 private int[] getMarkerCounts(List markers) { 278 int[] markerCounts = new int[4]; 279 Iterator iterator = markers.iterator(); 280 281 while (iterator.hasNext()) { 282 IMarker marker = (IMarker) iterator.next(); 283 284 if (MarkerUtil.isMarkerType(marker, IMarker.PROBLEM)) { 285 switch (MarkerUtil.getSeverity(marker)) { 286 case IMarker.SEVERITY_ERROR: 287 ++markerCounts[ERRORS]; 288 break; 289 case IMarker.SEVERITY_WARNING: 290 ++markerCounts[WARNINGS]; 291 break; 292 case IMarker.SEVERITY_INFO: 293 ++markerCounts[INFOS]; 294 break; 295 } 296 } else if (MarkerUtil.isMarkerType(marker, IMarker.TASK)) { 297 ++markerCounts[TASKS]; 298 } 299 } 300 301 return markerCounts; 302 } 303 304 310 private void updateMarkerCounts(IMarkerDelta markerDelta, int difference) { 311 if (visibleMarkerCounts == null) { 312 return; 313 } 314 315 if (markerDelta.isSubtypeOf(IMarker.PROBLEM)) { 316 int severity = markerDelta.getAttribute(IMarker.SEVERITY, 317 IMarker.SEVERITY_WARNING); 318 319 switch (severity) { 320 case IMarker.SEVERITY_ERROR: 321 visibleMarkerCounts[ERRORS] += difference; 322 break; 323 case IMarker.SEVERITY_WARNING: 324 visibleMarkerCounts[WARNINGS] += difference; 325 break; 326 case IMarker.SEVERITY_INFO: 327 visibleMarkerCounts[INFOS] += difference; 328 break; 329 } 330 } else if (markerDelta.isSubtypeOf(IMarker.TASK)) { 331 visibleMarkerCounts[TASKS] += difference; 332 } 333 } 334 335 339 private void updateViewer(List additions, List removals, List changes) { 340 341 Control ctrl = viewer.getControl(); 344 345 if (ctrl == null || ctrl.isDisposed()) { 346 return; 347 } 348 349 if (removals.size() > 0) { 353 354 viewer.cancelEditing(); 357 viewer.remove(removals.toArray()); 358 } 359 360 if (additions.size() > 0) { 361 viewer.add(additions.toArray()); 362 } 363 364 if (changes.size() > 0) { 365 viewer.update(changes.toArray(), null); 366 } 367 } 368 369 373 public void dispose() { 374 if (input != null) { 375 input.getWorkspace().removeResourceChangeListener(this); 376 input = null; 377 } 378 } 379 380 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 381 if (this.input != null) { 382 this.input.getWorkspace().removeResourceChangeListener(this); 383 } 384 385 this.input = (IResource) newInput; 386 387 if (this.input != null) { 388 this.input.getWorkspace().addResourceChangeListener(this, 389 IResourceChangeEvent.POST_CHANGE); 390 } 391 392 this.viewer = (TableViewer) viewer; 393 } 394 395 399 public Object [] getElements(Object parent) { 400 try { 401 IMarker[] markers = getMarkers(); 402 this.visibleMarkerCounts = getMarkerCounts(Arrays.asList(markers)); 403 404 if (getFilterOnMarkerLimit() && markers.length > getMarkerLimit()) { 405 if (!isMarkerLimitExceeded()) { 406 setMarkerLimitExceeded(true); 407 408 viewer.getControl().getDisplay().syncExec(new Runnable () { 409 public void run() { 410 viewer.refresh(); 411 } 412 }); 413 } 414 415 return new IMarker[0]; 416 } else { 417 if (isMarkerLimitExceeded()) { 418 setMarkerLimitExceeded(false); 419 420 viewer.getControl().getDisplay().syncExec(new Runnable () { 421 public void run() { 422 viewer.refresh(); 423 } 424 }); 425 } 426 427 return markers; 428 } 429 } catch (CoreException e) { 430 return new IMarker[0]; 431 } 432 } 433 434 440 public void resourceChanged(final IResourceChangeEvent event) { 441 445 IMarkerDelta[] markerDeltas = event.findMarkerDeltas(null, true); 446 447 if (markerDeltas == null) { 448 return; 449 } 450 451 int oldTotal = totalMarkerCount; 452 final List additions = new ArrayList (); 453 final List removals = new ArrayList (); 454 final List changes = new ArrayList (); 455 456 for (int i = 0; i < markerDeltas.length; i++) { 457 IMarkerDelta markerDelta = markerDeltas[i]; 458 459 if (markerDelta == null) { 460 continue; 461 } 462 463 int iKind = markerDelta.getKind(); 464 465 for (int j = 0; j < TasksFilter.ROOT_TYPES.length; j++) { 466 if (markerDelta.isSubtypeOf(TasksFilter.ROOT_TYPES[j])) { 467 468 472 if (totalMarkerCount != -1) { 473 switch (iKind) { 474 case IResourceDelta.ADDED: 475 totalMarkerCount++; 476 break; 477 case IResourceDelta.REMOVED: 478 totalMarkerCount--; 479 break; 480 } 481 } 482 483 493 494 IResource resource = markerDelta.getResource(); 495 496 if (resource == null) { 497 continue; 498 } 499 500 boolean affectedBy = taskList.checkResource(resource) 501 && taskList.getFilter().select(markerDelta); 502 503 if (affectedBy) { 504 IMarker marker = markerDelta.getMarker(); 505 506 switch (iKind) { 507 case IResourceDelta.ADDED: 508 additions.add(marker); 509 updateMarkerCounts(markerDelta, +1); 510 break; 511 case IResourceDelta.REMOVED: 512 removals.add(marker); 513 updateMarkerCounts(markerDelta, -1); 514 break; 515 case IResourceDelta.CHANGED: 516 changes.add(marker); 517 522 break; 523 } 524 } 525 526 break; 527 } 528 } 529 } 530 531 if (oldTotal == totalMarkerCount 532 && additions.size() + removals.size() + changes.size() == 0) { 533 return; 535 } 536 537 542 viewer.getControl().getDisplay().syncExec(new Runnable () { 543 public void run() { 544 if (getFilterOnMarkerLimit() 545 && sum(visibleMarkerCounts) > getMarkerLimit()) { 546 if (!isMarkerLimitExceeded()) { 547 setMarkerLimitExceeded(true); 548 viewer.refresh(); 549 } 550 } else if (taskList.isMarkerLimitExceeded()) { 551 setMarkerLimitExceeded(false); 552 viewer.refresh(); 553 } else { 554 updateViewer(additions, removals, changes); 555 } 556 557 564 taskList.markersChanged(); 565 } 566 }); 567 } 568 } 569 | Popular Tags |