1 package org.simantics.browsing.ui.common.state;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.util.Arrays;
7 import java.util.Collections;
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;
18 * @author Tuukka Lehtonen
20 public class IdentifiedStatePersistor implements StatePersistor {
22 private static final Logger LOGGER = LoggerFactory.getLogger(IdentifiedStatePersistor.class);
24 protected final String id;
26 public IdentifiedStatePersistor(String id) {
31 public ExplorerState deserialize(File stateLocation, NodeContext root) {
32 return stateLocation == null ? ExplorerState.EMPTY
33 : deserialize(getMementoPath(stateLocation, root));
37 public void serialize(File stateLocation, NodeContext root, ExplorerState state) {
38 if (stateLocation == null || root == null)
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);
50 protected ExplorerState deserialize(Path path) {
51 if (path == null || !Files.exists(path))
52 return ExplorerState.EMPTY;
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;
63 protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {
64 return NodeContext.NONE;
67 protected List<NodeContext> toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception {
68 return beans.length == 0 ? Collections.<NodeContext>emptyList()
69 : Arrays.asList(toNodeContexts(beans));
72 protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {
73 return GraphExplorerStateNodeBean.NONE;
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);
86 * the {@link ExplorerState} to transform into a
87 * {@link GraphExplorerStateBean}
89 * the input root node context that was used to initialize the
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;
102 protected Path getMementoPath(File stateLocation, NodeContext root) {
103 return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");