X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Factions%2FExportDocumentFile.java;fp=bundles%2Forg.simantics.document.ui%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fui%2Factions%2FExportDocumentFile.java;h=c612f99bc6b7f53acd8a05b2fb52304f725f0c27;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFile.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFile.java new file mode 100644 index 000000000..c612f99bc --- /dev/null +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFile.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 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.document.ui.actions; + +import java.io.File; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.db.request.Read; +import org.simantics.document.DocumentResource; +import org.simantics.graphfile.ontology.GraphFileResource; +import org.simantics.graphfile.util.GraphFileUtil; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ExceptionUtils; + +/** + * Action for exporting file based documents. + * + * @author Marko Luukkainen + * + */ +public class ExportDocumentFile implements ActionFactory { + + + @Override + public Runnable create(Object target) { + + if(!(target instanceof Resource)) + return null; + + final Resource resource = (Resource)target; + + return new Runnable() { + @Override + public void run() { + try { + String resourceName = SimanticsUI.getSession().syncRequest(new Read() { + public String perform(ReadGraph graph) throws DatabaseException { + DocumentResource doc = DocumentResource.getInstance(graph); + GraphFileResource gf = GraphFileResource.getInstance(graph); + if (!graph.isInstanceOf(resource, doc.FileDocument)) + return null; + return graph.getPossibleRelatedValue(resource, gf.HasResourceName); + }; + }); + + FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE); + dialog.setFileName(resourceName); + final String filename = dialog.open(); + if (filename == null) + return; + exportDocument(resource, filename); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError("Cannot export document.", e); + } + + } + }; + } + + public static void exportDocument(Resource document, String fileName) throws DatabaseException { + GraphFileUtil.writeDataToFile(document, new File(fileName)); + } +}