]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ExportDocumentFile.java
Externalize strings in org.simantics.document.ui
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ExportDocumentFile.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.ui.actions;
13
14 import java.io.File;
15
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;
30
31 /**
32  * Action for exporting file based documents.
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class ExportDocumentFile implements ActionFactory {
38
39         
40         @Override
41         public Runnable create(Object target) {
42
43                 if(!(target instanceof Resource))
44                         return null;
45
46                 final Resource resource = (Resource)target;
47
48                 return new Runnable() {
49                         @Override
50                         public void run() {
51                                 try {
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))
57                                                                 return null;
58                                                         String resourceName = graph.getPossibleRelatedValue(resource, gf.HasResourceName);
59                                                         if (resourceName != null) {
60                                                                 return resourceName;
61                                                         } else {
62                                                                 Layer0 L0 = Layer0.getInstance(graph);
63                                                                 return graph.getPossibleRelatedValue(resource, L0.HasName);
64                                                         }
65                                                 };
66                                         });
67                                         
68                                         FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE);
69                                         dialog.setFileName(name);
70                                         final String filename = dialog.open();
71                                         if (filename == null)
72                                                 return;
73                                         exportDocument(resource, filename);
74                                 } catch (DatabaseException e) {
75                                         ExceptionUtils.logAndShowError(Messages.ExportDocumentFile_CannotExportDocument, e);
76                                 }
77
78                         }
79                 };
80         }
81         
82         public static void exportDocument(Resource document, String fileName) throws DatabaseException {
83             GraphFileUtil.writeDataToFile(document, new File(fileName));
84         }
85 }