]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2ce39ec68466fffcd0fe29d97c54653c1de5e041
[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.Platform;\r
20 import org.eclipse.swt.SWT;\r
21 import org.eclipse.swt.widgets.FileDialog;\r
22 import org.eclipse.swt.widgets.Shell;\r
23 import org.eclipse.ui.handlers.HandlerUtil;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.WriteGraph;\r
26 import org.simantics.db.common.request.WriteRequest;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.sysdyn.mdlImport.MdlParser;\r
29 import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
30 import org.simantics.sysdyn.ui.Activator;\r
31 import org.simantics.ui.SimanticsUI;\r
32 \r
33 /**\r
34  * Class for importing Vensim models (.mdl) to Simantics SysDyn using MdlParser \r
35  * \r
36  * @author Teemu Lempinen\r
37  *\r
38  */\r
39 public class ImportMdlHandler extends AbstractHandler {\r
40 \r
41         public static String IMPORTMDLTPATH = "IMPORT_MDL_PATH";\r
42 \r
43         @Override\r
44         public Object execute(ExecutionEvent event) throws ExecutionException {\r
45                 \r
46                 final Resource project = SimanticsUI.getProject().get();\r
47                 if(project == null) return null;\r
48 \r
49                 // Get the .mdl file\r
50                 Shell shell = HandlerUtil.getActiveShellChecked(event);\r
51                 FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
52                 fd.setText("Import .mdl");\r
53                 String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMDLTPATH);\r
54                 if(path.isEmpty() || !(new File(path).exists()))\r
55                         path = Platform.getLocation().toOSString();\r
56                 fd.setFilterPath(path);\r
57                 String[] filterExt = {"*.mdl"};\r
58                 fd.setFilterExtensions(filterExt);\r
59                 String selected = fd.open();\r
60                 if(selected == null) return null;\r
61 \r
62                 File file = new File(selected);\r
63                 if(!file.isFile()) return null;\r
64                 \r
65                 Activator.getDefault().getPreferenceStore().setValue(IMPORTMDLTPATH, (new File(selected)).getParent());\r
66                 \r
67                 // Convert Vensim model to Simantics SysDyn format using MdlParser\r
68                 final Model model = MdlParser.parse(file);\r
69                 \r
70                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
71                         \r
72                         @Override\r
73                         public void perform(WriteGraph graph) throws DatabaseException {\r
74                                 model.write(graph, project);\r
75                         }\r
76                 });\r
77                 \r
78                 return null;\r
79         }\r
80 \r
81 }\r