Search in sources :

Example 1 with JTextAreaPrintStream

use of org.meteoinfo.console.editor.JTextAreaPrintStream in project MeteoInfo by meteoinfo.

the class EditorDockable method runPythonScript.

/**
 * Run Jython script
 *
 * @param jTextArea_Output
 */
public void runPythonScript(final JTextArea jTextArea_Output) {
    SwingWorker worker = new SwingWorker<String, String>() {

        PrintStream oout = System.out;

        PrintStream oerr = System.err;

        @Override
        protected String doInBackground() throws Exception {
            JTextAreaWriter writer = new JTextAreaWriter(jTextArea_Output);
            JTextAreaPrintStream printStream = new JTextAreaPrintStream(System.out, jTextArea_Output);
            jTextArea_Output.setText("");
            // Create an instance of the PythonInterpreter
            // Py.getSystemState().setdefaultencoding("utf-8");
            // UPythonInterpreter interp = new UPythonInterpreter();
            PythonInterpreter interp = new PythonInterpreter();
            interp.setOut(writer);
            interp.setErr(writer);
            System.setOut(printStream);
            System.setErr(printStream);
            // }
            try {
                interp.exec("import sys");
                interp.set("milapp", EditorDockable.this.parent);
            // interp.exec("sys.path.append('" + path + "')");
            // interp.exec("import mipylib");
            // interp.exec("from mipylib.miscript import *");
            // interp.exec("from meteoinfo.numeric.JNumeric import *");
            // interp.exec("mis = MeteoInfoScript()");
            // interp.set("miapp", _parent);
            // for (String jarfn : jarfns) {
            // interp.exec("sys.path.append('" + jarfn + "')");
            // }
            } catch (Exception e) {
                e.printStackTrace();
            }
            TextEditorPane textArea = getActiveTextArea();
            // textArea.setEncoding(fn);
            String code = textArea.getText();
            // if (code.contains("coding=utf-8")){
            // code = code.replace("coding=utf-8", "coding = utf-8");
            // }
            String encoding = EncodingUtil.findEncoding(code);
            if (encoding != null) {
                try {
                    interp.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    interp.execfile(new ByteArrayInputStream(code.getBytes()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return "";
        }

        @Override
        protected void done() {
            System.setOut(oout);
            System.setErr(oerr);
        }
    };
    worker.execute();
}
Also used : PrintStream(java.io.PrintStream) JTextAreaPrintStream(org.meteoinfo.console.editor.JTextAreaPrintStream) PythonInterpreter(org.python.util.PythonInterpreter) JTextAreaPrintStream(org.meteoinfo.console.editor.JTextAreaPrintStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SwingWorker(javax.swing.SwingWorker) MITextEditorPane(org.meteoinfo.console.editor.MITextEditorPane) TextEditorPane(org.fife.ui.rsyntaxtextarea.TextEditorPane) JTextAreaWriter(org.meteoinfo.console.editor.JTextAreaWriter) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException) AWTException(java.awt.AWTException)

Aggregations

AWTException (java.awt.AWTException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 SwingWorker (javax.swing.SwingWorker)1 BadLocationException (javax.swing.text.BadLocationException)1 TextEditorPane (org.fife.ui.rsyntaxtextarea.TextEditorPane)1 JTextAreaPrintStream (org.meteoinfo.console.editor.JTextAreaPrintStream)1 JTextAreaWriter (org.meteoinfo.console.editor.JTextAreaWriter)1 MITextEditorPane (org.meteoinfo.console.editor.MITextEditorPane)1 PythonInterpreter (org.python.util.PythonInterpreter)1