--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.function;\r
+\r
+import java.io.File;\r
+import java.nio.file.Path;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.ExplorerState;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.NodeContext.ConstantKey;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder;\r
+import org.simantics.browsing.ui.common.NodeContextBuilder.MapNodeContext;\r
+import org.simantics.browsing.ui.model.actions.ActionBrowseContext;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;\r
+import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;\r
+import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
+import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;\r
+import org.simantics.browsing.ui.swt.GraphExplorerStateBean;\r
+import org.simantics.browsing.ui.swt.GraphExplorerStateNodeBean;\r
+import org.simantics.browsing.ui.swt.IdentifiedStatePersistor;\r
+import org.simantics.browsing.ui.swt.NodeContextValueBean;\r
+import org.simantics.browsing.ui.swt.StringArrayBean;\r
+import org.simantics.browsing.ui.swt.StringBean;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.impl.ArrayListBinding;\r
+import org.simantics.databoard.util.Bean;\r
+import org.simantics.databoard.util.StringUtil;\r
+import org.simantics.db.Resource;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ * @author Tuukka Lehtonen\r
+ */\r
+class StandardPersistor extends IdentifiedStatePersistor {\r
+\r
+ public StandardPersistor(String configurationId) {\r
+ super(configurationId);\r
+ }\r
+\r
+ @Override\r
+ protected Path getMementoPath(File stateLocation, NodeContext root) {\r
+ if (root != null) {\r
+ Object input = root.getConstant(BuiltinKeys.INPUT);\r
+ if (input instanceof Resource) {\r
+ Resource r = (Resource) input;\r
+ return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id + "#" + r.getResourceId() + ".ge"));\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {\r
+ Object input = root.getConstant(BuiltinKeys.INPUT);\r
+ if (input instanceof Resource)\r
+ return super.toStateBean(state, root);\r
+ return null;\r
+ }\r
+\r
+ @Override\r
+ protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {\r
+ ArrayList<GraphExplorerStateNodeBean> result = new ArrayList<GraphExplorerStateNodeBean>(contexts.length);\r
+ for (NodeContext node : contexts) {\r
+ GraphExplorerStateNodeBean bean = buildNodeBean(node);\r
+ if (bean != null) {\r
+ result.add(bean);\r
+ }\r
+ }\r
+ return result.toArray(GraphExplorerStateNodeBean.NONE);\r
+ }\r
+\r
+ @Override\r
+ protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {\r
+ ArrayList<NodeContext> result = new ArrayList<NodeContext>(beans.length);\r
+ for (GraphExplorerStateNodeBean bean : beans) {\r
+ NodeContext node = bean != null ? buildNodeContext(bean) : null;\r
+ if (node != null) {\r
+ result.add(node);\r
+ }\r
+ }\r
+ return result.toArray(NodeContext.NONE);\r
+ }\r
+\r
+ protected NodeContext buildNodeContext(GraphExplorerStateNodeBean node) throws Exception {\r
+ ArrayList<Object> keysAndValues = new ArrayList<Object>(node.map.size());\r
+ for(Map.Entry<String, Bean> entry : node.map.entrySet()) {\r
+ ConstantKey<?> key = decodeKey(entry.getKey());\r
+ Object value = decodeValue(key, entry.getValue());\r
+ keysAndValues.add(key);\r
+ keysAndValues.add(value);\r
+ }\r
+ return NodeContextBuilder.buildWithData(keysAndValues.toArray());\r
+ }\r
+\r
+ protected GraphExplorerStateNodeBean buildNodeBean(NodeContext nodeContext) {\r
+ if (nodeContext instanceof MapNodeContext) {\r
+ MapNodeContext ctx = (MapNodeContext) nodeContext;\r
+ GraphExplorerStateNodeBean node = new GraphExplorerStateNodeBean();\r
+ for (ConstantKey<?> key : ctx.getKeys()) {\r
+ Object value = ctx.getConstant(key);\r
+ if (value instanceof Resource) {\r
+ node.map.put(key.getClass().getName(), new NodeContextValueBean((Resource) value));\r
+ } else if (value instanceof EntityNodeType) {\r
+ node.map.put(key.getClass().getName(), new NodeContextValueBean((EntityNodeType) value));\r
+ } else if (value instanceof SpecialNodeType) {\r
+ node.map.put(key.getClass().getName(), new NodeContextValueBean((SpecialNodeType) value));\r
+ } else if (key == BuiltinKeys.UI_CONTEXT) {\r
+ node.map.put(key.getClass().getName(), new StringBean((String)value));\r
+ } else if (key == BuiltinKeys.BROWSE_CONTEXT) {\r
+ String[] uris = value != null ? ((BrowseContext) value).getURIs() : null;\r
+ node.map.put(key.getClass().getName(), new StringArrayBean(uris));\r
+ } else if (key == BuiltinKeys.ACTION_BROWSE_CONTEXT) {\r
+ String[] uris = value != null ? ((ActionBrowseContext) value).getURIs() : null;\r
+ node.map.put(key.getClass().getName(), new StringArrayBean(uris));\r
+ }\r
+ }\r
+ return node;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private ConstantKey<?> decodeKey(String name) {\r
+ if(BuiltinKeys.INPUT.getClass().getName().equals(name)) return BuiltinKeys.INPUT;\r
+ else if(BuiltinKeys.UI_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.UI_CONTEXT;\r
+ else if(BuiltinKeys.BROWSE_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.BROWSE_CONTEXT;\r
+ else if(BuiltinKeys.ACTION_BROWSE_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.ACTION_BROWSE_CONTEXT;\r
+ else if(NodeType.TYPE.getClass().getName().equals(name)) return NodeType.TYPE;\r
+ else throw new IllegalArgumentException(name);\r
+ }\r
+\r
+ private Object decodeValue(ConstantKey<?> key, Bean bean) throws Exception {\r
+ \r
+ if (key == BuiltinKeys.BROWSE_CONTEXT) {\r
+ String[] uris = (String[]) bean.getField("strings");\r
+ return uris == null ? null : BrowseContexts.toBrowseContext(Simantics.getSession(), uris);\r
+ } else if (key == BuiltinKeys.ACTION_BROWSE_CONTEXT) {\r
+ String[] uris = (String[]) bean.getField("strings");\r
+ return uris == null ? null : BrowseContexts.toActionBrowseContext(Simantics.getSession(), uris);\r
+ } else if (key == BuiltinKeys.UI_CONTEXT) {\r
+ return bean.getField("string");\r
+ }\r
+\r
+ String name = (String)bean.getField("name");\r
+ if(Resource.class.getName().equals(name)) {\r
+ return bean.getField("resource", Bindings.getBinding(Resource.class));\r
+ } else if(EntityNodeType.class.getName().equals(name)) {\r
+ @SuppressWarnings("unchecked")\r
+ List<Resource> entityType = (List<Resource>)bean.getField("resources", \r
+ new ArrayListBinding(Bindings.getBinding(Resource.class)));\r
+ return EntityNodeType.create(entityType);\r
+ } else if(SpecialNodeType.class.getName().equals(name)) {\r
+ Resource resource = (Resource)bean.getField("resource", Bindings.getBinding(Resource.class));\r
+ String contentType = (String)bean.getField("className");\r
+ Class<?> clazz = getClass().getClassLoader().loadClass(contentType);\r
+ return new SpecialNodeType(resource, clazz);\r
+ }\r
+ throw new IllegalArgumentException("key = " + key + ", bean = " + bean);\r
+ }\r
+\r
+}
\ No newline at end of file