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
7 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
18 package org.scilab.modules.xcos.block.actions;
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;
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;
41 * Generate code for the current graph.
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;
58 * associated Scilab graph
60 public CodeGenerationAction(ScilabGraph scilabGraph) {
65 * Menu for diagram menubar
71 public static MenuItem createMenu(ScilabGraph scilabGraph) {
72 return createMenu(scilabGraph, CodeGenerationAction.class);
80 * @see org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
83 public void actionPerformed(ActionEvent e) {
84 final XcosDiagram graph = (XcosDiagram) getGraph(e);
86 // action disabled when the cell is edited
87 final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
88 if (comp.isEditing()) {
92 Object selectedObj = graph.getSelectionCell();
93 if (!(selectedObj instanceof SuperBlock)) {
94 graph.error(XcosMessages.ERROR_GENERATING_C_CODE);
98 graph.info(XcosMessages.GENERATING_C_CODE);
99 graph.setCellsLocked(true);
100 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
102 final JavaController controller = new JavaController();
103 final SuperBlock block = (SuperBlock) selectedObj;
109 String blk = buildCall("scicos_new", Long.toHexString(block.getUID()));
112 * Prepare command and callback
114 String cmd = buildCall("blk = xcosCodeGeneration", blk.toCharArray());
116 final ActionListener action = new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
120 graph.getView().clear(this, true, true);
122 // Now read new Block
123 graph.getModel().beginUpdate();
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);
132 BlockPositioning.updateBlockView(graph, block);
134 graph.getModel().endUpdate();
136 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
137 graph.setCellsLocked(false);
138 graph.info(XcosMessages.EMPTY_INFO);
146 asynchronousScilabExec(action, cmd);
148 } catch (InterpreterException ex) {
149 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
150 graph.setCellsLocked(false);
151 graph.info(XcosMessages.EMPTY_INFO);