UI locking fixes for GraphExplorer implementations
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / state / IdentifiedStatePersistor.java
1 package org.simantics.browsing.ui.common.state;
2
3 import java.io.File;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.simantics.browsing.ui.ExplorerState;
11 import org.simantics.browsing.ui.NodeContext;
12 import org.simantics.browsing.ui.StatePersistor;
13 import org.simantics.databoard.util.StringUtil;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /**
18  * @author Tuukka Lehtonen
19  */
20 public class IdentifiedStatePersistor implements StatePersistor {
21
22         private static final Logger LOGGER = LoggerFactory.getLogger(IdentifiedStatePersistor.class);
23
24         protected final String id;
25
26         public IdentifiedStatePersistor(String id) {
27                 this.id = id;
28         }
29
30         @Override
31         public ExplorerState deserialize(File stateLocation, NodeContext root) {
32                 return stateLocation == null ? ExplorerState.EMPTY
33                                 : deserialize(getMementoPath(stateLocation, root));
34         }
35
36         @Override
37         public void serialize(File stateLocation, NodeContext root, ExplorerState state) {
38                 if (stateLocation == null || root == null)
39                         return;
40                 try {
41                         GraphExplorerStateBean bean = toStateBean(state, root);
42                         Path memento = getMementoPath(stateLocation, root);
43                         if (bean != null && memento != null)
44                                 Files.write(memento, bean.serialize());
45                 } catch (Throwable t) {
46                         LOGGER.error("Failed to serialize ExplorerState " + state, t);
47                 }
48         }
49
50         protected ExplorerState deserialize(Path path) {
51                 if (path == null || !Files.exists(path))
52                         return ExplorerState.EMPTY;
53                 try {
54                         GraphExplorerStateBean stateBean = new GraphExplorerStateBean();
55                         stateBean.deserialize( Files.readAllBytes(path) );
56                         return toState(stateBean);
57                 } catch (Throwable t) {
58                         LOGGER.error("Failed to deserialize ExplorerState from " + path, t);
59                         return ExplorerState.EMPTY;
60                 }
61         }
62
63         protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {
64                 return NodeContext.NONE;
65         }
66
67         protected List<NodeContext> toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception {
68                 return beans.length == 0 ? Collections.<NodeContext>emptyList()
69                                 : Arrays.asList(toNodeContexts(beans));
70         }
71
72         protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {
73                 return GraphExplorerStateNodeBean.NONE;
74         }
75
76         protected ExplorerState toState(GraphExplorerStateBean stateBean) throws Exception {
77                 return new ExplorerState(
78                                 toNodeContexts(stateBean.topNodePath),
79                                 stateBean.topNodePathChildIndexes,
80                                 toNodeContextList(stateBean.expandedNodes),
81                                 stateBean.columnWidths);
82         }
83
84         /**
85          * @param state
86          *            the {@link ExplorerState} to transform into a
87          *            {@link GraphExplorerStateBean}
88          * @param root
89          *            the input root node context that was used to initialize the
90          *            explorer state
91          * @return
92          */
93         protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {
94                 GraphExplorerStateBean ib = new GraphExplorerStateBean();
95                 ib.topNodePath = toNodeBeans(state.topNodePath);
96                 ib.topNodePathChildIndexes = state.topNodePathChildIndex;
97                 ib.expandedNodes = toNodeBeans(state.expandedNodes.toArray(NodeContext.NONE));
98                 ib.columnWidths = state.columnWidths;
99                 return ib;
100         }
101
102         protected Path getMementoPath(File stateLocation, NodeContext root) {
103                 return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");
104         }
105
106 }