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.db.Resource;
\r
30 import org.simantics.sysdyn.ui.Activator;
\r
31 import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;
\r
32 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
\r
33 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
34 import org.simantics.utils.ui.AdaptionUtils;
\r
37 * Imports an exported function library (or shared function library) transferable graph.
\r
38 * @author Teemu Lempinen
\r
41 public class ImportFunctionLibrary extends AbstractHandler {
\r
44 * Called from a functions folder node
\r
47 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
48 ISelection sel = HandlerUtil.getCurrentSelection(event);
\r
49 Resource r = ResourceAdaptionUtils.toSingleResource(sel);
\r
51 FunctionsFolder mn = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class);
\r
54 if(r == null) return null;
\r
56 final Resource functionLibrary = r;
\r
59 // Get a transferable graph file
\r
60 Shell shell = HandlerUtil.getActiveShellChecked(event);
\r
61 FileDialog fd = new FileDialog(shell, SWT.OPEN);
\r
62 fd.setText("Import Function Library");
\r
64 String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTFUNCTIONLIBRARYPATH);
\r
65 if(path.isEmpty() || !(new File(path).exists()))
\r
66 path = Platform.getLocation().toOSString();
\r
67 fd.setFilterPath(path);
\r
68 String[] filterExt = {"*.tg"};
\r
69 fd.setFilterExtensions(filterExt);
\r
70 final String selected = fd.open();
\r
71 if(selected == null) return null;
\r
73 Job job = new DatabaseJob("Import function") {
\r
76 protected IStatus run(IProgressMonitor monitor) {
\r
77 IStatus status = ImportUtilsUI.importFunctionLibrary(functionLibrary, selected, monitor);
\r