// for dynamic UI
public IMemento saveViewState(IMemento memento, IViewReference ref,
MultiStatus res) {
final MultiStatus result = res;
final IMemento viewMemento = memento
.createChild(IWorkbenchConstants.TAG_VIEW);
viewMemento.putString(IWorkbenchConstants.TAG_ID, ViewFactory
.getKey(ref));
if (ref instanceof ViewReference) {
viewMemento.putString(IWorkbenchConstants.TAG_PART_NAME,
((ViewReference) ref).getPartName());
}
final IViewReference viewRef = ref;
final IViewPart view = (IViewPart) ref.getPart(false);
if (view != null) {
SafeRunner.run(new SafeRunnable() {
public void run() {
if (view instanceof IWorkbenchPart3) {
Map properties = ((IWorkbenchPart3) view)
.getPartProperties();
if (!properties.isEmpty()) {
IMemento propBag = viewMemento
.createChild(IWorkbenchConstants.TAG_PROPERTIES);
Iterator i = properties.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry) i.next();
IMemento p = propBag.createChild(
IWorkbenchConstants.TAG_PROPERTY,
(String) entry.getKey());
p.putTextData((String) entry.getValue());
}
}
}
view.saveState(viewMemento
.createChild(IWorkbenchConstants.TAG_VIEW_STATE));
}
public void handleException(Throwable e) {
result
.add(new Status(
IStatus.ERROR,
PlatformUI.PLUGIN_ID,
0,
NLS.bind(WorkbenchMessages.ViewFactory_couldNotSave, viewRef.getTitle() ),
e));
}
});
} else {
IMemento mem = null;
IMemento props = null;
// if we've created the reference once, any previous workbench
// state memento is there. After once, there is no previous
// session state, so it should be null.
if (ref instanceof ViewReference) {
mem = ((ViewReference) ref).getMemento();
if (mem!=null) {
props = mem.getChild(IWorkbenchConstants.TAG_PROPERTIES);
}
if (mem!=null) {
mem = mem.getChild(IWorkbenchConstants.TAG_VIEW_STATE);
}
}
if (props != null) {
viewMemento.createChild(IWorkbenchConstants.TAG_PROPERTIES)
.putMemento(props);
}
if (mem != null) {
IMemento child = viewMemento
.createChild(IWorkbenchConstants.TAG_VIEW_STATE);
child.putMemento(mem);
}
}
return viewMemento;
}