Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tree & TreeItems with SWT.SELECTION
Tree & TreeItems with SWT.SELECTION [message #453851] Tue, 12 April 2005 10:49 Go to next message
Ivan is currently offline IvanFriend
Messages: 149
Registered: July 2009
Senior Member
Hi all,
IŽm using a Tree to represent graphically a query. The query can be
modified by using different popup menus appended to the TreeItems. Under
Windows XP it works fine, but under linux unfortunately not. There is a
problem with the event I'm using:

- under Windows whenever I right-click on a TreeItem, it implies that the

TreeItem gets the Selection and therefore the correct popup-menu appears;

- under Linux when I right-click on a TreeItem, the TreeItem DOES NOT get
the
Selection and therefore a wrong popup-menu appears. Then, the user has
to,
first left-click on a TreeItem to select it and second right-click on it
to
get the correct popup-menu.

IŽm using the event MouseDown. Is there an event that can produce the same
(correct) behaviour under every platform? Have you understood the problem?
Does anybody have this problem?

Thanks,
Ivan
Re: Tree & TreeItems with SWT.SELECTION [message #453875 is a reply to message #453851] Wed, 13 April 2005 14:58 Go to previous messageGo to next message
Ivan is currently offline IvanFriend
Messages: 149
Registered: July 2009
Senior Member
Nobody can help me?

Ivan
Re: Tree & TreeItems with SWT.SELECTION [message #453914 is a reply to message #453875] Wed, 13 April 2005 18:14 Go to previous messageGo to next message
Emil Crumhorn is currently offline Emil CrumhornFriend
Messages: 169
Registered: July 2009
Senior Member
If you detect the right click on the MouseDown event you can get what
treeitem is under the point where the mouse was clicked.

Something like:

tree.addMouseListener(new MouseListener() {
public void mouseDown(MouseEvent e) {
TreeItem ti = tree.getItem(new Point(e.x, e.y));
// tree item can be null if mouse was not over a tree item
}

etc...
}

Then just select that item via the tree.setSelection(new TreeItem[] { ti });
or such, and pop up your right click menu in the code.

Hope that's what you wanted.

Emil

"Ivan" <zorziivan83@hotmail.com> wrote in message
news:b95a15a8a331e71808dc62dd099e3b74$1@www.eclipse.org...
> Nobody can help me?
>
> Ivan
>
Re: Tree & TreeItems with SWT.SELECTION [message #453916 is a reply to message #453851] Wed, 13 April 2005 19:33 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
I do understand the problem. You should be using Control.setMenu() and
allowing the operating system to show the menu rather than doing it yourself
on MouseDown. This is just a guess. Can you provide a small stand alone
snippet for me to debug?

"Ivan" <zorziivan83@hotmail.com> wrote in message
news:0fa70666c025981571fff46ca5e2c7ae$1@www.eclipse.org...
> Hi all,
> I
Re: Tree & TreeItems with SWT.SELECTION [message #453949 is a reply to message #453914] Thu, 14 April 2005 17:08 Go to previous messageGo to next message
Ivan is currently offline IvanFriend
Messages: 149
Registered: July 2009
Senior Member
Thanks Steve! I have your book and it's really well done!
Here is a small snippet of the code that should have different results
depending on the OS:

public class TestTree {

public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
final Tree tree = new Tree (shell, SWT.BORDER | SWT.V_SCROLL |
SWT.H_SCROLL);
final Menu rootMenu = new Menu(shell, SWT.POP_UP);
MenuItem addChild = new MenuItem(rootMenu, SWT.PUSH);
addChild.setText("Add first Item");
final Menu childMenu = new Menu(shell, SWT.POP_UP);
MenuItem childMenuItem = new MenuItem(childMenu, SWT.PUSH);
childMenuItem.setText("delete Item");
final TreeItem rootItem = new TreeItem(tree, SWT.PUSH);
rootItem.setText("Root");
tree.setSize (100, 100);
tree.addListener (SWT.MouseDown, new Listener () {
public void handleEvent (Event event) {
if (tree.getTopItem().getItemCount() == 0) {
tree.setMenu(rootMenu);
} else if (tree.getTopItem().getItemCount() == 1) {
TreeItem[] itemSelected = tree.getSelection();
if (itemSelected[0].getText() == "Root") {
tree.setMenu(null);
} else
tree.setMenu(childMenu);
} else tree.setMenu(null);
}
});


addChild.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent arg0) {
TreeItem item = new TreeItem(rootItem, SWT.PUSH);
item.setText("Child");


}

public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub

}

});

childMenuItem.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent arg0) {
TreeItem[] item = tree.getSelection();
item[0].dispose();
}

public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub

}

});



shell.setSize (200, 200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

Please give me some feedback.
Thanks in advance,
Ivan
Re: Tree & TreeItems with SWT.SELECTION [message #454113 is a reply to message #453916] Mon, 18 April 2005 09:33 Go to previous messageGo to next message
Ivan is currently offline IvanFriend
Messages: 149
Registered: July 2009
Senior Member
Did you try the snippet?

Ivan
Re: Tree & TreeItems with SWT.SELECTION [message #454178 is a reply to message #454113] Mon, 18 April 2005 20:08 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Yes. The problem seems to be exactly what I thought. On Windows, menus are
displayed on MouseUp. On GTK, it's mouse down. This is the correct
behavior for the platform and it is abstracted away using Control.setMenu()
and the SWT.MenuDetect event. I'm not sure exactly what your code is
supposed to be doing so I can't really confirm this 100% but setting menus
on MouseDown won't work.

"Ivan" <zorziivan83@hotmail.com> wrote in message
news:1a54e1e778eff13defaff661dd70d22a$1@www.eclipse.org...
> Did you try the snippet?
>
> Ivan
>
Re: Tree & TreeItems with SWT.SELECTION [message #454461 is a reply to message #454178] Thu, 21 April 2005 10:22 Go to previous messageGo to next message
Ivan is currently offline IvanFriend
Messages: 149
Registered: July 2009
Senior Member
I would like to know which event I should use to handle this platform
problem. If setting menus [tree.setMenu(popupMenu)] on MouseDown wonŽt
work, is there another event that can have same results on both Linux and
Windows?
My application should do the following:
- the user selects a TreeItem
- every TreeItem has a Data behind (treeItem.setData(Object)) and
depending on the Data associated with every TreeItem, a different popup
menu is set to the Tree using tree.setMenu(popupMenu).
- the correct popupMenu is displayed on the screen.

I want that the mouse selection and the display of the popupMenu happen at
the same time: I mean, as soon a TreeItem gets selection via a mouse click
(possibly the right-click) the correct popupMenu is set to the Tree and
displayed on the screen.

I can give you another snippet to try that best show you the problem.

Thanks,

Ivan
Re: Tree & TreeItems with SWT.SELECTION [message #454593 is a reply to message #454461] Mon, 25 April 2005 13:05 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
See
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet97.java?rev=HEAD&a mp;content-type=text/vnd.viewcvs-markup

"Ivan" <zorziivan83@hotmail.com> wrote in message
news:751ff08f40721c2b0a3dce00e94f7128$1@www.eclipse.org...
> I would like to know which event I should use to handle this platform
> problem. If setting menus [tree.setMenu(popupMenu)] on MouseDown won
Previous Topic:SWT Table error on Windows 2000 Pro
Next Topic:Inefficient image loadings?
Goto Forum:
  


Current Time: Thu Feb 25 17:15:09 GMT 2021

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

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

Back to the top