Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Cell popup menu
Cell popup menu [message #1415851] Wed, 03 September 2014 20:33 Go to next message
Peter Deussen is currently offline Peter DeussenFriend
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 #1416035 is a reply to message #1415851] Thu, 04 September 2014 07:46 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Ah, that's a tricky one. Smile

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.

Re: Cell popup menu [message #1416047 is a reply to message #1416035] Thu, 04 September 2014 08:19 Go to previous message
Peter Deussen is currently offline Peter DeussenFriend
Messages: 2
Registered: September 2014
Junior Member
Dirk Fauth wrote on Thu, 04 September 2014 07:46
Ah, that's a tricky one. Smile

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

Next Topic:GlazedListTreeRowModel vs. TreeRowModel
Goto Forum:
  


Current Time: Thu Apr 25 16:43:07 GMT 2024

Powered by FUDForum. Page generated in 0.02993 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top