Search code examples
vaadinshortcutvaadin7

Select-all shortcut (Ctrl-A) in Vaadin Table?


I have the following snippet in my UI Builder code:

table.addShortcutListener(new ShortcutListener("Select all", null, KeyCode.A, ModifierKey.CTRL) {

  @Override
  public void handleAction(Object sender, Object target) {
    AbstractSelect t = (AbstractSelect) target;
    if (t.isMultiSelect()) {
      t.setValue(t.getItemIds());
    }
  }
});
return table;

This allows to press Ctrl+A to select all items in a table. This usually works the first time I load a view until I make one of the tables invisible (setVisible(false)). After making the tables visible again, it no longer works (not even on reloading the page) and I get the following console output whenever I press Ctrl+A:

WARNING: Ignoring action for disabled connector c.b.a.web.ui.builder.table.TranslatedHeaderTable
Nov 03, 2014 11:15:00 AM com.vaadin.event.ConnectorActionManager handleAction

What is wrong with my code? How would I achieve my goal?


Solution

  • It seems there are some problems with the Table implementation of the Action.Notifier interface. In this Vaadin Forum Post, the Vaadin Devs suggest to add the ShortcutListener not to the Table itself but to a Panel that the Table is enclosed within.

    My new implementation:

    private void addCtrlAHandler(final AbstractSelect table) {
        Panel panelEnclosingTable = table.findAncestor(Panel.class);
        Preconditions.checkArgument(panelEnclosingTable != null, "Table is not enclosed in a panel; cannot add shortcut handlers");
    
        panelEnclosingTable.addShortcutListener(new ShortcutListener("Select all", null, KeyCode.A, ModifierKey.CTRL) {
    
            @Override
            public void handleAction(Object sender, Object target) {
                if (table.isMultiSelect()) {
                    table.setValue(table.getItemIds());
                }
            }
        });
    }
    

    With this workaround, I get the expected behavior.