|
|
Re: How to remember jFace treeviewer's expand state? [message #1389016 is a reply to message #1386022] |
Fri, 27 June 2014 08:03 |
Christophe Bouhier 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 |
Christophe Bouhier 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]);
> }
> }
> }
>
> }
>
>
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03863 seconds