Advertisement
Guest User

Untitled

a guest
Aug 21st, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.io.PrintStream;
  2. import java.io.PrintWriter;
  3. import java.io.StringWriter;
  4.  
  5. import javax.script.ScriptEngine;
  6. import javax.script.ScriptEngineFactory;
  7.  
  8. import org.eclipse.swt.SWT;
  9. import org.eclipse.swt.layout.FillLayout;
  10. import org.eclipse.swt.layout.GridData;
  11. import org.eclipse.swt.widgets.Button;
  12. import org.eclipse.swt.widgets.Display;
  13. import org.eclipse.swt.widgets.Event;
  14. import org.eclipse.swt.widgets.Label;
  15. import org.eclipse.swt.widgets.Listener;
  16. import org.eclipse.swt.widgets.Shell;
  17. import org.eclipse.swt.widgets.Text;
  18. import org.python.core.PyCode;
  19. import org.python.core.PyException;
  20. import org.python.core.PyObject;
  21. import org.python.jsr223.PyScriptEngine;
  22. import org.python.jsr223.PyScriptEngineFactory;
  23. import org.python.util.PythonInterpreter;
  24.  
  25. public class JythonWindow {
  26.  
  27.     /**
  28.      * @param args
  29.      */
  30.     public static void main(String[] args) {
  31.         Display display = new Display();
  32.        
  33.         Shell shell = new Shell();
  34.         shell.setText("Enter your Jython code!");
  35.         shell.setLayout(new FillLayout());
  36.        
  37.         final Text textArea = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
  38.         final Button button = new Button(shell, SWT.PUSH);
  39.         final Label label = new Label(shell, SWT.PUSH);
  40.        
  41.         textArea.setLayoutData(new GridData(GridData.FILL_BOTH));      
  42.         button.setText("Evaluate!");
  43.         button.addListener(SWT.Selection, new Listener() {
  44.             public void handleEvent(Event e) {
  45.                 switch (e.type) {
  46.                 case SWT.Selection:
  47.                   String result = evaluateJython(textArea.getText());
  48.                   label.setText("Jython's script result is:\n" + result);
  49.                   break;
  50.                 }
  51.             }
  52.         });    
  53.  
  54.         shell.open();
  55.         while (!shell.isDisposed()) {
  56.             if (!display.readAndDispatch()) {
  57.                
  58.                 display.sleep();
  59.             }
  60.         }
  61.         display.dispose();
  62.  
  63.     }
  64.  
  65.     protected static String evaluateJython(String script) {
  66.         try {          
  67.             PythonInterpreter interop = new PythonInterpreter();
  68.             PyCode code = interop.compile(script);
  69.             PyObject result = interop.eval(code);
  70.             return result.toString();
  71.         } catch (Throwable e) {
  72.             StringWriter sw = new StringWriter();
  73.             PrintWriter pw = new PrintWriter(sw);
  74.             e.printStackTrace(pw);
  75.             return sw.toString();
  76.         }      
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement