]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocument.java
Sync git svn branch with SVN repository r33345.
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ImportDocument.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.core.runtime.IProgressMonitor;\r
17 import org.eclipse.core.runtime.IStatus;\r
18 import org.eclipse.core.runtime.Status;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.widgets.Display;\r
21 import org.eclipse.swt.widgets.FileDialog;\r
22 import org.simantics.DatabaseJob;\r
23 import org.simantics.Simantics;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.WriteGraph;\r
27 import org.simantics.db.common.request.WriteRequest;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.document.ui.Activator;\r
30 import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
31 \r
32 /**\r
33  * Action for importing files as documents.\r
34  * \r
35  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
36  *\r
37  */\r
38 public class ImportDocument extends AddDocumentAction {\r
39         \r
40 \r
41         public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {\r
42                 super(graph, relationUri);\r
43         }\r
44 \r
45         @Override\r
46         public Runnable create(Object target) {\r
47                 if(!(target instanceof Resource))\r
48                         return null;\r
49                 final Resource resource = (Resource)target;\r
50                 return new Runnable() {\r
51                         \r
52                         @Override\r
53                         public void run() {\r
54                                 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN | SWT.MULTI);\r
55                                 // TODO : is there any way to read file/executable bindings from OS?\r
56                                 //        if is, use those extensions to filter this list.\r
57                                 //        note: in windows using "reg query ..." to read bindings form registry would work.\r
58                                 // Note : If the above mentioned filtering is implemented it should be made optional / configurable.\r
59                                 dialog.setFilterExtensions(new String[]{"*.*"});\r
60                                 if (dialog.open() == null) return;\r
61 \r
62                                 String filterPath = dialog.getFilterPath();\r
63                                 String[] filenames = dialog.getFileNames();\r
64                                 \r
65                                 ImportJob job = new ImportJob(filenames.length > 1 ? "Import files" : "Import file", resource, filterPath, filenames);\r
66                                 job.setUser(true);\r
67                                 job.schedule();\r
68                         }\r
69                 };\r
70         }\r
71         \r
72         private class ImportJob extends DatabaseJob {\r
73 \r
74                 public ImportJob(String name, Resource resource, String path, String[] filenames) {\r
75                         super(name);\r
76                         this.resource = resource;\r
77                         this.path = path;\r
78                         this.filenames = filenames;\r
79                 }\r
80 \r
81                 Resource resource;\r
82                 String path;\r
83                 String[] filenames;\r
84 \r
85                 @Override\r
86                 protected IStatus run(final IProgressMonitor monitor) {\r
87                         monitor.beginTask("Importing...", filenames.length);\r
88                         try {\r
89                                 Simantics.getSession().syncRequest(new WriteRequest() {\r
90                                         @Override\r
91                                         public void perform(WriteGraph graph) throws DatabaseException {\r
92                                                 try {\r
93                                                         graph.markUndoPoint();\r
94                                                     for (String filename : filenames) {\r
95                                                         File f = new File(path, filename);\r
96                                                         Resource newDoc = FileDocumentUtil.importFileWithName(graph, f.getAbsolutePath());\r
97                                                         linkDocument(graph, resource, newDoc);\r
98                                                             monitor.worked(1);\r
99                                                     }\r
100                                                 } catch (Exception e) {\r
101                                                         throw new DatabaseException(e);\r
102                                                 }\r
103                                         }\r
104                                 });\r
105                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Import succesful.");\r
106                         } catch (DatabaseException e) {\r
107                                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed.", e);\r
108                         } finally {\r
109                                 monitor.done();\r
110                         }\r
111                 }\r
112         }\r
113         \r
114 }\r