显示 NatTable 上下文菜单

Showing NatTable context menu

我使用 NatTable。如何根据单元格的内容在特定条件下显示上下文菜单项?以及如何 select 调用上下文菜单的单元格?我用下面的代码绑定菜单

uiBindingRegistry.registerMouseDownBinding(
            new MouseEventMatcher(SWT.NONE, null, MouseEventMatcher.RIGHT_BUTTON), new PopupMenuAction(menu));

更新: 我创建了这样的菜单,但是 'Test' 项目是可见的,尽管 isActive 总是 return false。怎么了?

menu = new PopupMenuBuilder(natTable).withMenuItemProvider(ITEM_ID, new IMenuItemProvider() {
        @Override
        public void addMenuItem(final NatTable natTable, final Menu popupMenu) {
            final MenuItem menuItem = new MenuItem(popupMenu, SWT.PUSH);
            menuItem.setText("Test");
            menuItem.setEnabled(true);
            menuItem.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(final SelectionEvent event) {
                    System.out.println("test");
                }
            });
        }
    }).withVisibleState(ITEM_ID, new IMenuItemState() {
        @Override
        public boolean isActive(final NatEventData natEventData) {
            return false;
        }
    }).build();

您需要 IMouseAction 到 select 单元格。来自 here 的示例代码以及其中添加到 select 调用的一些附加代码如下:

nattable.addConfiguration( new AbstractUiBindingConfiguration()

    uiBindingRegistry.registerMouseDownBinding( new MouseEventMatcher( SWT.NONE, GridRegion.BODY,
                            MouseEventMatcher.RIGHT_BUTTON ), new CellPopupMenuAction(menu, selectionLayer) );


});

class CellPopupMenuAction implements IMouseAction {

                private final Menu menu;
                private final SelectionLayer selectionLayer;

                public CellPopupMenuAction(Menu menu, SelectionLayer selectionLayer) {
                    this.menu = menu;
                    this.selectionLayer = selectionLayer;
                }

                @Override
                public void run(NatTable natTable, MouseEvent event) 
                {
                    if( selectionLayer.getSelectedRowCount() <= 1 )
                            {
                                int colPosition = LayerUtil.convertColumnPosition( natTable,
                                        natTable.getColumnPositionByX( event.x ), selectionLayer );
                                int rowPosition = LayerUtil.convertRowPosition( natTable,
                                        natTable.getRowPositionByY( event.y ), selectionLayer );

                                natTable.doCommand( new SelectCellCommand( selectionLayer, colPosition, rowPosition, false,
                                        false ) );
                            }
                    menu.setData(event.data);
                    menu.setVisible(true);
                }
            }

我假设您在代码中将 selectionLayer 作为私有变量。

给出的答案是正确的。虽然可以改进。您不需要 SelectionLayer。

class CellPopupMenuAction implements IMouseAction {

    private final Menu menu;

    public CellPopupMenuAction(Menu menu) {
        this.menu = menu;
    }

    @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);

        if (!cell.getDisplayMode().equals(DisplayMode.SELECT)) {
            natTable.doCommand(
                    new SelectCellCommand(
                            natTable,
                            columnPosition,
                            rowPosition,
                            false,
                            false));
        }

        menu.setData(MenuItemProviders.NAT_EVENT_DATA_KEY, event.data);
        menu.setVisible(true);
    }
}

这样您就完全不需要引用 SelectionLayer 甚至可以改进功能,因为如果您右键单击选定的单元格,SelectCellCommand 永远不会被触发。

我在上面使用了 SomeDude 的答案并且它起作用了,但是菜单项显示在看起来很奇怪的选择之前。我将菜单显示移动到 UI 线程 asyncExec 调用,首先进行选择,然后菜单出现在它上面 -

@Override
public void run(NatTable natTable, MouseEvent event) {

    if (selectionLayer.getSelectedRowCount() <= 1) {

        int colPosition = LayerUtil.convertColumnPosition(natTable, natTable.getColumnPositionByX(event.x),
                selectionLayer);

        int rowPosition = LayerUtil.convertRowPosition(natTable, natTable.getRowPositionByY(event.y),
                selectionLayer);

        natTable.doCommand(new SelectCellCommand(selectionLayer, colPosition, rowPosition, false, false));

    }

    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            menu.setData(event.data);
            menu.setVisible(true);
        }
    });

}