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 12:39  |
Eclipse User |
|
|
|
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
|
|
|
Re: RAP MenuDetect events triggered incorrectly [message #1021519 is a reply to message #1021199] |
Wed, 20 March 2013 05:06   |
Eclipse User |
|
|
|
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/
|
|
| | | |
Goto Forum:
Current Time: Wed Jul 23 19:45:33 EDT 2025
Powered by FUDForum. Page generated in 0.06088 seconds
|