1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.ui.actions;
15 import java.io.FileNotFoundException;
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;
32 * Action for importing files as document.
34 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37 public class ImportDocumentWithDetail extends AddDocumentAction {
40 public ImportDocumentWithDetail(ReadGraph graph, String relationUri) throws DatabaseException {
41 super(graph, relationUri);
45 public Runnable create(Object target) {
46 if(!(target instanceof Resource))
48 final Resource resource = (Resource)target;
49 return new Runnable() {
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();
61 final String filename = dialog.getFileName();
62 final String name = dialog.getName();
63 if (filename == null) {
64 dialog.getAnnotationConfigurator().dispose();
67 Simantics.getSession().asyncRequest(new WriteRequest() {
69 public void perform(WriteGraph graph) throws DatabaseException {
70 graph.markUndoPoint();
72 Resource newDoc = doDocumentImport(graph, resource, filename, name);
73 dialog.getAnnotationConfigurator().apply(graph,newDoc);
75 },new Callback<DatabaseException>() {
77 public void run(DatabaseException parameter) {
78 dialog.getAnnotationConfigurator().dispose();
79 if (parameter != null) {
80 ExceptionUtils.logAndShowError("Cannot import document.", parameter);
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);
97 public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {
98 File file = new File(filename);
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());