Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo

1

Desktop Application with Ruby



                  Getting started with

2

I am...

                        PHP ZEND
                        KOHANA JSE JEE
                        Ruby GWT JavaScript
                        jQuery sinatra
                        BackboneJS CSS3
                        HTML5 MySQL Drupal
Anis Uddin Ahmad        MongoDB PHPUNIT Groovy
CTO & Co-Founder        JRuby Symfony2 SWING
WNeeds Ltd.
                        sqlite Doctrain solr Phing grails ...

3

!=

4

Ruby is not ONLY for web

5

It's a generic purpose language

6

generic purpose?
●
    Web Application
●
    Desktop Application
●
    Mobile Application (Yes iPhone too!)
●
    DSLs
●
    Antyhing you can think*

7

So, Ruby can make Desktop App?




                  Okay...
                 But HOW?

8

Many Ways!

  Shoes       Ruby-GNOME2 / GTK

WxRuby                 Ruby-Tk

    Ruby Cocoa / MacRuby

QtRuby                JRuby + Swing

         FxRuby      JRuby + SWT

9

Many Ways!

  Shoes       Ruby-GNOME2 / GTK

WxRuby                 Ruby-Tk

    Ruby Cocoa / MacRuby

QtRuby            JRuby + Swing
         FxRuby    JRuby + SWT

10

Shoes




 cross-platform toolkit for
writing graphical apps easily
  and artfully using Ruby

11

