]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
50277c1ccd56fb8daea0fcc0c9bc3942a2353248
[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.imports;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileInputStream;\r
16 import java.io.IOException;\r
17 \r
18 import org.eclipse.core.commands.AbstractHandler;\r
19 import org.eclipse.core.commands.ExecutionEvent;\r
20 import org.eclipse.core.commands.ExecutionException;\r
21 import org.eclipse.core.runtime.Platform;\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.db.Resource;\r
28 import org.simantics.db.WriteGraph;\r
29 import org.simantics.db.common.request.WriteRequest;\r
30 import org.simantics.db.exception.DatabaseException;\r
31 import org.simantics.layer0.Layer0;\r
32 import org.simantics.layer0.utils.direct.GraphUtils;\r
33 import org.simantics.sysdyn.SysdynResource;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.utils.datastructures.Pair;\r
36 \r
37 /**\r
38  * Imports external functions to SysdynModelicaFunctions using FileDialog.\r
39  * \r
40  * @author Teemu Lempinen\r
41  *\r
42  */\r
43 public class ImportExternalFunctionFilesHandler extends AbstractHandler {\r
44 \r
45         public static final String[] C_EXTENSIONS = {"*.c","*.h","*.a","*.o"};\r
46         @Override\r
47         public Object execute(ExecutionEvent event) throws ExecutionException {\r
48                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
49                 Pair<String, String[]> selected = importFiles(shell, "Import...", C_EXTENSIONS);\r
50                 if(selected.second == null || selected.second.length < 1) return null;\r
51 \r
52                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
53                         \r
54                         @Override\r
55                         public void perform(WriteGraph graph) throws DatabaseException {\r
56                                 // TODO: include files to database\r
57                         }\r
58                 });\r
59                 \r
60                 return null;\r
61         }\r
62         \r
63         /**\r
64          * Opens a {@link FileDialog} to select the imported files.\r
65          *  \r
66          * @param shell SWT Shell\r
67          * @param text Header text for the FileDialog\r
68          * @param filter Filters for the FileDialog\r
69          * @return File names and paths for the files to be imported\r
70          */\r
71         public static Pair<String, String[]> importFiles(Shell shell, String text, String[] filter) {\r
72                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
73                 fd.setText(text);\r
74                 fd.setFilterPath(Platform.getLocation().toOSString());\r
75                 fd.setFilterExtensions(filter);\r
76                 fd.open();\r
77                 return new Pair<String, String[]>(fd.getFilterPath(), fd.getFileNames());\r
78         }\r
79         \r
80         \r
81         /**\r
82          * Saves the given files as byte arrays to a function\r
83          * \r
84          * @param graph WriteGraph\r
85          * @param function Function where the files are to be added\r
86          * @param files Files to be added\r
87          * @throws DatabaseException\r
88          */\r
89         public static void addFilesToFunction(WriteGraph graph, Resource function, Pair<String, String[]> files) throws DatabaseException {\r
90                 SysdynResource sr = SysdynResource.getInstance(graph);\r
91                 Layer0 l0 = Layer0.getInstance(graph);\r
92                 \r
93                 if(!graph.isInstanceOf(function, sr.SysdynModelicaFunction))\r
94                         return;\r
95 \r
96                 for(String filename : files.second) {\r
97                         File file = new File(files.first, filename);\r
98                                         \r
99                         String name = file.getName();\r
100                         \r
101                         Resource externalFile = GraphUtils.create2(graph, \r
102                                         sr.ExternalFunctionFile,\r
103                                         l0.PartOf, function,\r
104                                         l0.HasName, name);\r
105                         \r
106                         try {\r
107                                 byte[] fileBArray = new byte[(int)file.length()];\r
108                                 FileInputStream fis = new FileInputStream(file);\r
109                                 fis.read(fileBArray);\r
110                                 graph.claimLiteral(externalFile, sr.ExternalFunctionFile_externalFile, fileBArray, Bindings.BYTE_ARRAY);\r
111                                 fis.close();\r
112                         } catch (IOException e) {\r
113                                 e.printStackTrace();\r
114                         }\r
115 \r
116                 }\r
117                 \r
118         }\r
119 \r
120 }\r