1 11 12 package org.eclipse.ui.views.markers.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.swt.graphics.Image; 23 24 28 public class FieldMarkerGroup implements IField { 29 30 class EntryMapping { 31 MarkerGroupingEntry groupingEntry; 32 33 38 EntryMapping(MarkerGroupingEntry entry) { 39 groupingEntry = entry; 40 } 41 42 47 public boolean hasAttributes() { 48 return false; 49 } 50 51 58 public MarkerGroupingEntry testAttribute(ConcreteMarker marker) { 59 return null; 60 } 61 } 62 63 private static MarkerGroupingEntry undefinedEntry = new MarkerGroupingEntry( 64 MarkerMessages.FieldCategory_Uncategorized, null, 0); 65 66 class AttributeMapping extends EntryMapping { 67 68 String attribute; 69 70 String attributeValue; 71 72 80 AttributeMapping(MarkerGroupingEntry entry, String attributeName, 81 String value) { 82 super(entry); 83 attribute = attributeName; 84 attributeValue = value; 85 } 86 87 92 public boolean hasAttributes() { 93 return true; 94 } 95 96 101 public MarkerGroupingEntry testAttribute(ConcreteMarker marker) { 102 Object value; 103 104 if(!marker.getMarker().exists()) 105 return null; 107 try { 108 value = marker.getMarker().getAttribute(attribute); 109 } catch (CoreException e) { 110 Util.log(e); 111 return null; 112 } 113 114 if (value != null && attributeValue.equals(value.toString())) { 115 return groupingEntry; 116 } 117 return null; 118 } 119 } 120 121 private String title; 122 123 private String id; 124 125 private Map typesToMappings = new HashMap (); 126 127 private boolean showing; 128 129 135 public FieldMarkerGroup(String name, String identifier) { 136 title = name; 137 id = identifier; 138 } 139 140 145 public String getDescription() { 146 return title; 147 } 148 149 154 public Image getDescriptionImage() { 155 return null; 156 } 157 158 163 public String getColumnHeaderText() { 164 return title; 165 } 166 167 172 public Image getColumnHeaderImage() { 173 return null; 174 } 175 176 181 public String getValue(Object obj) { 182 MarkerNode node = (MarkerNode) obj; 183 184 if (node.isConcrete()) { 185 MarkerGroupingEntry groupingEntry = getMapping((ConcreteMarker) node); 186 return groupingEntry.getLabel(); 187 } 188 return node.getDescription(); 189 } 190 191 197 private MarkerGroupingEntry getMapping(ConcreteMarker marker) { 198 199 if (marker.getGroup() == null) { 200 marker.setGroup(findGroupValue(marker)); 201 } 202 return (MarkerGroupingEntry) marker.getGroup(); 203 } 204 205 212 private MarkerGroupingEntry findGroupValue(ConcreteMarker marker) { 213 214 if (typesToMappings.containsKey(marker.getType())) { 215 EntryMapping defaultMapping = null; 216 Iterator mappings = ((Collection ) typesToMappings.get(marker 217 .getType())).iterator(); 218 while (mappings.hasNext()) { 219 EntryMapping mapping = (EntryMapping) mappings.next(); 220 if (mapping.hasAttributes()) { 221 MarkerGroupingEntry entry = mapping.testAttribute(marker); 222 if (entry != null) { 223 return entry; 224 } 225 } else { 226 defaultMapping = mapping; 228 } 229 } 230 if (defaultMapping != null) { 231 return defaultMapping.groupingEntry; 232 } 233 234 } 235 236 return undefinedEntry; 237 238 } 243 244 public Image getImage(Object obj) { 245 return null; 246 } 247 248 254 public int compare(Object obj1, Object obj2) { 255 256 MarkerGroupingEntry entry1 = getMapping(((MarkerNode) obj1) 257 .getConcreteRepresentative()); 258 MarkerGroupingEntry entry2 = getMapping(((MarkerNode) obj2) 259 .getConcreteRepresentative()); 260 return entry2.getPriority() - entry1.getPriority(); 261 262 } 263 264 269 public int getDefaultDirection() { 270 return TableComparator.ASCENDING; 271 } 272 273 278 public int getPreferredWidth() { 279 return 75; 280 } 281 282 287 public boolean isShowing() { 288 return showing; 289 } 290 291 296 public void setShowing(boolean showing) { 297 this.showing = showing; 298 299 } 300 301 307 308 public void setAsDefault(String markerType, MarkerGroupingEntry entry) { 309 addEntry(markerType, new EntryMapping(entry)); 310 311 } 312 313 319 private void addEntry(String markerType, EntryMapping entry) { 320 321 MarkerType[] allDerived = getMarkerTypes(markerType); 322 323 for (int i = 0; i < allDerived.length; i++) { 324 Collection entries = new HashSet (); 325 MarkerType type = allDerived[i]; 326 if (typesToMappings.containsKey(type.getId())) { 327 entries = (Collection ) typesToMappings.get(markerType); 328 } else { 329 entries = new HashSet (); 330 } 331 332 entries.add(entry); 333 typesToMappings.put(type.getId(), entries); 334 } 335 336 } 337 338 343 private MarkerType[] getMarkerTypes(String markerType) { 344 MarkerTypesModel model = MarkerTypesModel.getInstance(); 345 Collection types = new HashSet (); 346 347 MarkerType type = model.getType(markerType); 348 if (type != null) { 349 types.add(type); 350 MarkerType[] subs = type.getAllSubTypes(); 351 for (int j = 0; j < subs.length; j++) { 352 types.add(subs[j]); 353 } 354 } 355 356 if (types.isEmpty()) { 357 return new MarkerType[0]; 358 } 359 360 MarkerType[] typesArray = new MarkerType[types.size()]; 361 types.toArray(typesArray); 362 return typesArray; 363 } 364 365 373 public void mapAttribute(String markerType, String attribute, 374 String attributeValue, MarkerGroupingEntry entry) { 375 addEntry(markerType, new AttributeMapping(entry, attribute, 376 attributeValue)); 377 378 } 379 380 385 public String getId() { 386 return id; 387 } 388 389 393 public void remove(MarkerGroupingEntry entry) { 394 Iterator entries = typesToMappings.values().iterator(); 395 Collection removeCollection = new ArrayList (); 396 while(entries.hasNext()){ 397 Collection mappings = (Collection ) entries.next(); 398 Iterator mappingsIterator = mappings.iterator(); 399 while(mappingsIterator.hasNext()){ 400 EntryMapping next = (EntryMapping) mappingsIterator.next(); 401 if(next.groupingEntry.equals(entry)){ 402 removeCollection.add(next); 403 } 404 405 } 406 mappings.removeAll(removeCollection); 407 removeCollection.clear(); 408 } 409 410 } 411 } 412 | Popular Tags |