Merge commit '0b471805f017da83d715a0d8409f53bdd009d31e'
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ExportDocumentFile.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.document.ui.actions;\r
13 \r
14 import java.io.File;\r
15 \r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.widgets.Display;\r
18 import org.eclipse.swt.widgets.FileDialog;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.adapter.ActionFactory;\r
23 import org.simantics.db.request.Read;\r
24 import org.simantics.document.DocumentResource;\r
25 import org.simantics.graphfile.ontology.GraphFileResource;\r
26 import org.simantics.graphfile.util.GraphFileUtil;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.ui.SimanticsUI;\r
29 import org.simantics.utils.ui.ExceptionUtils;\r
30 \r
31 /**\r
32  * Action for exporting file based documents.\r
33  * \r
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
35  *\r
36  */\r
37 public class ExportDocumentFile implements ActionFactory {\r
38 \r
39         \r
40         @Override\r
41         public Runnable create(Object target) {\r
42 \r
43                 if(!(target instanceof Resource))\r
44                         return null;\r
45 \r
46                 final Resource resource = (Resource)target;\r
47 \r
48                 return new Runnable() {\r
49                         @Override\r
50                         public void run() {\r
51                                 try {\r
52                                         String name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
53                                                 public String perform(ReadGraph graph) throws DatabaseException {\r
54                                                         DocumentResource doc = DocumentResource.getInstance(graph);\r
55                                                         GraphFileResource gf = GraphFileResource.getInstance(graph);\r
56                                                         if (!graph.isInstanceOf(resource, doc.FileDocument))\r
57                                                                 return null;\r
58                                                         String resourceName = graph.getPossibleRelatedValue(resource, gf.HasResourceName);\r
59                                                         if (resourceName != null) {\r
60                                                                 return resourceName;\r
61                                                         } else {\r
62                                                                 Layer0 L0 = Layer0.getInstance(graph);\r
63                                                                 return graph.getPossibleRelatedValue(resource, L0.HasName);\r
64                                                         }\r
65                                                 };\r
66                                         });\r
67                                         \r
68                                         FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.SAVE);\r
69                                         dialog.setFileName(name);\r
70                                         final String filename = dialog.open();\r
71                                         if (filename == null)\r
72                                                 return;\r
73                                         exportDocument(resource, filename);\r
74                                 } catch (DatabaseException e) {\r
75                                         ExceptionUtils.logAndShowError("Cannot export document.", e);\r
76                                 }\r
77 \r
78                         }\r
79                 };\r
80         }\r
81         \r
82         public static void exportDocument(Resource document, String fileName) throws DatabaseException {\r
83             GraphFileUtil.writeDataToFile(document, new File(fileName));\r
84         }\r
85 }\r