]> gerrit.simantics Code Review - simantics/platform.git/blob
42a0a7c94fa8bd9476b069a73bf00698796fd8e9
[simantics/platform.git] /
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 import java.io.FileNotFoundException;
16
17 import org.eclipse.swt.widgets.Display;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.document.AddDocumentAction;
25 import org.simantics.document.FileDocumentUtil;
26 import org.simantics.document.ui.dialogs.FileDetailDialog;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.datastructures.Callback;
29 import org.simantics.utils.ui.ExceptionUtils;
30
31 /**
32  * Action for importing files as document.
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class ImportDocumentWithDetail extends AddDocumentAction {
38         
39
40         public ImportDocumentWithDetail(ReadGraph graph, String relationUri) throws DatabaseException {
41                 super(graph, relationUri);
42         }
43
44         @Override
45         public Runnable create(Object target) {
46                 if(!(target instanceof Resource))
47                         return null;
48                 final Resource resource = (Resource)target;
49                 return new Runnable() {
50                         
51                         @Override
52                         public void run() {
53                                 final FileDetailDialog dialog = new FileDetailDialog(Display.getCurrent().getActiveShell(), resource);
54                                 // TODO : is there any way to read file/executable bindings from OS?
55                                 //        if is, use those extensions to filter this list.
56                                 //        note: in windows using "reg query ..." to read bindings form registry would work.
57                                 if (dialog.open() == FileDetailDialog.CANCEL) {
58                                         dialog.getAnnotationConfigurator().dispose();
59                                         return;
60                                 }
61                                 final String filename = dialog.getFileName();
62                                 final String name = dialog.getName();
63                                 if (filename == null) {
64                                         dialog.getAnnotationConfigurator().dispose();
65                                         return;
66                                 }
67                                 Simantics.getSession().asyncRequest(new WriteRequest() {
68                                         @Override
69                                         public void perform(WriteGraph graph) throws DatabaseException {
70                                             graph.markUndoPoint();
71                                         
72                                             Resource newDoc = doDocumentImport(graph, resource, filename, name);
73                                                 dialog.getAnnotationConfigurator().apply(graph,newDoc);
74                                         }
75                                 },new Callback<DatabaseException>() {                           
76                                         @Override
77                                         public void run(DatabaseException parameter) {
78                                                 dialog.getAnnotationConfigurator().dispose();
79                                                 if (parameter != null) {
80                                                         ExceptionUtils.logAndShowError("Cannot import document.", parameter);
81                                                 }
82                                                 
83                                         }
84                                 });
85                         }
86                 };
87         }
88         
89         public Resource doDocumentImport(WriteGraph graph, Resource target, String filename, String name) throws DatabaseException {
90         Layer0 l0 = Layer0.getInstance(graph);
91         Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);
92         graph.claimLiteral(newDoc, l0.HasName, name);
93         linkDocument(graph, target, newDoc);
94         return newDoc;
95         }
96         
97         public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {
98             File file = new File(filename);
99             if (!file.exists())
100                 throw new FileNotFoundException("File not found - " + file.getAbsolutePath());
101             ImportDocumentWithDetail document = new ImportDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
102             return document.doDocumentImport(graph, target, filename, file.getName());
103         }
104
105 }