Refactorization of the management of the actions in menus, button, shortcuts
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / actions / LoadIntoScilabAction.java
1 /*
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  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 package org.scilab.modules.scinotes.actions;
15
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.OutputStreamWriter;
20
21 import javax.swing.KeyStroke;
22
23 import org.scilab.modules.action_binding.InterpreterManagement;
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.utils.SciNotesMessages;
31
32 /**
33  * LoadIntoScilabAction class
34  * @author Allan CORNET
35  *
36  */
37 public final class LoadIntoScilabAction extends DefaultAction {
38
39     /**
40      * serialVersionUID
41      */
42     private static final long serialVersionUID = -5659317486404897280L;
43
44     private static final int MAX_LINES_RECOMMANDED = 100;
45     private static final int NB_STANDARD_COLUMNS = 80;
46     private static final int MAX_CHARACTERS_RECOMMANDED = MAX_LINES_RECOMMANDED * NB_STANDARD_COLUMNS;
47
48     /**
49      * Constructor
50      * @param name the name of the action
51      * @param editor SciNotes
52      */
53     public LoadIntoScilabAction(String name, SciNotes editor) {
54         super(name, editor);
55     }
56
57     /**
58      * doAction
59      */
60     public void doAction() {
61         String text = getEditor().getTextPane().getText();
62
63         if (text.compareTo("") != 0) {
64             boolean bContinue = false;
65
66             if (maxLinesRecommandedDetected(text)) {
67                 if (ScilabModalDialog.show(getEditor(), SciNotesMessages.BIG_FILE_WARNING, SciNotesMessages.LOAD_INTO_SCILAB,
68                                            IconType.QUESTION_ICON, ButtonType.YES_NO) == AnswerOption.YES_OPTION) {
69                     bContinue = true;
70                 } else {
71                     bContinue = false;
72                 }
73             } else {
74                 bContinue = true;
75             }
76
77             if (bContinue) {
78                 String tmpFilename = "LOAD_INTO_SCILAB-";
79
80                 // save file as UTF-8
81                 try {
82                     File f = File.createTempFile(tmpFilename, ".sce");
83                     String tmpFullFilename = f.getAbsolutePath();
84                     OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f) , "UTF-8");
85                     out.write(text);
86                     out.flush();
87                     out.close();
88                     String cmdToExec = "exec('" + tmpFullFilename + "', 1)";
89                     InterpreterManagement.requestScilabExec(cmdToExec);
90                 } catch (IOException e) {
91                     ScilabModalDialog.show(getEditor(), SciNotesMessages.COULD_NOT_FIND_TMPFILE);
92                 }
93             }
94         }
95     }
96
97     /**
98      * createMenu
99      * @param label label of the menu
100      * @param editor SciNotes
101      * @param key KeyStroke
102      * @return MenuItem
103      */
104     public static MenuItem createMenu(String label, SciNotes editor, KeyStroke key) {
105         return createMenu(label, null, new LoadIntoScilabAction(label, editor), key);
106     }
107
108     /**
109      * check if string is not too big
110      * @param text to scan
111      * @return true or false
112      */
113     private static boolean maxLinesRecommandedDetected(String text) {
114         if (text.length() > MAX_CHARACTERS_RECOMMANDED) {
115             return true;
116         }
117
118         String[] splitedStringOnLineSeparator = text.split(System.getProperty("line.separator"));
119         if (splitedStringOnLineSeparator.length > MAX_LINES_RECOMMANDED) {
120             return true;
121         }
122
123         return false;
124     }
125 }