Tree node deselection [message #453954] |
Thu, 14 April 2005 14:56  |
Eclipse User |
|
|
|
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 16:08   |
Eclipse User |
|
|
|
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 #464683 is a reply to message #453956] |
Mon, 28 November 2005 12:33  |
Eclipse User |
|
|
|
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;
}
|
|
|
Powered by
FUDForum. Page generated in 0.03708 seconds