]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
bf16f6c9c20b9ebd6f44fcae801f14b9d3d15a07
[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.ui.SimanticsUI;\r
47 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
48 \r
49 public class ImportFunctionLibrary  extends AbstractHandler {\r
50 \r
51         @Override\r
52         public Object execute(ExecutionEvent event) throws ExecutionException {\r
53                 Resource project = SimanticsUI.getProject().get();\r
54                 if(project == null) return null;\r
55 \r
56         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
57         final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);\r
58         if(functionLibrary == null) return null;\r
59         \r
60                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
61                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
62                 fd.setText("Import Function Library");\r
63                 fd.setFilterPath(Platform.getLocation().toOSString());\r
64                 String[] filterExt = {"*.tg"};\r
65                 fd.setFilterExtensions(filterExt);\r
66                 String selected = fd.open();\r
67                 if(selected == null) return null;\r
68 \r
69                 TransferableGraph1 tg = null;\r
70                 try {\r
71                         tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
72                 } catch (RuntimeBindingConstructionException e) {\r
73                         e.printStackTrace();\r
74                 } catch (IOException e) {\r
75                         e.printStackTrace();\r
76                 }\r
77                 if(tg == null) return null;\r
78 \r
79         \r
80                 try {\r
81                         Boolean hasSharedOntologies;\r
82                         hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
83 \r
84                                 @Override\r
85                                 public Boolean perform(ReadGraph graph) throws DatabaseException {\r
86                                         try {\r
87                                                 graph.getResource("http://SharedOntologies");\r
88                                         } catch (ResourceNotFoundException e) {\r
89                                                 return false;\r
90                                         }               \r
91                                         return true;\r
92                                 }\r
93                         });\r
94 \r
95                         if(!hasSharedOntologies) {\r
96                                 SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
97 \r
98                                         @Override\r
99                                         public void perform(WriteGraph graph) throws DatabaseException {\r
100                                                 Layer0 l0 = Layer0.getInstance(graph);\r
101                                                 GraphUtils.create2(graph, l0.Library, \r
102                                                                 l0.HasName, "SharedOntologies",\r
103                                                                 l0.PartOf, graph.getResource("http:/"));\r
104                                         }\r
105                                 });\r
106 \r
107                         }\r
108                 } catch (DatabaseException e) {\r
109                         e.printStackTrace();\r
110                         return null;\r
111                 }\r
112 \r
113                 \r
114                 SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
115                 try {\r
116                         DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);\r
117                 } catch (Exception e) {\r
118                         e.printStackTrace();\r
119                 }\r
120                 \r
121                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
122                         \r
123                         @Override\r
124                         public void run(ReadGraph graph) throws DatabaseException {\r
125                                 FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
126                         }\r
127                 });\r
128                 return null;\r
129         }\r
130         \r
131         private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {\r
132 \r
133                 \r
134                 Resource model = null;\r
135                 \r
136                 public SysdynFunctionLibraryImportAdvisor(Resource library) {\r
137                         super(library);\r
138                 }\r
139                 \r
140                 @Override\r
141                 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
142                         if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
143                                 library = null;\r
144                                 try {\r
145                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
146                                         Layer0 l0 = Layer0.getInstance(graph);\r
147                                         Resource r = library;\r
148                                         while(!graph.isInstanceOf(r, sr.SysdynModel) && graph.isInstanceOf(r, l0.Ontology))\r
149                                                 r = graph.getSingleObject(r, l0.PartOf);\r
150                                         if(graph.isInstanceOf(r, sr.SysdynModel))\r
151                                                 model = r;\r
152                                         \r
153                                         library = graph.getResource("http://SharedOntologies");\r
154                                 } catch (ResourceNotFoundException e) {\r
155                                         e.printStackTrace();\r
156                                 }\r
157                         }\r
158                 }\r
159                 \r
160                 @Override\r
161                 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
162                         Layer0 l0 = graph.getService(Layer0.class);\r
163                         this.root = graph.newResource();\r
164                         graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root);\r
165                         String name = root.name;\r
166                         String newName = nameMappings.get(name);\r
167                         graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
168                         if(model != null && root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
169                                 graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, this.root);\r
170                         }\r
171                         return this.root;\r
172 \r
173                 }\r
174                         \r
175         }\r
176 \r
177 }\r