﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-实践-全程-随笔分类-JRuby</title><link>http://www.blogjava.net/leeguannan/category/29927.html</link><description>够了，让我们实践吧！</description><language>zh-cn</language><lastBuildDate>Fri, 07 Mar 2008 01:59:52 GMT</lastBuildDate><pubDate>Fri, 07 Mar 2008 01:59:52 GMT</pubDate><ttl>60</ttl><item><title>使用JRuby为你的客户端助力</title><link>http://www.blogjava.net/leeguannan/archive/2008/03/07/184390.html</link><dc:creator>阿南</dc:creator><author>阿南</author><pubDate>Fri, 07 Mar 2008 01:20:00 GMT</pubDate><guid>http://www.blogjava.net/leeguannan/archive/2008/03/07/184390.html</guid><wfw:comment>http://www.blogjava.net/leeguannan/comments/184390.html</wfw:comment><comments>http://www.blogjava.net/leeguannan/archive/2008/03/07/184390.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/leeguannan/comments/commentRss/184390.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/leeguannan/services/trackbacks/184390.html</trackback:ping><description><![CDATA[<p>&#160;&#160;&#160; 预言了两天，终于决定在我们的RCP客户端中增加执行JRuby的功能。说是预言其实也没有什么好预言的，JRuby早有耳闻，Ruby也一直在学习。其实要解决的问题只有一个---解决Java实例如何给JRuby，然后有JRuby操作，其实不难，JRbuy官方的WIKI上有一个例子，但是那个例子有太多硬编码的问题，稍稍改造，将硬编码的内容抽取到JRuby中，就好了~</p>  <p>&#160;&#160;&#160; 我想说的其实是在RCP中加入JRuby的作用是：</p>  <p>&#160;&#160;&#160; 实施人员只需要写脚本就可以随意操作界面上的任意东西；</p>  <p>&#160;&#160;&#160; 使产品更进一步达到零二次开发的阶段；</p>  <p>&#160;&#160;&#160; 使用JRuby来开发SWT的界面，还是有比较复杂，在熟悉SWT开发和JRuby的情况下画一个比较复杂的界面时候就会非常复杂！这里还是建议使用类似于XSWT等XML界面描述语言，然后配合脚本完成功能。</p>  <p>下面给出一个可以在运行JRuby的SWTShell：</p>  <table cellspacing="0" cellpadding="2" width="545" border="0"><tbody>     <tr>       <td valign="top" width="543">         <p>package com.glnpu.jruby; </p>          <p>import java.util.ArrayList;           <br />import java.util.List; </p>          <p>import org.eclipse.swt.SWT;           <br />import org.eclipse.swt.events.SelectionAdapter;            <br />import org.eclipse.swt.events.SelectionEvent;            <br />import org.eclipse.swt.widgets.Button;            <br />import org.eclipse.swt.widgets.Display;            <br />import org.eclipse.swt.widgets.Shell;            <br />import org.eclipse.swt.widgets.Text;            <br />import org.jruby.Ruby;            <br />import org.jruby.javasupport.JavaEmbedUtils;            <br />import org.jruby.runtime.builtin.IRubyObject; </p>          <p>public class RunJRUBY extends Shell { </p>          <p>&#160;&#160;&#160; private RunJRUBY run;           <br />&#160;&#160;&#160; private Text text;            <br />&#160;&#160;&#160; /**            <br />&#160;&#160;&#160;&#160; * Launch the application            <br />&#160;&#160;&#160;&#160; * @param args            <br />&#160;&#160;&#160;&#160; */            <br />&#160;&#160;&#160; public static void main(String args[]) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Display display = Display.getDefault();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RunJRUBY shell = new RunJRUBY(display, SWT.SHELL_TRIM);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; shell.open();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; shell.layout();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; while (!shell.isDisposed()) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!display.readAndDispatch())            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; display.sleep();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (Exception e) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e.printStackTrace();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160; } </p>          <p>&#160;&#160;&#160; /**           <br />&#160;&#160;&#160;&#160; * Create the shell            <br />&#160;&#160;&#160;&#160; * @param display            <br />&#160;&#160;&#160;&#160; * @param style            <br />&#160;&#160;&#160;&#160; */            <br />&#160;&#160;&#160; public RunJRUBY(Display display, int style) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; super(display, style);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; run = this;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; createContents();            <br />&#160;&#160;&#160; } </p>          <p>&#160;&#160;&#160; /**           <br />&#160;&#160;&#160;&#160; * Create contents of the window            <br />&#160;&#160;&#160;&#160; */            <br />&#160;&#160;&#160; protected void createContents() {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; setText(&quot;SWT Application&quot;);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; setSize(500, 375); </p>          <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; text = new Text(this, SWT.V_SCROLL | SWT.BORDER | SWT.WRAP | SWT.H_SCROLL);           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; text.setBounds(0, 0, 492, 312); </p>          <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; final Button button = new Button(this, SWT.NONE);           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; button.addSelectionListener(new SelectionAdapter() {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; public void widgetSelected(final SelectionEvent e) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Ruby runtime = Ruby.getDefaultInstance();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //允许传对象，作为参数给JRuby            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IRubyObject rootRubyObject = JavaEmbedUtils.newRuntimeAdapter().eval( runtime, text.getText() );            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; JavaEmbedUtils.invokeMethod( runtime, rootRubyObject, &quot;run&quot;, new Object[] {run}, null );            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //不传对象，直接运行JRbuy            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //runtime.evalScriptlet(text.getText());            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (Exception e1) {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.err.println(e1.toString());            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e1.printStackTrace();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; });            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; button.setText(&quot;button&quot;);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; button.setBounds(335, 326, 48, 22);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; //            <br />&#160;&#160;&#160; } </p>          <p>&#160;&#160;&#160; @Override           <br />&#160;&#160;&#160; protected void checkSubclass() {            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Disable the check that prevents subclassing of SWT components            <br />&#160;&#160;&#160; } </p>          <p>}</p>       </td>     </tr>   </tbody></table>  <p>下面是可以执行的JRuby代码：</p>  <table cellspacing="0" cellpadding="2" width="549" border="0"><tbody>     <tr>       <td valign="top" width="547">         <p>require 'java'           <br />module SWTTest            <br />&#160; include_package 'org.eclipse.swt'            <br />&#160; include_package 'org.eclipse.swt.layout'            <br />&#160; include_package 'org.eclipse.swt.widgets'            <br />&#160; include_package 'org.eclipse.swt.events'            <br />end            <br />&#160;&#160;&#160; class TestDialog &lt; SWTTest::Dialog            <br />&#160;&#160;&#160;&#160;&#160; @shell            <br />&#160;&#160;&#160;&#160;&#160; @parentShell            <br />&#160;&#160;&#160;&#160;&#160; def initialize shell            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; super(shell, SWTTest::SWT::NONE)            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @parentShell = shell            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; open            <br />&#160;&#160;&#160;&#160;&#160; end            <br />&#160;&#160;&#160;&#160;&#160; def open            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; createContents()            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @shell.open();            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @shell.layout();&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160; end            <br />&#160;&#160;&#160;&#160;&#160; def createContents            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @shell = SWTTest::Shell.new(@parentShell, SWTTest::SWT::DIALOG_TRIM | SWTTest::SWT::APPLICATION_MODAL)            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @shell.setSize(500, 375);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @shell.setText(&quot;SWT Dialog&quot;);            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; button = SWTTest::Button.new(@shell, SWTTest::SWT::PUSH)            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; button.setText(&quot;Test Button 1&quot;)&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; button.setBounds(147, 116, 48, 22);            <br />&#160;&#160;&#160;&#160;&#160; end            <br />&#160;&#160;&#160; end            <br />&#160; class TestMain            <br />&#160;&#160;&#160;&#160;&#160; def run shell            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TestDialog.new shell            <br />&#160;&#160;&#160;&#160;&#160; end            <br />&#160; end            <br />&#160; TestMain.new</p>       </td>     </tr>   </tbody></table>  <p>在JRuby代码的最下面有一个TestMain的类，主要是用于调用的~这一点是和其他的写法不同的！</p>  <p>至于它有多强大，就看大家怎么用了~而且java和JRuby是运行在同一个JVM之上的，它可以使用此JVM下的所有对象！</p><img src ="http://www.blogjava.net/leeguannan/aggbug/184390.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/leeguannan/" target="_blank">阿南</a> 2008-03-07 09:20 <a href="http://www.blogjava.net/leeguannan/archive/2008/03/07/184390.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>