]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
cdd40a04ca91c5fae0d3f2bd75b77d549d23780d
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.handlers.imports;\r
13 \r
14 import java.io.File;\r
15 \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
35 \r
36 /**\r
37  * Imports an exported function library (or shared function library) transferable graph. \r
38  * @author Teemu Lempinen\r
39  *\r
40  */\r
41 public class ImportFunctionLibrary  extends AbstractHandler {\r
42         \r
43         /**\r
44          * Called from a functions folder node\r
45          */\r
46         @Override\r
47         public Object execute(ExecutionEvent event) throws ExecutionException {\r
48         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
49         Resource r = ResourceAdaptionUtils.toSingleResource(sel);\r
50         if(r == null) {\r
51                 FunctionsFolder mn = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class);\r
52                 r = mn.data;\r
53         }\r
54         if(r == null) return null;\r
55         \r
56         final Resource functionLibrary = r;\r
57         \r
58         \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
63                 \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
72 \r
73                 Job job = new DatabaseJob("Import function") {\r
74 \r
75                     @Override\r
76                     protected IStatus run(IProgressMonitor monitor) {\r
77                         IStatus status = ImportUtilsUI.importFunctionLibrary(functionLibrary, selected, monitor);\r
78                         return status;\r
79                     }\r
80                 };\r
81 \r
82                 job.setUser(true);\r
83                 job.schedule();\r
84 \r
85                 return null;\r
86         }\r
87         \r
88 }\r