-package org.simantics.browsing.ui.swt;\r
-\r
-import java.io.File;\r
-import java.nio.file.Files;\r
-import java.nio.file.Path;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.simantics.browsing.ui.ExplorerState;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.StatePersistor;\r
-import org.simantics.databoard.util.StringUtil;\r
-import org.simantics.db.common.utils.Logger;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class IdentifiedStatePersistor implements StatePersistor {\r
-\r
- protected final String id;\r
-\r
- public IdentifiedStatePersistor(String id) {\r
- this.id = id;\r
- }\r
-\r
- @Override\r
- public ExplorerState deserialize(File stateLocation, NodeContext root) {\r
- return stateLocation == null ? ExplorerState.EMPTY\r
- : deserialize(getMementoPath(stateLocation, root));\r
- }\r
-\r
- @Override\r
- public void serialize(File stateLocation, NodeContext root, ExplorerState state) {\r
- if (stateLocation == null || root == null)\r
- return;\r
- try {\r
- GraphExplorerStateBean bean = toStateBean(state, root);\r
- Path memento = getMementoPath(stateLocation, root);\r
- if (bean != null && memento != null)\r
- Files.write(memento, bean.serialize());\r
- } catch (Throwable t) {\r
- Logger.defaultLogError(t);\r
- }\r
- }\r
-\r
- protected ExplorerState deserialize(Path path) {\r
- if (path == null || !Files.exists(path))\r
- return ExplorerState.EMPTY;\r
- try {\r
- GraphExplorerStateBean stateBean = new GraphExplorerStateBean();\r
- stateBean.deserialize( Files.readAllBytes(path) );\r
- return toState(stateBean);\r
- } catch (Throwable t) {\r
- Logger.defaultLogError(t);\r
- return ExplorerState.EMPTY;\r
- }\r
- }\r
-\r
- protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {\r
- return NodeContext.NONE;\r
- }\r
-\r
- protected List<NodeContext> toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception {\r
- return beans.length == 0 ? Collections.<NodeContext>emptyList()\r
- : Arrays.asList(toNodeContexts(beans));\r
- }\r
-\r
- protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {\r
- return GraphExplorerStateNodeBean.NONE;\r
- }\r
-\r
- protected ExplorerState toState(GraphExplorerStateBean stateBean) throws Exception {\r
- return new ExplorerState(\r
- toNodeContexts(stateBean.topNodePath),\r
- stateBean.topNodePathChildIndexes,\r
- toNodeContextList(stateBean.expandedNodes),\r
- stateBean.columnWidths);\r
- }\r
-\r
- /**\r
- * @param state\r
- * the {@link ExplorerState} to transform into a\r
- * {@link GraphExplorerStateBean}\r
- * @param root\r
- * the input root node context that was used to initialize the\r
- * explorer state\r
- * @return\r
- */\r
- protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {\r
- GraphExplorerStateBean ib = new GraphExplorerStateBean();\r
- ib.topNodePath = toNodeBeans(state.topNodePath);\r
- ib.topNodePathChildIndexes = state.topNodePathChildIndex;\r
- ib.expandedNodes = toNodeBeans(state.expandedNodes.toArray(NodeContext.NONE));\r
- ib.columnWidths = state.columnWidths;\r
- return ib;\r
- }\r
-\r
- protected Path getMementoPath(File stateLocation, NodeContext root) {\r
- return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");\r
- }\r
-\r
+package org.simantics.browsing.ui.swt;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.simantics.browsing.ui.ExplorerState;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.StatePersistor;
+import org.simantics.databoard.util.StringUtil;
+import org.simantics.db.common.utils.Logger;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class IdentifiedStatePersistor implements StatePersistor {
+
+ protected final String id;
+
+ public IdentifiedStatePersistor(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public ExplorerState deserialize(File stateLocation, NodeContext root) {
+ return stateLocation == null ? ExplorerState.EMPTY
+ : deserialize(getMementoPath(stateLocation, root));
+ }
+
+ @Override
+ public void serialize(File stateLocation, NodeContext root, ExplorerState state) {
+ if (stateLocation == null || root == null)
+ return;
+ try {
+ GraphExplorerStateBean bean = toStateBean(state, root);
+ Path memento = getMementoPath(stateLocation, root);
+ if (bean != null && memento != null)
+ Files.write(memento, bean.serialize());
+ } catch (Throwable t) {
+ Logger.defaultLogError(t);
+ }
+ }
+
+ protected ExplorerState deserialize(Path path) {
+ if (path == null || !Files.exists(path))
+ return ExplorerState.EMPTY;
+ try {
+ GraphExplorerStateBean stateBean = new GraphExplorerStateBean();
+ stateBean.deserialize( Files.readAllBytes(path) );
+ return toState(stateBean);
+ } catch (Throwable t) {
+ Logger.defaultLogError(t);
+ return ExplorerState.EMPTY;
+ }
+ }
+
+ protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {
+ return NodeContext.NONE;
+ }
+
+ protected List<NodeContext> toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception {
+ return beans.length == 0 ? Collections.<NodeContext>emptyList()
+ : Arrays.asList(toNodeContexts(beans));
+ }
+
+ protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {
+ return GraphExplorerStateNodeBean.NONE;
+ }
+
+ protected ExplorerState toState(GraphExplorerStateBean stateBean) throws Exception {
+ return new ExplorerState(
+ toNodeContexts(stateBean.topNodePath),
+ stateBean.topNodePathChildIndexes,
+ toNodeContextList(stateBean.expandedNodes),
+ stateBean.columnWidths);
+ }
+
+ /**
+ * @param state
+ * the {@link ExplorerState} to transform into a
+ * {@link GraphExplorerStateBean}
+ * @param root
+ * the input root node context that was used to initialize the
+ * explorer state
+ * @return
+ */
+ protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {
+ GraphExplorerStateBean ib = new GraphExplorerStateBean();
+ ib.topNodePath = toNodeBeans(state.topNodePath);
+ ib.topNodePathChildIndexes = state.topNodePathChildIndex;
+ ib.expandedNodes = toNodeBeans(state.expandedNodes.toArray(NodeContext.NONE));
+ ib.columnWidths = state.columnWidths;
+ return ib;
+ }
+
+ protected Path getMementoPath(File stateLocation, NodeContext root) {
+ return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id) + ".ge");
+ }
+