Home » Eclipse Projects » Rich Client Platform (RCP) » How to properly receive TreeEvents from a Tree(Event firing seems to be inconsistent)
How to properly receive TreeEvents from a Tree [message #797288] |
Mon, 13 February 2012 09:27 |
Mihai Codoban Messages: 3 Registered: February 2012 |
Junior Member |
|
|
Hello,
In a Navigator, I want to change node icon state to represent the expanded/collapsed state of the underlying TreeItem.
I looked at the API and it seems that there is a TreeListener for TreeEvents which, by the sound of the documentation, should be fired when a node is expanded or collapsed.
So in my navigator constructor I hooked a TreeListener to the Tree, setting the TreeItem image accordingly:
tree.addTreeListener(new TreeListener() {
public void treeCollapsed(TreeEvent e) {
update((TreeItem) e.item, false);
}
public void treeExpanded(TreeEvent e) {
update((TreeItem) e.item, true);
}
private void update(TreeItem item, boolean isExpanded) {
ITreeNode modelNode = (ITreeNode) item.getData();
item.setImage(modelNode.getImage(isExpanded));
}
});
This works out fine when clicking on the +/- icons and when calling the Tree.setExpanded() method but, unfortunately, it does not get fired in other situations where the nodes are expanded/collapsed:
- when double clicking on a node (fixed it by also adding a double click listener on the tree that does much of the same as the TreeListener)
- when programatically setting the tree selection via the CommonNavigator.selectReveal() or Tree.setSelection. It seems that they internally expand the nodes without firing TreeEvents. This is quite frustrating because I programatically set the selection quite often and the node icons do not get updated accordingly because the event is not fired.
Does anybody know how to receive the TreeEvents in all cases when the nodes expanded / collapsed?
Thanks
|
|
|
Re: How to properly receive TreeEvents from a Tree [message #797323 is a reply to message #797288] |
Mon, 13 February 2012 10:22 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 2012-02-13 10:27, Mihai Codoban wrote:
> Hello,
> In a Navigator, I want to change node icon state to represent the
> expanded/collapsed state of the underlying TreeItem.
>
> I looked at the API and it seems that there is a TreeListener for
> TreeEvents which, by the sound of the documentation, should be fired
> when a node is expanded or collapsed.
>
> So in my navigator constructor I hooked a TreeListener to the Tree,
> setting the TreeItem image accordingly:
>
> tree.addTreeListener(new TreeListener() {
>
> public void treeCollapsed(TreeEvent e) {
> update((TreeItem) e.item, false);
> }
>
> public void treeExpanded(TreeEvent e) {
> update((TreeItem) e.item, true);
> }
>
> private void update(TreeItem item, boolean isExpanded) {
> ITreeNode modelNode = (ITreeNode) item.getData();
>
> item.setImage(modelNode.getImage(isExpanded));
> }
> });
>
> This works out fine when clicking on the +/- icons and when calling the
> Tree.setExpanded() method but, unfortunately, it does not get fired in
> other situations where the nodes are expanded/collapsed:
>
> - when double clicking on a node (fixed it by also adding a double click
> listener on the tree that does much of the same as the TreeListener)
> - when programatically setting the tree selection via the
> CommonNavigator.selectReveal() or Tree.setSelection. It seems that they
> internally expand the nodes without firing TreeEvents. This is quite
> frustrating because I programatically set the selection quite often and
> the node icons do not get updated accordingly because the event is not
> fired.
>
> Does anybody know how to receive the TreeEvents in all cases when the
> nodes expanded / collapsed?
If you expect that to happen even for a selection change event I
recommend to provide a selection listener as well.
But I really wonder why you want to expand the tree each time when a
user is clicking on any item - sounds like an awful user experience to
me ;-)
In other words: I don't consider the SWT behaviour as a bug, but as a
reasonable and useful design decision. If you have your own ideas of
program behaviour as you describe, you have to add a single event
handler more - looks like an acceptable cost to me.
Greetings from Bremen,
Daniel Krügler
|
|
| |
Re: How to properly receive TreeEvents from a Tree [message #797371 is a reply to message #797365] |
Mon, 13 February 2012 11:43 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 2012-02-13 12:35, Mihai Codoban wrote:
> I'm not trying to expand the tree on any selection. That would be, as
> you said, just horrible. :)
>
> I'm just trying to get notified anytime the tree is expanded or
> collapsed, but unfortunately there are situations when treeitems are
> expanded/collapsed but no events are sent. For example, on double click
> on the tree, the clicked node is automatically expanded (default RCP /
> OS behaviour) but the events are not sent.
> Same goes for programatically setting the selection in the tree
> (setReveal(), setSelection()). (I use this to keep the tree synchronized
> with selections from other views).
>
> I fixed the double click notification by listening to double clicks as
> well. I also sort of fixed the programatical selection by using wrappers
> around the normal functions that traverse the tree upwards and update
> the labels.
>
> But these are all just hacks to get around the events not being fired
> and I was wondering if there is better, cleaner way to listen to
> absolutely all expand / collapse events.
OK, I misunderstood your usecase description. In regard to the
double-click behaviour this seems manageable, but I agree that your
reveal/selection problem looks problematic.
Greetings from Bremen,
Daniel Krügler
|
|
|
Re: How to properly receive TreeEvents from a Tree [message #798359 is a reply to message #797365] |
Tue, 14 February 2012 15:22 |
Grant Gayed Messages: 2150 Registered: July 2009 |
Senior Member |
|
|
Hi,
In general SWT does not send events for changes that are directly
triggered programatically (historical decision). If you expand/collapse
an item programatically then this is the place where you need to invoke
the method to change the item's image. It's not as nice design-wise as
having this happen in just the Expand/Collapse listener, but it's what's
needed given the relevant policy of the toolkit.
Grant
On 2/13/2012 6:35 AM, Mihai Codoban wrote:
> I'm not trying to expand the tree on any selection. That would be, as
> you said, just horrible. :)
>
> I'm just trying to get notified anytime the tree is expanded or
> collapsed, but unfortunately there are situations when treeitems are
> expanded/collapsed but no events are sent. For example, on double click
> on the tree, the clicked node is automatically expanded (default RCP /
> OS behaviour) but the events are not sent.
> Same goes for programatically setting the selection in the tree
> (setReveal(), setSelection()). (I use this to keep the tree synchronized
> with selections from other views).
>
> I fixed the double click notification by listening to double clicks as
> well. I also sort of fixed the programatical selection by using wrappers
> around the normal functions that traverse the tree upwards and update
> the labels.
>
> But these are all just hacks to get around the events not being fired
> and I was wondering if there is better, cleaner way to listen to
> absolutely all expand / collapse events.
|
|
| |
Goto Forum:
Current Time: Fri May 03 00:55:15 GMT 2024
Powered by FUDForum. Page generated in 0.03486 seconds
|