1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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
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.sysdyn.ui.Activator;
\r
40 import org.simantics.sysdyn.ui.handlers.imports.ImportModelHandler;
\r
41 import org.simantics.ui.SimanticsUI;
\r
42 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
43 import org.simantics.utils.datastructures.Pair;
\r
46 * Exports a selected model
\r
48 * @author Teemu Lempinen
\r
51 public class ExportModelHandler extends AbstractHandler {
\r
54 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
55 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
56 final Resource model = ResourceAdaptionUtils.toSingleResource(sel);
\r
57 if(model == null) return null;
\r
59 // FIXME: Model browser doesn't change its selection even if the selected object is removed,
\r
60 // so you can try to export a removed model
\r
63 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
66 public String perform(ReadGraph graph) throws DatabaseException {
\r
67 if (!graph.hasStatement(model, Layer0.getInstance(graph).PartOf))
\r
69 Layer0 l0 = Layer0.getInstance(graph);
\r
70 String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, Bindings.STRING ));
\r
76 } catch (DatabaseException e1) {
\r
77 e1.printStackTrace();
\r
79 // Do not export if the resource has no name
\r
80 if(name == null) return null;
\r
82 // Find a location (and name) for the exported library using FileDialog
\r
83 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
84 FileDialog fd = new FileDialog(shell, SWT.SAVE);
\r
85 fd.setText("Export Model");
\r
86 fd.setFileName(name);
\r
87 String path = Activator.getDefault().getPreferenceStore().getString(ImportModelHandler.IMPORTMODELTPATH);
\r
88 if(path.isEmpty() || !(new File(path).exists()))
\r
89 path = Platform.getLocation().toOSString();
\r
90 fd.setFilterPath(path);
\r
91 String[] filterExt = {"*.tg"};
\r
92 fd.setFilterExtensions(filterExt);
\r
93 final String selected = fd.open();
\r
94 if(selected == null) return null;
\r
96 // Save location to preference store
\r
97 Activator.getDefault().getPreferenceStore().setValue(ImportModelHandler.IMPORTMODELTPATH, (new File(selected)).getParent());
\r
99 // Asynchronously create the file using transferable graph
\r
100 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
103 public void run(ReadGraph graph) throws DatabaseException {
\r
104 Layer0 l0 = Layer0.getInstance(graph);
\r
105 String name = graph.syncRequest(new PossibleRelatedValue<String>(model, l0.HasName, Bindings.STRING ));
\r
106 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
107 roots.add(Pair.make(model, name));
\r
108 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, model));
\r
111 Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
112 } catch (RuntimeBindingConstructionException e) {
\r
113 e.printStackTrace();
\r
114 } catch (IOException e) {
\r
115 e.printStackTrace();
\r