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