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.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;
31 * Action for importing files as document.
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
36 public class ImportDocumentWithDetail extends AddDocumentAction {
39 public ImportDocumentWithDetail(ReadGraph graph, String relationUri) throws DatabaseException {
40 super(graph, relationUri);
44 public Runnable create(Object target) {
45 if(!(target instanceof Resource))
47 final Resource resource = (Resource)target;
48 return new Runnable() {
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();
60 final String filename = dialog.getFileName();
61 final String name = dialog.getName();
62 if (filename == null) {
63 dialog.getAnnotationConfigurator().dispose();
66 Simantics.getSession().asyncRequest(new WriteRequest() {
68 public void perform(WriteGraph graph) throws DatabaseException {
69 graph.markUndoPoint();
71 Resource newDoc = doDocumentImport(graph, resource, filename, name);
72 dialog.getAnnotationConfigurator().apply(graph,newDoc);
74 },new Callback<DatabaseException>() {
76 public void run(DatabaseException parameter) {
77 dialog.getAnnotationConfigurator().dispose();
78 if (parameter != null) {
79 ExceptionUtils.logAndShowError("Cannot import document.", parameter);
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);
96 public static Resource importDocumentWithDetailSCL(WriteGraph graph, Resource target, String filename) throws FileNotFoundException, DatabaseException {
97 File file = new File(filename);
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());