Cell popup menu [message #1415851] |
Wed, 03 September 2014 20:33 |
Peter Deussen Messages: 2 Registered: September 2014 |
Junior Member |
|
|
Hi,
how do get a popup menu that opens at right clicks at specific positions only. More precisely, here is what I want to achieve:
1) right click on a cell in the first column of a NatTable -> popup opens
2) right click anywhere else -> nothing happens.
Below is my code so far. What happens is that the menu opens on right click everywhere (so case 2 is not captured). Row index, column index, contents of the cell where the right click happened can be retrieved just fine.
So what do I wrong?
Thanks, Peter
class BodyMenuConfiguration extends AbstractUiBindingConfiguration {
private final SelectionLayer selectionLayer;
private Menu bodyMenu;
public BodyMenuConfiguration(final NatTable natTable, SelectionLayer selectionLayer) {
this.selectionLayer = selectionLayer;
this.bodyMenu = createBodyMenu(natTable).build();
natTable.addDisposeListener(new DisposeListener() {
// code to dispose the menu }
});
}
// construct the menu
private PopupMenuBuilder createBodyMenu(NatTable natTable) {
Menu menu = new Menu(natTable);
natTable.setMenu(menu);
MenuItem mntmCopy = new MenuItem(menu, SWT.NONE);
mntmCopy.setText("Copy");
return new PopupMenuBuilder(natTable, menu);
}
@Override
public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
// create a specific mouse event matcher to get right clicks on
// the first column only
MouseEventMatcher matcher = new MouseEventMatcher(SWT.NONE, GridRegion.BODY, 3) {
@Override
public boolean matches(NatTable natTable, MouseEvent event, LabelStack regionLabels) {
if (super.matches(natTable, event, regionLabels)) {
// some clever code to compute the column where the click happened
if (coords.getColumnPosition() == 1) {
return true;
}
}
return false;
}
};
uiBindingRegistry.registerMouseDownBinding(matcher, new CellPopupMenuAction(bodyMenu, selectionLayer));
}
}
So far, so good. Now lets look at the action associated with the mouse click:
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) {
ILayerCell cell = // get the cell where the click happened
}
}
Finally, let's not forget to add to the configuration.
natTable.addConfiguration(new BodyMenuConfiguration(natTable, bodyLayerStack.getSelectionLayer()));
|
|
|
|
Re: Cell popup menu [message #1416047 is a reply to message #1416035] |
Thu, 04 September 2014 08:19 |
Peter Deussen Messages: 2 Registered: September 2014 |
Junior Member |
|
|
Dirk Fauth wrote on Thu, 04 September 2014 07:46Ah, that's a tricky one.
You are configuring a SWT context menu by using natTable.setMenu(). This means SWT opens the context menu, and not NatTable controls the opening of the context menu.
You need to remove natTable.setMenu() from your createBodyMenu() method, then everything should work as intended as it is then controlled by NatTable.
Yes, supprisingly, it seems to be tricky. I tried your solution, the result is that the menu doesn't show up any more, regardless where the click happens. So we miss something here. Thanks, Peter
EDIT:
Got it, I simply forgot to show the menu; the code should look like this:
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) {
// code as before
// and here's what I forgot:
menu.setData(event.data);
menu.setVisible(true);
}
}
Thank you for very much!
Peter
[Updated on: Thu, 04 September 2014 08:29] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03873 seconds