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.exports;
\r
14 import java.io.FileOutputStream;
\r
15 import java.io.IOException;
\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
36 * Exports external function files from SysdynModelicaFunctions
\r
38 * @author Teemu Lempinen
\r
41 public class ExportExternalFunctionFilesHandler extends AbstractHandler {
\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
52 return exportFiles(shell, resources);
\r
56 * Exports selected file resources to files on disk. Assumes all resources are external files.
\r
58 * @param shell SWT Shell
\r
59 * @param resources External file resources
\r
62 public static Object exportFiles(Shell shell, final Resource[] resources) {
\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
74 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\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
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
87 } catch (IOException e) {
\r
88 e.printStackTrace();
\r