1 11 package org.eclipse.ui.views.markers.internal; 12 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Comparator ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 import org.eclipse.jface.viewers.ViewerComparator; 22 import org.eclipse.osgi.util.NLS; 23 24 30 public class MarkerAdapter { 31 32 class MarkerCategory extends MarkerNode { 33 34 MarkerAdapter markerAdapter; 35 36 int start; 37 38 int end; 39 40 private ConcreteMarker[] children; 41 42 private String name; 43 44 52 MarkerCategory(MarkerAdapter adapter, int startIndex, int endIndex, 53 String categoryName) { 54 markerAdapter = adapter; 55 start = startIndex; 56 end = endIndex; 57 name = categoryName; 58 } 59 60 65 public MarkerNode[] getChildren() { 66 67 if (children == null) { 68 69 if (building) { 72 return Util.EMPTY_MARKER_ARRAY; 73 } 74 75 ConcreteMarker[] allMarkers = markerAdapter.lastMarkers 76 .toArray(); 77 78 int totalSize = getDisplayedSize(); 79 children = new ConcreteMarker[totalSize]; 80 81 System.arraycopy(allMarkers, start, children, 0, totalSize); 82 view.getTableSorter().sort(view.getViewer(), children); 84 85 for (int i = 0; i < children.length; i++) { 86 children[i].setCategory(this); 87 } 88 } 89 return children; 90 91 } 92 93 98 int getDisplayedSize() { 99 if (view.getMarkerLimit() > 0) { 100 return Math.min(getTotalSize(), view.getMarkerLimit()); 101 } 102 return getTotalSize(); 103 } 104 105 110 public MarkerNode getParent() { 111 return null; 112 } 113 114 119 public String getDescription() { 120 121 int size = end - start + 1; 122 123 if (size <= view.getMarkerLimit()) { 124 125 if (size == 1) 126 return NLS.bind(MarkerMessages.Category_One_Item_Label, 127 new Object [] { name }); 128 129 return NLS.bind(MarkerMessages.Category_Label, new Object [] { 130 name, String.valueOf(getDisplayedSize()) }); 131 } 132 return NLS.bind(MarkerMessages.Category_Limit_Label, new Object [] { 133 name, String.valueOf(getDisplayedSize()), 134 String.valueOf(getTotalSize()) }); 135 } 136 137 142 private int getTotalSize() { 143 return end - start + 1; 144 } 145 146 151 public boolean isConcrete() { 152 return false; 153 } 154 155 160 public ConcreteMarker getConcreteRepresentative() { 161 return markerAdapter.lastMarkers.getMarker(start); 162 } 163 164 169 public String getName() { 170 return name; 171 } 172 } 173 174 MarkerView view; 175 176 private MarkerList lastMarkers; 177 178 private MarkerCategory[] categories; 179 180 private boolean building = true; 182 184 189 MarkerAdapter(MarkerView markerView) { 190 view = markerView; 191 } 192 193 199 public CategoryComparator getCategorySorter() { 200 return (CategoryComparator) view.getViewer().getComparator(); 201 } 202 203 209 public void buildAllMarkers(IProgressMonitor monitor) { 210 building = true; 211 MarkerList newMarkers; 212 try { 213 int markerLimit = view.getMarkerLimit(); 214 monitor.beginTask(MarkerMessages.MarkerView_19, 215 markerLimit == -1 ? 60 : 100); 216 try { 217 monitor.subTask(MarkerMessages.MarkerView_waiting_on_changes); 218 219 if (monitor.isCanceled()) 220 return; 221 222 monitor 223 .subTask(MarkerMessages.MarkerView_searching_for_markers); 224 SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 225 10); 226 MarkerFilter[] filters = view.getEnabledFilters(); 227 if (filters.length > 0) 228 newMarkers = MarkerList.compute(filters, subMonitor, true); 229 else 230 newMarkers = MarkerList.compute(new MarkerFilter[] { view 232 .getAllFilters()[0] }, subMonitor, true); 233 234 if (monitor.isCanceled()) 235 return; 236 237 view.refreshMarkerCounts(monitor); 238 239 } catch (CoreException e) { 240 Util.log(e); 241 newMarkers = new MarkerList(); 242 return; 243 } 244 245 if (monitor.isCanceled()) 246 return; 247 248 ViewerComparator sorter = view.getViewer().getComparator(); 249 250 if (markerLimit == -1 || isShowingHierarchy()) { 251 sorter.sort(view.getViewer(), newMarkers.toArray()); 252 } else { 253 254 monitor.subTask(MarkerMessages.MarkerView_18); 255 SubProgressMonitor mon = new SubProgressMonitor(monitor, 40); 256 257 newMarkers = SortUtil.getFirst(newMarkers, (Comparator ) sorter, 258 markerLimit, mon); 259 if (monitor.isCanceled()) 260 return; 261 262 sorter.sort(view.getViewer(), newMarkers.toArray()); 263 } 264 265 if (newMarkers.getSize() == 0) { 266 categories = new MarkerCategory[0]; 267 lastMarkers = newMarkers; 268 monitor.done(); 269 return; 270 } 271 272 monitor.subTask(MarkerMessages.MarkerView_queueing_updates); 273 274 if (monitor.isCanceled()) 275 return; 276 277 if (isShowingHierarchy()) { 278 MarkerCategory[] newCategories = buildHierarchy(newMarkers, 0, 279 newMarkers.getSize() - 1, 0); 280 if (monitor.isCanceled()) 281 return; 282 categories = newCategories; 283 } 284 285 lastMarkers = newMarkers; 286 monitor.done(); 287 } finally { 288 building = false; 289 } 290 291 } 292 293 298 boolean isShowingHierarchy() { 299 300 ViewerComparator sorter = view.getViewer().getComparator(); 301 if (sorter instanceof CategoryComparator) { 302 return ((CategoryComparator) sorter).getCategoryField() != null; 303 } 304 return false; 305 } 306 307 321 MarkerCategory[] buildHierarchy(MarkerList markers, int start, int end, 322 int sortIndex) { 323 CategoryComparator sorter = getCategorySorter(); 324 325 if (sortIndex > 0) { 326 return null; } 328 329 Collection categories = new ArrayList (); 330 331 Object previous = null; 332 int categoryStart = start; 333 334 Object [] elements = markers.getArray(); 335 336 for (int i = start; i <= end; i++) { 337 338 if (previous != null) { 339 if (sorter.compare(previous, elements[i], sortIndex, false) != 0) { 341 categories.add(new MarkerCategory(this, categoryStart, 342 i - 1, getNameForIndex(markers, categoryStart))); 343 categoryStart = i; 344 } 345 } 346 previous = elements[i]; 347 348 } 349 350 if (end >= categoryStart) { 351 categories.add(new MarkerCategory(this, categoryStart, end, 352 getNameForIndex(markers, categoryStart))); 353 } 354 355 MarkerCategory[] nodes = new MarkerCategory[categories.size()]; 360 categories.toArray(nodes); 361 return nodes; 362 363 } 364 365 373 private String getNameForIndex(MarkerList markers, int categoryStart) { 374 return getCategorySorter().getCategoryField().getValue( 375 markers.toArray()[categoryStart]); 376 } 377 378 383 public MarkerList getCurrentMarkers() { 384 if (lastMarkers == null) { view.scheduleMarkerUpdate(Util.SHORT_DELAY); 386 building = true; 387 } 388 if (building) { 389 return new MarkerList(); 390 } 391 return lastMarkers; 392 } 393 394 400 public Object [] getElements() { 401 402 if (lastMarkers == null) { view.scheduleMarkerUpdate(Util.SHORT_DELAY); 404 building = true; 405 } 406 if (building) { 407 return Util.EMPTY_MARKER_ARRAY; 408 } 409 if (isShowingHierarchy() && categories != null) { 410 return categories; 411 } 412 return lastMarkers.toArray(); 413 } 414 415 422 public boolean hasNoMarkers() { 423 return lastMarkers == null; 424 } 425 426 432 public MarkerCategory[] getCategories() { 433 if (building) { 434 return null; 435 } 436 return categories; 437 } 438 439 443 boolean isBuilding() { 444 return building; 445 } 446 447 } 448 | Popular Tags |