Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Mihai Codoban is currently offline Mihai CodobanFriend
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 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
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 #797365 is a reply to message #797323] Mon, 13 February 2012 11:35 Go to previous messageGo to next message
Mihai Codoban is currently offline Mihai CodobanFriend
Messages: 3
Registered: February 2012
Junior Member
I'm not trying to expand the tree on any selection. That would be, as you said, just horrible. Smile

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.
Re: How to properly receive TreeEvents from a Tree [message #797371 is a reply to message #797365] Mon, 13 February 2012 11:43 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
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 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
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.
Re: How to properly receive TreeEvents from a Tree [message #798950 is a reply to message #798359] Wed, 15 February 2012 09:02 Go to previous message
Mihai Codoban is currently offline Mihai CodobanFriend
Messages: 3
Registered: February 2012
Junior Member
Ok, thanks for the info. It would have been nice if this could have been configured (tell the Tree when to send events)
I'll just subclass the Navigator and add icon management responsibility to it, and all the communication with the Tree will be handled transparently through my new Navigator.


Mihai

[Updated on: Wed, 15 February 2012 09:04]

Report message to a moderator

Previous Topic:how to hide viewpart borders
Next Topic:Loading each object in multiple resources
Goto Forum:
  


Current Time: Fri Dec 04 06:15:25 GMT 2020

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

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

Back to the top