Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Tree node deselection
Tree node deselection [message #453954] Thu, 14 April 2005 18:56 Go to next message
Robin Raddatz is currently offline Robin RaddatzFriend
Messages: 10
Registered: July 2009
Junior Member
I have a tree that should allow only a single selection. If the user has
selected a node and then right-clicks, I pop up a certain menu. However,
if the user has selected no nodes, I want to pop up a different generic
menu. I can't seem to figure out how to allow the user to deselect all
nodes in the tree. Once a node is selected, there is always a node
selected, so they will never see that generic menu. Is there a way to do
this? I see you can do it if you set the style to SWT.MULTI, but I don't
want to allow the user to select more than one node.

Thanks,
Robin
Re: Tree node deselection [message #453955 is a reply to message #453954] Thu, 14 April 2005 20:08 Go to previous messageGo to next message
Emil Crumhorn is currently offline Emil CrumhornFriend
Messages: 169
Registered: July 2009
Senior Member
Just listen to the mouse event, get the treenode for the location where the
mouse was pressed, and if that's null, you know that they clicked somewhere
there is no node.. at that point you can deselect all nodes in the tree (and
show your menu).

Something along the lines of

tree.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
TreeItem ti = tree.getItem(new Point(e.x, e.y));
if (ti == null) {
tree.deselectAll();
// etc....
}
}
});

or just SWT.MouseDown, or any other listener you prefer.

Emil

"Robin" <rraddatz@us.ibm.com> wrote in message
news:5f789b3ac3a3ffed7faf5630d408ac80$1@www.eclipse.org...
>I have a tree that should allow only a single selection. If the user has
>selected a node and then right-clicks, I pop up a certain menu. However,
>if the user has selected no nodes, I want to pop up a different generic
>menu. I can't seem to figure out how to allow the user to deselect all
>nodes in the tree. Once a node is selected, there is always a node
>selected, so they will never see that generic menu. Is there a way to do
>this? I see you can do it if you set the style to SWT.MULTI, but I don't
>want to allow the user to select more than one node.
>
> Thanks,
> Robin
>
Re: Tree node deselection [message #453956 is a reply to message #453955] Thu, 14 April 2005 20:46 Go to previous messageGo to next message
Robin Raddatz is currently offline Robin RaddatzFriend
Messages: 10
Registered: July 2009
Junior Member
Thank you so much! This was really helpful.

Robin
Re: Tree node deselection [message #464683 is a reply to message #453956] Mon, 28 November 2005 17:33 Go to previous message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
One problem with that solution is that if the user clicks on the +/- to expand/collapse the tree,
all items will be deselected, which may not be always a good idea. The code below dodges that nit,
so long as Tree continues to noify TreeListeners before MouseListeners.

HTH,
Paul

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Tree;

/**
* Deselector is a MouseAdapter and a TreeListener that un-selects all
* items in a given Tree. Usage: <pre><code>
* new Deselector(tree); </code></pre>
*
* @author keyser
*/
public class Deselector extends MouseAdapter implements TreeListener {

/**
* Create a deselector for the given Tree
* @param tree Tree to which to attach the Deselector
*/
public Deselector(final Tree tree) {
super();
tree.addMouseListener(this);
tree.addTreeListener(this);
}

/**
* @see org.eclipse.swt.events.TreeListener#treeCollapsed(org.eclips e.swt.events.TreeEvent)
*/
public void treeCollapsed(final TreeEvent e) {
// seems to arrive here before arriving in MouseUp
doit = false;
}

/**
* @see org.eclipse.swt.events.TreeListener#treeExpanded(org.eclipse .swt.events.TreeEvent)
*/
public void treeExpanded(final TreeEvent e) {
// seems to arrive here before arriving in MouseUp
doit = false;
}

/**
* @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt .events.MouseEvent)
*/
public void mouseUp(final MouseEvent e) {
if (doit) {
final Tree tree = (Tree) e.widget;
if (null == tree.getItem(new Point(e.x, e.y))) {
tree.deselectAll();
}
}
doit = true;
}

protected boolean doit = true;
}
Previous Topic:How to use TextViewer from JFace
Next Topic:"Network Graphical editor"
Goto Forum:
  


Current Time: Wed Sep 18 17:33:15 GMT 2019

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

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

Back to the top