37
Desktop Application using JRuby + SWT 呂旺燊

2012 java two-desktop-appliction-using-j-ruby-with-swt

  • Upload
    tka

  • View
    326

  • Download
    4

Embed Size (px)

Citation preview

Page 1: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Desktop Application using JRuby + SWT呂旺燊

Page 2: 2012 java two-desktop-appliction-using-j-ruby-with-swt

專家講座 C :Desktop Application using JRuby + SWT

Page 3: 2012 java two-desktop-appliction-using-j-ruby-with-swt

個人簡介

呂旺燊 Twitter: @tkalu RoR programmer 任職於和多 handlino.com

Page 4: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Fire.app

http://fireapp.handlino.com/

Page 5: 2012 java two-desktop-appliction-using-j-ruby-with-swt

HTML prototyping tool網頁原型設計工具

HTML + CSS + JavaScript

Page 6: 2012 java two-desktop-appliction-using-j-ruby-with-swt

HTML

● ERB

● HAML

● Slim

● Markdown

支援多種 Template language

Page 7: 2012 java two-desktop-appliction-using-j-ruby-with-swt

CSS

http://sass-lang.com/ http://compass-style.org/

Page 8: 2012 java two-desktop-appliction-using-j-ruby-with-swt

JavaScript

http://coffeescript.org/

Page 9: 2012 java two-desktop-appliction-using-j-ruby-with-swt

ServerSimple Web Server可以在瀏覽器裏面透過 http://127.0.0.1:24681的網址瀏覽專案內容

Support LiveReload protocol當檔案修改之後 , 瀏覽器會自動更新Ref.● http://livereload.com/● LiveReload Protocol

Page 10: 2012 java two-desktop-appliction-using-j-ruby-with-swt

跨平台支援

Linux OS X Windows

Page 11: 2012 java two-desktop-appliction-using-j-ruby-with-swt

簡潔的介面 - 1

Page 12: 2012 java two-desktop-appliction-using-j-ruby-with-swt

簡潔的介面 - 2

Page 13: 2012 java two-desktop-appliction-using-j-ruby-with-swt

為什麼打造 Fire.app

故事要從遇見 Compass 說起了 ......

Page 14: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Compass Awesome!!!

from http://sonspring.com/journal/sass-for-designers

Page 15: 2012 java two-desktop-appliction-using-j-ruby-with-swt

痛恨 command line....

Page 16: 2012 java two-desktop-appliction-using-j-ruby-with-swt

需要跨平台

Page 17: 2012 java two-desktop-appliction-using-j-ruby-with-swt

需要 GUI - 1

● Tk● wxRuby● qtRuby● Ruby/GTK● Shoes● FXRuby● MacRuby

Ruby GUI Library

Cross-Platform

Page 18: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Eclipse

Java + SWT

JRuby + SWT

需要 GUI - 2

Page 19: 2012 java two-desktop-appliction-using-j-ruby-with-swt

成果

2010.12 推出 Compass.app

2012.04 推出 Fire.app

Page 20: 2012 java two-desktop-appliction-using-j-ruby-with-swt

為什麼選擇 JRuby+SWT?

誰需要 JRuby + SWT?

或是

Page 21: 2012 java two-desktop-appliction-using-j-ruby-with-swt

誰需要 JRuby + SWT ?

1. 熟悉 Ruby 想寫 GUIa

2. 想使用的 Library 只有 Ruby 的版本

3. 很熟 SWT 想改用 Ruby 的語法寫程式

Page 22: 2012 java two-desktop-appliction-using-j-ruby-with-swt

回到正題

如何用 JRuby 搭配 SWT 開發程式

Page 23: 2012 java two-desktop-appliction-using-j-ruby-with-swt

JRuby 中使用 Java 程式

#使用 Java 的環境require 'java'

#載入指定的 jar 檔require 'path/to/mycode.jar'

#取得目前的 Java 版號java.lang.System.getProperties["java.runtime.version"]

更多資料請參考https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby

Page 24: 2012 java two-desktop-appliction-using-j-ruby-with-swt

載入 SWT

前往 SWT 官網下載檔案後解壓縮後可以取得 swt.jar 後 , 透過下列兩行程式碼即可載入 SWT

