Search code examples
nattable

Example showing Multi Edit in Nattable


I have a requirement wherein on a single click in the cell, normal editing must be possible and on double clicking in the cell a dialog should open for editing the cell. The two are possible individually. I see a method "boolean supportMultiEdit(IConfigRegistry configRegistry, List configLabels)" but there is no example to show the working. Has anyone used it or can show it's configuration.


Solution

  • Multi edit means it is possible to edit multiple cells at once. This is of course done in an editor, as it makes no sense to perform multi edit inline. You should rather have a look at openInline(IConfigRegistry, List<String>) or even better the EditConfigAttributes#OPEN_IN_DIALOG to solve what you are looking for.

    But you are actually seeking for a way to handle opening an editor differently on different UI interactions. So you need to register the corresponding UI bindings. This is already discussed in the NatTable Forum.

    And the EditorExample shows quite a lot of possible configuration options available for editing. And almost every editable example shows multi editing capabilities. You simply need to select multiple cells you want to edit and then start typing or pressing F2.

    The following code would do the trick with a configuration based on a label that is added in the UI binding action:

    public class OpenEditorConfiguration extends AbstractRegistryConfiguration {
    
        @Override
        public void configureRegistry(IConfigRegistry configRegistry) {
            configRegistry.registerConfigAttribute(
                EditConfigAttributes.OPEN_IN_DIALOG,
                Boolean.TRUE,
                DisplayMode.EDIT,
                "open_in_dialog");
        }
    
        @Override
        public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
            uiBindingRegistry.registerDoubleClickBinding(
                new CellEditorMouseEventMatcher(GridRegion.BODY),
                new IMouseAction() {
    
                    @Override
                    public void run(NatTable natTable, MouseEvent event) {
                        int columnPosition = natTable.getColumnPositionByX(event.x);
                        int rowPosition = natTable.getRowPositionByY(event.y);
                        ILayerCell cell = natTable.getCellByPosition(columnPosition, rowPosition);
                        cell.getConfigLabels().add("open_in_dialog");
    
                        natTable.doCommand(new EditCellCommand(
                            natTable, 
                            natTable.getConfigRegistry(), 
                            cell));
                    }
                });
        }
    }