Xcos: implement code generation action
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / actions / CodeGenerationAction.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-2011 - DIGITEO - Clement DAVID
5  * Copyright (C) 2012-2016 - Scilab Enterprises - Clement DAVID
6  *
7  * Copyright (C) 2012 - 2016 - Scilab Enterprises
8  *
9  * This file is hereby licensed under the terms of the GNU GPL v2.0,
10  * pursuant to article 5.3.4 of the CeCILL v.2.1.
11  * This file was originally licensed under the terms of the CeCILL v2.1,
12  * and continues to be available under such terms.
13  * For more information, see the COPYING file which you should have received
14  * along with this program.
15  *
16  */
17
18 package org.scilab.modules.xcos.block.actions;
19
20 import java.awt.Cursor;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.util.logging.Logger;
24 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
25 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.asynchronousScilabExec;
26 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
27 import org.scilab.modules.graph.ScilabComponent;
28
29 import org.scilab.modules.graph.ScilabGraph;
30 import org.scilab.modules.gui.menuitem.MenuItem;
31 import org.scilab.modules.xcos.JavaController;
32 import org.scilab.modules.xcos.block.BasicBlock;
33 import org.scilab.modules.xcos.block.SuperBlock;
34 import org.scilab.modules.xcos.graph.XcosDiagram;
35 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
36 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
37 import org.scilab.modules.xcos.utils.BlockPositioning;
38 import org.scilab.modules.xcos.utils.XcosMessages;
39
40 /**
41  * Generate code for the current graph.
42  */
43 @SuppressWarnings(value = { "serial" })
44 public class CodeGenerationAction extends SuperBlockSelectedAction {
45     /** Name of the action */
46     public static final String NAME = XcosMessages.CODE_GENERATION;
47     /** Icon name of the action */
48     public static final String SMALL_ICON = "";
49     /** Mnemonic key of the action */
50     public static final int MNEMONIC_KEY = 0;
51     /** Accelerator key for the action */
52     public static final int ACCELERATOR_KEY = 0;
53
54     /**
55      * Constructor
56      *
57      * @param scilabGraph
58      *            associated Scilab graph
59      */
60     public CodeGenerationAction(ScilabGraph scilabGraph) {
61         super(scilabGraph);
62     }
63
64     /**
65      * Menu for diagram menubar
66      *
67      * @param scilabGraph
68      *            associated diagram
69      * @return the menu
70      */
71     public static MenuItem createMenu(ScilabGraph scilabGraph) {
72         return createMenu(scilabGraph, CodeGenerationAction.class);
73     }
74
75     /**
76      * Action !!!
77      *
78      * @param e
79      *            parameter
80      * @see org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
81      */
82     @Override
83     public void actionPerformed(ActionEvent e) {
84         final XcosDiagram graph = (XcosDiagram) getGraph(e);
85
86         // action disabled when the cell is edited
87         final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
88         if (comp.isEditing()) {
89             return;
90         }
91
92         Object selectedObj = graph.getSelectionCell();
93         if (!(selectedObj instanceof SuperBlock)) {
94             graph.error(XcosMessages.ERROR_GENERATING_C_CODE);
95             return;
96         }
97
98         graph.info(XcosMessages.GENERATING_C_CODE);
99         graph.setCellsLocked(true);
100         graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
101
102         final JavaController controller = new JavaController();
103         final SuperBlock block = (SuperBlock) selectedObj;
104
105         try {
106             /*
107              * Export data
108              */
109             String blk = buildCall("scicos_new", Long.toHexString(block.getUID()));
110
111             /*
112              * Prepare command and callback
113              */
114             String cmd = buildCall("blk = xcosCodeGeneration", blk.toCharArray());
115
116             final ActionListener action = new ActionListener() {
117                 @Override
118                 public void actionPerformed(ActionEvent e) {
119
120                     graph.getView().clear(this, true, true);
121
122                     // Now read new Block
123                     graph.getModel().beginUpdate();
124                     try {
125                         ScicosObjectOwner last = XcosCellFactory.getLastCreated();
126                         if (last != null && last.getUID() != 0l) {
127                             BasicBlock modified = XcosCellFactory.createBlock(controller, last);
128                             if (modified != null) {
129                                 block.updateBlockSettings(controller, graph, modified);
130                             }
131                         }
132                         BlockPositioning.updateBlockView(graph, block);
133                     } finally {
134                         graph.getModel().endUpdate();
135
136                         graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
137                         graph.setCellsLocked(false);
138                         graph.info(XcosMessages.EMPTY_INFO);
139                     }
140                 }
141             };
142
143             /*
144              * Execute
145              */
146             asynchronousScilabExec(action, cmd);
147
148         } catch (InterpreterException ex) {
149             graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
150             graph.setCellsLocked(false);
151             graph.info(XcosMessages.EMPTY_INFO);
152         }
153     }
154 }