As mentioned in this thread, take a look at the late ResourceNavigator
, from the
org.eclipse.ui.views.navigator
package.
("late" because that class is no longer used directly, see the Eclipse Wiki).
(as seen in the eclipse Java Model)
It uses a TreeViewer
, and saves the expanded elements and selected elements, among other state.
The model elements are IResource
objects, which are persisted using the IPersistableElement
/IElementFactory
mechanism.
The IPersistableElement
adapter is registered against IResource
in class WorkbenchAdapter
, but this could instead be obtained by the model element implementing IPersistableElement
directly.
The corresponding IElementFactory
is declared in an elementFactory
extension in org.eclipse.ui.ide
's plugin.xml
.
The restored resources know how to get their own children (and parent), via the tree's content provider, so not all elements of the tree need to be persisted.
A similar approach could be used for saving the viewer's input resource.
For ResourceNavigator
, there's a level of indirection here through its FrameList
's current frame, but if you step through it in the debugger, you'll see that it's essentially doing the same thing.
Small extract (but the rest of the code save also many other things, including selection)
if (frameList.getCurrentIndex() > 0) {
//save frame, it's not the "home"/workspace frame
TreeFrame currentFrame = (TreeFrame) frameList.getCurrentFrame();
IMemento frameMemento = memento.createChild(TAG_CURRENT_FRAME);
currentFrame.saveState(frameMemento);
} else {
//save visible expanded elements
Object JavaDoc expandedElements[] = viewer.getVisibleExpandedElements();
if (expandedElements.length > 0) {
IMemento expandedMem = memento.createChild(TAG_EXPANDED);
for (int i = 0; i < expandedElements.length; i++) {
if (expandedElements[i] instanceof IResource) {
IMemento elementMem = expandedMem
.createChild(TAG_ELEMENT);
elementMem.putString(TAG_PATH,
((IResource) expandedElements[i]).getFullPath()
.toString());
}
}
}
[...]
}