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