1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 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.File;
\r
16 import org.eclipse.core.commands.AbstractHandler;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.core.runtime.Platform;
\r
20 import org.eclipse.jface.viewers.ISelection;
\r
21 import org.eclipse.swt.SWT;
\r
22 import org.eclipse.swt.widgets.FileDialog;
\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.primitiverequest.PossibleRelatedValue;
\r
29 import org.simantics.db.common.request.ReadRequest;
\r
30 import org.simantics.db.exception.DatabaseException;
\r
31 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
\r
32 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
\r
33 import org.simantics.db.request.Read;
\r
34 import org.simantics.graph.db.TransferableGraphSource;
\r
35 import org.simantics.graph.db.TransferableGraphs;
\r
36 import org.simantics.layer0.Layer0;
\r
37 import org.simantics.sysdyn.ui.Activator;
\r
38 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
\r
39 import org.simantics.ui.SimanticsUI;
\r
40 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
43 * Exports a function library
\r
45 * @author Teemu Lempinen
\r
46 * @author Tuomas Miettinen
\r
49 public class ExportFunctionLibrary extends AbstractHandler {
\r
52 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
54 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
55 final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);
\r
56 if(functionLibrary == null) return null;
\r
60 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
63 public String perform(ReadGraph graph) throws DatabaseException {
\r
64 if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))
\r
66 Layer0 l0 = Layer0.getInstance(graph);
\r
67 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
73 } catch (DatabaseException e1) {
\r
74 e1.printStackTrace();
\r
76 // Do not export if the resource has no name
\r
77 if(name == null) return null;
\r
79 // Find a location (and name) for the exported library using FileDialog
\r
80 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
81 FileDialog fd = new FileDialog(shell, SWT.SAVE);
\r
82 fd.setText("Export Function Library");
\r
83 fd.setFileName(name);
\r
84 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTFUNCTIONLIBRARYPATH);
\r
85 if(path.isEmpty() || !(new File(path).exists()))
\r
86 path = Platform.getLocation().toOSString();
\r
87 fd.setFilterPath(path);
\r
88 String[] filterExt = {"*.sysdynFunctions"};
\r
89 fd.setFilterExtensions(filterExt);
\r
90 fd.setOverwrite(true);
\r
91 final String selected = fd.open();
\r
92 if(selected == null) return null;
\r
94 // Save location to preference store
\r
95 Activator.getDefault().getPreferenceStore().setValue(ImportUtilsUI.IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent());
\r
97 // Asynchronously create the file using transferable graph
\r
98 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
101 public void run(ReadGraph graph) throws DatabaseException {
\r
102 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, functionLibrary);
\r
103 TransferableGraphSource s = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));
\r
105 TransferableGraphs.writeTransferableGraph(graph, "sysdynFunctionLibrary", 1, s,new File(selected));
\r
106 } catch (Exception e) {
\r
107 e.printStackTrace();
\r