<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大夢(mèng)想家

    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 {
                        //允許傳對(duì)象,作為參數(shù)給JRuby
                        IRubyObject rootRubyObject = JavaEmbedUtils.newRuntimeAdapter().eval( runtime, text.getText() );
                        JavaEmbedUtils.invokeMethod( runtime, rootRubyObject, "run", new Object[] {run}, null );
                        //不傳對(duì)象,直接運(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下的所有對(duì)象!



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

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

    評(pí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/

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

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

    JRuby對(duì)Swing的封裝,不過我用的是SWT,而且主要是用它在于我的程序交互,更類似于Office的VBA的功能!

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

    自己定義個(gè)DSL不是更爽?ruby搞這個(gè)是小case
    主站蜘蛛池模板: 激情亚洲一区国产精品| 亚洲成av人片在www鸭子| 亚洲中文字幕无码亚洲成A人片| 日本一区二区三区在线视频观看免费 | 亚洲精品国产免费| 亚洲免费在线观看| 亚洲欧美日韩中文字幕在线一区| 免费视频爱爱太爽了| 西西人体44rt高清亚洲 | 国产偷国产偷亚洲高清在线| 最近免费中文字幕4| 在线观看亚洲一区二区| 国产免费网站看v片在线| 免费真实播放国产乱子伦| 最新亚洲春色Av无码专区| 全免费a级毛片免费**视频| 亚洲毛片在线免费观看| 中文字幕无码成人免费视频| 亚洲男人电影天堂| 亚洲精品无码AV中文字幕电影网站| 2019中文字幕在线电影免费| 一级毛片在线完整免费观看| 在线a亚洲v天堂网2018| 四虎影视久久久免费| 国内精品99亚洲免费高清| 美女网站在线观看视频免费的| 国产亚洲精品久久久久秋霞| 在线jyzzjyzz免费视频| 人碰人碰人成人免费视频| 亚洲国产成人超福利久久精品| 久久精品网站免费观看| 国产精品亚洲lv粉色| 久久精品亚洲福利| 777成影片免费观看| 亚洲日韩一区精品射精| 亚洲午夜视频在线观看| 69成人免费视频无码专区| 一级毛片免费视频| 亚洲AV无码专区国产乱码不卡| 中文字幕亚洲图片| 国产精品成人无码免费|