2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2009 - DIGITEO - Allan CORNET
5 * Copyright (C) 2010 - Calixte DENIZET
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * Copyright (C) 2021 - Stéphane MOTTELET
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.scinotes.actions;
20 import javax.swing.JButton;
21 import javax.swing.KeyStroke;
22 import javax.swing.SwingUtilities;
25 import org.scilab.modules.gui.console.ScilabConsole;
26 import org.scilab.modules.gui.menuitem.MenuItem;
27 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
28 import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
29 import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
30 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
31 import org.scilab.modules.scinotes.SciNotes;
32 import org.scilab.modules.scinotes.ScilabDocument;
33 import org.scilab.modules.scinotes.utils.SciNotesMessages;
34 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
35 import org.scilab.modules.action_binding.InterpreterManagement;
36 import org.scilab.modules.console.SciPromptView;
37 import org.scilab.modules.console.AdvCLIManagement;
40 * ExecuteFileIntoScilabAction Class
41 * @author Bruno JOFRET
42 * @author Allan CORNET
43 * @author Calixte DENIZET
46 public class ExecuteFileIntoScilabAction extends DefaultAction {
51 private static final long serialVersionUID = -8625083632641564277L;
53 protected boolean saveBefore;
57 * @param name the name of the action
58 * @param editor SciNotes
60 public ExecuteFileIntoScilabAction(String name, SciNotes editor) {
65 * Execute the file into Scilab
66 * @param editor the Scilab editor
68 protected void executeFile(SciNotes editor, String filePath) {
69 if (filePath == null) {
73 filePath = filePath.replaceAll("\"", "\"\"");
74 filePath = filePath.replaceAll("'", "''");
75 if (filePath.compareTo("") != 0) {
76 String cmdToExec = "exec('" + filePath + "', -1)";
77 if (ScilabConsole.isExistingConsole())
79 ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(cmdToExec, true, false);
81 /* This happens when SciNotes is launched as standalone (ie without
82 * Scilab) or Scilab launched in -nw mode */
85 System.out.println(cmdToExec);
86 ScilabInterpreterManagement.synchronousScilabExec(cmdToExec);
88 System.out.print(AdvCLIManagement.GetCurrentPrompt());
90 catch (ScilabInterpreterManagement.InterpreterException e) {
91 System.out.println(e.getMessage());
101 public void doAction() {
102 SciNotes editor = getEditor();
103 if (((ScilabDocument) getEditor().getTextPane().getDocument()).isContentModified()) {
104 if (saveBefore || ScilabModalDialog.show(getEditor(), SciNotesMessages.EXECUTE_WARNING, SciNotesMessages.EXECUTE_FILE_INTO_SCILAB,
105 IconType.WARNING_ICON, ButtonType.CANCEL_OR_SAVE_AND_EXECUTE) == AnswerOption.SAVE_EXECUTE_OPTION) {
106 if (editor.save(getEditor().getTabPane().getSelectedIndex(), true)) {
107 this.executeFile(editor, editor.getTextPane().getName());
111 this.executeFile(editor, editor.getTextPane().getName());
117 * @param tooltip the tooltip
118 * @param icon an icon name searched in SCI/modules/gui/images/icons/
119 * @param editor SciNotes
122 public static JButton createButton(String tooltip, String icon, SciNotes editor) {
123 return createButton(tooltip, icon, new ExecuteFileIntoScilabAction(tooltip, editor));
128 * @param label label of the menu
129 * @param editor SciNotes
130 * @param key KeyStroke
133 public static MenuItem createMenu(String label, SciNotes editor, KeyStroke key) {
134 return createMenu(label, null, new ExecuteFileIntoScilabAction(label, editor), key);