]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocumentFolder.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ImportDocumentFolder.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 org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.DirectoryDialog;
19 import org.eclipse.swt.widgets.Display;
20 import org.simantics.DatabaseJob;
21 import org.simantics.Simantics;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.adapter.ActionFactory;
28 import org.simantics.document.FileDocumentUtil;
29 import org.simantics.document.ui.Activator;
30
31 /**
32  * Action for importing files as documents.
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class ImportDocumentFolder implements ActionFactory {
38         Resource folderType;
39         Resource relation;
40
41         public ImportDocumentFolder(ReadGraph graph, String folderTypeUri, String relationUri) throws DatabaseException {
42                 folderType = graph.getResource(folderTypeUri);
43                 relation = graph.getResource(relationUri);
44         }
45
46         @Override
47         public Runnable create(Object target) {
48                 if(!(target instanceof Resource))
49                         return null;
50                 final Resource resource = (Resource)target;
51                 return new Runnable() {
52                         
53                         @Override
54                         public void run() {
55                                 DirectoryDialog dialog = new DirectoryDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
56                                 final String filename = dialog.open();
57                                 if (filename == null) {
58                                         return;
59                                 }
60
61                                 ImportJob job = new ImportJob("Import folder", resource, filename);
62                                 job.setUser(true);
63                                 job.schedule();
64                         }
65                 };
66         }
67         
68         
69         private class ImportJob extends DatabaseJob {
70
71                 public ImportJob(String name, Resource resource, String filename) {
72                         super(name);
73                         this.resource = resource;
74                         this.filename = filename;
75                 }
76
77                 Resource resource;
78                 String filename;
79
80                 @Override
81                 protected IStatus run(final IProgressMonitor monitor) {
82                         try {
83                                 Simantics.getSession().syncRequest(new WriteRequest() {
84                                         @Override
85                                         public void perform(WriteGraph graph) throws DatabaseException {
86                                                 try {
87                                                     graph.markUndoPoint();
88                                                         FileDocumentUtil.importFolderWithName(graph,filename,resource,folderType,relation,monitor);
89                                                 } catch (Exception e) {
90                                                         throw new DatabaseException(e);
91                                                 }
92                                         }
93                                 });
94                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, "Folder imported.");
95                         } catch (DatabaseException e) {
96                                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot import document folder.", e);
97                         }
98                 }
99         }
100         
101 }