1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.adapters;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.FileDialog;
19 import org.eclipse.swt.widgets.Shell;
20 import org.simantics.Simantics;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.Session;
25 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.common.utils.Logger;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.adapter.ActionFactory;
30 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
31 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
32 import org.simantics.graph.db.TransferableGraphSource;
33 import org.simantics.graph.db.TransferableGraphs;
34 import org.simantics.layer0.Layer0;
36 public class ExportModelActionFactory implements ActionFactory {
41 public ExportModelActionFactory(String extension, String format) {
42 this.extension = extension;
47 public Runnable create(Object target_) {
48 final Resource target = (Resource)target_;
49 return new Runnable() {
53 Shell shell = Display.getDefault().getActiveShell();
55 Session session = Simantics.getSession();
59 final Layer0 L0 = Layer0.getInstance(session);
61 final String name = session.syncRequest(new PossibleRelatedValue<String>(target, L0.HasName, Bindings.STRING));
63 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
64 dialog.setFilterExtensions(new String[] { "*." + extension });
65 dialog.setFileName(name + "." + extension);
66 String newFileName = dialog.open();
67 if(newFileName == null) return;
69 if(!newFileName.endsWith(extension))
70 newFileName += "." + extension;
72 try (TransferableGraphSource s = session.sync(new ModelTransferableGraphSourceRequest(createConf(session, target)))) {
73 TransferableGraphs.writeTransferableGraph(session, format, 1, s, new File(newFileName));
76 } catch (Exception e) {
77 Logger.defaultLogError(e);
84 private TransferableGraphConfiguration2 createConf(Session session, final Resource model) throws DatabaseException {
85 return session.sync(new UniqueRead<TransferableGraphConfiguration2>() {
87 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
88 return new TransferableGraphConfiguration2(graph, model, true, false);