1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.handlers.imports;
\r
14 import java.io.File;
\r
15 import java.io.FileInputStream;
\r
16 import java.io.IOException;
\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
38 * Imports external functions to SysdynModelicaFunctions using FileDialog.
\r
40 * @author Teemu Lempinen
\r
43 public class ImportExternalFunctionFilesHandler extends AbstractHandler {
\r
45 public static final String[] C_EXTENSIONS = {"*.c","*.h","*.a","*.o"};
\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
52 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
55 public void perform(WriteGraph graph) throws DatabaseException {
\r
56 // TODO: include files to database
\r
64 * Opens a {@link FileDialog} to select the imported files.
\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
71 public static Pair<String, String[]> importFiles(Shell shell, String text, String[] filter) {
\r
72 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
74 fd.setFilterPath(Platform.getLocation().toOSString());
\r
75 fd.setFilterExtensions(filter);
\r
77 return new Pair<String, String[]>(fd.getFilterPath(), fd.getFileNames());
\r
82 * Saves the given files as byte arrays to a function
\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
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
93 if(!graph.isInstanceOf(function, sr.SysdynModelicaFunction))
\r
96 for(String filename : files.second) {
\r
97 File file = new File(files.first, filename);
\r
99 String name = file.getName();
\r
101 Resource externalFile = GraphUtils.create2(graph,
\r
102 sr.ExternalFunctionFile,
\r
103 l0.PartOf, function,
\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
112 } catch (IOException e) {
\r
113 e.printStackTrace();
\r