使用JRuby为你的客户端助力

    预言了两天,终于决定在我们的RCP客户端中增加执行JRuby的功能。说是预言其实也没有什么好预言的,JRuby早有耳闻,Ruby也一直在学习。其实要解决的问题只有一个---解决Java实例如何给JRuby,然后有JRuby操作,其实不难,JRbuy官方的WIKI上有一个例子,但是那个例子有太多硬编码的问题,稍稍改造,将硬编码的内容抽取到JRuby中,就好了~

    我想说的其实是在RCP中加入JRuby的作用是:

    实施人员只需要写脚本就可以随意操作界面上的任意东西;

    使产品更进一步达到零二次开发的阶段;

    使用JRuby来开发SWT的界面,还是有比较复杂,在熟悉SWT开发和JRuby的情况下画一个比较复杂的界面时候就会非常复杂!这里还是建议使用类似于XSWT等XML界面描述语言,然后配合脚本完成功能。

下面给出一个可以在运行JRuby的SWTShell:

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
    }

}

下面是可以执行的JRuby代码:

require 'java'
module SWTTest
  include_package 'org.eclipse.swt'
  include_package 'org.eclipse.swt.layout'
  include_package 'org.eclipse.swt.widgets'
  include_package 'org.eclipse.swt.events'
end
    class TestDialog < SWTTest::Dialog
      @shell
      @parentShell
      def initialize shell
          super(shell, SWTTest::SWT::NONE)
          @parentShell = shell
          open
      end
      def open
          createContents()
          @shell.open();
          @shell.layout();       
      end
      def createContents
          @shell = SWTTest::Shell.new(@parentShell, SWTTest::SWT::DIALOG_TRIM | SWTTest::SWT::APPLICATION_MODAL)
          @shell.setSize(500, 375);
          @shell.setText("SWT Dialog");
          button = SWTTest::Button.new(@shell, SWTTest::SWT::PUSH)
          button.setText("Test Button 1")    
          button.setBounds(147, 116, 48, 22);
      end
    end
  class TestMain
      def run shell
          TestDialog.new shell
      end
  end
  TestMain.new

在JRuby代码的最下面有一个TestMain的类,主要是用于调用的~这一点是和其他的写法不同的!

至于它有多强大,就看大家怎么用了~而且java和JRuby是运行在同一个JVM之上的,它可以使用此JVM下的所有对象!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值