package com.glnpu.jruby; import java.util.ArrayList; import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.jruby.Ruby; import org.jruby.javasupport.JavaEmbedUtils; import org.jruby.runtime.builtin.IRubyObject; public class RunJRUBY extends Shell { private RunJRUBY run; private Text text; /** * Launch the application * @param args */ public static void main(String args[]) { try { Display display = Display.getDefault(); RunJRUBY shell = new RunJRUBY(display, SWT.SHELL_TRIM); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } catch (Exception e) { e.printStackTrace(); } } /** * Create the shell * @param display * @param style */ public RunJRUBY(Display display, int style) { super(display, style); run = this; createContents(); } /** * Create contents of the window */ protected void createContents() { setText("SWT Application"); setSize(500, 375); text = new Text(this, SWT.V_SCROLL | SWT.BORDER | SWT.WRAP | SWT.H_SCROLL); text.setBounds(0, 0, 492, 312); final Button button = new Button(this, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { Ruby runtime = Ruby.getDefaultInstance(); try { //允许传对象,作为参数给JRuby IRubyObject rootRubyObject = JavaEmbedUtils.newRuntimeAdapter().eval( runtime, text.getText() ); JavaEmbedUtils.invokeMethod( runtime, rootRubyObject, "run", new Object[] {run}, null ); //不传对象,直接运行JRbuy //runtime.evalScriptlet(text.getText()); } catch (Exception e1) { System.err.println(e1.toString()); e1.printStackTrace(); } } }); button.setText("button"); button.setBounds(335, 326, 48, 22); // } @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components } } |