1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.document.ui.actions;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.FileDialog;
19 import org.simantics.Simantics;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.ActionFactory;
24 import org.simantics.db.request.Read;
25 import org.simantics.document.DocumentResource;
26 import org.simantics.graphfile.ontology.GraphFileResource;
27 import org.simantics.graphfile.util.GraphFileUtil;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.utils.ui.ExceptionUtils;
32 * Action for exporting file based documents.
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class ExportDocumentFile implements ActionFactory {
41 public Runnable create(Object target) {
43 if(!(target instanceof Resource))
46 final Resource resource = (Resource)target;
48 return new Runnable() {
52 String name = Simantics.getSession().syncRequest(new Read<String>() {
53 public String perform(ReadGraph graph) throws DatabaseException {
54 DocumentResource doc = DocumentResource.getInstance(graph);
55 GraphFileResource gf = GraphFileResource.getInstance(graph);
56 if (!graph.isInstanceOf(resource, doc.FileDocument))
58 String resourceName = graph.getPossibleRelatedValue(resource, gf.HasResourceName);
59 if (resourceName != null) {
62 Layer0 L0 = Layer0.getInstance(graph);
63 return graph.getPossibleRelatedValue(resource, L0.HasName);
68 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE);
69 dialog.setFileName(name);
70 final String filename = dialog.open();
73 exportDocument(resource, filename);
74 } catch (DatabaseException e) {
75 ExceptionUtils.logAndShowError(Messages.ExportDocumentFile_CannotExportDocument, e);
82 public static void exportDocument(Resource document, String fileName) throws DatabaseException {
83 GraphFileUtil.writeDataToFile(document, new File(fileName));