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