TreeTable Expand Event

Hello,
I was just wondering if there is an easy way to implement an expand event within a treetable?
The clickItem even doesnt trigger when you attempt to expand or collapse an item.

I attempted

table.addListener(Tree.ExpandEvent.class, this, "expandEvent");

but encoutered and illegal argument exception

Thanks for any guidance you can give

Hi,

The TreeTable doesn’t currently support Expand/Collapse events like the Tree component. I guess those could be added quite easily. Patch or/and ticket is welcome :wink:

Depending on what you want to do on those events, it might be best for you to implement the Collapsible interface in your data source. Then you would get your container notified when node is collapsed/expanded. I would also make your code cleaner if you need to persist the tree state and help the TreeTable to scale better if you have vast amounts of data in your container.

cheers,
matti

I’ve been unable to get implementing Collapsible to work. I have a container that extends HierarchicalContainer and implements Collapsible. Implementation is simple. I have a set tracking expanded items as follows:

private Set<Item> expandedItems = new HashSet<Item>();

My implementation is:

    public void setCollapsed(final Object itemId, final boolean collapsed) {
        if (hasChildren(itemId)) {
            if (collapsed) {
                expandedItems.remove(itemId);
            } else {
                if (!expandedItems.contains(itemId)) {
                    expandedItems.add((Item) itemId);
                }
            }
        }
    }

    public boolean isCollapsed(final Object itemId) {
        if (hasChildren(itemId)) {
            return !expandedItems.contains(itemId);
        }
        return true;
    }

I verified that TreeTable is calling this implementation from CollapsibleStrategy. However, the result is a TreeTable where all the items are always expanded. Even when you click on an item and the little arrow toggles between a right arrow and a down arrow, the sub-items remain visible.

Any ideas what is going on?

Matti,

How do you go about submitting a patch? Here is my submission to add expand and collapse events in TreeTable with minimal changes. Somewhere declare the following:

private final com.vaadin.ui.Tree shadowTree = new com.vaadin.ui.Tree();

make to following changes to toggleChildVisibility:

    private void toggleChildVisibility(final Object itemId) {
        final ContainerStrategy cs = getContainerStrategy();
        if (cs.isNodeOpen(itemId)) {
            fireEvent(shadowTree.new CollapseEvent(this, itemId));
        } else {
            fireEvent(shadowTree.new ExpandEvent(this, itemId));
        }
        getContainerStrategy().toggleChildVisibility(itemId);
        // ensure that page still has first item in page, ignore buffer refresh
        // (forced in this method)
        setCurrentPageFirstItemIndex(getCurrentPageFirstItemIndex());

        requestRepaint();
    }

That will work for the user to listen in on all component events and check if the event is a collapse event or expand event. To explicitly enable listening to those events add the following code to the class (just copied from Tree):


    /**
     * Adds the expand listener.
     * 
     * @param listener
     *            the Listener to be added.
     */
    public void addListener(final ExpandListener listener) {
        addListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD);
    }

    /**
     * Removes the expand listener.
     * 
     * @param listener
     *            the Listener to be removed.
     */
    public void removeListener(final ExpandListener listener) {
        removeListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD);
    }

    /**
     * Adds the collapse listener.
     * 
     * @param listener
     *            the Listener to be added.
     */
    public void addListener(final CollapseListener listener) {
        addListener(CollapseEvent.class, listener, CollapseListener.COLLAPSE_METHOD);
    }

    /**
     * Removes the collapse listener.
     * 
     * @param listener
     *            the Listener to be removed.
     */
    public void removeListener(final CollapseListener listener) {
        removeListener(CollapseEvent.class, listener, CollapseListener.COLLAPSE_METHOD);
    }

Hi,

The whole process should go like this:

  1. create a ticket about the enhancement/bugfix to dev.vaadin.com.
  2. assign the ticket to yourself while you are working on it
  3. check out the project from SVN using command line or IDE
  4. code your changes
  5. test your changes
  6. create a patch using “svn diff” or with you IDE (dev.vaadin.com should have more specific instructions)
  7. attach the patch to the ticket
  8. assign the ticket to e.g. me (or some other Vaadin committer) for a review and commit

I haven’t checked whether this “process” is described somewhere. If it isn’t at the dev.vaadin.com it should definitely be added there.

cheers,
matti

Thanks. Submitted a patch with the above code. Also created a Ticket for the issue using the external Collapsible implementation, detailed above.

Jonathan