Imediava's Blog

Just another WordPress.com site

Tag Archives: SWT

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.

@Override
public void createPartControl(final Composite parent) {

     parent.setLayout(new GridLayout());
     group = new Group(parent, SWT.NONE);
     group.setText("Groovy SWT");
     group.setBackground(parent.getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
     group.setLayout(new GridLayout());

     Label label1 = new Label(group, SWT.NONE);
     label1.setText("groove fun !" );
     label1.setBackground(parent.getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));

     Label label2 = new Label(group, SWT.NONE);
     label2.setText("Email: ckl@dacelo.nl");
     label2.setBackground(parent.getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));

}

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

 def subapp = jface.shell( window.getShell())
 {
 	gridLayout()
    group( text:"Groovy SWT", background:[255, 255, 255] )
    {
    	gridLayout()
        label( text:"groove fun !" ,background:[255, 255, 255] )
        label( text:"Email: ckl@dacelo.nl", 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.

Tips

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.

Advertisements
%d bloggers like this: