开发者

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 someth开发者_如何学编程ing 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.


I think the problem is that the shortcut listener is attached to the parent window/panel of the table, and not the table itself (the Vaadin way of handling shortcut actions).

So try wrapping each table with a Panel ("light" style if you don't want the extra borders), and see if that helps.

Otherwise I think you need to add/remove the individual table shortcut handlers each time you change the tab.


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();
                }
            }
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