Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » jface TreeViewer - add new tree item to root without setInput()?(Need to dynamically add root items to tree viewer. )
icon5.gif  jface TreeViewer - add new tree item to root without setInput()? [message #1734736] Fri, 10 June 2016 19:08 Go to next message
Sean Horton is currently offline Sean HortonFriend
Messages: 7
Registered: June 2016
Junior Member
I'm having some unexpected difficulty with the jface TreeViewer.

The tree is working, but I've hit a situation that could cause user frustration in our app.

We have a TreeViewer tree as follows:

- TreeItem1
- Child1
- Child2
- TreeItem2
- Child1

This tree is created by using TreeViewer.setInput(TreeItems)

The problem is that we need to add tree items to the root level. For example, if we started with the tree above, we may dynamically need to create a new item as follows:

- TreeItem1
- Child1
- Child2
- TreeItem2
- Child1
- TreeItem3
- Child1


So, we dynamically added a new root TreeItem (TreeItem3). Currently we are achieving this by calling TreeViewer.setInput(TreeItems) again. The problem is:

1) This is a lot of unnecessary work to update a single tree item as it's causing the modal to be completely new.
2) This can be called multiple times in a short period of time, causing the tree to bounce to the top of the list each time.

Questions:
Is there a way to refresh the root list of the tree without setInput()?

I've tried TreeViewer.add(TreeViewer.getInput(), <newItem>). However, this only updates the tree, not the modal. A TreeViewer.refresh() will then remove the added item. Is there a way I can update the ContentProvider directly?

Also using ITreeContentProvider.
Re: jface TreeViewer - add new tree item to root without setInput()? [message #1734773 is a reply to message #1734736] Sat, 11 June 2016 19:13 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6325
Registered: July 2009
Senior Member
With JFace viewers you don't add items to the viewer or control (Tree), but rather to the underlying model
(IContentProvider) and then refresh() the viewer.

Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper



Am 11.06.2016 um 17:24 schrieb Sean Horton:
> I'm having some unexpected difficulty with the jface TreeViewer.
> The tree is working, but I've hit a situation that could cause user frustration in our app.
> We have a TreeViewer tree as follows:
>
> - TreeItem1
> - Child1
> - Child2
> - TreeItem2
> - Child1
> This tree is created by using TreeViewer.setInput(TreeItems)
>
> The problem is that we need to add tree items to the root level. For example, if we started with the tree above, we
> may dynamically need to create a new item as follows:
> - TreeItem1
> - Child1
> - Child2
> - TreeItem2
> - Child1 - TreeItem3
> - Child1
>
>
> So, we dynamically added a new root TreeItem (TreeItem3). Currently we are achieving this by calling
> TreeViewer.setInput(TreeItems) again. The problem is:
>
> 1) This is a lot of unnecessary work to update a single tree item as it's causing the modal to be completely new. 2)
> This can be called multiple times in a short period of time, causing the tree to bounce to the top of the list each time.
> Questions:
> Is there a way to refresh the root list of the tree without setInput()?
> I've tried TreeViewer.add(TreeViewer.getInput(), <newItem>). However, this only updates the tree, not the modal.
> A TreeViewer.refresh() will then remove the added item. Is there a way I can update the ContentProvider directly?
> Also using ITreeContentProvider.
>
Re: jface TreeViewer - add new tree item to root without setInput()? [message #1734775 is a reply to message #1734773] Sat, 11 June 2016 19:36 Go to previous messageGo to next message
Sean Horton is currently offline Sean HortonFriend
Messages: 7
Registered: June 2016
Junior Member
Thanks for the response Eike!

Looking at IContentProvider I see methods dispose() and inputChanged(Viewer viewer, Object oldInput, Object newInput)

Is there available examples on making such changes to the model? I'm assuming changes would be made in the inputChanged() method. Examples I have found, including Vogella http://www.vogella.com/tutorials/EclipseJFaceTree/article.html do not include example code in inputChanged().

I'm not sure how I modify the model with these methods.
Re: jface TreeViewer - add new tree item to root without setInput()? [message #1734779 is a reply to message #1734775] Sun, 12 June 2016 05:24 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6325
Registered: July 2009
Senior Member
Am 11.06.2016 um 21:36 schrieb Sean Horton:
> Thanks for the response Eike!
>
> Looking at IContentProvider I see methods dispose() and inputChanged(Viewer viewer, Object oldInput, Object newInput)
These IContentProvider methods are called by the viewer when you call Viewer.setInput() and Viewer.dispose().

>
> Is there available examples on making such changes to the model? I'm assuming changes would be made in the
> inputChanged() method. Examples I have found, including Vogella
> http://www.vogella.com/tutorials/EclipseJFaceTree/article.html do not include example code in inputChanged().
Often IContentProviders operate on a "fixed" model so that there's no point in remembering anything in inputChanged().
For examples have a look at the subclasses of IContentProvider. Some will leave inputChanged() empty, others not.

> I'm not sure how I modify the model with these methods.
The ITreeContentProvider methods getElements() and/or getChildren() are called by the viewer when you call
Viewer.refresh(). All you need to do is to return the current/new array of children from your ITreeContentProvider.

Cheers
/Eike

----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Re: jface TreeViewer - add new tree item to root without setInput()? [message #1734802 is a reply to message #1734779] Mon, 13 June 2016 01:23 Go to previous message
Sean Horton is currently offline Sean HortonFriend
Messages: 7
Registered: June 2016
Junior Member
Awesome! Thank you!

Your last paragraph made a lightbulb go off!

I just tried putting some "get info from database" logic into the ContentProvider and calling a refresh on the tree. This triggers the ContentProvider to access the data from the database again.

Makes so much more sense now, thanks again. Will start optimizing more tomorrow.
Previous Topic:[DataBinding] Control order of event processing
Next Topic:MessageConsoleStream error
Goto Forum:
  


Current Time: Wed Nov 22 05:53:42 GMT 2017

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

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