X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FIdentifiedStatePersistor.java;h=5a3bb75b225af675b90ca05a2bbb021343ba8965;hp=b7412531a9a5e1a961badbf04e163ea1208b54da;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/IdentifiedStatePersistor.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/IdentifiedStatePersistor.java index b7412531a..5a3bb75b2 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/IdentifiedStatePersistor.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/IdentifiedStatePersistor.java @@ -1,103 +1,103 @@ -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 toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception { - return beans.length == 0 ? Collections.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"); - } - +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 toNodeContextList(GraphExplorerStateNodeBean[] beans) throws Exception { + return beans.length == 0 ? Collections.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"); + } + } \ No newline at end of file