Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » RAP MenuDetect events triggered incorrectly(MenuDetect event and menu presentation triggered incorrectly)
RAP MenuDetect events triggered incorrectly [message #1021199] Tue, 19 March 2013 16:39 Go to next message
John Gymer is currently offline John GymerFriend
Messages: 198
Registered: November 2012
Location: UK
Senior Member
Good afternoon gents...

I have what I believe is a small problem with RAP when using MenuDetect events on Tables/Trees. Firstly, here is a snippet for RAP:

/* DEMONSTRATES RAP PROBLEM WITH MENUDETECT ONLY OCCURRING ONCE IN TABLE HEADERS */
package bug.snippet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Bugsy {
	private Display display;
	private Shell shell;
	private static Table tab;
	private static TableColumn col;
	int answer = -1;
	private Menu menu;
	
	public void begin() {
		System.out.println("BugSnippy Starting...");
		
		// create the Shell
		display = new Display();
		shell = new Shell(display, SWT.APPLICATION_MODAL|SWT.TITLE|SWT.CLOSE);
		shell.setText("Shell");
		//shell.setMaximized(true);
		shell.setBounds(10, 10, 900, 500);
		shell.setBackground(new Color(null, new RGB(255,128,128)));
		FormLayout layout = new FormLayout();
		shell.setLayout(layout);

		//create the table
		tab = new Table(shell, SWT.FULL_SELECTION|SWT.MULTI|SWT.BORDER);
		tab.setHeaderVisible(true);
		tab.setLinesVisible(true);

		col = new TableColumn(tab, SWT.NONE);
		col.setResizable(true);
		col.setWidth(700);
		col.setText("My Column");

		col = new TableColumn(tab, SWT.NONE);
		col.setResizable(true);
		col.setWidth(225);
		col.setText("My 2nd Column");

		//add some data to the table
		String[] tableData = new String[2]; //2 columns
		
		for (int i = 0; i < 100; i++) {
			tableData[0] = "My Value " + i;
			tableData[1] = "Wonderful " + i*10;
			TableItem tableItem = new TableItem(tab, SWT.None, 0);
			tableItem.setText(tableData);
		}
		
		//set table's position
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 5);
		fd.top = new FormAttachment(0, 5);
		fd.width = 800;
		fd.height = 400;
		tab.setLayoutData(fd);

		tab.addListener(SWT.MenuDetect, menuDetectListener);

		// construct the table's context popup menu
		menu = new Menu(shell, SWT.POP_UP);
		MenuItem mi = new MenuItem(menu, SWT.NONE);
		mi.setText("Select All");
		mi.addListener(SWT.Selection, menuItemListener);
		
		shell.open();
		while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
		}
		display.dispose();

		System.out.println("BugSnippy Done!");
	}

	Listener menuDetectListener = new Listener() {
		public void handleEvent(Event event) {
			Point pt = display.map(null, tab, new Point(event.x, event.y));
			Rectangle clientArea;
			boolean header;
			clientArea = tab.getClientArea();
			header = clientArea.y <= pt.y && pt.y < (clientArea.y + tab.getHeaderHeight());
			if (header) {
				System.out.println("Context menu required here in table header");
				tab.setMenu(menu);
			}
			else {
				System.out.println("No context menu in this location thanks - table header only");
				tab.setMenu(null);
			}
		}
	};

	Listener menuItemListener = new Listener() {
		public void handleEvent(Event event) {
			System.out.println("Item on Context Menu Selected!");
		}
	};

}


The purpose of the code is to detect a right-click (context menu) in a Table (behaviour is the same in a Tree) and only popup the context menu if the user requested it from the Table's header, not when clicking inside the data rows.

So we:
- add a MenuDetect listener to the Table
- create our pop-up menu up-front, but don't use it yet
- in the MenuDetect listener we determine where the click was done - inside the Table header or the data
- if in the header, we set the Table's Menu to our popup, otherwise null

SWT behaves as expected i.e. the user right-clicks anywhere in the Table header and the popup menu appears ok, otherwise no popup if clicked in the data rows.

RAP almost behaves like this, except that there appears to be some difference in the sequence of events being processed... if you right-click in the header, you see the MenuDetect get triggered ok, and the Table.setMenu(menu) is done, but on that initial click you don't get the menu actually appear. Similarly, if you already did a previous right-click in the heading (so that Table.setMenu(menu) was done), then the next right-click is in the data rows, you DO see the popup.

