]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4190860dfb4e94441b703b59cfcd0a35fef30630
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 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.Status;\r
22 import org.eclipse.core.runtime.jobs.Job;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.widgets.FileDialog;\r
25 import org.eclipse.swt.widgets.Shell;\r
26 import org.eclipse.ui.handlers.HandlerUtil;\r
27 import org.simantics.DatabaseJob;\r
28 import org.simantics.modeling.ModelingUtils;\r
29 import org.simantics.sysdyn.ui.Activator;\r
30 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;\r
31 \r
32 public class ImportSharedLibraryHandler extends AbstractHandler {\r
33 \r
34     @Override\r
35     public Object execute(ExecutionEvent event) throws ExecutionException {\r
36 \r
37         // Get imported transferable graph file using FileDialog\r
38         Shell shell = HandlerUtil.getActiveShellChecked(event);\r
39         FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
40         fd.setText("Import Model");\r
41 \r
42         String path = Activator.getDefault().getPreferenceStore().getString(ImportUtilsUI.IMPORTMODELTPATH);\r
43         if(path.isEmpty() || !(new File(path).exists())){\r
44                 String execDir = System.getProperty("user.dir");\r
45                 String slash = System.getProperty("file.separator");\r
46                 path = execDir + slash + "sampleModels"; \r
47         }\r
48         \r
49         fd.setFilterPath(path);\r
50         String[] filterExt = {"*.sharedLibrary; *.tg", "*.*"};\r
51         fd.setFilterExtensions(filterExt);\r
52         final String selected = fd.open();\r
53         if(selected == null) return null;\r
54 \r
55         Job job = new DatabaseJob("Import model") {\r
56 \r
57             @Override\r
58             protected IStatus run(IProgressMonitor monitor) {\r
59                 ModelingUtils.importSharedOntology(selected);\r
60                 return Status.OK_STATUS;\r
61             }\r
62         };\r
63         job.setUser(true);\r
64         job.schedule();\r
65 \r
66         return null;\r
67 \r
68     }\r
69 }\r