gwtproject / gwt

GWT Open Source Project
http://www.gwtproject.org
1.52k stars 374 forks source link

Regression of Issue 1467 - Tree scrolls to selected item on expanding a node #5869

Closed dankurka closed 9 years ago

dankurka commented 9 years ago

Originally reported on Google Code with ID 5870

(If this is your first time submitting an issue report to the GWT Issue
Tracker, please see the sample issue report linked below for an example of
a well reported issue)
Sample issue report:
http://code.google.com/p/google-web-toolkit/wiki/IssueReportSample

Found in GWT Release (e.g. 1.5.3, 1.6 RC):
2.1.0

Encountered on OS / Browser (e.g. WinXP, IE6-7, FF3):
Linux / Firefox 3.6.13

Detailed description (please be as specific as possible):
Same as issue 1467.   Issue 1467 was originally resolved, but the problem is back again
in the newest versions of GWT.

1) Make a large tree that when expanded takes up more than the vertical
space on the screen.
2) Select an element, scroll the selected element off the screen, and
expand any node
3) The selected element scrolls back into view, away from what was just
expanded.

Shortest code snippet which demonstrates issue (please indicate where
actual result differs from expected result):

The same test case as 1467 fails in 2.1.0

private TreeItem buildTree(String prefix, int count, int levels) {
    TreeItem item = new TreeItem(new Label(prefix));
    item.setUserObject(prefix);
    if (levels > 0) {
        for (int i = 0; i < count; i++) {
            item.addItem(buildTree(prefix+"-"+i, count, levels - 1));
        }
    }
    return item;
}

public void onModuleLoad() {
    Tree bigTree = new Tree();
    bigTree.addItem(buildTree("TreeItem", 6, 3));
    RootPanel.get("slot1").add(bigTree);
}

Workaround if you have one:

It looks like the cause of the problem is in 
com.google.gwt.user.client.ui.Tree#onBrowserEvent

With SVN change #4659, there was a change from
      case Event.ONCLICK: {
        Element e = DOM.eventGetTarget(event);
        if (shouldTreeDelegateFocusToElement(e)) {
          // The click event should have given focus to this element already.
          // Avoid moving focus back up to the tree (so that focusable widgets
          // attached to TreeItems can receive keyboard events).
        } else if (curSelection != null
            && curSelection.getContentElem().isOrHasChild(e)) {
          setFocus(true);
        }
        break;
      }

to

      case Event.ONCLICK: {
        Element e = DOM.eventGetTarget(event);
        if (shouldTreeDelegateFocusToElement(e)) {
          // The click event should have given focus to this element already.
          // Avoid moving focus back up to the tree (so that focusable widgets
          // attached to TreeItems can receive keyboard events).
        } else if (curSelection != null) {
          setFocus(true);
        }
        break;
      }

Notice the removal of "&& curSelection.getContentElem().isOrHasChild(e)"

If I add that part back in, the bug seems to be fixed.

Issue #1467 lists a number of other workarounds that involve extending the Tree class

Links to relevant GWT Developer Forum posts:

Reported by oldchica@outlook.com on 2011-01-13 01:24:55

dankurka commented 9 years ago
CellTree doesn't seem to have this bug, however I cannot easily switch to it (as much
as i would like to), because I need to support dragging of tree items, and the gwt-dnd
library which I am currently using requires Widgets for drag sources.

Reported by oldchica@outlook.com on 2011-01-13 01:48:59

dankurka commented 9 years ago

Reported by dankurka@google.com on 2013-06-02 19:36:00