Create my Own listener (TreeTable)

Hello,

I would like to create my own listener in order to
detect when user Expand/Collapse columns
of my TreeTable.
I read it in the l
book of Vaadin
, however I don’t know how to start !
Could someone give me some help ?

Thanks,
Best regards.

No idea ?

There is no separate listener for this (you can listen to column resize events or reorder events), but you can override setColumnCollapsed(propertyId, collapsed) and there call your listeners after calling the superclass method.

Hello,
my simple implementation for Table is as follows:

package cz.pse.tableconfigurator.component;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.tepi.filtertable.FilterTable;

import com.vaadin.ui.Component;

@SuppressWarnings("serial")
public class FilterTableWithCollapseEvent extends FilterTable {

    public FilterTableWithCollapseEvent() {
        super();
    }

    public FilterTableWithCollapseEvent(String caption) {
        super(caption);
    }
   
   @Override
    public void changeVariables(Object source, Map<String, Object> variables) {
        handleColumnCollapseEvent(variables);
        super.changeVariables(source, variables);
    }

    /**
     * Handles the column collapse event sent by the client.
     *
     * @param variables
     */
    private void handleColumnCollapseEvent(Map<String, Object> variables) {
        if (variables.containsKey("collapsedcolumns")) {
            String[] collapsedCollumns  = (String[]
) variables.get("collapsedcolumns");
            Object[] visibleColumns = getVisibleColumns();
            for (int j = 0; j < visibleColumns.length; j++) {
                boolean isCollapsed = ArrayUtils.contains(collapsedCollumns , columnIdMap.key(visibleColumns[j]
));
            	if (isColumnCollapsed(visibleColumns[j]
) != isCollapsed){
                    fireEvent(new ColumnCollapseEvent(this, visibleColumns[j]
, isCollapsed));
            	}
            }
        }
    }
   
    public void addColumnCollapseListener(ColumnCollapseListener listener){
        addListener("columnCollapse", ColumnCollapseEvent.class, listener,     ColumnCollapseEvent.COLUMN_COLLAPSE_METHOD);
    }
   
    public void removeColumnCollapseListener(ColumnCollapseListener listener){
        removeListener("columnCollapse", ColumnCollapseEvent.class, listener);
    }
   
    public static class ColumnCollapseEvent extends Component.Event {
        public static final Method COLUMN_COLLAPSE_METHOD;

        static {
            try {
                COLUMN_COLLAPSE_METHOD = ColumnCollapseListener.class
                        .getDeclaredMethod("columnCollapse",
                                new Class[] { ColumnCollapseEvent.class });
            } catch (final java.lang.NoSuchMethodException e) {
                // This should never happen
                throw new java.lang.RuntimeException(e);
            }
        }

        private final boolean collapsed;
        private final Object columnPropertyId;

        /**
         * Constructor
         *
         * @param source
         *            The source of the event
         * @param propertyId
         *            The columns property id
         * @param collapsed
         *            Indicates if colum was collapsed or elapsed
         */
        public ColumnCollapseEvent(Component source, Object propertyId, boolean collapsed) {
            super(source);
            this.collapsed = collapsed;
            columnPropertyId = propertyId;
        }

        /**
         * Get the column property id of the column that was resized.
         *
         * @return The column property id
         */
        public Object getPropertyId() {
            return columnPropertyId;
        }
       
        public boolean getCollapsed(){
            return collapsed;
        }
       
    }
   
    /**
     * Interface for listening to column collapse events.
     */
    public interface ColumnCollapseListener extends Serializable {

        /**
         * This method is triggered when the column has been collapsed or elapsed
         *
         * @param event
         *            The event which contains the column property id and new collapse state
         */
        public void columnCollapse(ColumnCollapseEvent event);
    }


}

Agata,

Thanks for your code.
That is exactly what I was looking for. :):slight_smile:

Jennifer.

Agata,

I am getting the event as many as collapsed columns.
However, I am interested in getting the collapsed column name via the event.

String collapsedCollumns contains ids.
I thought the Id can be used as an index to get the collapsed column name from visibleColumns.
However, that doesn’t match.

If you can think any way to get collaped column name, then please advise.
Thanks,
Jennifer.

Hello Jennifer,
you can get this information from
ColumnCollapseEvent:


  • getComponent()
    returns a table, when event has occured

  • getPropertyId()
    returns property ID of collapsed column (the one from
    visibleColumns
    )

  • getCollapsed()
    returns
    true
    , if column was collapsed and
    false
    , if the collumn was expanded

Agata

Thank you for the reply, Agata.
I will try what you said above and will post the details later.

Thanks again. :slight_smile:
Jennifer.

Hi Agita,

I think getPropertyId() returns visibleColumns (this is the full list of visible columns) rather than a property ID of collapsed column.

Please advise me how to update handleColumnCollapseEvent() to return a property ID of collapsed column (the one from visibleColumns) as you mentioned above.
Thanks,
Jennifer.

Oh, I see. Please look at my edited post (
https://vaadin.com/forum#!/thread/1634152/3502612
).

Agata,

String collapsedCollumns = (String
) variables.get(“collapsedcolumns”);
Object visibleColumns = getVisibleColumns();

Suppose that I have an example to show ‘Field1, Field2, Field3, Field4’ as visibleColumns.
If I collapse Field1 and Field2, then I am getting [“1”,“2”]
in collapsedCollumns and I can get the column name “Field1” by visibleColumns[1]
and “Field2” by visibleColumns[2]
with no problem.

However, if I call Table.setColumnCollapsed(“Field1”, true) in my application during the initialization as a default and I collapsed Field2 from GUI screen manually, then I am getting [“3”, “4”]
in collapsedCollumns.
I don’t understand why property ids got changed when I call setColumnCollapsed() programatically.

Any idea?
Thanks,
Jennifer.

Hello Jennifer,
I figured it out:
variables.get(“collapsedcolumns”)
doesn’t contain indexes from
visibleCollumns

,
but those indexes can be obtain through
columnIdMap
(see next version of post with code). But this change would fire the event, only if the collapsed state is changed from UI. To make it work, when calling the method programatically also,
setColumnCollapsed(Object propertyId, boolean collapsed)
should be overrided : public void setColumnCollapsed(Object propertyId, boolean collapsed) throws IllegalStateException { super.setColumnCollapsed(propertyId, collapsed); fireEvent(new ColumnCollapseEvent(this, propertyId, collapsed)); }
,not
changeVariables(Object source, Map<String, Object> variables)

Hi Agata,

Overriding setColumnCollapsed is a perfect idea.
It solved my problems and issues.
It works for both GUI action and programatical calls.

Thank you so much for your help on this.
Jennifer.

Hi,
I am new to Vaadin, And having confusion while getting list(name or property Id) of collapsed column or columns which are not collapsed.
I have created a class which will override setColumnCollapsed as well as change variable
suppose class name is MyFilterTable extends FilterTable where i have written given code now I am creating FilterTable using above class
GetCollapsedColumn.java >

MyFilterTable pagedFilterTable = new MyFiilterTable();
pagedFilterTable.setColumnCollapsingAllowed(true);
pagedFilterTable.setColumnReorderingAllowed(true);

I don’t understand how to get Collapsed column here

Please advice me how to get Collpased column names.