]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/ImportDocumentWithDetail.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / ImportDocumentWithDetail.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 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.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                                 }, e -> {
75                                         dialog.getAnnotationConfigurator().dispose();
76                                         if (e != null)
77                                                 ExceptionUtils.logAndShowError("Cannot import document.", e);
78                                 });
79                         }
80                 };
81         }
82         
83         public Resource doDocumentImport(WriteGraph graph, Resource target, String filename, String name) throws DatabaseException {
84         Layer0 l0 = Layer0.getInstance(graph);
85         Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);
86         graph.claimLiteral(newDoc, l0.HasName, name);
87         linkDocument(graph, target, newDoc);
88         return newDoc;
89         }
90         
91         public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {
92             File file = new File(filename);
93             if (!file.exists())
94                 throw new FileNotFoundException("File not found - " + file.getAbsolutePath());
95             ImportDocumentWithDetail document = new ImportDocumentWithDetail(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
96             return document.doDocumentImport(graph, target, filename, file.getName());
97         }
98
99 }