Lines 201-206
Link Here
|
201 |
} |
201 |
} |
202 |
|
202 |
|
203 |
ViewerSorter sorter = getSorter (); |
203 |
ViewerSorter sorter = getSorter (); |
|
|
204 |
TreePath parentPath = internalGetSorterParentPath(widget, sorter); |
204 |
Item[] items = getChildren(widget); |
205 |
Item[] items = getChildren(widget); |
205 |
|
206 |
|
206 |
//As the items are sorted already we optimize for a |
207 |
//As the items are sorted already we optimize for a |
Lines 227-238
Link Here
|
227 |
index = -1; |
228 |
index = -1; |
228 |
} |
229 |
} |
229 |
else{ |
230 |
else{ |
230 |
lastInsertion = insertionPosition(items,sorter,lastInsertion, element); |
231 |
lastInsertion = insertionPosition(items,sorter,lastInsertion, element, parentPath); |
231 |
//As we are only searching the original array we keep track of those positions only |
232 |
//As we are only searching the original array we keep track of those positions only |
232 |
if(lastInsertion == items.length) |
233 |
if(lastInsertion == items.length) |
233 |
index = -1; |
234 |
index = -1; |
234 |
else{//See if we should just refresh |
235 |
else{//See if we should just refresh |
235 |
while(lastInsertion < items.length && sorter.compare(this,element,items[lastInsertion].getData()) == 0){ |
236 |
while(lastInsertion < items.length && internalCompare(sorter,parentPath,element,items[lastInsertion].getData()) == 0){ |
236 |
//As we cannot assume the sorter is consistent with equals() - therefore we can |
237 |
//As we cannot assume the sorter is consistent with equals() - therefore we can |
237 |
// just check against the item prior to this index (if any) |
238 |
// just check against the item prior to this index (if any) |
238 |
if (items[lastInsertion].getData().equals(element)) { |
239 |
if (items[lastInsertion].getData().equals(element)) { |
Lines 285-296
Link Here
|
285 |
* themself. |
286 |
* themself. |
286 |
* @param element |
287 |
* @param element |
287 |
* element to find position for. |
288 |
* element to find position for. |
|
|
289 |
* @param parentPath the tree path for the element's parent or <code>null</code> |
290 |
* if the element is a root element or the sorter is not a {@link TreePathViewerSorter} |
288 |
* @return the index to use when inserting the element. |
291 |
* @return the index to use when inserting the element. |
289 |
* |
292 |
* |
290 |
*/ |
293 |
*/ |
291 |
|
294 |
|
292 |
private int insertionPosition(Item[] items, ViewerSorter sorter, int lastInsertion, Object element) { |
295 |
private int insertionPosition(Item[] items, ViewerSorter sorter, int lastInsertion, Object element, TreePath parentPath) { |
293 |
|
296 |
|
294 |
int size = items.length; |
297 |
int size = items.length; |
295 |
if (sorter == null) |
298 |
if (sorter == null) |
296 |
return size; |
299 |
return size; |
Lines 299-305
Link Here
|
299 |
while (min <= max) { |
302 |
while (min <= max) { |
300 |
int mid = (min + max) / 2; |
303 |
int mid = (min + max) / 2; |
301 |
Object data = items[mid].getData(); |
304 |
Object data = items[mid].getData(); |
302 |
int compare = sorter.compare(this, data, element); |
305 |
int compare = internalCompare(sorter, parentPath, data, element); |
303 |
if (compare == 0) { |
306 |
if (compare == 0) { |
304 |
return mid;//Return if we already match |
307 |
return mid;//Return if we already match |
305 |
} |
308 |
} |
Lines 343-349
Link Here
|
343 |
*/ |
346 |
*/ |
344 |
protected int indexForElement(Widget parent, Object element) { |
347 |
protected int indexForElement(Widget parent, Object element) { |
345 |
ViewerSorter sorter = getSorter(); |
348 |
ViewerSorter sorter = getSorter(); |
346 |
|
349 |
TreePath parentPath = internalGetSorterParentPath(parent, sorter); |
347 |
|
350 |
|
348 |
Item[] items = getChildren(parent); |
351 |
Item[] items = getChildren(parent); |
349 |
int count = items.length; |
352 |
int count = items.length; |
Lines 355-361
Link Here
|
355 |
while (min <= max) { |
358 |
while (min <= max) { |
356 |
int mid = (min + max) / 2; |
359 |
int mid = (min + max) / 2; |
357 |
Object data = items[mid].getData(); |
360 |
Object data = items[mid].getData(); |
358 |
int compare = sorter.compare(this, data, element); |
361 |
int compare = internalCompare(sorter, parentPath, data, element); |
359 |
if (compare == 0) { |
362 |
if (compare == 0) { |
360 |
// find first item > element |
363 |
// find first item > element |
361 |
while (compare == 0) { |
364 |
while (compare == 0) { |
Lines 364-370
Link Here
|
364 |
break; |
367 |
break; |
365 |
} |
368 |
} |
366 |
data = items[mid].getData(); |
369 |
data = items[mid].getData(); |
367 |
compare = sorter.compare(this, data, element); |
370 |
compare = internalCompare(sorter, parentPath, data, element); |
368 |
} |
371 |
} |
369 |
return mid; |
372 |
return mid; |
370 |
} |
373 |
} |
Lines 377-382
Link Here
|
377 |
} |
380 |
} |
378 |
|
381 |
|
379 |
/** |
382 |
/** |
|
|
383 |
* Return the tree path that should be used as the parent path for the |
384 |
* given widget and sorter. A <code>null</code> is returned if either |
385 |
* the sorter is not a {@link TreePathViewerSorter} or if the parent |
386 |
* widget is not an {@link Item} (i.e. is the root of the tree). |
387 |
* |
388 |
* @param parent the parent widget |
389 |
* @param sorter the sorter |
390 |
* @return the tree path that should be used as the parent path for the |
391 |
* given widget and sorter |
392 |
*/ |
393 |
private TreePath internalGetSorterParentPath(Widget parent, ViewerSorter sorter) { |
394 |
TreePath path; |
395 |
if (sorter instanceof TreePathViewerSorter && parent instanceof Item) { |
396 |
Item item = (Item) parent; |
397 |
path = getTreePathFromItem(item); |
398 |
} else { |
399 |
path = null; |
400 |
} |
401 |
return path; |
402 |
} |
403 |
|
404 |
/** |
405 |
* Compare the two elements using the given sorter. If the |
406 |
* sorter is a {@link TreePathViewerSorter}, the provided |
407 |
* tree path will be used. If the tree path is null and the |
408 |
* sorter is a tree path sorter, then the elements are root elements |
409 |
* @param sorter the sorter |
410 |
* @param parentPath the path of the elements' parent |
411 |
* @param e1 the first element |
412 |
* @param e2 the seocnd element |
413 |
* @return the result of comparing the two elements |
414 |
*/ |
415 |
private int internalCompare(ViewerSorter sorter, TreePath parentPath, Object e1, Object e2) { |
416 |
if (sorter instanceof TreePathViewerSorter) { |
417 |
TreePathViewerSorter tpvs = (TreePathViewerSorter) sorter; |
418 |
return tpvs.compare(this, parentPath, e1, e2); |
419 |
} |
420 |
return sorter.compare(this, e1, e2); |
421 |
} |
422 |
|
423 |
/** |
424 |
* Return the sorted children for the given tree path. The |
425 |
* path is relative to the viewer input. |
426 |
* A path of <code>null</code> indicates that the children for |
427 |
* the input of the viewer are desired. |
428 |
* @param parentPath the parent path |
429 |
* @return the sorted children |
430 |
* @since 3.2 |
431 |
*/ |
432 |
protected Object[] getSortedChildren(TreePath parentPath) { |
433 |
ViewerSorter sorter = getSorter(); |
434 |
Object parent; |
435 |
if (parentPath == null) { |
436 |
parent = getInput(); |
437 |
} else { |
438 |
parent = parentPath.getLastSegment(); |
439 |
} |
440 |
if (sorter instanceof TreePathViewerSorter) { |
441 |
TreePathViewerSorter tpvs = (TreePathViewerSorter) sorter; |
442 |
Object[] result = getFilteredChildren(parent); |
443 |
// be sure we're not modifying the original array from the model |
444 |
result = (Object[]) result.clone(); |
445 |
tpvs.sort(this, parentPath, result); |
446 |
return result; |
447 |
} |
448 |
return super.getSortedChildren(parent); |
449 |
} |
450 |
|
451 |
/* (non-Javadoc) |
452 |
* @see org.eclipse.jface.viewers.StructuredViewer#getSortedChildren(java.lang.Object) |
453 |
*/ |
454 |
protected Object[] getSortedChildren(Object parent) { |
455 |
ViewerSorter sorter = getSorter(); |
456 |
if (sorter instanceof TreePathViewerSorter) { |
457 |
Widget w = internalGetWidgetToSelect(parent); |
458 |
if (w != null) |
459 |
return getSortedChildren(w, parent); |
460 |
} |
461 |
return super.getSortedChildren(parent); |
462 |
} |
463 |
|
464 |
/** |
465 |
* Get the sorted children for the parent using a |
466 |
* {@link TreePathViewerSorter} if there is one and |
467 |
* {@link StructuredViewer#getSortedChildren(Object)} if there isn't. |
468 |
* |
469 |
* @param widget |
470 |
* the widget for the parent |
471 |
* @param parent |
472 |
* the parent element |
473 |
* @return the sorted children |
474 |
*/ |
475 |
private Object[] getSortedChildren(Widget widget, Object parent) { |
476 |
ViewerSorter sorter = getSorter(); |
477 |
TreePath parentPath = internalGetSorterParentPath(widget, sorter); |
478 |
if (sorter instanceof TreePathViewerSorter) { |
479 |
return getSortedChildren(parentPath); |
480 |
} |
481 |
return super.getSortedChildren(parent); |
482 |
} |
483 |
|
484 |
/** |
380 |
* Adds the given child element to this viewer as a child of the given |
485 |
* Adds the given child element to this viewer as a child of the given |
381 |
* parent element. If this viewer does not have a sorter, the element is |
486 |
* parent element. If this viewer does not have a sorter, the element is |
382 |
* added at the end of the parent's list of children; otherwise, the |
487 |
* added at the end of the parent's list of children; otherwise, the |
Lines 514-520
Link Here
|
514 |
Object d = widget.getData(); |
619 |
Object d = widget.getData(); |
515 |
if (d != null) { |
620 |
if (d != null) { |
516 |
Object parentElement = d; |
621 |
Object parentElement = d; |
517 |
Object[] children = getSortedChildren(parentElement); |
622 |
Object[] children = getSortedChildren(widget, parentElement); |
518 |
for (int i = 0; i < children.length; i++) { |
623 |
for (int i = 0; i < children.length; i++) { |
519 |
createTreeItem(widget, children[i], -1); |
624 |
createTreeItem(widget, children[i], -1); |
520 |
} |
625 |
} |
Lines 1953-1959
Link Here
|
1953 |
// If the children weren't passed in, get them now since they're needed |
2058 |
// If the children weren't passed in, get them now since they're needed |
1954 |
// below. |
2059 |
// below. |
1955 |
if (elementChildren == null) { |
2060 |
if (elementChildren == null) { |
1956 |
elementChildren = getSortedChildren(parent); |
2061 |
elementChildren = getSortedChildren(widget, parent); |
1957 |
} |
2062 |
} |
1958 |
|
2063 |
|
1959 |
Control tree = getControl(); |
2064 |
Control tree = getControl(); |
Lines 2083-2089
Link Here
|
2083 |
} |
2188 |
} |
2084 |
} |
2189 |
} |
2085 |
|
2190 |
|
2086 |
/** |
2191 |
/** |
2087 |
* Updates the "+"/"-" icon of the tree node from the given element. It |
2192 |
* Updates the "+"/"-" icon of the tree node from the given element. It |
2088 |
* calls <code>isExpandable</code> to determine whether an element is |
2193 |
* calls <code>isExpandable</code> to determine whether an element is |
2089 |
* expandable. |
2194 |
* expandable. |