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

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

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

    大夢想家

    5年開發工程師,2年實施經理,X年售前顧問,......
    數據加載中……
    使用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下的所有對象!



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

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

    評論

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

    hi,Jruby已經有GUI方面的DSL,封裝了swing,可以“描述”界面咯。看看這個 http://www.alef1.org/jean/swiby/ 和這個 http://cheri.rubyforge.org/
      回復  更多評論    

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

      回復  更多評論    

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

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

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

    自己定義個DSL不是更爽?ruby搞這個是小case
      回復  更多評論    
    主站蜘蛛池模板: 亚洲欧洲日韩国产一区二区三区| 精品久久香蕉国产线看观看亚洲| 久久精品国产亚洲AV电影| 国产精品免费久久久久久久久| www.亚洲色图| 无码 免费 国产在线观看91| 免费**毛片在线播放直播| 国产一区二区三区亚洲综合 | 久草福利资源网站免费| 国产亚洲精品资源在线26u| 91免费福利视频| 亚洲国产成人久久综合一| 污污网站18禁在线永久免费观看| 亚洲VA中文字幕无码毛片| 美丽姑娘免费观看在线观看中文版| 亚洲日本一区二区三区| 国产精品1024永久免费视频 | 久久99亚洲网美利坚合众国| 222www在线观看免费| 久久精品国产99国产精品亚洲 | 亚洲日本中文字幕一区二区三区| 国产精品免费久久久久久久久| 亚洲av无码一区二区三区网站| 1000部啪啪毛片免费看| 亚洲 欧洲 自拍 另类 校园| 国产精品久久免费视频| 久草免费福利在线| 久久综合亚洲鲁鲁五月天| 成人黄软件网18免费下载成人黄18免费视频 | 免费99精品国产自在现线| 亚洲欧洲无码一区二区三区| 亚洲AV无码乱码精品国产| 男女作爱在线播放免费网站| 亚洲无线一二三四区| 国产精品久久久久影院免费| 成全视成人免费观看在线看| 亚洲人成影院77777| 亚洲免费日韩无码系列| 最近中文字幕无免费| 黄色一级视频免费| 中文字幕亚洲综合精品一区|