]> gerrit.simantics Code Review - simantics/platform.git/blob
7235f55f09382b854d3dada7ca025df36610f763
[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.ui.dialogs.FileDetailDialog;
25 import org.simantics.document.ui.graphfile.FileDocumentUtil;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.utils.datastructures.Callback;
28 import org.simantics.utils.ui.ExceptionUtils;
29
30 /**
31  * Action for importing files as document.
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 public class ImportDocumentWithDetail extends AddDocumentAction {
37         
38
39         public ImportDocumentWithDetail(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                                 final FileDetailDialog dialog = new FileDetailDialog(Display.getCurrent().getActiveShell(), resource);
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                                 if (dialog.open() == FileDetailDialog.CANCEL) {
57                                         dialog.getAnnotationConfigurator().dispose();
58                                         return;
59                                 }
60                                 final String filename = dialog.getFileName();
61                                 final String name = dialog.getName();
62                                 if (filename == null) {
63                                         dialog.getAnnotationConfigurator().dispose();
64                                         return;
65                                 }
66                                 Simantics.getSession().asyncRequest(new WriteRequest() {
67                                         @Override
68                                         public void perform(WriteGraph graph) throws DatabaseException {
69                                             graph.markUndoPoint();
70                                         
71                                             Resource newDoc = doDocumentImport(graph, resource, filename, name);
72                                                 dialog.getAnnotationConfigurator().apply(graph,newDoc);
73                                         }
74                                 },new Callback<DatabaseException>() {                           
75                                         @Override
76                                         public void run(DatabaseException parameter) {
77                                                 dialog.getAnnotationConfigurator().dispose();
78                                                 if (parameter != null) {
79                                                         ExceptionUtils.logAndShowError("Cannot import document.", parameter);
80                                                 }
81                                                 
82                                         }
83                                 });
84                         }
85                 };
86         }
87         
88         public Resource doDocumentImport(WriteGraph graph, Resource target, String filename, String name) throws DatabaseException {
89         Layer0 l0 = Layer0.getInstance(graph);
90         Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);
91         graph.claimLiteral(newDoc, l0.HasName, name);
92         linkDocument(graph, target, newDoc);
93         return newDoc;
94         }
95         
96         public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {
97             File file = new File(filename);
98             if (!file.exists())
99                 throw new FileNotFoundException("File not found - " + file.getAbsolutePath());
100             ImportDocumentWithDetail document = new ImportDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
101             return document.doDocumentImport(graph, target, filename, file.getName());
102         }
103
104 }