import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; import javax.script.ScriptEngine; import javax.script.ScriptEngineFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.python.core.PyCode; import org.python.core.PyException; import org.python.core.PyObject; import org.python.jsr223.PyScriptEngine; import org.python.jsr223.PyScriptEngineFactory; import org.python.util.PythonInterpreter; public class JythonWindow { /** * @param args */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(); shell.setText("Enter your Jython code!"); shell.setLayout(new FillLayout()); final Text textArea = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL); final Button button = new Button(shell, SWT.PUSH); final Label label = new Label(shell, SWT.PUSH); textArea.setLayoutData(new GridData(GridData.FILL_BOTH)); button.setText("Evaluate!"); button.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { switch (e.type) { case SWT.Selection: String result = evaluateJython(textArea.getText()); label.setText("Jython's script result is:\n" + result); break; } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } protected static String evaluateJython(String script) { try { PythonInterpreter interop = new PythonInterpreter(); PyCode code = interop.compile(script); PyObject result = interop.eval(code); return result.toString(); } catch (Throwable e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); return sw.toString(); } } }