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;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.util.ArrayList;
\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.jface.viewers.ISelection;
\r
23 import org.eclipse.swt.SWT;
\r
24 import org.eclipse.swt.widgets.FileDialog;
\r
25 import org.eclipse.swt.widgets.Shell;
\r
26 import org.eclipse.ui.handlers.HandlerUtil;
\r
27 import org.simantics.databoard.Bindings;
\r
28 import org.simantics.databoard.Files;
\r
29 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
\r
30 import org.simantics.db.ReadGraph;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
\r
33 import org.simantics.db.common.request.ReadRequest;
\r
34 import org.simantics.db.exception.DatabaseException;
\r
35 import org.simantics.db.layer0.util.TransferableGraphRequest2;
\r
36 import org.simantics.db.request.Read;
\r
37 import org.simantics.graph.representation.TransferableGraph1;
\r
38 import org.simantics.layer0.Layer0;
\r
39 import org.simantics.ui.SimanticsUI;
\r
40 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
41 import org.simantics.utils.datastructures.Pair;
\r
43 public class ExportFunctionLibrary extends AbstractHandler {
\r
46 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
48 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
49 final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);
\r
50 if(functionLibrary == null) return null;
\r
54 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
57 public String perform(ReadGraph graph) throws DatabaseException {
\r
58 if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))
\r
60 Layer0 l0 = Layer0.getInstance(graph);
\r
61 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
67 } catch (DatabaseException e1) {
\r
68 e1.printStackTrace();
\r
70 if(name == null) return null;
\r
72 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
73 FileDialog fd = new FileDialog(shell, SWT.SAVE);
\r
74 fd.setText("Export..");
\r
75 fd.setFileName(name);
\r
76 fd.setFilterPath(Platform.getLocation().toOSString());
\r
77 String[] filterExt = {"*.tg"};
\r
78 fd.setFilterExtensions(filterExt);
\r
79 final String selected = fd.open();
\r
80 if(selected == null) return null;
\r
83 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
86 public void run(ReadGraph graph) throws DatabaseException {
\r
87 Layer0 l0 = Layer0.getInstance(graph);
\r
88 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
89 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
90 roots.add(Pair.make(functionLibrary, name));
\r
91 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));
\r
94 Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
95 } catch (RuntimeBindingConstructionException e) {
\r
96 e.printStackTrace();
\r
97 } catch (IOException e) {
\r
98 e.printStackTrace();
\r