Community
Participate
Working Groups
Hi, I found a major bug in the TreeViewer of JFace 3.1.2 when adding items to a TreeViewer (with a Sorter) and some of the items are already exists in the TreeViewer. For Example, I have 2 TreeViewers: TreeViewer 1: + Element 1 + Element 5 + Element 10 TreeViewer 2: + Element 1 + Element 2 + Element 3 + Element 4 + Element 5 + Element 6 + Element 7 + Element 8 + Element 9 + Element 10 Now I move all Items from TreeViewer 2 to TreeViewer 1. During the TreeItem-creation-process an IllegalArgumentException (Index out of Bounds) occurs. Is there any solution for this problem? Regards, Lasse Knudsen
Created attachment 79886 [details] A JUnit Test for reproducing the problem
I have fixed this by changing the createAddedElements(Widget widget, Object[] elements)-Method in the AbstractTreeViewer implementation. Now the new variable insertedItems is used for changing the index variable instead of i. int newItems = 0; // NEW indicator for variable index instead of i for (int i = 0; i < elements.length; i++) { boolean newItem = true; Object element = elements[i]; int index; if (sorter == null) { if (itemExists(items, element)) { refresh(element); newItem = false; } index = -1; } else { lastInsertion = insertionPosition(items, sorter, lastInsertion, element); // As we are only searching the original array we keep track of // those positions only if (lastInsertion == items.length) index = -1; else {// See if we should just refresh while (lastInsertion < items.length && sorter.compare(this, element, items[lastInsertion].getData()) == 0) { // As we cannot assume the sorter is consistent with // equals() - therefore we can // just check against the item prior to this index (if // any) if (items[lastInsertion].getData().equals(element)) { // refresh the element in case it has new children refresh(element); newItem = false; } lastInsertion++;// We had an insertion so increment } // Did we get to the end? if (lastInsertion == items.length) index = -1; else { // Add the index as the array is growing // REPLACED i by newItems index = lastInsertion + newItems; } } } if (newItem) { createTreeItem(widget, element, index); newItems++; } } Is it okay or can this cause problems I am not aware of? Regards, Lasse
It seems that this problem is also reproducable in Eclipse 3.3
Excellent bug report! This seems to be a bug that we went undetected (or at least not narrowed down and fixed) for a long time.
Fix released to HEAD. I ended up re-structuring the whole method, and I fixed another bug having to do with equally sorted elements that are not the same. I did keep the original idea of maintaining a counter for how many new items we have created.
Thanks a lot, Lasse!
Reopening because of test failures in N20071107-0010.
Fixed. That'll teach me - adding a green test case to a green test suite does not necessarily result in a green test suite.
Glad to hear that I could help. :-) Thanks for fixing.