Marthin Marthin - 9 days ago 5
Java Question

Vaadin: Tabs with Table event handling

We have implemented a tab view in Vaadin where each tab has a instance of our extended Table class.

In our table we have added a shortcutlistener that listens to ENTER-keypress. Once enter is pressed the table becomes either editable or non-editable.

Our problem is this: If we make Table(1) editable and switch tab then Table (1) in the old tab still holds controll over the ENTER event and hence we can't perform a new ENTER-keypress event in the Table (2) and make Table(2) editable.

Is there some way to bind some kind of of event to a table that says something like:

If Table.focus() is false then
release event.ENTER


Or if there is some other way, like
Table.OnFocus()
you could take control or something.

Answer

So the way I solved this was by clearing and adding action handlers just as Jouni suggested.

This is a sample of the code:

From the class extending Table

public void initTableListeners(){
    extValueChangeListener = new ExtendedValueChangeListener();
    extMouseListener = new ExtendedMouseListener();
    extShortcutListener = new ExtendedShortcutListener("enter", KeyCode.ENTER);

    setTableListeners();
}

/**
 * Registers the default listeners to the table.
 */
public void setTableListeners(){        
    if(!hasListeners(extValueChangeListener.getClass())){           
        addListener(extValueChangeListener);
    }
    if(!hasListeners(extMouseListener.getClass())){         
        addListener(extMouseListener);
    }
    if(!hasListeners(extShortcutListener.getClass())){          
        addShortcutListener(extShortcutListener);
    }
}

/**
 * Clears the listeners registered to the table.
 */
public void clearTableListeners(){
        removeListener(extValueChangeListener);
        removeListener(extMouseListener);
        removeShortcutListener(extShortcutListener);

}

@Override
public EditTable getTable() {       
    return this;
}

And this is from tab listener:

class ExtendedSelectedTabChangeListener implements SelectedTabChangeListener{
        @Override
        public void selectedTabChange(SelectedTabChangeEvent event) {
            // clear old tables listeners
            if(currentTab != null){
                Component table = currentTab.getComponent();
                if(table instanceof EditTableInterface){
                    ((EditTableInterface) table).getTable().clearTableListeners();
                }
            }

            // add new listeners to new table
            currentTab = tabsheet.getTab(tabsheet.getSelectedTab());
            if(currentTab != null){
                Component table = currentTab.getComponent();
                if(table instanceof EditTableInterface){
                    ((EditTableInterface)table).getTable().setTableListeners();
                }
            }
        }

    }