Re: [Jython-dev] Jython lost the ability of Chinese coding support after version 2.2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: [Jython-dev] Jython lost the ability of Chinese coding support after version 2.2

yaqiang.wang@gmail.com
Dear Eero,

Thanks for your quick response. I didn't use Jython console. I use BeanShell JConsole and Jython InteractiveConsole to implement the console function in a JDialog. I learn this way from the code of OpenJump and I don't know if there are other way to do it. The source code is pasted blow:

package meteoinfo.classes;

import bsh.JavaCharStream;
import bsh.util.JConsole;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import org.python.core.PyObject;
import org.python.util.InteractiveConsole;

/**
*
* @author yaqiang
*/
public class PythonInteractiveInterpreter extends InteractiveConsole implements Runnable {

transient Reader in;
transient PrintStream out;
transient PrintStream err;
JConsole console;

public PythonInteractiveInterpreter(JConsole console) {
super();
this.console = console;
in = console.getIn();
out = console.getOut();
err = console.getErr();
setOut(out);
setErr(err);
}

@Override
public void run() {
boolean eof = false;
JavaCharStream stream = new JavaCharStream(in, 1, 1);

exec("_ps1 = sys.ps1");
PyObject ps1Obj = get("_ps1");
String ps1 = ps1Obj.toString();

exec("_ps2 = sys.ps2");
PyObject ps2Obj = get("_ps2");
String ps2 = ps2Obj.toString();
out.print(getDefaultBanner() + "\n");

out.print(ps1);
String line = "";

while (!eof) {
// try to sync up the console
System.out.flush();
System.err.flush();
Thread.yield(); // this helps a little

try {
boolean eol = false;
line = "";

while (!eol) {
char aChar = stream.readChar();
eol = (aChar == '\n');
if (!eol) {
line = line + aChar;
}
}

//hitting Enter at prompt returns a semicolon
//get rid of it since it returns an error when executed
if (line.equals(";")) {
line = "";
}

{
boolean retVal = push(line);

if (retVal) {
out.print(ps2);
} else {
out.print(ps1);
}
}
} catch (IOException ex) {
}
}
}
}


package meteoinfo.forms;

import bsh.util.JConsole;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import meteoinfo.classes.PythonInteractiveInterpreter;

/**
*
* @author yaqiang
*/
public class FrmConsole extends javax.swing.JDialog {

private FrmMain frmMain = null;

/**
* Creates new form FrmConsole
* @param parent
* @param modal
*/
public FrmConsole(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setSize(600, 400);

frmMain = (FrmMain)parent;
}

/**
* Initialize console
*/
public void InitializeConsole(){
JConsole console = new JConsole();
console.setPreferredSize(new Dimension(600, 400));
console.println(new ImageIcon(this.getClass().getResource("/meteoinfo/resources/jython_small_c.png")));
this.getContentPane().add(console, BorderLayout.CENTER);
PythonInteractiveInterpreter interp = new PythonInteractiveInterpreter(console);
interp.set("miapp", frmMain);
interp.exec("import sys");
new Thread(interp).start();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FrmConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FrmConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FrmConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FrmConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FrmConsole dialog = new FrmConsole(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}



*************************************************
Dr. Yaqiang Wang
Chinese Academy of Meteorological Sciences (CAMS)
46, Zhong-Guan-Cun South Avenue
Beijing, 100081
China
             
[hidden email]

http://www.meteothinker.com
**************************************************

------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users