]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2115825793ce2aacffd0092793bed779b80e8b33
[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.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 public class ImportExternalFunctionFilesHandler extends AbstractHandler {\r
39 \r
40         public static final String[] C_EXTENSIONS = {"*.c","*.h","*.a","*.o"};\r
41         @Override\r
42         public Object execute(ExecutionEvent event) throws ExecutionException {\r
43                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
44                 Pair<String, String[]> selected = importFiles(shell, "Import...", C_EXTENSIONS);\r
45                 if(selected.second == null || selected.second.length < 1) return null;\r
46 \r
47                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
48                         \r
49                         @Override\r
50                         public void perform(WriteGraph graph) throws DatabaseException {\r
51                                 // TODO: include files to database\r
52                         }\r
53                 });\r
54                 \r
55                 return null;\r
56         }\r
57         \r
58         /**\r
59          * Import \r
60          * \r
61          * @param shell\r
62          * @return\r
63          */\r
64         public static Pair<String, String[]> importFiles(Shell shell, String text, String[] filter) {\r
65                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
66                 fd.setText(text);\r
67                 fd.setFilterPath(Platform.getLocation().toOSString());\r
68                 fd.setFilterExtensions(filter);\r
69                 fd.open();\r
70                 return new Pair<String, String[]>(fd.getFilterPath(), fd.getFileNames());\r
71         }\r
72         \r
73         public static void addFilesToFunction(WriteGraph graph, Resource function, Pair<String, String[]> files) throws DatabaseException {\r
74                 SysdynResource sr = SysdynResource.getInstance(graph);\r
75                 Layer0 l0 = Layer0.getInstance(graph);\r
76                 \r
77                 if(!graph.isInstanceOf(function, sr.SysdynModelicaFunction))\r
78                         return;\r
79 \r
80                 for(String filename : files.second) {\r
81                         File file = new File(files.first, filename);\r
82                                         \r
83                         String name = file.getName();\r
84                         \r
85                         Resource externalFile = GraphUtils.create2(graph, \r
86                                         sr.ExternalFunctionFile,\r
87                                         l0.PartOf, function,\r
88                                         l0.HasName, name);\r
89                         \r
90                         try {\r
91                                 byte[] fileBArray = new byte[(int)file.length()];\r
92                                 FileInputStream fis = new FileInputStream(file);\r
93                                 fis.read(fileBArray);\r
94                                 graph.claimLiteral(externalFile, sr.HasExternalFile, fileBArray, Bindings.BYTE_ARRAY);\r
95                                 \r
96                                 FunctionUtils.createExternalFunctionFile(graph, externalFile);\r
97                         } catch (IOException e) {\r
98                                 e.printStackTrace();\r
99                         }\r
100 \r
101                 }\r
102                 \r
103         }\r
104 \r
105 }\r