1 11 package org.eclipse.ui.views.markers.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.resources.IMarker; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IConfigurationElement; 25 import org.eclipse.core.runtime.IExtension; 26 import org.eclipse.core.runtime.IExtensionPoint; 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.dynamichelpers.ExtensionTracker; 31 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 32 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 33 import org.eclipse.osgi.util.NLS; 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 36 37 44 public class MarkerSupportRegistry implements IExtensionChangeHandler { 45 46 private static final String DESCRIPTION = "onDescription"; 48 private static final String ENABLED = "enabled"; 50 private static final Object ERROR = "ERROR"; 52 static final String ID = "id"; 54 private static final Object INFO = "INFO"; 56 private static final Object WARNING = "WARNING"; 58 private static final String MARKER_ID = "markerId"; 60 63 public static final String MARKER_SUPPORT = "markerSupport"; 65 private static final String NAME = "name"; 67 private static final Object ON_ANY = "ON_ANY"; 69 private static final Object ON_ANY_IN_SAME_CONTAINER = "ON_ANY_IN_SAME_CONTAINER"; 71 private static final Object ON_SELECTED_AND_CHILDREN = "ON_SELECTED_AND_CHILDREN"; 73 private static final Object ON_SELECTED_ONLY = "ON_SELECTED_ONLY"; 75 private static final Object PROBLEM_FILTER = "problemFilter"; 77 private static final String SCOPE = "scope"; 79 private static final String SELECTED_TYPE = "selectedType"; 81 private static final String SEVERITY = "severity"; 83 private static final String MARKER_TYPE_REFERENCE = "markerTypeReference"; 85 private static final String MARKER_CATEGORY = "markerTypeCategory"; 87 private static final String ATTRIBUTE_MAPPING = "markerAttributeMapping"; 89 private static final String MARKER_GROUPING = "markerGrouping"; 91 private static final String ATTRIBUTE = "attribute"; 93 private static final String VALUE = "value"; 95 private static final String LABEL = "label"; 97 private static final String MARKER_ATTRIBUTE_GROUPING = "markerAttributeGrouping"; 99 private static final String DEFAULT_GROUPING_ENTRY = "defaultGroupingEntry"; 101 private static final String MARKER_TYPE = "markerType"; 103 private static final String PRIORITY = "priority"; 105 private static final String MARKER_GROUPING_ENTRY = "markerGroupingEntry"; 107 private static final Object SEVERITY_ID = "org.eclipse.ui.ide.severity"; 109 private static MarkerSupportRegistry singleton; 110 111 private static Object creationLock = new Object (); 113 114 119 public static MarkerSupportRegistry getInstance() { 120 if (singleton == null) { 121 synchronized (creationLock) { 122 if (singleton == null) { 123 singleton = new MarkerSupportRegistry(); 125 } 126 } 127 } 128 return singleton; 129 } 130 131 private Collection registeredFilters = new ArrayList (); 132 133 private Map markerGroups = new HashMap (); 134 135 private Map markerGroupingEntries = new HashMap (); 136 137 private HashMap categories = new HashMap (); 138 139 private HashMap hierarchyOrders = new HashMap (); 140 141 private MarkerType rootType; 142 143 146 private MarkerSupportRegistry() { 147 IExtensionTracker tracker = PlatformUI.getWorkbench() 148 .getExtensionTracker(); 149 IExtensionPoint point = Platform.getExtensionRegistry() 150 .getExtensionPoint(IDEWorkbenchPlugin.IDE_WORKBENCH, 151 MARKER_SUPPORT); 152 if (point == null) { 153 return; 154 } 155 IExtension[] extensions = point.getExtensions(); 156 Map groupingEntries = new HashMap (); 158 Set attributeMappings = new HashSet (); 159 for (int i = 0; i < extensions.length; i++) { 160 IExtension extension = extensions[i]; 161 processExtension(tracker, extension, groupingEntries, 162 attributeMappings); 163 } 164 postProcessExtensions(groupingEntries, attributeMappings); 165 tracker.registerHandler(this, ExtensionTracker 166 .createExtensionPointFilter(point)); 167 168 } 169 170 184 private void processExtension(IExtensionTracker tracker, 185 IExtension extension, Map groupingEntries, 186 Collection attributeMappings) { 187 IConfigurationElement[] elements = extension.getConfigurationElements(); 188 189 for (int j = 0; j < elements.length; j++) { 190 IConfigurationElement element = elements[j]; 191 if (element.getName().equals(PROBLEM_FILTER)) { 192 ProblemFilter filter = newFilter(element); 193 registeredFilters.add(filter); 194 tracker.registerObject(extension, filter, 195 IExtensionTracker.REF_STRONG); 196 197 continue; 198 } 199 if (element.getName().equals(MARKER_GROUPING)) { 200 201 FieldMarkerGroup group = new FieldMarkerGroup(element 202 .getAttribute(LABEL), element.getAttribute(ID)); 203 markerGroups.put(group.getId(), group); 204 tracker.registerObject(extension, group, 205 IExtensionTracker.REF_STRONG); 206 } 207 208 if (element.getName().equals(MARKER_GROUPING_ENTRY)) { 209 210 MarkerGroupingEntry entry = new MarkerGroupingEntry(element 211 .getAttribute(LABEL), element.getAttribute(ID), 212 (Integer.valueOf(element.getAttribute(PRIORITY)) 213 .intValue())); 214 215 String groupName = element.getAttribute(MARKER_GROUPING); 216 217 Collection entries; 218 if (groupingEntries.containsKey(groupName)) { 219 entries = (Collection ) groupingEntries.get(groupName); 220 } else { 221 entries = new HashSet (); 222 } 223 224 entries.add(entry); 225 groupingEntries.put(groupName, entries); 226 227 tracker.registerObject(extension, entry, 228 IExtensionTracker.REF_STRONG); 229 } 230 231 if (element.getName().equals(MARKER_ATTRIBUTE_GROUPING)) { 232 233 AttributeMarkerGrouping grouping = new AttributeMarkerGrouping( 234 element.getAttribute(ATTRIBUTE), element 235 .getAttribute(MARKER_TYPE), element 236 .getAttribute(DEFAULT_GROUPING_ENTRY), element); 237 238 attributeMappings.add(grouping); 239 240 tracker.registerObject(extension, grouping, 241 IExtensionTracker.REF_STRONG); 242 } 243 244 if (element.getName().equals(MARKER_CATEGORY)) { 245 246 String [] markerTypes = getMarkerTypes(element); 247 String categoryName = element.getAttribute(NAME); 248 249 for (int i = 0; i < markerTypes.length; i++) { 250 categories.put(markerTypes[i], categoryName); 251 252 } 253 tracker.registerObject(extension, categoryName, 254 IExtensionTracker.REF_STRONG); 255 } 256 257 } 258 } 259 260 271 private void postProcessExtensions(Map groupingEntries, 272 Collection attributeMappings) { 273 processGroupingEntries(groupingEntries); 274 processAttributeMappings(attributeMappings); 275 } 276 277 282 private void processGroupingEntries(Map groupingEntries) { 283 Iterator entriesIterator = groupingEntries.keySet().iterator(); 284 while (entriesIterator.hasNext()) { 285 String nextGroupId = (String ) entriesIterator.next(); 286 Iterator nextEntriesIterator = ((Collection ) groupingEntries 287 .get(nextGroupId)).iterator(); 288 if (markerGroups.containsKey(nextGroupId)) { 289 while (nextEntriesIterator.hasNext()) { 290 MarkerGroupingEntry next = (MarkerGroupingEntry) nextEntriesIterator 291 .next(); 292 markerGroupingEntries.put(next.getId(), next); 293 next.setGroupingEntry((FieldMarkerGroup) markerGroups 294 .get(nextGroupId)); 295 296 } 297 } else { 298 while (nextEntriesIterator.hasNext()) { 299 MarkerGroupingEntry next = (MarkerGroupingEntry) nextEntriesIterator 300 .next(); 301 IDEWorkbenchPlugin 302 .log(NLS 303 .bind( 304 "markerGroupingEntry {0} defines invalid group {1}", new String [] { next.getId(), 306 nextGroupId })); 307 } 308 } 309 } 310 } 311 312 317 private void processAttributeMappings(Collection attributeMappings) { 318 Iterator mappingsIterator = attributeMappings.iterator(); 319 while (mappingsIterator.hasNext()) { 320 AttributeMarkerGrouping next = (AttributeMarkerGrouping) mappingsIterator 321 .next(); 322 String defaultEntryId = next.getDefaultGroupingEntry(); 323 if (defaultEntryId != null) { 324 if (markerGroupingEntries.containsKey(defaultEntryId)) { 325 MarkerGroupingEntry entry = (MarkerGroupingEntry) markerGroupingEntries 326 .get(defaultEntryId); 327 entry.setAsDefault(next.getMarkerType()); 328 } else { 329 IDEWorkbenchPlugin.log(NLS.bind( 330 "Reference to invalid markerGroupingEntry {0}", defaultEntryId)); 332 } 333 } 334 IConfigurationElement[] mappings = next.getElement().getChildren( 335 ATTRIBUTE_MAPPING); 336 337 for (int i = 0; i < mappings.length; i++) { 338 String entryId = mappings[i] 339 .getAttribute(MARKER_GROUPING_ENTRY); 340 341 if (markerGroupingEntries.containsKey(entryId)) { 342 MarkerGroupingEntry entry = (MarkerGroupingEntry) markerGroupingEntries 343 .get(entryId); 344 entry.mapAttribute(next.getMarkerType(), next 345 .getAttribute(), mappings[i].getAttribute(VALUE)); 346 } else { 347 IDEWorkbenchPlugin.log(NLS.bind( 348 "Reference to invaild markerGroupingEntry {0}", defaultEntryId)); 350 } 351 352 } 353 } 354 355 } 356 357 363 private String [] getMarkerTypes(IConfigurationElement element) { 364 IConfigurationElement[] types = element 365 .getChildren(MARKER_TYPE_REFERENCE); 366 String [] ids = new String [types.length]; 367 for (int i = 0; i < ids.length; i++) { 368 ids[i] = types[i].getAttribute(ID); 369 } 370 return ids; 371 } 372 373 379 public void addExtension(IExtensionTracker tracker, IExtension extension) { 380 Map groupingEntries = new HashMap (); 381 Set attributeMappings = new HashSet (); 382 processExtension(tracker, extension, groupingEntries, attributeMappings); 383 postProcessExtensions(groupingEntries, attributeMappings); 384 } 385 386 391 public Collection getRegisteredFilters() { 392 Collection filteredFilters = new ArrayList (); 393 Iterator registeredIterator = registeredFilters.iterator(); 394 while (registeredIterator.hasNext()) { 395 ProblemFilter next = (ProblemFilter) registeredIterator.next(); 396 if (next.isFilteredOutByActivity()) { 397 continue; 398 } 399 filteredFilters.add(next); 400 } 401 402 return filteredFilters; 403 } 404 405 413 private int getScopeValue(IConfigurationElement element) { 414 String scope = element.getAttribute(SCOPE); 415 if (scope == null) { 416 return -1; 417 } 418 if (scope.equals(ON_ANY)) { 419 return MarkerFilter.ON_ANY; 420 } 421 if (scope.equals(ON_SELECTED_ONLY)) { 422 return MarkerFilter.ON_SELECTED_ONLY; 423 } 424 if (scope.equals(ON_SELECTED_AND_CHILDREN)) { 425 return MarkerFilter.ON_SELECTED_AND_CHILDREN; 426 } 427 if (scope.equals(ON_ANY_IN_SAME_CONTAINER)) { 428 return MarkerFilter.ON_ANY_IN_SAME_CONTAINER; 429 } 430 431 return -1; 432 } 433 434 442 private int getSeverityValue(IConfigurationElement element) { 443 String severity = element.getAttribute(SEVERITY); 444 if (severity == null) { 445 return -1; 446 } 447 if (severity.equals(INFO)) { 448 return ProblemFilter.SEVERITY_INFO; 449 } 450 if (severity.equals(WARNING)) { 451 return ProblemFilter.SEVERITY_WARNING; 452 } 453 if (severity.equals(ERROR)) { 454 return ProblemFilter.SEVERITY_ERROR; 455 } 456 457 return -1; 458 } 459 460 467 private ProblemFilter newFilter(IConfigurationElement element) { 468 ProblemFilter filter = new ProblemFilter(element.getAttribute(NAME)); 469 470 filter.createContributionFrom(element); 471 472 String enabledValue = element.getAttribute(ENABLED); 473 filter.setEnabled(enabledValue == null 474 || Boolean.valueOf(enabledValue).booleanValue()); 475 476 int scopeValue = getScopeValue(element); 477 if (scopeValue >= 0) { 478 filter.setOnResource(scopeValue); 479 } 480 481 String description = element.getAttribute(DESCRIPTION); 482 if (description != null) { 483 boolean contains = true; 484 if (description.charAt(0) == '!') { description = description.substring(1, description.length()); 486 contains = false; 487 } 488 filter.setContains(contains); 489 filter.setDescription(description); 490 } 491 492 int severityValue = getSeverityValue(element); 493 if (severityValue > 0) { 494 filter.setSelectBySeverity(true); 495 filter.setSeverity(severityValue); 496 } else { 497 filter.setSelectBySeverity(false); 498 } 499 500 List selectedTypes = new ArrayList (); 501 IConfigurationElement[] types = element.getChildren(SELECTED_TYPE); 502 for (int j = 0; j < types.length; j++) { 503 String markerId = types[j].getAttribute(MARKER_ID); 504 if (markerId != null) { 505 MarkerType type = filter.getMarkerType(markerId); 506 if (type == null) { 507 IStatus status = new Status(IStatus.WARNING, 508 IDEWorkbenchPlugin.IDE_WORKBENCH, IStatus.WARNING, 509 MarkerMessages.ProblemFilterRegistry_nullType, null); 510 IDEWorkbenchPlugin.getDefault().getLog().log(status); 511 } else { 512 selectedTypes.add(type); 513 } 514 } 515 } 516 517 if (selectedTypes.size() > 0) { 518 filter.setSelectedTypes(selectedTypes); 520 } 521 522 return filter; 523 524 } 525 526 532 public void removeExtension(IExtension extension, Object [] objects) { 533 534 Collection removedGroups = new ArrayList (); 535 536 for (int i = 0; i < objects.length; i++) { 537 if (objects[i] instanceof ProblemFilter) { 538 registeredFilters.remove(objects[i]); 539 } 540 541 if (objects[i] instanceof FieldMarkerGroup) { 542 markerGroups.remove(((FieldMarkerGroup) objects[i]).getId()); 543 removedGroups.add(objects[i]); 544 } 545 546 if (objects[i] instanceof MarkerGroupingEntry) { 547 MarkerGroupingEntry entry = (MarkerGroupingEntry) objects[i]; 548 entry.getMarkerGroup().remove(entry); 549 markerGroupingEntries.remove(entry.getId()); 550 } 551 552 if (objects[i] instanceof String ) { 553 removeValues(objects[i], categories); 554 } 555 556 } 557 558 Iterator entriesIterator = markerGroupingEntries.keySet().iterator(); 559 Collection removedKeys = new ArrayList (); 560 while (entriesIterator.hasNext()) { 561 String entryId = (String ) entriesIterator.next(); 562 MarkerGroupingEntry entry = (MarkerGroupingEntry) markerGroupingEntries 563 .get(entryId); 564 if (removedGroups.contains(entry.getMarkerGroup())) { 565 removedKeys.add(entryId); 566 } 567 } 568 569 Iterator removedIterator = removedKeys.iterator(); 570 while (removedIterator.hasNext()) { 571 markerGroupingEntries.remove(removedIterator.next()); 572 } 573 574 } 575 576 583 private void removeValues(Object value, HashMap cache) { 584 Collection keysToRemove = new ArrayList (); 585 Iterator keys = cache.keySet().iterator(); 586 while (keys.hasNext()) { 587 String key = (String ) keys.next(); 588 Object next = cache.get(key); 589 if (next instanceof Collection ) { 590 Collection collection = (Collection ) next; 591 if (collection.contains(value)) { 592 collection.remove(value); 593 if (collection.isEmpty()) { 594 keysToRemove.add(key); 595 } 596 break; 597 } 598 } else { 599 if (cache.get(key).equals(value)) { 600 keysToRemove.add(key); 601 } 602 } 603 } 604 Iterator keysToRemoveIterator = keysToRemove.iterator(); 605 while (keysToRemoveIterator.hasNext()) { 606 cache.remove(keysToRemoveIterator.next()); 607 } 608 } 609 610 617 public String getCategory(IMarker marker) { 618 try { 619 return getCategory(marker.getType()); 620 } catch (CoreException e) { 621 Util.log(e); 622 } 623 return null; 624 } 625 626 633 public String getCategory(String markerType) { 634 if (categories.containsKey(markerType)) { 635 return (String ) categories.get(markerType); 636 } 637 return null; 638 } 639 640 646 public TableComparator getSorterFor(String type) { 647 if (hierarchyOrders.containsKey(type)) { 648 return (TableComparator) hierarchyOrders.get(type); 649 } 650 651 TableComparator sorter = findSorterInChildren(type, getRootType()); 652 if (sorter == null) { 653 return new TableComparator(new IField[0], new int[0], new int[0]); 654 } 655 return sorter; 656 } 657 658 663 private MarkerType getRootType() { 664 if (rootType == null) { 665 rootType = (MarkerTypesModel.getInstance()) 666 .getType(IMarker.PROBLEM); 667 } 668 return rootType; 669 } 670 671 679 private TableComparator findSorterInChildren(String typeName, MarkerType type) { 680 681 MarkerType[] types = type.getAllSubTypes(); 682 TableComparator defaultSorter = null; 683 if (hierarchyOrders.containsKey(type.getId())) { 684 defaultSorter = (TableComparator) hierarchyOrders.get(type.getId()); 685 } 686 687 for (int i = 0; i < types.length; i++) { 688 MarkerType[] subtypes = types[i].getAllSubTypes(); 689 for (int j = 0; j < subtypes.length; j++) { 690 TableComparator sorter = findSorterInChildren(typeName, subtypes[j]); 691 if (sorter != null) { 692 return sorter; 693 } 694 } 695 } 696 return defaultSorter; 697 698 } 699 700 705 public Collection getMarkerGroups() { 706 return markerGroups.values(); 707 } 708 709 714 public IField getDefaultGroup() { 715 716 return (IField) markerGroups.get(SEVERITY_ID); 717 } 718 719 } 720 | Popular Tags |