The problem is that I don’t want a user to be able to close a branch of the tree if that branch has the currently selected node. The main problem is, that GWT Tree seems to forget the selection once you close a branch. This is confusing. I was looking at event handlers like CloseHandler, but I don’t think I can prevent the close from happening (or should I attempt to immediately re-open the same node?)
For context, here’s the tree I’m talking about:
http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/ui/Tree.html
From reading how
TreeandTreeItemwork, it looks like the best option is to subclassTreeItemand overridesetState(boolean, boolean)to return early if some condition is true.The downside of this is that you will need to create one of your own instances where ever you want this logic to work. One way to make that easier would be to override the various
addItem(...)andaddTextItem(...)methods as well, to always create instances of this subclass.The
CloseEventis fired (looking at GWT 2.4.0, this may be different in older versions) from within the methodTree.fireStateChanged(TreeItem, boolean. This is a package protected method, so not accessible, and not the method which doesnt the actual heavy lifting of changing the dom.Tree.fireStateChangedis called from TreeItem.setState, the method overridden above. This method appears responsible not only for checking if it makes sense to open, but also avoiding redraw if not needed, and performing the actual redraw. It calls to the Tree to ask it to fire the events. Changing this code will ensure that even in the cases where events are not fired (not found in the GWT code, you may have some), it will still perform your logic.TreeItem.setState(boolean,boolean)is called from two places within GWT, one fromTreeItem.setState(boolean), as a convenience methods, and once when the user clicks an element (seeTree.elementClicked(Element)). The other override,TreeItem.setState(boolean)is called from three places, all concerned with keyboard navigation. From this, we can be sure that this is the main methods concerned with changing the tree expand/collapse status, and we can be confident that changing it will correctly affect the tree’s behavior.