Starting with Shoes
●
    Download (http://shoesrb.com/downloads))
●
    Run Shoes

12

Starting with Shoes
●
    Download (http://shoesrb.com/downloads))
●
    Run Shoes

13

Shoes example
Shoes.app :width => 300, :height => 200 do
  button("Click me!") { alert("Good job.") }
end

14

Shoes example
Shoes.app :width => 300, :height => 200 do
  button("Click me!") { alert("Good job.") }
end

15

Shoes example (Clock)
Shoes.app do
  @time = title "0:00"
  every 1 do
    @time.replace(Time.now.strftime("%I:%M %p"))
  end
end

16

Hackety Hack!




   http://hackety.com/

17

Packaging Shoes

18

JRuby
The Ruby Programming Language on the JVM

19

Ruby for desktop application? Yes we can!

20

The Redcar Editor




    http://redcareditor.com/

21

Why JRuby?
●
    High performance

22

Why JRuby?
●
    High performance
●
    Real threading

23

Why JRuby?
●
    High performance
●
    Real threading
●
    Vast array of libraries (gems + JARs)

24

Why JRuby?
●
    High performance
●
    Real threading
●
    Vast array of libraries (gems + JARs)
●
    Platform Independent

25

Why JRuby?
●
    High performance
●
    Real threading
●
    Vast array of libraries (gems + JARs)
●
    Platform Independent
●
    Enterprise Acceptance

26

Why JRuby?
●
    High performance
●
    Real threading
●
    Vast array of libraries (gems + JARs)
●
    Platform Independent
●
    Enterprise Acceptance



          The best of both worlds!

27

Let's make with JRuby!

28

Get JRuby
●
     Download
●
     Extract
●
     Add bin subdirectory to your $PATH
      –   (PATH=path/to/jruby/bin:$PATH)
●
     Test it: jruby -v




●
    Assuming you have jdk 1.7 installed
●
    Can be installed with rvm too

29

Import Java Swing in .rb
include Java

import javax.swing.JFrame
import javax.swing.JComboBox
import javax.swing.JButton
import javax.swing.JPanel
import javax.swing.JLabel
import javax.swing.JTextField

import java.awt.GridLayout

30

Make a Frame (JFrame)
class NumberConverter < JFrame

   def initialize
     super('Number Format Converter')

      set_size(400,140);
      set_visible(true);
       set_default_close_operation(JFrame::EXIT_ON_CLOSE);
   end

end

num_converter = NumberConverter.new

31

Let's run it!
$ cd go/to/source/dir
$ jruby number_converter.rb

32

Place container
super('Number Format Converter')

main = JPanel.new;

get_content_pane().add("Center", main);

33

Set Layout
super('Number Format Converter')

main = JPanel.new;
main.set_layout(GridLayout.new(3,3,2,2))

get_content_pane().add("Center", main)

34

Add Components
main.set_layout(GridLayout.new(3,3,2,2))

main.add(JLabel.new("Convert From : ", JLabel::RIGHT))
main.add(@cmbFrom = JComboBox.new)
main.add(@input = JTextField.new(15))

35

Where are they going?
main.set_layout(GridLayout.new(3,3,2,2))

main.add(JLabel.new("Convert From : ", JLabel::RIGHT))
main.add(@cmbFrom = JComboBox.new)
main.add(@input = JTextField.new(15))




              1           2               3

              4           5               6

              7           8               9

36

Add the rest of
main.add(@input = JTextField.new(15))

# Second row
main.add(JLabel.new("Convert To : ", JLabel::RIGHT));
main.add(@cmbTo = JComboBox.new);
main.add(result = JTextField.new(15));

# Third row
main.add(JLabel.new(" ")); # Leave this cell blank
main.add(btn = JButton.new("CONVERT"));

37

Let's run again!
$ cd go/to/source/dir
$ jruby number_converter.rb

38

More with components
main.add(result = JTextField.new(15));
result.set_editable(false);

main.add(btn = Jbutton.new("CONVERT"));

# Add options to comboBoxes
num_formats = ["Decimal", "Binary", "HexaDecimal", "Octal"]
num_formats.each{|format| @cmbFrom.add_item format }
num_formats.each{|format| @cmbTo.add_item format }

39

Set Event Handler
main.add(btn = Jbutton.new("CONVERT"));
btn.add_action_listener do |evt|
   result.set_text(convert.upcase);
end

40

Set Event Handler cont.
main.add(btn = Jbutton.new("CONVERT"));
btn.add_action_listener do |evt|
   result.set_text(convert.upcase);
end




  def convert
     # Take the value of @input
     # Take formats form @cmbFrom and @cmbTo
     # Convert to required format
  end

end # end of class NumFormat

41

Yep! It's Done!!




https://github.com/ajaxray/jruby-swing

42

Developing cross platform desktop application with Ruby

43

Java <=> JRuby Transformation

44

Java <=> JRuby Transformation
new Car(color, wheels)
Car.new color, wheels

45

Java <=> JRuby Transformation
JLabel.LEFT
JLabel::LEFT

46

Java <=> JRuby Transformation
Obj.longFuncName();
Obj.long_func_name

47

Java <=> JRuby Transformation
String[] options = {"all", "any"};
combo = new JcomboBox(options);


options.each{ |format|
    combo.add_item format
}

48

Java <=> JRuby Transformation
btn.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent ae) {
          doSomething();
      }
});


btn.add_action_listener do |evt|
  do_something
end

49

Deployment




                             Warbler

http://rawr.rubyforge.org/   https://github.com/jruby/warbler

50

BTW, JRuby is not ONLY for Desktop
●
    It's just Ruby*
●
    Rails just works




    *Here are differences- https://github.com/jruby/jruby/wiki/DifferencesBetweenMriAndJruby

51

JRuby Frameworks
                   https://github.com/jruby/jruby/wiki/GUIFrameworks

●
    Cheri::Swing
●
    Limelight
●
    Monkeybars
●
    RSwing
●
    Rubeus
●
    Swiby

52

JRuby Frameworks
                   https://github.com/jruby/jruby/wiki/GUIFrameworks

●
    Cheri::Swing
●
    Limelight
●
    Monkeybars
●
    RSwing
●
    Rubeus
●
    Swiby

Frame.new("hello, world") do |frame|
  frame.default_close_operation = :exit_on_close
  frame.size = [200, 200]
  ...

53

Questions?

54

Developing cross platform desktop application with Ruby

More Related Content

Developing cross platform desktop application with Ruby