]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/IdentifiedStatePersistor.java
Merge remote-tracking branch 'origin/svn' commit 'ccc1271c9d6657fb9dcf4cf3cb115fa0c8c...
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / IdentifiedStatePersistor.java
1 package org.simantics.browsing.ui.swt;\r
2 \r
3 import java.io.File;\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
9 \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
15 \r
16 /**\r
17  * @author Tuukka Lehtonen\r
18  */\r
19 public class IdentifiedStatePersistor implements StatePersistor {\r
20 \r
21         protected final String id;\r
22 \r
23         public IdentifiedStatePersistor(String id) {\r
24                 this.id = id;\r
25         }\r
26 \r
27         @Override\r
28         public ExplorerState deserialize(File stateLocation, NodeContext root) {\r
29                 return stateLocation == null ? ExplorerState.EMPTY\r
30                                 : deserialize(getMementoPath(stateLocation, root));\r
31         }\r
32 \r
33         @Override\r
34         public void serialize(File stateLocation, NodeContext root, ExplorerState state) {\r
35                 if (stateLocation == null || root == null)\r
36                         return;\r
37                 try {\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
44                 }\r
45         }\r
46 \r
47         protected ExplorerState deserialize(Path path) {\r
48                 if (path == null || !Files.exists(path))\r
49                         return ExplorerState.EMPTY;\r
50                 try {\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
57                 }\r
58         }\r
59 \r
60         protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {\r
61                 return NodeContext.NONE;\r
62         }\r
63 \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
67         }\r
68 \r
69         protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {\r
70                 return GraphExplorerStateNodeBean.NONE;\r
71         }\r
72 \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
79         }\r
80 \r
81         /**\r
82          * @param state\r
83          *            the {@link ExplorerState} to transform into a\r
84          *            {@link GraphExplorerStateBean}\r
85          * @param root\r
86          *            the input root node context that was used to initialize the\r
87          *            explorer state\r
88          * @return\r
89          */\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
96                 return ib;\r
97         }\r
98 \r
99         protected Path getMementoPath(File stateLocation, NodeContext root) {\r
100                 return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");\r
101         }\r
102 \r
103 }