2nd right-click in the same area behaves correctly. So, it is like the Table.setMenu() is being called too late, and the popup is honoured from the previous round of events.

Sorry if this seems a bit convoluted... once you understand what I am on about, you'll see what I mean - it is quite straight-forward to reproduce!

Let me know if this does or doesn't make sense... if necessary I could do a desktop share to show you the behaviour to make it clear...?

Thanks, John


---
Just because you can doesn't mean you should
Re: RAP MenuDetect events triggered incorrectly [message #1021519 is a reply to message #1021199] Wed, 20 March 2013 09:06 Go to previous messageGo to next message
Tim Buschtoens is currently offline Tim BuschtoensFriend
Messages: 387
Registered: July 2009
Senior Member
Hi.

This seems to be related to:
Bug 314671 - [Table] Unable to add pop-up menu to table header
https://bugs.eclipse.org/bugs/show_bug.cgi?id=314671

The case is slightly different, but the cause is the same.
As a workaround, try this:

if (header) {
System.out.println("Context menu required here in table header");
//tab.setMenu(menu);
menu.setLocation( event.x, event.y );
menu.setVisible( true );
} else {
System.out.println("No context menu in this location thanks - table
header only");
//tab.setMenu(null);
}


Greetings,
Tim

Am 19.03.2013 17:39, schrieb John Gymer:
> Good afternoon gents...
>
> I have what I believe is a small problem with RAP when using MenuDetect
> events on Tables/Trees. Firstly, here is a snippet for RAP:
>
>
> /* DEMONSTRATES RAP PROBLEM WITH MENUDETECT ONLY OCCURRING ONCE IN TABLE
> HEADERS */
> package bug.snippet;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.RGB;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FormAttachment;
> import org.eclipse.swt.layout.FormData;
> import org.eclipse.swt.layout.FormLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.Menu;
> import org.eclipse.swt.widgets.MenuItem;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Table;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.swt.widgets.TableItem;
>
> public class Bugsy {
> private Display display;
> private Shell shell;
> private static Table tab;
> private static TableColumn col;
> int answer = -1;
> private Menu menu;
>
> public void begin() {
> System.out.println("BugSnippy Starting...");
>
> // create the Shell
> display = new Display();
> shell = new Shell(display,
> SWT.APPLICATION_MODAL|SWT.TITLE|SWT.CLOSE);
> shell.setText("Shell");
> //shell.setMaximized(true);
> shell.setBounds(10, 10, 900, 500);
> shell.setBackground(new Color(null, new RGB(255,128,128)));
> FormLayout layout = new FormLayout();
> shell.setLayout(layout);
>
> //create the table
> tab = new Table(shell, SWT.FULL_SELECTION|SWT.MULTI|SWT.BORDER);
> tab.setHeaderVisible(true);
> tab.setLinesVisible(true);
>
> col = new TableColumn(tab, SWT.NONE);
> col.setResizable(true);
> col.setWidth(700);
> col.setText("My Column");
>
> col = new TableColumn(tab, SWT.NONE);
> col.setResizable(true);
> col.setWidth(225);
> col.setText("My 2nd Column");
>
> //add some data to the table
> String[] tableData = new String[2]; //2 columns
>
> for (int i = 0; i < 100; i++) {
> tableData[0] = "My Value " + i;
> tableData[1] = "Wonderful " + i*10;
> TableItem tableItem = new TableItem(tab, SWT.None, 0);
> tableItem.setText(tableData);
> }
>
> //set table's position
> FormData fd = new FormData();
> fd.left = new FormAttachment(0, 5);
> fd.top = new FormAttachment(0, 5);
> fd.width = 800;
> fd.height = 400;
> tab.setLayoutData(fd);
>
> tab.addListener(SWT.MenuDetect, menuDetectListener);
>
> // construct the table's context popup menu
> menu = new Menu(shell, SWT.POP_UP);
> MenuItem mi = new MenuItem(menu, SWT.NONE);
> mi.setText("Select All");
> mi.addListener(SWT.Selection, menuItemListener);
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
>
> System.out.println("BugSnippy Done!");
> }
>
> Listener menuDetectListener = new Listener() {
> public void handleEvent(Event event) {
> Point pt = display.map(null, tab, new Point(event.x,
> event.y));
> Rectangle clientArea;
> boolean header;
> clientArea = tab.getClientArea();
> header = clientArea.y <= pt.y && pt.y < (clientArea.y +
> tab.getHeaderHeight());
> if (header) {
> System.out.println("Context menu required here in table
> header");
> tab.setMenu(menu);
> }
> else {
> System.out.println("No context menu in this location
> thanks - table header only");
> tab.setMenu(null);
> }
> }
> };
>
> Listener menuItemListener = new Listener() {
> public void handleEvent(Event event) {
> System.out.println("Item on Context Menu Selected!");
> }
> };
>
> }
>
>
> The purpose of the code is to detect a right-click (context menu) in a
> Table (behaviour is the same in a Tree) and only popup the context menu
> if the user requested it from the Table's header, not when clicking
> inside the data rows.
>
> So we:
> - add a MenuDetect listener to the Table
> - create our pop-up menu up-front, but don't use it yet
> - in the MenuDetect listener we determine where the click was done -
> inside the Table header or the data
> - if in the header, we set the Table's Menu to our popup, otherwise null
>
> SWT behaves as expected i.e. the user right-clicks anywhere in the Table
> header and the popup menu appears ok, otherwise no popup if clicked in
> the data rows.
>
> RAP almost behaves like this, except that there appears to be some
> difference in the sequence of events being processed... if you
> right-click in the header, you see the MenuDetect get triggered ok, and
> the Table.setMenu(menu) is done, but on that initial click you don't get
> the menu actually appear. Similarly, if you already did a previous
> right-click in the heading (so that Table.setMenu(menu) was done), then
> the next right-click is in the data rows, you DO see the popup.
>
> 2nd right-click in the same area behaves correctly. So, it is like the
> Table.setMenu() is being called too late, and the popup is honoured from
> the previous round of events.
>
> Sorry if this seems a bit convoluted... once you understand what I am on
> about, you'll see what I mean - it is quite straight-forward to reproduce!
>
> Let me know if this does or doesn't make sense... if necessary I could
> do a desktop share to show you the behaviour to make it clear...?
>
> Thanks, John
>

--
Tim Buschtöns

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: RAP MenuDetect events triggered incorrectly [message #1021531 is a reply to message #1021519] Wed, 20 March 2013 09:25 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 198
Registered: November 2012
Location: UK
Senior Member
Thanks Tim, it does sound like the same problem as reported in 2011.

However, the work-around does not work... for this reason I believe...

It seems that the popup menu is displayed before the MenuDetect event is triggered. In fact, the MenuDetect must be triggered and processed before the current context menu is displayed.

Is this possible to fix?

An alternative would be to be able to add a menu to the Table's header specifically rather than the entire Table... I've read some posts about this, but not sure if it was ever implemented even in SWT.

Thanks, John


---
Just because you can doesn't mean you should
Re: RAP MenuDetect events triggered incorrectly [message #1022645 is a reply to message #1021531] Fri, 22 March 2013 09:26 Go to previous messageGo to next message
Tim Buschtoens is currently offline Tim BuschtoensFriend
Messages: 387
Registered: July 2009
Senior Member
Hi.

> However, the work-around does not work... for this reason I believe...
>
> It seems that the popup menu is displayed before the MenuDetect event is
> triggered. In fact, the MenuDetect must be triggered and processed
> before the current context menu is displayed.

Sorry, i can't follow. For me (with the workaround) the menu is
displayed if (and only if) i right click on the header. What do you
experience?

Greetings,
Tim

--
Tim Buschtöns

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: RAP MenuDetect events triggered incorrectly [message #1024723 is a reply to message #1022645] Tue, 26 March 2013 09:09 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 198
Registered: November 2012
Location: UK
Senior Member
Hi Tim,
Ok, something is amiss then... I've just refreshed to the latest nightly build to check if the problem goes away, but it is still there.
Basically, start the snippet application and right-click in the list data somewhere... the popup menu appears, but it shouldn't. Click again in the list area and the popup does NOT appear (correct behaviour). Now right-click in the listbox header area - NO popup appears, but it should. Right-click again in the header area and the popup menu appears correctly.
Does that make sense? Events being triggered in the wrong order I would suspect.
I'm away now for a couple of weeks, so apologies if there is no reply for a while!
Cheers, John


---
Just because you can doesn't mean you should
Previous Topic:How to enable the browser scroll bar, not using rap scroll bar?
Next Topic:How to enable the browser scroll bar, not using rap scroll bar?
Goto Forum:
  


Current Time: Sun Dec 21 21:42:08 GMT 2014

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

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