]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/function/StandardPersistor.java
Fixed URI of the root resource in fail-safe import
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / function / StandardPersistor.java
1 /*******************************************************************************
2  * Copyright (c) 2014 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.function;
13
14 import java.io.File;
15 import java.nio.file.Path;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19
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.model.actions.ActionBrowseContext;
28 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
29 import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;
30 import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
31 import org.simantics.browsing.ui.model.nodetypes.NodeType;
32 import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;
33 import org.simantics.browsing.ui.swt.GraphExplorerStateBean;
34 import org.simantics.browsing.ui.swt.GraphExplorerStateNodeBean;
35 import org.simantics.browsing.ui.swt.IdentifiedStatePersistor;
36 import org.simantics.browsing.ui.swt.NodeContextValueBean;
37 import org.simantics.browsing.ui.swt.StringArrayBean;
38 import org.simantics.browsing.ui.swt.StringBean;
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;
44
45 /**
46  * @author Antti Villberg
47  * @author Tuukka Lehtonen
48  */
49 class StandardPersistor extends IdentifiedStatePersistor {
50
51         public StandardPersistor(String configurationId) {
52                 super(configurationId);
53         }
54
55         @Override
56         protected Path getMementoPath(File stateLocation, NodeContext root) {
57                 if (root != null) {
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"));
62                         }
63                 }
64                 return null;
65         }
66
67         @Override
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);
72                 return null;
73         }
74
75         @Override
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);
80                         if (bean != null) {
81                                 result.add(bean);
82                         }
83                 }
84                 return result.toArray(GraphExplorerStateNodeBean.NONE);
85         }
86
87         @Override
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;
92                         if (node != null) {
93                                 result.add(node);
94                         }
95                 }
96                 return result.toArray(NodeContext.NONE);
97         }
98
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);
106                 }
107                 return NodeContextBuilder.buildWithData(keysAndValues.toArray());
108         }
109
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));
130                                 }
131                         }
132                         return node;
133                 }
134                 return null;
135         }
136
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);
144         }
145
146         private Object decodeValue(ConstantKey<?> key, Bean bean) throws Exception {
147                 
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");
156                 }
157
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);
171                 }
172                 throw new IllegalArgumentException("key = " + key + ", bean = " + bean);
173         }
174
175 }