大夢想家

          5年開發(fā)工程師,2年實(shí)施經(jīng)理,X年售前顧問,......
          數(shù)據(jù)加載中……
          使用JRuby為你的客戶端助力

              預(yù)言了兩天,終于決定在我們的RCP客戶端中增加執(zhí)行JRuby的功能。說是預(yù)言其實(shí)也沒有什么好預(yù)言的,JRuby早有耳聞,Ruby也一直在學(xué)習(xí)。其實(shí)要解決的問題只有一個(gè)---解決Java實(shí)例如何給JRuby,然后有JRuby操作,其實(shí)不難,JRbuy官方的WIKI上有一個(gè)例子,但是那個(gè)例子有太多硬編碼的問題,稍稍改造,將硬編碼的內(nèi)容抽取到JRuby中,就好了~

              我想說的其實(shí)是在RCP中加入JRuby的作用是:

              實(shí)施人員只需要寫腳本就可以隨意操作界面上的任意東西;

              使產(chǎn)品更進(jìn)一步達(dá)到零二次開發(fā)的階段;

              使用JRuby來開發(fā)SWT的界面,還是有比較復(fù)雜,在熟悉SWT開發(fā)和JRuby的情況下畫一個(gè)比較復(fù)雜的界面時(shí)候就會(huì)非常復(fù)雜!這里還是建議使用類似于XSWT等XML界面描述語言,然后配合腳本完成功能。

          下面給出一個(gè)可以在運(yùn)行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 {
                              //允許傳對象,作為參數(shù)給JRuby
                              IRubyObject rootRubyObject = JavaEmbedUtils.newRuntimeAdapter().eval( runtime, text.getText() );
                              JavaEmbedUtils.invokeMethod( runtime, rootRubyObject, "run", new Object[] {run}, null );
                              //不傳對象,直接運(yùn)行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
              }

          }

          下面是可以執(zhí)行的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代碼的最下面有一個(gè)TestMain的類,主要是用于調(diào)用的~這一點(diǎn)是和其他的寫法不同的!

          至于它有多強(qiáng)大,就看大家怎么用了~而且java和JRuby是運(yùn)行在同一個(gè)JVM之上的,它可以使用此JVM下的所有對象!



          客戶虐我千百遍,我待客戶如初戀!

          posted on 2008-03-07 09:20 阿南 閱讀(1297) 評論(4)  編輯  收藏 所屬分類: 個(gè)人原創(chuàng)

          評論

          # re: 使用JRuby為你的客戶端助力 2008-03-07 11:00 dennis

          hi,Jruby已經(jīng)有GUI方面的DSL,封裝了swing,可以“描述”界面咯??纯催@個(gè) http://www.alef1.org/jean/swiby/ 和這個(gè) http://cheri.rubyforge.org/
            回復(fù)  更多評論    

          # re: 使用JRuby為你的客戶端助力 2008-03-07 12:55 魔域私服

            回復(fù)  更多評論    

          # re: 使用JRuby為你的客戶端助力 2008-03-07 14:17 阿南

          JRuby對Swing的封裝,不過我用的是SWT,而且主要是用它在于我的程序交互,更類似于Office的VBA的功能!
            回復(fù)  更多評論    

          # re: 使用JRuby為你的客戶端助力 2008-03-07 18:57 dennis

          自己定義個(gè)DSL不是更爽?ruby搞這個(gè)是小case
            回復(fù)  更多評論    
          主站蜘蛛池模板: 内乡县| 衡水市| 潮州市| 上犹县| 中宁县| 灵武市| 井冈山市| 彰化县| 额尔古纳市| 五莲县| 安乡县| 上思县| 绥化市| 永靖县| 长沙市| 瑞安市| 中卫市| 临澧县| 苏尼特右旗| 洛南县| 郓城县| 扎囊县| 聊城市| 龙门县| 博客| 安泽县| 洛宁县| 招远市| 神农架林区| 资中县| 青海省| 凌源市| 和龙市| 德州市| 象山县| 广西| 个旧市| 怀集县| 抚顺县| 乌兰县| 江安县|