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