1 /*******************************************************************************
2 * Copyright (c) 2014 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.function;
15 import java.nio.file.Path;
16 import java.util.ArrayList;
17 import java.util.List;
20 import org.simantics.Simantics;
21 import org.simantics.browsing.ui.BuiltinKeys;
22 import org.simantics.browsing.ui.ExplorerState;
23 import org.simantics.browsing.ui.NodeContext;
24 import org.simantics.browsing.ui.NodeContext.ConstantKey;
25 import org.simantics.browsing.ui.common.NodeContextBuilder;
26 import org.simantics.browsing.ui.common.NodeContextBuilder.MapNodeContext;
27 import org.simantics.browsing.ui.common.state.GraphExplorerStateBean;
28 import org.simantics.browsing.ui.common.state.GraphExplorerStateNodeBean;
29 import org.simantics.browsing.ui.common.state.IdentifiedStatePersistor;
30 import org.simantics.browsing.ui.common.state.StringArrayBean;
31 import org.simantics.browsing.ui.common.state.StringBean;
32 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
33 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
34 import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;
35 import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
36 import org.simantics.browsing.ui.model.nodetypes.NodeType;
37 import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;
38 import org.simantics.browsing.ui.swt.NodeContextValueBean;
39 import org.simantics.databoard.Bindings;
40 import org.simantics.databoard.binding.impl.ArrayListBinding;
41 import org.simantics.databoard.util.Bean;
42 import org.simantics.databoard.util.StringUtil;
43 import org.simantics.db.Resource;
46 * @author Antti Villberg
47 * @author Tuukka Lehtonen
49 class StandardPersistor extends IdentifiedStatePersistor {
51 public StandardPersistor(String configurationId) {
52 super(configurationId);
56 protected Path getMementoPath(File stateLocation, NodeContext root) {
58 Object input = root.getConstant(BuiltinKeys.INPUT);
59 if (input instanceof Resource) {
60 Resource r = (Resource) input;
61 return stateLocation.toPath().resolve(StringUtil.escapeToFileName(id + "#" + r.getResourceId() + ".ge"));
68 protected GraphExplorerStateBean toStateBean(ExplorerState state, NodeContext root) {
69 Object input = root.getConstant(BuiltinKeys.INPUT);
70 if (input instanceof Resource)
71 return super.toStateBean(state, root);
76 protected GraphExplorerStateNodeBean[] toNodeBeans(NodeContext[] contexts) {
77 ArrayList<GraphExplorerStateNodeBean> result = new ArrayList<GraphExplorerStateNodeBean>(contexts.length);
78 for (NodeContext node : contexts) {
79 GraphExplorerStateNodeBean bean = buildNodeBean(node);
84 return result.toArray(GraphExplorerStateNodeBean.NONE);
88 protected NodeContext[] toNodeContexts(GraphExplorerStateNodeBean[] beans) throws Exception {
89 ArrayList<NodeContext> result = new ArrayList<NodeContext>(beans.length);
90 for (GraphExplorerStateNodeBean bean : beans) {
91 NodeContext node = bean != null ? buildNodeContext(bean) : null;
96 return result.toArray(NodeContext.NONE);
99 protected NodeContext buildNodeContext(GraphExplorerStateNodeBean node) throws Exception {
100 ArrayList<Object> keysAndValues = new ArrayList<Object>(node.map.size());
101 for(Map.Entry<String, Bean> entry : node.map.entrySet()) {
102 ConstantKey<?> key = decodeKey(entry.getKey());
103 Object value = decodeValue(key, entry.getValue());
104 keysAndValues.add(key);
105 keysAndValues.add(value);
107 return NodeContextBuilder.buildWithData(keysAndValues.toArray());
110 protected GraphExplorerStateNodeBean buildNodeBean(NodeContext nodeContext) {
111 if (nodeContext instanceof MapNodeContext) {
112 MapNodeContext ctx = (MapNodeContext) nodeContext;
113 GraphExplorerStateNodeBean node = new GraphExplorerStateNodeBean();
114 for (ConstantKey<?> key : ctx.getKeys()) {
115 Object value = ctx.getConstant(key);
116 if (value instanceof Resource) {
117 node.map.put(key.getClass().getName(), new NodeContextValueBean((Resource) value));
118 } else if (value instanceof EntityNodeType) {
119 node.map.put(key.getClass().getName(), new NodeContextValueBean((EntityNodeType) value));
120 } else if (value instanceof SpecialNodeType) {
121 node.map.put(key.getClass().getName(), new NodeContextValueBean((SpecialNodeType) value));
122 } else if (key == BuiltinKeys.UI_CONTEXT) {
123 node.map.put(key.getClass().getName(), new StringBean((String)value));
124 } else if (key == BuiltinKeys.BROWSE_CONTEXT) {
125 String[] uris = value != null ? ((BrowseContext) value).getURIs() : null;
126 node.map.put(key.getClass().getName(), new StringArrayBean(uris));
127 } else if (key == BuiltinKeys.ACTION_BROWSE_CONTEXT) {
128 String[] uris = value != null ? ((ActionBrowseContext) value).getURIs() : null;
129 node.map.put(key.getClass().getName(), new StringArrayBean(uris));
137 private ConstantKey<?> decodeKey(String name) {
138 if(BuiltinKeys.INPUT.getClass().getName().equals(name)) return BuiltinKeys.INPUT;
139 else if(BuiltinKeys.UI_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.UI_CONTEXT;
140 else if(BuiltinKeys.BROWSE_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.BROWSE_CONTEXT;
141 else if(BuiltinKeys.ACTION_BROWSE_CONTEXT.getClass().getName().equals(name)) return BuiltinKeys.ACTION_BROWSE_CONTEXT;
142 else if(NodeType.TYPE.getClass().getName().equals(name)) return NodeType.TYPE;
143 else throw new IllegalArgumentException(name);
146 private Object decodeValue(ConstantKey<?> key, Bean bean) throws Exception {
148 if (key == BuiltinKeys.BROWSE_CONTEXT) {
149 String[] uris = (String[]) bean.getField("strings");
150 return uris == null ? null : BrowseContexts.toBrowseContext(Simantics.getSession(), uris);
151 } else if (key == BuiltinKeys.ACTION_BROWSE_CONTEXT) {
152 String[] uris = (String[]) bean.getField("strings");
153 return uris == null ? null : BrowseContexts.toActionBrowseContext(Simantics.getSession(), uris);
154 } else if (key == BuiltinKeys.UI_CONTEXT) {
155 return bean.getField("string");
158 String name = (String)bean.getField("name");
159 if(Resource.class.getName().equals(name)) {
160 return bean.getField("resource", Bindings.getBinding(Resource.class));
161 } else if(EntityNodeType.class.getName().equals(name)) {
162 @SuppressWarnings("unchecked")
163 List<Resource> entityType = (List<Resource>)bean.getField("resources",
164 new ArrayListBinding(Bindings.getBinding(Resource.class)));
165 return EntityNodeType.create(entityType);
166 } else if(SpecialNodeType.class.getName().equals(name)) {
167 Resource resource = (Resource)bean.getField("resource", Bindings.getBinding(Resource.class));
168 String contentType = (String)bean.getField("className");
169 Class<?> clazz = getClass().getClassLoader().loadClass(contentType);
170 return new SpecialNodeType(resource, clazz);
172 throw new IllegalArgumentException("key = " + key + ", bean = " + bean);