Table of content

The word puzzle game
A simple form written with Swiby
A tool: the console
The same form with apply/restore buttons
Changing the look and feel
Installing as a rubygem
Running an application from a remote site
The Swiby caching system

A simple form written for Swiby

The transfer_form.rb script is the version that opens a frame, as a standalone application.

To run the script move to the code directory and execute:

If you change the value, here the to account, with an invalid and click the ok button, a error message is displayed.

If you set a valid account number and change other data (like the amount and the from account).

When you click on the ok button, it copies the data from the input fields to the underlying object. Then, it shows a summary of the transfer. The symmary uses the account object's values.

The code is

 91 transfer_form = form do
 92 
 93   title "Transfer Form"
 94 
 95   width 400
 96 
 97   content do
 98     data current
 99     input "Date", :date
100     section
101     input "Amount", :amount
102     next_row
103     section "From"
104     combo "Account", my_accounts, :account_from do |selection| 
105       context['account_from.owner'].value = selection.owner
106       context['account_from.address'].value = selection.address
107     end
108     input "Name", :account_from / :owner, :readonly => true
109     input "Address", :account_from / :address, :readonly => true
110     section "To"
111     input "Account", :account_to / :number
112     input "Name", :account_to / :owner
113     input "Address", :account_to / :address
114     button "Save beneficiary"
115     next_row
116     command :ok, :cancel do
117       
118       def on_validate
119         
120         acc_number = values['account_to.number'].value
121         
122         if Account.valid?(acc_number)
123           true
124         else
125           message_box "#{Account.format(acc_number)} is not a valid account number"
126           false
127         end
128         
129       end
130       
131     end
132     next_row
133     button "Console" do
134       open_console self
135     end
136   end
137   
138   on_close do
139     message_box(current.summary)
140   end
141 
142 end
143 

The important things are:

A tool: the console

Open the console tool windows by clicking on the console button (in the form presented in the previous section).

A new window opens, as shown in next picture.

Move the console window to see both windows.

Type Ruby commands

The text area, in the console window, is an editor window where you can type ruby script. You can access the form by using context. For instance, let's change the amount value, type next script.

    context[:amount].value = 110

Here is what you should see in the console.

Hit the execute button to update the amount field in the previous form.

As another example, type next script:

  context.each(TextField) do |comp|
    puts comp.value
  end

Execute the script and look at the ouput (in the command line window).

Show info

If you click on the Show into button, you should see markers on the transfer form, see next picture.

With the Windows L&F some painting conflicts appear.

The markers contain a number, the number is the index you can use to access the visual component (context[i].value = ...).

Moving the mouse over the markers reveal more information.

The markers with a light gray background do not have any name.

Starting the console

You also can start the console and pass the main script.

  > jruby -Ilib -Isamples lib/swiby/tools/console.rb samples/user_form.rb

It open the console and runs the script, if the script creates a form, the console bind the context method to it. The result of the previous command produces next form (after clicking on the Show info button).

The same form with apply/restore buttons

You also can add apply and restore buttons, see the explanation here.

Changing the look and feel

By default Swiby is setting the current system look and feel. To set another one, add the additional jars to the CLASSPATH and use the JVM option to set the class. Next command sets the NimROD L&F:

    > set CLASSPATH=<some-path>/nimrodlf-0.99b.jar
    > jruby -J-Dswing.defaultlaf=com.nilo.plaf.nimrod.NimRODLookAndFeel -Ilib -Isamples samples\transfer_form.rb

The first screenshots, in this page, use the NimROD look and feel.

Installing as a rubygem

RubyGems is the rubyish way to install ruby libraries and tools. Swiby is available as a rubygem.

You either can download the gem file and then install it locally or let the gem tool do it.

Before going for the installation, make sure your gem command is the one provided by jruby, if you didn't install any native Ruby intepreter you don't need to worry about this.

You can force the jruby's gem by setting the PATH variable accordingly.

Let gem do it

The easiest way to install the latest version of Swiby is by running the normal gem command:

    > gem install swiby --source http://www.alef1.org
    Successfully installed swiby, version 0.0.1
    Installing ri documentation for swiby-0.0.1...
    Installing RDoc documentation for swiby-0.0.1...

Now you can start the banking demo by executing:

    > jruby -S sweb http://www.alef1.org/swiby/banking/banking.rb

You can also start it from the subverion repository (here we use version in the trunk, that can be unstable:

    > jruby -S sweb https://svn.codehaus.org/swiby/trunk/core/demo/banking/banking.rb

Download the gem file

Another way to install Swiby is by downloading the gem file form http://www.alef1.org/gems/swiby-0.0.1.gem and run next command:

    > gem install swiby-0.0.1.gem
    Successfully installed swiby, version 0.0.1
    Installing ri documentation for swiby-0.0.1...
    Installing RDoc documentation for swiby-0.0.1...

The command was run from the directory were the gem was downloaded.

Uninstalling the gem

If you want to uninstall Swiby, run next command:

   > gem uninstall Swiby

Running an application from a remote site (and the cache)

You can run an application deployed on any HTTP server, for instance the banking demo is deployed at http://www.alef1.org/swiby/banking. Running the application from the server, provided you installed the gem is as simples as:

    > jruby -S sweb http://www.alef1.org/swiby/banking/banking.rb

Is you didn't install Swiby as a gem, go to the your-install-dir/core/ and execute next command:

    > jruby -Ilib lib\swiby\sweb.rb http://www.alef1.org/swiby/banking/banking.rb

The Swiby caching system

When running an application from a remote server, Swiby, by default, downloads all the files on the local machine, copying them in a per-user cache.

On Windows XP systems, if you are logged as james, the cache is located at: C:\Documents and Settings\james\.swiby\cache\.

The caching system creates a subfolder as the server name, if the remote server is www.alef1.org, you get:

    C:\Documents and Settings\james\.swiby\cache\www.alef1.org

It then adds subfolders depending on the URL:

    C:\Documents and Settings\james\.swiby\cache\www.alef1.org\swiby\banking

You can delete the cache by removing any of the subfolders.