]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
251ab7a452c73e280c9a5c412a478b8b1c52b571
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 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.exports;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.core.commands.AbstractHandler;\r
17 import org.eclipse.core.commands.ExecutionEvent;\r
18 import org.eclipse.core.commands.ExecutionException;\r
19 import org.eclipse.core.runtime.Platform;\r
20 import org.eclipse.jface.viewers.ISelection;\r
21 import org.eclipse.swt.SWT;\r
22 import org.eclipse.swt.widgets.FileDialog;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 import org.eclipse.ui.handlers.HandlerUtil;\r
25 import org.simantics.databoard.Bindings;\r
26 import org.simantics.db.ReadGraph;\r
27 import org.simantics.db.Resource;\r
28 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
29 import org.simantics.db.common.request.ReadRequest;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
32 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
33 import org.simantics.db.request.Read;\r
34 import org.simantics.graph.db.TransferableGraphSource;\r
35 import org.simantics.graph.db.TransferableGraphs;\r
36 import org.simantics.layer0.Layer0;\r
37 import org.simantics.sysdyn.ui.Activator;\r
38 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;\r
39 import org.simantics.ui.SimanticsUI;\r
40 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
41 \r
42 /**\r
43  * Exports a function library\r
44  * \r
45  * @author Teemu Lempinen\r
46  * @author Tuomas Miettinen\r
47  *\r
48  */\r
49 public class ExportFunctionLibrary  extends AbstractHandler {\r
50 \r
51         @Override\r
52         public Object execute(ExecutionEvent event) throws ExecutionException {\r
53 \r
54         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
55         final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);\r
56         if(functionLibrary == null) return null;\r
57                 \r
58                 String name = null;\r
59                 try {\r
60                         name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
61 \r
62                                 @Override\r
63                                 public String perform(ReadGraph graph) throws DatabaseException {\r
64                                         if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))\r
65                                                 return null;\r
66                                         Layer0 l0 = Layer0.getInstance(graph);\r
67                                         String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
68                                         return name;\r
69                                         \r
70                                 }\r
71                                 \r
72                         });\r
73                 } catch (DatabaseException e1) {\r
74                         e1.printStackTrace();\r
75                 }\r
76                 // Do not export if the resource has no name\r
77                 if(name == null) return null;\r
78                 \r
79                 // Find a location (and name) for the exported library using FileDialog\r
80                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
81                 FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
82                 fd.setText("Export Function Library");\r
83                 fd.setFileName(name);\r
84                 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTFUNCTIONLIBRARYPATH);\r
85                 if(path.isEmpty() || !(new File(path).exists()))\r
86                         path = Platform.getLocation().toOSString();\r
87                 fd.setFilterPath(path);\r
88                 String[] filterExt = {"*.sysdynFunctions"};\r
89                 fd.setFilterExtensions(filterExt);\r
90                 fd.setOverwrite(true);\r
91                 final String selected = fd.open();\r
92                 if(selected == null) return null;\r
93                 \r
94                 // Save location to preference store\r
95                 Activator.getDefault().getPreferenceStore().setValue(ImportUtilsUI.IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent());\r
96 \r
97                 // Asynchronously create the file using transferable graph\r
98                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
99 \r
100                     @Override\r
101                     public void run(ReadGraph graph) throws DatabaseException {\r
102                         TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, functionLibrary);\r
103                         TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
104                         try {\r
105                             TransferableGraphs.writeTransferableGraph(graph, "sysdynFunctionLibrary", 1, s,new File(selected));\r
106                         } catch (Exception e) {\r
107                             e.printStackTrace();\r
108                         }\r
109                     }\r
110                 });\r
111 \r
112                 return null;\r
113         }\r
114 \r
115 }\r