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.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
35 * Class for importing Vensim models (.mdl) to Simantics SysDyn using MdlParser
\r
37 * @author Teemu Lempinen
\r
40 public class ImportMdlHandler extends AbstractHandler {
\r
42 public static String IMPORTMDLTPATH = "IMPORT_MDL_PATH";
\r
45 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
47 final Resource project = SimanticsUI.getProject().get();
\r
48 if(project == null) return null;
\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
63 File file = new File(selected);
\r
64 if(!file.isFile()) return null;
\r
66 Activator.getDefault().getPreferenceStore().setValue(IMPORTMDLTPATH, (new File(selected)).getParent());
\r
68 // Convert Vensim model to Simantics SysDyn format using MdlParser
\r
69 final Model model = MdlParser.parse(file);
\r
71 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
74 public void perform(WriteGraph graph) throws DatabaseException {
\r
75 model.write(graph, project, new WriteContext());
\r