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;h=652476d907058187e8593ea407587a108fa6d002;hb=e88be95edf1f80781646cfdf717ec1b663264179;hp=c612f99bc6b7f53acd8a05b2fb52304f725f0c27;hpb=969bd23cab98a79ca9101af33334000879fb60c5;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 index c612f99bc..652476d90 100644 --- 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 @@ -1,78 +1,85 @@ -/******************************************************************************* - * 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)); - } -} +/******************************************************************************* + * 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.Simantics; +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.layer0.Layer0; +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 name = Simantics.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; + String resourceName = graph.getPossibleRelatedValue(resource, gf.HasResourceName); + if (resourceName != null) { + return resourceName; + } else { + Layer0 L0 = Layer0.getInstance(graph); + return graph.getPossibleRelatedValue(resource, L0.HasName); + } + }; + }); + + FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE); + dialog.setFileName(name); + 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)); + } +}