]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
53b012c59286fff2800dec430734ae2fcec7daa1
[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.exports;\r
13 \r
14 import java.io.FileOutputStream;\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.widgets.DirectoryDialog;\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.request.ReadRequest;\r
29 import org.simantics.db.common.utils.NameUtils;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.sysdyn.SysdynResource;\r
32 import org.simantics.ui.SimanticsUI;\r
33 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
34 \r
35 /**\r
36  * Exports external function files from SysdynModelicaFunctions\r
37  * \r
38  * @author Teemu Lempinen\r
39  *\r
40  */\r
41 public class ExportExternalFunctionFilesHandler extends AbstractHandler {\r
42 \r
43         @Override\r
44         public Object execute(ExecutionEvent event) throws ExecutionException {\r
45             // Find shell and resources to be exported\r
46                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
47                 ISelection sel = HandlerUtil.getCurrentSelection(event);\r
48                 final Resource[] resources = ResourceAdaptionUtils.toResources(sel);\r
49                 if (resources.length < 1)\r
50                         return null;\r
51 \r
52                 return exportFiles(shell, resources);\r
53         }\r
54         \r
55         /**\r
56          * Exports selected file resources to files on disk. Assumes all resources are external files.\r
57          * \r
58          * @param shell SWT Shell\r
59          * @param resources External file resources\r
60          * @return null\r
61          */\r
62         public static Object exportFiles(Shell shell, final Resource[] resources) {\r
63             \r
64             // Select a path where to export the files\r
65                 DirectoryDialog dd = new DirectoryDialog(shell);\r
66                 dd.setFilterPath(Platform.getLocation().toOSString());\r
67                 dd.setText("Export files to...");\r
68                 dd.setMessage("Select a directory");\r
69                 final String dir = dd.open();\r
70                 if (dir == null) {\r
71                         return null;\r
72                 }\r
73 \r
74                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
75 \r
76                         @Override\r
77                         public void run(ReadGraph graph) throws DatabaseException {\r
78                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
79                                 // Get byte arrays from each resource and write them to disk\r
80                                 for(Resource r : resources) {\r
81                                         try {\r
82                                                 String name = NameUtils.getSafeName(graph, r);\r
83                                                 FileOutputStream fos = new FileOutputStream(dir + "\\" + name);\r
84                                                 byte[] fileBArray = graph.getPossibleRelatedValue(r, sr.HasExternalFile, Bindings.BYTE_ARRAY);\r
85                                                 fos.write(fileBArray);\r
86                                                 fos.close();\r
87                                         } catch (IOException e) {\r
88                                                 e.printStackTrace();\r
89                                         }\r
90                                 }\r
91                                 \r
92                         }\r
93                 });\r
94                 return null;\r
95         }\r
96 \r
97 }\r