X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fadapters%2FExportModelActionFactory.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fadapters%2FExportModelActionFactory.java;h=f3a15f69626ea85acbb0ec480199693195024a35;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java new file mode 100644 index 000000000..f3a15f696 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.adapters; + +import java.io.File; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.primitiverequest.PossibleRelatedValue; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2; +import org.simantics.graph.db.TransferableGraphSource; +import org.simantics.graph.db.TransferableGraphs; +import org.simantics.layer0.Layer0; + +public class ExportModelActionFactory implements ActionFactory { + + String format; + String extension; + + public ExportModelActionFactory(String extension, String format) { + this.extension = extension; + this.format = format; + } + + @Override + public Runnable create(Object target_) { + final Resource target = (Resource)target_; + return new Runnable() { + @Override + public void run() { + + Shell shell = Display.getDefault().getActiveShell(); + + Session session = Simantics.getSession(); + + try { + + final Layer0 L0 = Layer0.getInstance(session); + + final String name = session.syncRequest(new PossibleRelatedValue(target, L0.HasName, Bindings.STRING)); + + FileDialog dialog = new FileDialog(shell, SWT.SAVE); + dialog.setFilterExtensions(new String[] { "*." + extension }); + dialog.setFileName(name + "." + extension); + String newFileName = dialog.open(); + if(newFileName == null) return; + + if(!newFileName.endsWith(extension)) + newFileName += "." + extension; + + try (TransferableGraphSource s = session.sync(new ModelTransferableGraphSourceRequest(createConf(session, target)))) { + TransferableGraphs.writeTransferableGraph(session, format, 1, s, new File(newFileName)); + } + + } catch (Exception e) { + Logger.defaultLogError(e); + } + + } + }; + } + + private TransferableGraphConfiguration2 createConf(Session session, final Resource model) throws DatabaseException { + return session.sync(new UniqueRead() { + @Override + public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException { + return new TransferableGraphConfiguration2(graph, model, true, false); + } + }); + } + +}