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
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.scinotes.actions;
20 import javax.swing.JButton;
21 import javax.swing.KeyStroke;
23 import org.scilab.modules.gui.console.ScilabConsole;
24 import org.scilab.modules.gui.menuitem.MenuItem;
25 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
26 import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
27 import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
28 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
29 import org.scilab.modules.scinotes.SciNotes;
30 import org.scilab.modules.scinotes.ScilabDocument;
31 import org.scilab.modules.scinotes.utils.SciNotesMessages;
34 * ExecuteFileIntoScilabAction Class
35 * @author Bruno JOFRET
36 * @author Allan CORNET
37 * @author Calixte DENIZET
40 public class ExecuteFileIntoScilabAction extends DefaultAction {
45 private static final long serialVersionUID = -8625083632641564277L;
47 protected boolean saveBefore;
51 * @param name the name of the action
52 * @param editor SciNotes
54 public ExecuteFileIntoScilabAction(String name, SciNotes editor) {
59 * Execute the file into Scilab
60 * @param editor the Scilab editor
62 protected void executeFile(SciNotes editor, String filePath) {
63 if (filePath == null) {
67 filePath = filePath.replaceAll("\"", "\"\"");
68 filePath = filePath.replaceAll("'", "''");
69 if (filePath.compareTo("") != 0) {
70 String cmdToExec = "exec('" + filePath + "', -1)";
72 ScilabConsole.getConsole().getAsSimpleConsole().sendCommandsToScilab(cmdToExec, true, false);
73 } catch (NoClassDefFoundError e) {
74 /* This happens when SciNotes is launch as standalone (ie without
76 ScilabModalDialog.show(editor, SciNotesMessages.COULD_NOT_FIND_CONSOLE);
84 public void doAction() {
85 SciNotes editor = getEditor();
86 if (((ScilabDocument) getEditor().getTextPane().getDocument()).isContentModified()) {
87 if (saveBefore || ScilabModalDialog.show(getEditor(), SciNotesMessages.EXECUTE_WARNING, SciNotesMessages.EXECUTE_FILE_INTO_SCILAB,
88 IconType.WARNING_ICON, ButtonType.CANCEL_OR_SAVE_AND_EXECUTE) == AnswerOption.SAVE_EXECUTE_OPTION) {
89 if (editor.save(getEditor().getTabPane().getSelectedIndex(), true)) {
90 this.executeFile(editor, editor.getTextPane().getName());
94 this.executeFile(editor, editor.getTextPane().getName());
100 * @param tooltip the tooltip
101 * @param icon an icon name searched in SCI/modules/gui/images/icons/
102 * @param editor SciNotes
105 public static JButton createButton(String tooltip, String icon, SciNotes editor) {
106 return createButton(tooltip, icon, new ExecuteFileIntoScilabAction(tooltip, editor));
111 * @param label label of the menu
112 * @param editor SciNotes
113 * @param key KeyStroke
116 public static MenuItem createMenu(String label, SciNotes editor, KeyStroke key) {
117 return createMenu(label, null, new ExecuteFileIntoScilabAction(label, editor), key);