从Nattable中删除行

编程入门 行业动态 更新时间:2024-10-22 12:30:09
本文介绍了从Nattable中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在星云Nattable中实现行删除逻辑. 我打算这样做:

I want to implement a row deletion logic in a Nebula Nattable. This is what I plan to do:

  • 在 blog.vogella/2015/02/03/nattable-context-menus-with-eclipse-menus/
  • 在菜单中添加SWT操作,以实现删除
  • 我的问题是,这是完成此任务的最佳方法:

    my question is, which is the best way to accomplish this:

    • 是否应该从数据模型中删除相应的值,并在执行this.natview.refresh();时刷新表视图? OR
    • 是否应该从SelectionLayer中获取行并将其删除(如果是,该怎么办?)? OR
    • 通过IConfiguration对此功能是否有默认支持?
    • Should I delete the corresponding value from my data model and the table view is refreshed when I execute this.natview.refresh();? OR
    • Should I get the rows from SelectionLayer and delete them (if so how do I do ?)? OR
    • is there any default support for this function through IConfiguration?
    推荐答案

    在NatTable中,您通常会执行以下操作:

    In NatTable you would typically do the following:

  • 创建用于删除行的命令

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

  • 为该命令创建命令处理程序

  • 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; } }

  • 将命令处理程序注册到主体DataLayer

  • Register the command handler to the body DataLayer

    bodyDataLayer.registerCommandHandler( new DeleteRowCommandHandler<your type>(bodyDataProvider.getList()));

  • 将菜单项添加到您的菜单配置中,以触发命令

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

  • 使用此命令不需要调用NatTable#refresh(),因为命令处理程序将触发RowDeleteEvent.我也不建议在这种情况下调用NatTable#refresh(),因为它可能会更改和刷新超过其应有的值,并且不会正确地更新其他状态,这可以通过触发RowDeleteEvent来正确完成.

    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.

    请注意,所示示例删除了为其打开上下文菜单的行.如果应该删除所有 selected 行,则应创建一个知道SelectionLayer的命令处理程序,并检索所选行,如其他答案所示.

    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.

    更多推荐

    从Nattable中删除行

    本文发布于:2023-10-23 13:56:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1521017.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Nattable

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!