Imediava's Blog

Just another site

Prototyping SWT with GroovyMonkey

As a part of an Eclipse plugin I’m developing for my Master’s degree project lately I’ve had to deal with SWT.  Coming from some background at developing GUIs with Swing and Windows Forms I have to admit that SWT it’s not easy to learn. Apart from a bit counterintuitive I find it too verbose.

So far, whenever I wanted to develop a new Wizard or View I had to code it and play with layouts and widgets till I got the look I wanted.

On the other hand, just quite recently I’ve discovered that there’s an scripting environment for Eclipse called GroovyMonkey that can help the task of protoyping with SWT.

To play with it, I’m gonna use an example to compare the code you need to write to create a really simple interface with both approaches to show GroovyMonkey benefits over Java SWT. The example I’m gonna use is extracted from the installation of GroovyMonkey in Eclipse. We’re gonna create the following shell:

Example Shell

The code for this shell with SWT and Java is quite long for what it does, not too elegant and prone to error.

public void createPartControl(final Composite parent) {

     parent.setLayout(new GridLayout());
     group = new Group(parent, SWT.NONE);
     group.setText("Groovy SWT");
     group.setLayout(new GridLayout());

     Label label1 = new Label(group, SWT.NONE);
     label1.setText("groove fun !" );

     Label label2 = new Label(group, SWT.NONE);


The code we would need with GroovyMonkey is shorter and simpler to understand. Advantages whose impact grows with more complex user interfaces.

 def subapp = window.getShell())
    group( text:"Groovy SWT", background:[255, 255, 255] )
        label( text:"groove fun !" ,background:[255, 255, 255] )
        label( text:"Email:", background:[255, 255, 255] )

So, those features have made me decide to give GrooveMonkey a try as my tool for quick GUI prototyping with SWT. In the following section, I’m gonna share my experience through a small list of tips I myself find useful.

Since I’m a completely beginner with GrooveMonkey and with Groovy in general, some of the tips may seem extremely obvious for somewhat experienced users. Nonetheless It haven’t been easy for me to find information about how to start with GroovyMonkey. Thus, this article is written with the hope that it can somehow be useful to those like me who have never used GroovyMonkey before and need help in their very first steps.


Check GroovySWT documentation

This applies to you specially if you’re using GroovyMonkey for prototyping SWT GUIs like I’m doing.
GroovySWT webpage provides a short explanation of the way the library works. If you wanna dig deeper the examples it provides should become your best source.

How to assign values to fields

To assign a value to a field you can take to approaches: either you use the classical “set” method provided by the Java API or you pass the value as a named parameter to the object constructor.

The first way is more flexible since it allows to change the value of the object at any point of the execution. The code to use this approach for setting the text of a label example shell is similar to the Java code:

   miLabel.setText("groove fun !")

On the other hand, the second way is more concise and it’s the preferable way to assign values when creating objects.

   text(text : "groove fun !")

It’s important to point out that this way of assigning values to properties can be used for any property which follows the naming convention marked for JavaBeans. As an illustration we are gonna use the same approach to assign layout data to a control. This can be done since the method setLayoutData exists for any control. The following snippet shows how to do it.

import org.eclipse.swt.layout.GridData

text(style: "Border", layoutData: gridData(grabExcessHorizontalSpace : true, horizontalAlignment : GridData.FILL, verticalAlignment : GridData.FILL))

The last example also shows that this approach can be used recursively, this way increasing it’s benefits.

Styles for controls

The way styles are asigned to controls is also changed with GroovyMonkey.

GroovyMonkey takes the SWT.None as a default value that doesn’t need to be expressed to be assigned to a control.

When other style values need to be assigned the way to do it is passing them through the named parameter style. The value for this parameter is a string which contains the list of styles we wanna set, separated with commas. To represent every style we need to use it’s name deprived from the SWT prefix. Uppercases are ignored so any representation with the same letters as the style name is accepted by GroovyMonkey.

As an example this is the way to create a mulitilined text field with border:

text ( style: 'Border, Multi')

So that’s all for now. Maybe I’ll come back with more tips for GroovyMonkey users when I have more experience with it.


2 responses to “Prototyping SWT with GroovyMonkey

  1. Commission Inferno Review November 22, 2011 at 3:27 pm

    That’s some revelatory blog post!!

  2. mohamed August 1, 2012 at 3:58 pm

    good article

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: