javabindingnattable

Why can't I implement a CellEditorMouseEventMatcher with a specific CellEditor in Java NatTable?


I want to implement a MouseEvent in my NatTable for all cells, which are implemented through the ComboBoxCellEditor class. If a single click occurs on the combo box, the drop down box with the entries shall be opened. Therefor I registered a FirstSingleClickBinding in my UiBindingRegistry.

I used the BodyCellEditorMouseEventMatcher and it worked perfectly. But since this matcher is deprecated I don’t want to use it any longer. So it says that I should use the CellEditorMouseEventMatcher which I tried but it doesn’t work the way I expect it. The documentation of the CellEditorMouseEventMatcher says the following:

Implementation of {@link IMouseEventMatcher} that will check if editing
* should be activated. For this it is possible to specify the region label to
* react on, the mouse button that was used to click and if an editor is
* registered for the cell on which the mouse click was executed. If no region
* label is specified, only the mouse button and the presence of a cell editor
* is evaluated.

But I can’t use an cell editor as a parameter so what does the last sentence mean? I just have the option to give a button and/or a region label as parameters. How can I use this EventMatcher to have the same result as with the BodyCellEditorMouseEventMatcher? I use different types of cells so it is important that just the combo box ones get this behavior.

Here is my “deprecated” code:

private void editCombosOnSingleClick(final UiBindingRegistry uiBindingRegistry) {
    uiBindingRegistry.registerFirstSingleClickBinding(new BodyCellEditorMouseEventMatcher(ComboBoxCellEditor.class),
        new MouseEditAction());
}

If you need more information please let me know. Thank you :)


Solution

  • With the CellEditorMouseEventMatcher you don't need to specify the editor implementation. You only specify the region and the button (both optional). If an editor should be opened or not is simply done by checking if there is an editor. The type of the editor doesn't matter.

    uiBindingRegistry.registerSingleClickBinding(
                new CellEditorMouseEventMatcher(GridRegion.BODY, MouseEventMatcher.LEFT_BUTTON),
                new MouseEditAction());
    

    With the BodyCellEditorMouseEventMatcher you needed to register a new matcher for every editor you added. This is a design flaw, because the type of the editor should not matter. The control whether a cell is editable or not is done via IEditableRule or even by registering a cell editor.

    So in the end you need to ensure that you either only register editors for editable cells, which in turn means you need to adjust the configuration to ensure that the TextCellEditor is not registered as default editor (see DefaultEditConfiguration). Or you specify an IEditableRule that only evaluates to true if the editor type is ComboBoxCellEditor or even better register the IEditableRule#ALWAYS_EDITABLE only for the same label as the editor and by default the IEditableRule#NEVER_EDITABLE.