1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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.sysdyn.ui.handlers.imports;
\r
14 import java.io.File;
\r
16 import org.eclipse.core.commands.AbstractHandler;
\r
17 import org.eclipse.core.commands.ExecutionEvent;
\r
18 import org.eclipse.core.commands.ExecutionException;
\r
19 import org.eclipse.core.runtime.IProgressMonitor;
\r
20 import org.eclipse.core.runtime.IStatus;
\r
21 import org.eclipse.core.runtime.Platform;
\r
22 import org.eclipse.core.runtime.jobs.Job;
\r
23 import org.eclipse.jface.viewers.ISelection;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.widgets.FileDialog;
\r
26 import org.eclipse.swt.widgets.Shell;
\r
27 import org.eclipse.ui.handlers.HandlerUtil;
\r
28 import org.simantics.DatabaseJob;
\r
29 import org.simantics.browsing.ui.common.node.AbstractNode;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.sysdyn.ui.Activator;
\r
32 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
\r
33 import org.simantics.utils.ui.AdaptionUtils;
\r
36 * Imports modules from exported transferable graph files.
\r
38 * @author Teemu Lempinen
\r
41 public class ImportModuleHandler extends AbstractHandler {
\r
45 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
46 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
48 @SuppressWarnings("unchecked")
\r
49 AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);
\r
53 final Resource model = node.data;
\r
55 // Get imported transferable graph file using FileDialog
\r
56 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
57 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
58 fd.setText("Import Module");
\r
60 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODULETPATH);
\r
61 if(path.isEmpty() || !(new File(path).exists()))
\r
62 path = Platform.getLocation().toOSString();
\r
63 fd.setFilterPath(path);
\r
64 String[] filterExt = {"*.tg"};
\r
65 fd.setFilterExtensions(filterExt);
\r
66 final String selected = fd.open();
\r
67 if(selected == null) return null;
\r
69 Job job = new DatabaseJob("Import model") {
\r
72 protected IStatus run(IProgressMonitor monitor) {
\r
73 IStatus status = ImportUtilsUI.importModuleFile(model, selected, monitor);
\r