require "java"require "download/path/swt"

Page 25: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Hellow Word! display = org.eclipse.swt.widgets.Display.new shell = org.eclipse.swt.widgets.Shell.new(display) row_layout= org.eclipse.swt.layout.RowLayout.new(org.eclipse.swt.SWT::HORIZONTAL) shell.setLayout( row_layout)

label = org.eclipse.swt.widgets.Label.new(shell, org.eclipse.swt.SWT::HORIZONTAL ) label.setText( 'Hello World!' ) shell.open while(!shell.is_disposed) do display.sleep if(!display.read_and_dispatch) end

display.dispose

Page 26: 2012 java two-desktop-appliction-using-j-ruby-with-swt

更加的 Ruby 風格

module Swt import org.eclipse.swt.SWT import org.eclipse.swt.program.Program

module Widgets import org.eclipse.swt.widgets.Display import org.eclipse.swt.widgets.Label import org.eclipse.swt.widgets.Shell end module Layout import org.eclipse.swt.layout.RowLayout endend

Page 27: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Hello World! 加強版

display = Swt::Widgets::Display.get_currentshell = Swt::Widgets::Shell.new(display)shell.layout = Swt::Layout::RowLayout.new(Swt::SWT::HORIZONTAL)label = Swt::Widgets::Label.new(shell, Swt::SWT::HORIZONTAL )label.text = 'Hello World!'shell.open while(!shell.is_disposed) do display.sleep if(!display.read_and_dispatch) end

display.dispose

Page 28: 2012 java two-desktop-appliction-using-j-ruby-with-swt

其他開發心得

● SWT 跨平台問題●調整啟動速度●打包程式● Mac 上隱藏 Dock 的圖示

Page 29: 2012 java two-desktop-appliction-using-j-ruby-with-swt

SWT 跨平台問題SWT_LIB_PATH ="#{LIB_PATH}/swt"

if org.jruby.platform.Platform::IS_MAC os="osx"elsif org.jruby.platform.Platform::IS_LINUX os="linux"elsif org.jruby.platform.Platform::IS_WINDOWS os="win"end

if org.jruby.platform.Platform::ARCH =~ /64/ arch="64"else arch="32"end

require "#{SWT_LIB_PATH}/swt_#{os}#{arch}"

Page 30: 2012 java two-desktop-appliction-using-j-ruby-with-swt

調整啟動速度

1.不要使用 rubygem 管理 library

require "rubygems" 會花上約 0.7 秒

建議自己將需要的 library 的路徑加到 $LOAD_PATH

2. 需要使用時才 require library

JRuby require 的成本十分龐大 , 建議要用到的時候才進行 require 的動作 ,而不是程式一啟動就將需要 library

全部載入

其他技巧還可參考http://headius.blogspot.com/2010/03/jruby-startup-time-tips.html

Page 31: 2012 java two-desktop-appliction-using-j-ruby-with-swt

打包程式使用 Rawr http://github.com/rawr/rawr Rawr 包裝了launch4j提供 JRuby程式更加方便的打包方式

安裝 rawr 到系統 : gem install rawr建立專案 : rawr install {project_path}打包 Mac 程式: rake rawr:bundle:app打包 Windows 程式: rake rawr:bundle:exe

Page 32: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Mac 上隱藏 Dock 的圖示

在 OSX 中使用 SWT 的程式會預設在 Dock 中顯示圖示 ,如果想要做出 只顯示在 system tray 上面的效果 ,需會要修改 Info.plist,必須在 <dict> 中添加下列內容 :

<key>NSUIElement</key><string>1</string>

Page 33: 2012 java two-desktop-appliction-using-j-ruby-with-swt

範例程式

請前往下方 Github 網址下載https://github.com/tka/jruby-rawr-swt-helloworld

Page 34: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Live Demo

如果有時間的話 .....

Page 35: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Thank You

Page 36: 2012 java two-desktop-appliction-using-j-ruby-with-swt

如果還有時間 ....

JRuby on Androidhttp://ruboto.org/

Page 37: 2012 java two-desktop-appliction-using-j-ruby-with-swt

Thank You