Andy has posted 1 posts at DZone. View Full User Profile

Glimmer - Using Ruby to Build SWT User Interfaces

06.19.2008
| 51093 views |
  • submit to reddit
Layout of specific widgets may be further customized by specifying layout data. For GridLayout, layout data is specified through GridData objects.

For example, we may decide to have the text boxes in the previous example have a greater width:

group {
  layout GridLayout.new(2, false)
  
  label {text "First"}; 
  text {
    text "Bullet"
    layout_data GridData.new(100, default)
  }

  label {text "Last"}; 
  text {
    text "Tooth"
    layout_data GridData.new(100, default)
  }
}

This renders the following:

The used GridData constructor takes two parameters: width hint and height hint.

The width was set to 100 pixels for both text boxes. The height was kept at the default value (SWT::DEFAULT)

For more details about GridLayout, GridData, and other layout managers, please refer to the SWT API documentation.

So far we have covered  how to construct user-interfaces that can display data and gather input from the user. Next, we will demonstrate how to perform work based on actions taken by the user.

SWT widgets can be monitored for certain user-interface events, such as mouse clicks, focus gain and loss, and key presses.

With the original SWT API, events can be monitored by adding listeners to widgets. For example, to monitor the push of a button, you would add a SelectionListener that does some work in its widgetSelected event method.

With Glimmer, events can be monitored by declaring their name (following Ruby conventions) prefixed by “on”

Here is an example of how to monitor button selection:

import org.eclipse.swt.widgets.MessageBox

@shell1 = shell {
  composite {
    button {
      text 'Save'
      on_widget_selected {
        message_box = MessageBox.new(@shell1.widget, SWT::NULL)
 message_box.text = 'Information'
        message_box.message = 'Saved!'
        message_box.open
      }
    } 
  }
}
@shell1.open


This renders the following:

On click of the button, a  message box is opened to let the user know that the information entered is saved.

MessageBox is a class from SWT that represents message dialogs. It was imported using the JRuby import method. Its constructor takes a parent and style. To obtain the parent, we assigned the shell object to a Ruby class variable @shell1. Since Glimmer wraps all SWT constructed objects with Glimmer decorators ( e.g. Shell is wrapped with RShell,) to obtain the SWT Shell class and pass it as the parent to the MessageBox constructor, the widget method was called (e.g. @shell1.widget.)

In the original SWT API, MessageBox has setter methods to set its text and message attributes. However in JRuby, the developer has the option to set them following the Ruby attribute conventions (e.g. message_box.text = 'value') because JRuby automatically enhances all Java objects with methods that follow the Ruby convention.

Published at DZone with permission of its author, Andy Maleh.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)