[JRuby] Ruby and Swing: Profligacy

Discussion in 'Ruby' started by Charles Oliver Nutter, Oct 31, 2007.

  1. Profligacy is a JRuby/Swing framework created by Zed Shaw. It takes a
    different approach from the previously posted Cheri, in that it doesn't
    try to hide the Swing API. Instead, it just provides solutions for some
    of the ugliest parts of Swing like component layout.

    http://ihate.rubyforge.org/profligacy/

    From the page:

    "Profligacy is a JRuby library that makes building Swing Graphical User
    Interface much easier than with Raw code. It’s not a builder as with
    many other projects, but instead a simple Ruby way to structure the UI
    for the 80% common cases you’ll encounter."

    "The purpose of Profligacy is not to be a complete way of hiding Swing
    components from you. You’ll still be making JButtons and JLabels, you’ll
    just be putting them into a Ruby idiomatic code structure that doesn’t
    make your eyes hemorrhage diarrhea like when you try to code in Java."

    It's a much lighter-weight approach to doing Swing GUI development. The
    most interesting bit, however, is the layout engine:

    The only real innovation in Profligacy is a simpler way to configure a
    GroupLayout using a simple regex/wiki style syntax. This is a work in
    progress, but it should make building GUIs much much easier.

    Here's a medium-sized sample of building a simple GUI with layout in
    Profligacy:

    <code swing8_lel.rb>
    require 'profligacy/swing'
    require 'profligacy/lel'

    class PhoneBookDemo
    include_package 'javax.swing'
    include_package 'java.awt'
    include_package 'javax.swing.border'
    include Profligacy

    def initialize
    @search_style = :exact

    layout =
    "[>lab_name][*name][>lab_number][*number][*lab_options][<exact][<starts][<ends]"


    @ui = Swing::LEL.new(JFrame, layout) do |c,i|
    c.lab_name = JLabel.new "Name"
    c.lab_number = JLabel.new "Number"
    c.lab_options = JLabel.new "Search Options"
    c.name = JTextField.new 10
    c.number = JTextField.new 10

    c.exact = JRadioButton.new("Exact Match", true)
    c.starts = JRadioButton.new("Starts With")
    c.ends = JRadioButton.new("Ends With")

    b = ButtonGroup.new
    [c.exact, c.starts, c.ends].each {|x| b.add(x) }

    i.name = {:action => proc {|t,e| @ui.number.text = find(@names,
    e.source.text) } }
    i.number = {:action => proc {|t,e| @ui.name.text = find(@numbers,
    e.source.text) } }
    i.exact = {:action => proc {|t,e| @search_style = :exact } }
    i.starts = {:action => proc {|t,e| @search_style = :starts } }
    i.ends = {:action => proc {|t,e| @search_style = :ends } }
    end

    @ui.build:)args => "Phone Book")

    @names = {
    "Zed A. Shaw" => "917-555-5555",
    "Frank Blank" => "212-554-5555"
    }

    @numbers = {
    "917-555-5555" => "Zed A. Shaw",
    "212-554-5555" => "Frank Blank"
    }
    end

    protected
    def find(inside, text)
    results = case @search_style
    when :exact; inside.keys.grep /^#{text}$/
    when :starts; inside.keys.grep /^#{text}/
    when :ends; inside.keys.grep /#{text}$/
    end

    inside[results[0]] || ""
    end
    end

    SwingUtilities.invoke_later proc { PhoneBookDemo.new }.to_runnable
    </code>

    - Charlie
    Charles Oliver Nutter, Oct 31, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slim Baltagi
    Replies:
    0
    Views:
    391
    Slim Baltagi
    Dec 15, 2007
  2. Ronald Fischer
    Replies:
    2
    Views:
    202
    Scott Miller
    May 16, 2007
  3. Replies:
    0
    Views:
    133
  4. Axel Etzold

    profligacy / jruby gem question

    Axel Etzold, Aug 19, 2008, in forum: Ruby
    Replies:
    0
    Views:
    124
    Axel Etzold
    Aug 19, 2008
  5. Einar Magnús Boson

    jruby, profligacy

    Einar Magnús Boson, Nov 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    120
    James Britt
    Nov 20, 2008
Loading...

Share This Page