1 package org.simantics.browsing.ui.swt;
\r
4 import java.nio.file.Files;
\r
5 import java.nio.file.Path;
\r
6 import java.util.Arrays;
\r
7 import java.util.Collections;
\r
8 import java.util.List;
\r
10 import org.simantics.browsing.ui.ExplorerState;
\r
11 import org.simantics.browsing.ui.NodeContext;
\r
12 import org.simantics.browsing.ui.StatePersistor;
\r
13 import org.simantics.databoard.util.StringUtil;
\r
14 import org.simantics.db.common.utils.Logger;
\r
17 * @author Tuukka Lehtonen
\r
19 public class IdentifiedStatePersistor implements StatePersistor {
\r
21 protected final String id;
\r
23 public IdentifiedStatePersistor(String id) {
\r
28 public ExplorerState deserialize(File stateLocation, NodeContext root) {
\r
29 return stateLocation == null ? ExplorerState.EMPTY
\r
30 : deserialize(getMementoPath(stateLocation, root));
\r
34 public void serialize(File stateLocation, NodeContext root, ExplorerState state) {
\r
35 if (stateLocation == null || root == null)
\r
38 GraphExplorerStateBean bean = toStateBean(state, root);
\r
39 Path memento = getMementoPath(stateLocation, root);
\r
40 if (bean != null && memento != null)
\r
41 Files.write(memento, bean.serialize());
\r
42 } catch (Throwable t) {
\r
43 Logger.defaultLogError(t);
\r
47 protected ExplorerState deserialize(Path path) {
\r
48 if (path == null || !Files.exists(path))
\r
49 return ExplorerState.EMPTY;
\r
51 GraphExplorerStateBean stateBean = new GraphExplorerStateBean();
\r
52 stateBean.deserialize( Files.readAllBytes(path) );
\r
53 return toState(stateBean);
\r
54 } catch (Throwable t) {
\r
55 Logger.defaultLogError(t);
\r
56 return ExplorerState.EMPTY;
\r
60 protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {
\r
61 return NodeContext.NONE;
\r
64 protected List<NodeContext> toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception {
\r
65 return beans.length == 0 ? Collections.<NodeContext>emptyList()
\r
66 : Arrays.asList(toNodeContexts(beans));
\r
69 protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {
\r
70 return GraphExplorerStateNodeBean.NONE;
\r
73 protected ExplorerState toState(GraphExplorerStateBean stateBean) throws Exception {
\r
74 return new ExplorerState(
\r
75 toNodeContexts(stateBean.topNodePath),
\r
76 stateBean.topNodePathChildIndexes,
\r
77 toNodeContextList(stateBean.expandedNodes),
\r
78 stateBean.columnWidths);
\r
83 * the {@link ExplorerState} to transform into a
\r
84 * {@link GraphExplorerStateBean}
\r
86 * the input root node context that was used to initialize the
\r
90 protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {
\r
91 GraphExplorerStateBean ib = new GraphExplorerStateBean();
\r
92 ib.topNodePath = toNodeBeans(state.topNodePath);
\r
93 ib.topNodePathChildIndexes = state.topNodePathChildIndex;
\r
94 ib.expandedNodes = toNodeBeans(state.expandedNodes.toArray(NodeContext.NONE));
\r
95 ib.columnWidths = state.columnWidths;
\r
99 protected Path getMementoPath(File stateLocation, NodeContext root) {
\r
100 return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");
\r