Bug 205700 - [Viewers] IllegalArgumentException occurs when adding elements to TreeViewer
Summary: [Viewers] IllegalArgumentException occurs when adding elements to TreeViewer
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 critical (vote)
Target Milestone: 3.4 M4   Edit
Assignee: Boris Bokowski CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed, greatbug
Depends on:
Blocks:
 
Reported: 2007-10-08 05:25 EDT by Lasse Knudsen CLA
Modified: 2007-11-08 17:23 EST (History)
2 users (show)

See Also:


Attachments
A JUnit Test for reproducing the problem (5.02 KB, text/plain)
2007-10-08 05:27 EDT, Lasse Knudsen CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lasse Knudsen CLA 2007-10-08 05:25:23 EDT
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
Comment 1 Lasse Knudsen CLA 2007-10-08 05:27:52 EDT
Created attachment 79886 [details]
A JUnit Test for reproducing the problem
Comment 2 Lasse Knudsen CLA 2007-10-08 07:34:22 EDT
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
Comment 3 Lasse Knudsen CLA 2007-10-11 05:06:57 EDT
It seems that this problem is also reproducable in Eclipse 3.3
Comment 4 Boris Bokowski CLA 2007-11-06 14:10:16 EST
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.
Comment 5 Boris Bokowski CLA 2007-11-06 17:13:01 EST
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.
Comment 6 Boris Bokowski CLA 2007-11-06 17:14:11 EST
Thanks a lot, Lasse!
Comment 7 Boris Bokowski CLA 2007-11-07 11:45:51 EST
Reopening because of test failures in N20071107-0010.
Comment 8 Boris Bokowski CLA 2007-11-07 11:54:33 EST
Fixed. That'll teach me - adding a green test case to a green test suite does not necessarily result in a green test suite.
Comment 9 Lasse Knudsen CLA 2007-11-08 17:23:44 EST
Glad to hear that I could help. :-) Thanks for fixing.