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