Delete rows from Nattable

一世执手 提交于 2019-11-29 18:36:53

In NatTable you would typically do the following:

  1. Create a command for deleting a row

    public class DeleteRowCommand extends AbstractRowCommand {
    
        public DeleteRowCommand(ILayer layer, int rowPosition) {
            super(layer, rowPosition);
        }
    
        protected DeleteRowCommand(DeleteRowCommand command) {
            super(command);
        }
    
        @Override
        public ILayerCommand cloneCommand() {
            return new DeleteRowCommand(this);
        }
    
    }
    
  2. Create a command handler for that command

    public class DeleteRowCommandHandler<T> implements ILayerCommandHandler<DeleteRowCommand> {
    
        private List<T> bodyData;
    
        public DeleteRowCommandHandler(List<T> bodyData) {
            this.bodyData = bodyData;
        }
    
        @Override
        public Class<DeleteRowCommand> getCommandClass() {
            return DeleteRowCommand.class;
        }
    
        @Override
        public boolean doCommand(ILayer targetLayer, DeleteRowCommand command) {
            //convert the transported position to the target layer
            if (command.convertToTargetLayer(targetLayer)) {
                //remove the element
                this.bodyData.remove(command.getRowPosition());
                //fire the event to refresh
                targetLayer.fireLayerEvent(new RowDeleteEvent(targetLayer, command.getRowPosition()));
                return true;
            }
            return false;
        }
    
    }
    
  3. Register the command handler to the body DataLayer

    bodyDataLayer.registerCommandHandler(
            new DeleteRowCommandHandler<your type>(bodyDataProvider.getList()));
    
  4. Add a menu item to your menu configuration that fires the command

    new PopupMenuBuilder(natTable)
            .withMenuItemProvider(new IMenuItemProvider() {
    
                @Override
                public void addMenuItem(NatTable natTable, Menu popupMenu) {
                    MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH);
                    deleteRow.setText("Delete");
                    deleteRow.setEnabled(true);
    
                    deleteRow.addSelectionListener(new SelectionAdapter() {
                        @Override
                        public void widgetSelected(SelectionEvent event) {
                            int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition();
                            natTable.doCommand(new DeleteRowCommand(natTable, rowPosition));
                        }
                    });
                }
            })
            .build();
    

Using this you don't need to call NatTable#refresh() because the command handler fires a RowDeleteEvent. I also don't suggest to call NatTable#refresh() in such a case, as it might change and refresh more than it should and would not update other states correctly, which is done correctly by firing the RowDeleteEvent.

Note that the shown example deletes the row for which the context menu is opened. If all selected rows should be deleted, you should create a command handler that knows the SelectionLayer and retrieve the selected rows as shown in the other answer.

Arye Shemesh

In our application we do the following:

  1. Get selected row objects:

    SelectionLayer selectionLayer = body.getSelectionLayer();
    int[] selectedRowPositions = selectionLayer.getFullySelectedRowPositions();
    Vector<Your Model Objects> rowObjectsToRemove = new Vector<Your Model Objects>();
    
    for (int rowPosition : selectedRowPositions) {
        int rowIndex = selectionLayer.getRowIndexByPosition(rowPosition);
        rowObjectsToRemove .add(listDataProvider.getRowObject(rowIndex));
    }
    
  2. Remove them from the data provider

  3. call natTable.refresh()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!