Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops
Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533570] Fri, 14 May 2010 15:09 Go to next message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Dear All:

I have followed instructions in a prior post to come up with something like:
	tree.addListener(SWT.MouseDown,new Listener() {
		public void handleEvent(Event event) {
		    Point point=new Point(event.x,event.y);
		    TreeItem item=tree.getItem(point);
		    if (item!=null&&event.button==3) {
			    Menu menu=new Menu(getShell(),SWT.POP_UP);
			    MenuItem menuItem=new MenuItem(menu,SWT.PUSH);
			    menuItem.setText("&Find on Page");
			    menuItem.addListener(SWT.Selection,new Listener() {
				    public void handleEvent(Event event) {
                                        // do something
				    }
				});
			    Point shellLocation=shell.getLocation();
			    Point treeLocation=tree.getLocation();
			    menu.setLocation(shellLocation.x+treeLocation.x+event.x,shellLocation.y+treeLocation.y+event.y);
			    menu.setVisible(true);
			    /* I AM TRYING TO AVOID THIS LOOP IS THIS NECESSARY
			    while (!menu.isDisposed()&&menu.isVisible()) {
				if (!Display.getDefault().readAndDispatch())
				    Display.getDefault().sleep();
			    }
			    menu.dispose(); */
			}
		    }
		}
	    });


Specifically, this is similar to:
http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Showapopup menuwaitforittoclose.htm

Taking in or leaving out event loop above seems to have no effect. Is it necessary?

Also, if I don't have the event loop, when is the right time to dispose the menu?

Thank you
Misha
Re: Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533587 is a reply to message #533570] Fri, 14 May 2010 15:48 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Misha Koshelev wrote:
> Dear All:
>
> I have followed instructions in a prior post to come up with something
> like:
>
> tree.addListener(SWT.MouseDown,new Listener() {

Why not manage that at the same level as the tree, instead of trying to
create and dispose it from a listener. ex:

treeMenu = new Menu(tree);
tree.setMenu(menu);

menu.addListener(SWT.Show, new Listener() {
public void handleEvent(Event event) {
MenuItem[] oldItems = treeMenu.getItems();
for (int i=0; i<oldItems.length; i++) {
oldItems[i].dispose();
}
TreeItem[] items = tree.getSelection();
// check to see you're dealing with the correct item
TreeItem item = items[0];

// add the dynamic items you want based on "item"
MenuItem menuItem=new MenuItem(treeMenu,SWT.PUSH);
menuItem.setText("&Find on Page");
}
});

When you dispose the tree, dispose the treeMenu.

Later,
PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533588 is a reply to message #533587] Fri, 14 May 2010 15:49 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Paul Webster wrote:
> treeMenu = new Menu(tree);
> tree.setMenu(menu);

All references to menu should have been treeMenu. The horrors of cut&paste.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533625 is a reply to message #533588] Fri, 14 May 2010 19:11 Go to previous messageGo to next message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Thank you. I like your version.

This raises an important question - if the parent shell is closed, do I need to dispose of the child widgets (e.g., tree), or does it happen automatically?

Thank you
Misha
Re: Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533927 is a reply to message #533625] Mon, 17 May 2010 13:44 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2150
Registered: July 2009
Senior Member
> This raises an important question - if the parent shell is closed, do I
need to dispose of the child widgets (e.g., tree), or does it happen
automatically?

Disposing of a composite control (like Shell) automatically disposes all of
its child controls recursively as well.

Here are some links related to your questions in this thread:
http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet40.java?view=co
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet97.java?view=co
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet131.java?view=co
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet73.java?view=co

Grant
Re: Context-sensitive popup menu on TreeItem - trying to avoid extra Dispatch loops [message #533999 is a reply to message #533927] Mon, 17 May 2010 16:22 Go to previous message
Misha Koshelev is currently offline Misha KoshelevFriend
Messages: 31
Registered: May 2010
Member
Thank you so much for the helpful links!

Misha
Previous Topic:Untyped events for Browser widget?
Next Topic:Sorry for so many posts :( - Simualte KeyEvent "!" character
Goto Forum:
  


Current Time: Mon Dec 09 18:45:56 GMT 2019

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

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

Back to the top