Event for re-clicking the selected item in a tree?

Currently there doesn’t seem to be a way listen selecting the already selected item in a tree. In fact, clicking the active item in a tree doesn’t even seem to send a request to the server.

There is a use case where it might be useful to listen these kind of events. As an example, when a Tree is used for selecting an active view in the other section of a SplitPane and then the user navigates further in the other section, it would be intuitive to reset the view to its original state by re-clicking the item in the Tree.

Maybe there should be a way to tell a Tree that the selected item should remain “clickable” and clicking it should trigger an event. In most cases, the current behavior is probably the correct one. What do other people think?

Hi,

You can catch the events in such case by having
setNullSelectionAllowed(true)
. When the user clicks on already selected item, the selection will be null, but you can re-select the previously selected item from server-side when you handle the value change event. The selection won’t even flash or anything.


// Allow null selection - this is the default actually.
tree.setNullSelectionAllowed(true);

// When a tree item (planet or moon) is clicked, open the item in Details view.
tree.setImmediate(true);
tree.addListener(new ValueChangeListener() {
    String lastselected = null;
    
    public void valueChange(ValueChangeEvent event) {
        String planet = (String) tree.getValue();

        // Reselect a selected item if it is unselected by clicking it.
        if (planet == null) {
        	planet = lastselected;
        	tree.setValue(planet);
        }
        lastselected = planet;
        ...

So this probably would not require an API change, unless you want to have a more neat solution. The developers who could probably best comment on this are on vacation right now, but maybe they will comment on it later when they come back. You could make a ticket if you think this is worth an feature request.

It seems like the new ItemClickEvent makes this a lot easier, there even seems to be (haven’t tried it yet) support for fancy stuff like key modifiers, double clicks etc. Neat!

There is one thing that puzzled me, though. It seems that
setNullSelectionAllowed(true)
still needs to be called to enable the events, otherwise the already selected item is not re-clickable. Is this on purpose and if so, why?

AFAIK it’s a bug - though if I read the code right, it’s more subtle (read: weird): the click event seems to be (accidentally) non-immediate in that particular case - it should come trough the next time you press a button, for instance. I have yet to create a test-case and try, though…

Created ticket:
http://dev.itmill.com/ticket/2104

Best Regards,
Marc