显示NatTable上下文菜单 [英] Showing NatTable context menu
问题描述
我使用NatTable.如何根据单元格的内容在特定条件下显示上下文菜单项?以及如何选择调用了上下文菜单的单元格?我将菜单与以下代码绑定
I use NatTable. How to show context menu item on certain condition depending on the content of the cell? And how to select cell over which context menu was called? I bind menu with the following code
uiBindingRegistry.registerMouseDownBinding(
new MouseEventMatcher(SWT.NONE, null, MouseEventMatcher.RIGHT_BUTTON), new PopupMenuAction(menu));
UPD:
我创建这样的菜单,但是尽管isActive
仍然显示'Test'项目,但始终返回false
.怎么了?
UPD:
I create menu like this, but 'Test' item is visible in spite of isActive
always return false
. What's wrong with it?
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();
推荐答案
给出的答案是正确的.虽然可以改善.您不需要SelectionLayer.
The given answer is correct. Although it can be improved. You don't need the 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.
This way you completely remove the need to reference the SelectionLayer and even improve the functionality because the SelectCellCommand is never fired if you right click on a selected cell.
这篇关于显示NatTable上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!