Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » How to remember jFace treeviewer's expand state?
icon5.gif  How to remember jFace treeviewer's expand state? [message #1386022] Fri, 13 June 2014 02:45 Go to next message
Sam Su is currently offline Sam SuFriend
Messages: 2
Registered: August 2013
Junior Member
I'm developing a TreeViewer quite like the Eclipse PackageExplorer Tree, so How to remember jFace treeviewer's expand state so that when the application start/restart, it can revert to the last state?
Re: How to remember jFace treeviewer's expand state? [message #1386950 is a reply to message #1386022] Mon, 23 June 2014 08:47 Go to previous messageGo to next message
Nagesh Vashist is currently offline Nagesh VashistFriend
Messages: 1
Registered: October 2012
Junior Member
Hi,

I think you should use Eclipse preferences, to remember states.
Re: How to remember jFace treeviewer's expand state? [message #1389016 is a reply to message #1386022] Fri, 27 June 2014 08:03 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 13-06-14 04:45, Sam Su wrote:
> I'm developing a TreeViewer quite like the Eclipse PackageExplorer Tree,
> so How to remember jFace treeviewer's expand state so that when the
> application start/restart, it can revert to the last state?

You can stor in IMemento which you can obtain when a view/editor is
initiated.

public void init(IViewSite site, IMemento memento) throws
PartInitException {


The IMemento can store any value (Serialized to XML with XMLMemento),
perhaps like this:


public static void rememberSectionsInComposite(IMemento memento,
Composite cmpDetails, String key) {

Control[] controls = cmpDetails.getChildren();
for (int i = 0; i < controls.length; i++) {
Control c = controls[i];
if (c instanceof Section) {
// append the index of the key, to retrieve later.
memento.putBoolean(key + i, ((Section) c).isExpanded());
}
}
}


or even the widths


public static void retrieveStructuredViewerColumns(IMemento memento,
StructuredViewer viewer, String key) {

int[] columnWidths = retrieveIntArray(memento, key);
if (columnWidths.length > 0) {
if (viewer instanceof TableViewer) {

// No check on the size of the array.
TableColumn[] columns = ((TableViewer) viewer).getTable()
.getColumns();
for (int i = 0; i < columns.length; i++) {
TableColumn tc = columns[i];
tc.setWidth(columnWidths[i]);
}
} else if (viewer instanceof TreeViewer) {
TreeColumn[] columns = ((TreeViewer) viewer).getTree()
.getColumns();
for (int i = 0; i < columns.length; i++) {
TreeColumn tc = columns[i];
tc.setWidth(columnWidths[i]);
}
}
}

}
Re: How to remember jFace treeviewer's expand state? [message #1389021 is a reply to message #1389016] Fri, 27 June 2014 08:08 Go to previous messageGo to next message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
For the expansion of a StructuredViewer, you can store the selection.
as this can't easily be stored in IMemento, you can store the index,
then when you restore, you can use this index to get the object and set
a new StructuredSelection() on the StructuredViewer, which will
auto-expand your viewer and looks like you never quit the app :-)






On 27-06-14 10:03, Christophe Bouhier wrote:
> On 13-06-14 04:45, Sam Su wrote:
>> I'm developing a TreeViewer quite like the Eclipse PackageExplorer Tree,
>> so How to remember jFace treeviewer's expand state so that when the
>> application start/restart, it can revert to the last state?
>
> You can stor in IMemento which you can obtain when a view/editor is
> initiated.
>
> public void init(IViewSite site, IMemento memento) throws
> PartInitException {
>
>
> The IMemento can store any value (Serialized to XML with XMLMemento),
> perhaps like this:
>
>
> public static void rememberSectionsInComposite(IMemento memento,
> Composite cmpDetails, String key) {
>
> Control[] controls = cmpDetails.getChildren();
> for (int i = 0; i < controls.length; i++) {
> Control c = controls[i];
> if (c instanceof Section) {
> // append the index of the key, to retrieve later.
> memento.putBoolean(key + i, ((Section) c).isExpanded());
> }
> }
> }
>
>
> or even the widths
>
>
> public static void retrieveStructuredViewerColumns(IMemento memento,
> StructuredViewer viewer, String key) {
>
> int[] columnWidths = retrieveIntArray(memento, key);
> if (columnWidths.length > 0) {
> if (viewer instanceof TableViewer) {
>
> // No check on the size of the array.
> TableColumn[] columns = ((TableViewer) viewer).getTable()
> .getColumns();
> for (int i = 0; i < columns.length; i++) {
> TableColumn tc = columns[i];
> tc.setWidth(columnWidths[i]);
> }
> } else if (viewer instanceof TreeViewer) {
> TreeColumn[] columns = ((TreeViewer) viewer).getTree()
> .getColumns();
> for (int i = 0; i < columns.length; i++) {
> TreeColumn tc = columns[i];
> tc.setWidth(columnWidths[i]);
> }
> }
> }
>
> }
>
>
Re: How to remember jFace treeviewer's expand state? [message #1389047 is a reply to message #1389021] Fri, 27 June 2014 08:57 Go to previous messageGo to next message
Hussein MHANNA is currently offline Hussein MHANNAFriend
Messages: 45
Registered: February 2014
Location: LAVAL
Member
Hi,

The saveState method is called only if your view is open when the workbench shuts down and will not be called when the view is closed by the user.

The IMemento instance can be null if the view state was not saved from a previous session.

I recommend you tu use IDialogSettings to save/restore data of views between sessions.

The advantage of IDialogSettings over the view save/init mechanism is that you can control whenever you want to save/ restore persisted data of your view.

For more information see here.

An example of IDialogSettings is here.

Kind Regards,

Hussein


ALL4TEC
Re: How to remember jFace treeviewer's expand state? [message #1389051 is a reply to message #1389047] Fri, 27 June 2014 09:03 Go to previous message
Christophe Bouhier is currently offline Christophe BouhierFriend
Messages: 937
Registered: July 2009
Senior Member
On 27-06-14 10:57, Hussein MHANNA wrote:
> Hi,
>
> The saveState method is called only if your view is open when the
> workbench shuts down and will not be called when the view is closed by
> the user.

An IPartlistener does magic in this case.

>
> The IMemento instance can be null if the view state was not saved from a
> previous session.

if (memento == null) {
XMLMemento newMemento = XMLMemento
.createWriteRoot(MementoUtil.MEM_KEY_SCREEN_PART);
this.memento = newMemento;
}

>
> I recommend you tu use IDialogSettings to save/restore data of views
> between sessions.
>
> The advantage of IDialogSettings over the view save/init mechanism is
> that you can control whenever you want to save/ restore persisted data
> of your view.
>
> For more information see
> https://wiki.eclipse.org/FAQ_How_does_a_view_persist_its_state_between_sessions%3F.
>
>
> An example of IDialogSettings is
> https://wiki.eclipse.org/FAQ_How_do_I_save_settings_for_a_dialog_or_a_wizard%3F.
>
>
> Kind Regards,
>
> Hussein
Previous Topic:Undo/Redo keyboard shortcuts in a JFace dialog
Next Topic:SourceViewer : how to create batch of actions
Goto Forum:
  


Current Time: Fri Sep 21 23:48:09 GMT 2018

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

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

Back to the top