1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.document.ui.actions;
\r
14 import org.eclipse.swt.SWT;
\r
15 import org.eclipse.swt.widgets.Display;
\r
16 import org.eclipse.swt.widgets.FileDialog;
\r
17 import org.simantics.Simantics;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.WriteGraph;
\r
21 import org.simantics.db.common.request.WriteRequest;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.document.ui.graphfile.FileDocumentUtil;
\r
24 import org.simantics.utils.datastructures.Callback;
\r
25 import org.simantics.utils.ui.ExceptionUtils;
\r
28 * Action for importing files as documents.
\r
30 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
33 public class ImportDocument extends AddDocumentAction {
\r
36 public ImportDocument(ReadGraph graph, String relationUri) throws DatabaseException {
\r
37 super(graph, relationUri);
\r
41 public Runnable create(Object target) {
\r
42 if(!(target instanceof Resource))
\r
44 final Resource resource = (Resource)target;
\r
45 return new Runnable() {
\r
49 FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
\r
50 // TODO : is there any way to read file/executable bindings from OS?
\r
51 // if is, use those extensions to filter this list.
\r
52 // note: in windows using "reg query ..." to read bindings form registry would work.
\r
53 dialog.setFilterExtensions(new String[]{"*.*"});
\r
54 final String filename = dialog.open();
\r
55 if (filename == null) {
\r
58 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
60 public void perform(WriteGraph graph) throws DatabaseException {
\r
61 graph.markUndoPoint();
\r
63 Resource newDoc = FileDocumentUtil.importFileWithName(graph,filename);
\r
64 linkDocument(graph, resource, newDoc);
\r
66 },new Callback<DatabaseException>() {
\r
68 public void run(DatabaseException parameter) {
\r
69 if (parameter != null) {
\r
70 ExceptionUtils.logAndShowError("Cannot import document.", parameter);
\r