Xcos: fix some issues on Xcos->Scilab actions
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / actions / CompileAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Allan SIMON
4  * Copyright (C) 2010 - DIGITEO - ClĂ©ment DAVID
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 package org.scilab.modules.xcos.actions;
15
16 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
17 import static org.scilab.modules.xcos.utils.FileUtils.delete;
18
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.io.File;
22 import java.io.IOException;
23
24 import javax.swing.SwingWorker;
25
26 import org.apache.commons.logging.LogFactory;
27 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
28 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
29 import org.scilab.modules.graph.ScilabGraph;
30 import org.scilab.modules.gui.menuitem.MenuItem;
31 import org.scilab.modules.xcos.graph.XcosDiagram;
32 import org.scilab.modules.xcos.utils.FileUtils;
33 import org.scilab.modules.xcos.utils.XcosMessages;
34
35 /**
36  * Diagram compilation management
37  */
38 public class CompileAction extends SimulationNotRunningAction {
39         /** Name of the action */
40         public static final String NAME = XcosMessages.COMPILE;
41         /** Icon name of the action */
42         public static final String SMALL_ICON = "";
43         /** Mnemonic key of the action */
44         public static final int MNEMONIC_KEY = 0;
45         /** Accelerator key for the action */
46         public static final int ACCELERATOR_KEY = 0;
47
48         /**
49          * Constructor
50          * @param scilabGraph associated diagram
51          */
52         public CompileAction(ScilabGraph scilabGraph) {
53                 super(scilabGraph);
54         }
55
56         /**
57          * Create associated menu
58          * @param scilabGraph associated diagram
59          * @return the menu
60          */
61         public static MenuItem createMenu(ScilabGraph scilabGraph) {
62                 return createMenu(scilabGraph, CompileAction.class);
63         }
64
65         /**
66          * @param e parameter
67          * @see org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
68          */
69         @Override
70         public void actionPerformed(ActionEvent e) {
71                 ((XcosDiagram) getGraph(null)).info(XcosMessages.EXPORT_IN_PROGRESS);
72                 
73                 final File temp;
74                 try {
75                         temp = FileUtils.createTempFile();
76                 } catch (IOException e1) {
77                         LogFactory.getLog(CompileAction.class).error(e1);
78                         return;
79                 }
80                 
81                 (new SwingWorker<Void, Void>() {
82
83                         @Override
84                         protected Void doInBackground() throws Exception {
85                                 ((XcosDiagram) getGraph(null)).dumpToHdf5File(temp);
86                                 ((XcosDiagram) getGraph(null)).setReadOnly(true);
87                                 return null;
88                         }
89                         
90                         @Override
91                         protected void done() {
92                                 ((XcosDiagram) getGraph(null)).info(XcosMessages.COMPILATION_IN_PROGRESS);
93                                 
94                                 String cmd = buildCall("import_from_hdf5", temp.getAbsolutePath()) 
95                                                      + "cpr = xcos_compile(scs_m);";
96                                 
97                                 final ActionListener action = new ActionListener() {
98                                         @Override
99                                         public void actionPerformed(ActionEvent e) {
100                                                 delete(temp);
101                                                 ((XcosDiagram) getGraph(null)).setReadOnly(false);
102                                                 ((XcosDiagram) getGraph(null)).info(XcosMessages.EMPTY_INFO);
103                                         }
104                                 };
105                                 
106                                 try {
107                                         ScilabInterpreterManagement.asynchronousScilabExec(action, cmd);
108                                 } catch (InterpreterException e) {
109                                         LogFactory.getLog(CompileAction.class).error(e);
110                                 }
111                         }
112                         
113                 }).execute();
114         }
115 }