Xcos GUI: enable Compile action 11/17511/3
Paul Bignier [Mon, 30 Nov 2015 10:04:32 +0000 (11:04 +0100)]
Change-Id: I21e4e2e51381cc3b17f42f126429219a0b4ef45a

scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/CompileAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index 8522c3c..240d65c 100644 (file)
 
 package org.scilab.modules.xcos.actions;
 
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.asynchronousScilabExec;
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
+
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.logging.Logger;
+
+import javax.swing.Timer;
 
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
 import org.scilab.modules.graph.ScilabComponent;
 import org.scilab.modules.graph.ScilabGraph;
+import org.scilab.modules.graph.actions.base.GraphActionManager;
+import org.scilab.modules.graph.actions.base.OneBlockDependantAction;
 import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.xcos.JavaController;
+import org.scilab.modules.xcos.ObjectProperties;
 import org.scilab.modules.xcos.graph.XcosDiagram;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
@@ -25,7 +37,7 @@ import org.scilab.modules.xcos.utils.XcosMessages;
  * Diagram compilation management
  */
 @SuppressWarnings(value = { "serial" })
-public final class CompileAction extends SimulationNotRunningAction {
+public final class CompileAction extends OneBlockDependantAction {
     /** Name of the action */
     public static final String NAME = XcosMessages.COMPILE;
     /** Icon name of the action */
@@ -35,6 +47,9 @@ public final class CompileAction extends SimulationNotRunningAction {
     /** Accelerator key for the action */
     public static final int ACCELERATOR_KEY = 0;
 
+    private int counter;
+    private Timer displayTimer;
+
     /**
      * Constructor
      *
@@ -43,6 +58,14 @@ public final class CompileAction extends SimulationNotRunningAction {
      */
     public CompileAction(ScilabGraph scilabGraph) {
         super(scilabGraph);
+        char[] msg = (XcosMessages.COMPILATION_IN_PROGRESS + XcosMessages.DOTS).toCharArray();
+        final int minimalMsgLen = XcosMessages.COMPILATION_IN_PROGRESS.length();
+
+        counter = 0;
+        displayTimer = new Timer(1000, e -> {
+            counter = (counter + 1) % (XcosMessages.DOTS.length() + 1);
+            ((XcosDiagram) scilabGraph).info(new String(msg, 0, minimalMsgLen + counter));
+        });
     }
 
     /**
@@ -65,63 +88,78 @@ public final class CompileAction extends SimulationNotRunningAction {
     public void actionPerformed(ActionEvent e) {
         final XcosDiagram graph = (XcosDiagram) getGraph(e);
 
-        // action disabled when the cell is edited
+        // Action disabled when the cell is edited
         final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
         if (comp.isEditing()) {
             return;
         }
 
-        graph.info(XcosMessages.EXPORT_IN_PROGRESS);
-
-        // FIXME: implement the compilation
-        //        final ScilabDirectHandler handler = ScilabDirectHandler.acquire();
-        //        if (handler == null) {
-        //            return;
-        //        }
-        //        (new SwingWorker<Void, Void>() {
-        //
-        //            @Override
-        //            protected Void doInBackground() {
-        //                try {
-        //                    handler.writeDiagram(((XcosDiagram) getGraph(null)));
-        //                    ((XcosDiagram) getGraph(null)).setReadOnly(true);
-        //                } catch (Exception e) {
-        //                    cancel(true);
-        //                }
-        //                return null;
-        //            }
-        //
-        //            @Override
-        //            protected void done() {
-        //                if (isCancelled()) {
-        //                    graph.info(XcosMessages.EMPTY_INFO);
-        //
-        //                    handler.release();
-        //                    return;
-        //                }
-        //
-        //                graph.info(XcosMessages.COMPILATION_IN_PROGRESS);
-        //                String cmd = "cpr = xcos_compile(scs_m);";
-        //
-        //                final ActionListener action = new ActionListener() {
-        //                    @Override
-        //                    public void actionPerformed(ActionEvent e) {
-        //                        graph.setReadOnly(false);
-        //                        graph.info(XcosMessages.EMPTY_INFO);
-        //
-        //                        handler.release();
-        //                    }
-        //                };
-        //
-        //                try {
-        //                    ScilabInterpreterManagement.asynchronousScilabExec(action, cmd);
-        //                } catch (InterpreterException e) {
-        //                    Logger.getLogger(CompileAction.class.getName()).severe(e.toString());
-        //
-        //                    handler.release();
-        //                }
-        //            }
-        //
-        //        }).execute();
+        updateUI(true);
+        displayTimer.start();
+
+        final String cmd = createCompilationCommand(graph);
+        final ActionListener action = new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                displayTimer.stop();
+                updateUI(false);
+            }
+        };
+
+        try {
+            asynchronousScilabExec(action, cmd);
+        } catch (InterpreterException e1) {
+            final Logger log = Logger.getLogger(CompileAction.class.getName());
+            log.warning(e1.getMessage());
+        }
+    }
+
+    /**
+     * Create the command String
+     *
+     * @param diagram
+     *            the working diagram
+     * @return the command string
+     */
+    private String createCompilationCommand(final XcosDiagram diagram) {
+        String cmd;
+        final StringBuilder command = new StringBuilder();
+
+        /*
+         * Log compilation info
+         */
+        final Logger log = Logger.getLogger(CompileAction.class.getName());
+        log.finest("start compilation");
+
+        JavaController controller = new JavaController();
+        int[] debugLevel = new int[1];
+        controller.getObjectProperty(diagram.getUID(), diagram.getKind(), ObjectProperties.DEBUG_LEVEL, debugLevel);
+        command.append(buildCall("scicos_debug", debugLevel[0])).append("; ");
+
+        /*
+         * Export the schema on `scs_m`
+         */
+        command.append("scs_m = scicos_new(\"0x").append(Long.toHexString(diagram.getUID())).append("\"); ");
+        command.append("cpr = xcos_compile(scs_m); ");
+
+        cmd = command.toString();
+        return cmd;
+    }
+
+    /**
+     * Update the UI depending on the action selected or not
+     *
+     * @param started
+     *            the started status
+     */
+    public void updateUI(boolean started) {
+        GraphActionManager.setEnable(StartAction.class, !started);
+        ((XcosDiagram) getGraph(null)).setReadOnly(started);
+
+        if (started) {
+            ((XcosDiagram) getGraph(null)).info(XcosMessages.COMPILATION_IN_PROGRESS);
+        } else {
+            ((XcosDiagram) getGraph(null)).info(XcosMessages.EMPTY_INFO);
+        }
     }
 }
index b13c356..8143edd 100644 (file)
@@ -177,7 +177,6 @@ public final class XcosMessages {
     public static final String NO_BLOCK_SELECTED = Messages.gettext("No block selected");
 
     public static final String EXPORT_TO_XML = Messages.gettext("Export to XML") + DOTS;
-    public static final String EXPORT_IN_PROGRESS = Messages.gettext("Export in progress") + DOTS;
     public static final String IMPORT_FROM_XML = Messages.gettext("Import from XML") + DOTS;
 
     public static final String ADDTO = Messages.gettext("Add to");
@@ -236,8 +235,7 @@ public final class XcosMessages {
     public static final String GENERATING_C_CODE = Messages.gettext("Generating C Code for SuperBlock") + DOTS;
     public static final String ERROR_GENERATING_C_CODE = Messages.gettext("A SuperBlock must be selected to generate code");
     public static final String SIMULATION_IN_PROGRESS = Messages.gettext("Simulation in progress");
-    public static final String COMPILATION_IN_PROGRESS = Messages.gettext("Compilation in progress, results will be stored in the \'scicos_cpr\' variable")
-            + DOTS;
+    public static final String COMPILATION_IN_PROGRESS = Messages.gettext("Compilation in progress, results will be stored in the \'cpr\' variable");
     public static final String GENERATE_SUPERBLOCK = Messages.gettext("Generate SuperBlock, please wait") + DOTS;
     public static final String DRAW_LINK = Messages.gettext("Click on diagram to add link point or on a compatible target to finish");