Java Swul Examples

Stratego -- Strategies for Program Transformation

Introduction

The examples covered in this page show some of the capabilities of Java-Swul. Most examples are based on code in the xmpl directory in the Java-Swul package.

Nesting panels

The following Java-Swul code is taken from a file called xmpl/Test3.javaswul in the Java-Swul package.
import javax.swing.*;
import java.awt.*;
  
public class Test3 {
  public static void main(String[] ps) {
    JFrame frame = frame {
      title = "Welcome!"
      content = panel of border layout {
        center = label { text = "Hello World" }
        south = panel of grid layout {
          row = {
            button { text = "cancel" }
            button { text = "ok"}
          }
        }
      }
    };
    frame.pack();
    frame.setVisible(true);
  }
}
When we assimilate/compile/run (swulc -i Test3.javaswul -o Test3.java && javac Test3.java && java Test3) this program this will produce:

/pub/Stratego/JavaSwulExamples/Test3.png

The curious will immidiatly wonder what the command swulc -i Test3.javaswul -o Test3.java will produce in Test3.java. This is shown below.

import javax.swing.*;
import java.awt.*;
 
public class Test3
{
  public static void main(String[] ps)
  {
    JButton jButton_1;
    JButton jButton_0;
    JPanel jPanel_1;
    JLabel jLabel_0;
    JPanel jPanel_0;
    JFrame jFrame_0;
    jFrame_0 = new JFrame();
    jFrame_0.setTitle("Welcome!");
    jPanel_0 = new JPanel();
    BorderLayout borderLayout_0 = new BorderLayout();
    jPanel_0.setLayout(borderLayout_0);
    jFrame_0.setContentPane(jPanel_0);
    JFrame frame = jFrame_0;
    jLabel_0 = new JLabel();
    jLabel_0.setText("Hello World");
    jPanel_0.add(jLabel_0, BorderLayout.CENTER);
    jPanel_1 = new JPanel();
    GridLayout gridLayout_0 = new GridLayout(1, 2);
    jButton_0 = new JButton();
    jButton_0.setText("cancel");
    jButton_1 = new JButton();
    jButton_1.setText("ok");
    jPanel_1.setLayout(gridLayout_0);
    jPanel_1.add(jButton_0);
    jPanel_1.add(jButton_1);
    jPanel_0.add(jPanel_1, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
  }
}

Menubar

The creating of menubars is an excellent example of where in Java the long list of statements becomes an unreadable and hard to understand mess. The best example is the url where Sun explains how to use Menus.

This demostration menubar looks like this:

/pub/Stratego/JavaSwulExamples/MenuLookDemo.png

The code that generates this menu is not immidiatly clear in itself. Extra measures are needed as you can see in the next code sniplet.

/pub/Stratego/JavaSwulExamples/MenuLookDemo2.png

To accomplisch the same as shown in the above code sniplet you could write in Java-Swul the following:

label {
  text = "Both text and icon"
  icon = "images/middle.gif"
  mnemonic = b
}
label {
  icon = "images/middle.gif"
  mnemonic = d
}
seperator
radiobutton {
  text = "A radio button menu item"
  group = a
  selected = true
  mnemonic = r
}
radiobutton {
  text = "Another one"
  mnemonic = o
  group = a
}

Complete Java-Swul source and generated source can be downloaded.

Widget-o-rama

The next screenshot is from a file called xmpl/Test16.javaswul in the Java-Swul package.

/pub/Stratego/JavaSwulExamples/Test16.png

The Java-Swul source used to create this crowded interface contains the next code-example. The surrounding Java code is left out to save some space. The complete Java code is further down on this page.

    this {
      title = "Welcome"
      content = panel {
        border = raised bevel border
        layout = border layout {
          north = scrollpane of panel of grid layout {
            row = { textarea { 
                rows   = 10
                columns = 10
                text = "A 10 by 10 textarea"
              }
              tree { }
            }
          }
          center = panel of grid layout {
            row = {
              label { 
                text = "This an JLabel" 
              }
              button { 
                text = "This is a JButton" 
              }
              slider {
              }
            }
            row = {
              radiobutton {
                text = "JRadioButton in a group"
                group = radiogroup1
                selected = true
              }
              textfield { 
                text = "a JTextField" 
              }
              progressbar { 
                value = 23 
              }
            } 
            row = {
              checkbox { 
                text = "a JCheckBox in a group" 
              }
              combobox { 
              }
              togglebutton { 
                text = "a JToggleButton" 
                group = someGroup
              }
            }
            row = {
              radiobutton {
                text = "another JRadioButton"
                group = radiogroup1
              }
              label {
              }
              togglebutton { 
                text = "another JToggleButton" 
                group = someGroup
                selected = true
              } 
            } 
          }
        }
      }
    };
As you can see it is quite verbose. Writing things down in this structured way causes lots of lines with just an } and spaces. Java-Swul has some sugur to it. The line scrollpane of panel of grid layout { row = { ... } } is short for:
scrollpane { 
  content = panel {
    layout = grid layout {
      row = { ... }
    }
  }
}
So you can skip nestings that are acceptable with default properties.

Complete Java-Swul source and generated source can be downloaded.

  • /pub/Stratego/JavaSwulExamples/Test16.javaswul
  • /pub/Stratego/JavaSwulExamples/Test16.java

Finetuning the components

Events