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.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
34 * Class for importing Vensim models (.mdl) to Simantics SysDyn using MdlParser
\r
36 * @author Teemu Lempinen
\r
39 public class ImportMdlHandler extends AbstractHandler {
\r
41 public static String IMPORTMDLTPATH = "IMPORT_MDL_PATH";
\r
44 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
46 final Resource project = SimanticsUI.getProject().get();
\r
47 if(project == null) return null;
\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
62 File file = new File(selected);
\r
63 if(!file.isFile()) return null;
\r
65 Activator.getDefault().getPreferenceStore().setValue(IMPORTMDLTPATH, (new File(selected)).getParent());
\r
67 // Convert Vensim model to Simantics SysDyn format using MdlParser
\r
68 final Model model = MdlParser.parse(file);
\r
70 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
73 public void perform(WriteGraph graph) throws DatabaseException {
\r
74 model.write(graph, project);
\r