]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
3193d41635a1d6c0ba4c21d333f6b8b0d76ee2e8
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 \r
17 import org.eclipse.core.commands.AbstractHandler;\r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.core.commands.ExecutionException;\r
20 import org.eclipse.core.runtime.Platform;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.widgets.FileDialog;\r
24 import org.eclipse.swt.widgets.Shell;\r
25 import org.eclipse.ui.handlers.HandlerUtil;\r
26 import org.simantics.databoard.Bindings;\r
27 import org.simantics.databoard.Files;\r
28 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
29 import org.simantics.db.ReadGraph;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.WriteGraph;\r
32 import org.simantics.db.WriteOnlyGraph;\r
33 import org.simantics.db.common.request.ReadRequest;\r
34 import org.simantics.db.common.request.WriteRequest;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.db.exception.ResourceNotFoundException;\r
37 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
38 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
39 import org.simantics.db.request.Read;\r
40 import org.simantics.graph.representation.Root;\r
41 import org.simantics.graph.representation.TransferableGraph1;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.layer0.utils.direct.GraphUtils;\r
44 import org.simantics.sysdyn.SysdynResource;\r
45 import org.simantics.sysdyn.manager.FunctionUtils;\r
46 import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;\r
47 import org.simantics.ui.SimanticsUI;\r
48 import org.simantics.ui.utils.AdaptionUtils;\r
49 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
50 \r
51 public class ImportFunctionLibrary  extends AbstractHandler {\r
52 \r
53         @Override\r
54         public Object execute(ExecutionEvent event) throws ExecutionException {\r
55         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
56         Resource r = ResourceAdaptionUtils.toSingleResource(sel);\r
57         if(r == null) {\r
58                 FunctionsFolder mn = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class);\r
59                 r = mn.data;\r
60         }\r
61         if(r == null) return null;\r
62         \r
63         final Resource functionLibrary = r;\r
64         \r
65                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
66                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
67                 fd.setText("Import Function Library");\r
68                 fd.setFilterPath(Platform.getLocation().toOSString());\r
69                 String[] filterExt = {"*.tg"};\r
70                 fd.setFilterExtensions(filterExt);\r
71                 String selected = fd.open();\r
72                 if(selected == null) return null;\r
73 \r
74                 TransferableGraph1 tg = null;\r
75                 try {\r
76                         tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
77                 } catch (RuntimeBindingConstructionException e) {\r
78                         e.printStackTrace();\r
79                 } catch (IOException e) {\r
80                         e.printStackTrace();\r
81                 }\r
82                 if(tg == null) return null;\r
83 \r
84         \r
85                 try {\r
86                         Boolean hasSharedOntologies;\r
87                         hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
88 \r
89                                 @Override\r
90                                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
91                                         try {\r
92                                                 graph.getResource("http://SharedOntologies");\r
93                                         } catch (ResourceNotFoundException e) {\r
94                                                 return false;\r
95                                         }               \r
96                                         return true;\r
97                                 }\r
98                         });\r
99 \r
100                         if(!hasSharedOntologies) {\r
101                                 SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
102 \r
103                                         @Override\r
104                                         public void perform(WriteGraph graph) throws DatabaseException {\r
105                                                 Layer0 l0 = Layer0.getInstance(graph);\r
106                                                 GraphUtils.create2(graph, l0.Library, \r
107                                                                 l0.HasName, "SharedOntologies",\r
108                                                                 l0.PartOf, graph.getResource("http:/"));\r
109                                         }\r
110                                 });\r
111 \r
112                         }\r
113                 } catch (DatabaseException e) {\r
114                         e.printStackTrace();\r
115                         return null;\r
116                 }\r
117 \r
118                 \r
119                 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
120                 try {\r
121                         DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);\r
122                 } catch (Exception e) {\r
123                         e.printStackTrace();\r
124                 }\r
125                 \r
126                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
127                         \r
128                         @Override\r
129                         public void run(ReadGraph graph) throws DatabaseException {\r
130                                 FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
131                         }\r
132                 });\r
133                 return null;\r
134         }\r
135         \r
136         private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {\r
137 \r
138                 \r
139                 Resource model = null;\r
140                 \r
141                 public SysdynFunctionLibraryImportAdvisor(Resource library) {\r
142                         super(library);\r
143                 }\r
144                 \r
145                 @Override\r
146                 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
147                         if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
148                                 library = null;\r
149                                 try {\r
150                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
151                                         Layer0 l0 = Layer0.getInstance(graph);\r
152                                         Resource r = library;\r
153                                         while(!graph.isInstanceOf(r, sr.SysdynModel) && graph.isInstanceOf(r, l0.Ontology))\r
154                                                 r = graph.getSingleObject(r, l0.PartOf);\r
155                                         if(graph.isInstanceOf(r, sr.SysdynModel))\r
156                                                 model = r;\r
157                                         \r
158                                         library = graph.getResource("http://SharedOntologies");\r
159                                 } catch (ResourceNotFoundException e) {\r
160                                         e.printStackTrace();\r
161                                 }\r
162                         }\r
163                 }\r
164                 \r
165                 @Override\r
166                 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
167                         Layer0 l0 = graph.getService(Layer0.class);\r
168                         this.root = graph.newResource();\r
169                         graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);\r
170                         String name = root.name;\r
171                         String newName = nameMappings.get(name);\r
172                         graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
173                         if(model != null && root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
174                                 graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, this.root);\r
175                         }\r
176                         return this.root;\r
177 \r
178                 }\r
179                         \r
180         }\r
181 \r
182 }\r