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