Merge "InputStream returns -1 on EOF instead of throwing IOException"
[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 org.eclipse.swt.SWT;\r
15 import org.eclipse.swt.widgets.Display;\r
16 import org.eclipse.swt.widgets.FileDialog;\r
17 import org.simantics.Simantics;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.request.WriteRequest;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
24 import org.simantics.utils.datastructures.Callback;\r
25 import org.simantics.utils.ui.ExceptionUtils;\r
26 \r
27 /**\r
28  * Action for importing files as documents.\r
29  * \r
30  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
31  *\r
32  */\r
33 public class ImportDocument extends AddDocumentAction {\r
34         \r
35 \r
36         public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {\r
37                 super(graph, relationUri);\r
38         }\r
39 \r
40         @Override\r
41         public Runnable create(Object target) {\r
42                 if(!(target instanceof Resource))\r
43                         return null;\r
44                 final Resource resource = (Resource)target;\r
45                 return new Runnable() {\r
46                         \r
47                         @Override\r
48                         public void run() {\r
49                                 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);\r
50                                 // TODO : is there any way to read file/executable bindings from OS?\r
51                                 //        if is, use those extensions to filter this list.\r
52                                 //        note: in windows using "reg query ..." to read bindings form registry would work.\r
53                                 dialog.setFilterExtensions(new String[]{"*.*"});\r
54                                 final String filename = dialog.open();\r
55                                 if (filename == null) {\r
56                                         return;\r
57                                 }\r
58                                 Simantics.getSession().asyncRequest(new WriteRequest() {\r
59                                         @Override\r
60                                         public void perform(WriteGraph graph) throws DatabaseException {\r
61                                             graph.markUndoPoint();\r
62                                             \r
63                                                 Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);\r
64                                                 linkDocument(graph, resource, newDoc);\r
65                                         }\r
66                                 },new Callback<DatabaseException>() {                           \r
67                                         @Override\r
68                                         public void run(DatabaseException parameter) {\r
69                                                 if (parameter != null) {\r
70                                                         ExceptionUtils.logAndShowError("Cannot import document.", parameter);\r
71                                                 }\r
72                                                 \r
73                                         }\r
74                                 });\r
75                         }\r
76                 };\r
77         }\r
78         \r
79 }\r