Comments:

The HelloCube example illustrates the use of a Cube in a SugarCubes program. The Cube here implements a window which contains a button. By pressing the button the user generates an event "Hello" in the reactive machine, which trigger the execution of the body of the cube which awaits for the event "Hello" (windowBehavior). This causes the Window to change its title from nothing to "Hello World!" (the HC_DisplayHello java atomic action). On the other hand by clicking the close box of the window, the user generates the destruction event associated to the window/cube, triggering its preemption which results in the termination of the program, executing the termination notification to Java (HC_Dispose).

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).

javac -classpath SugarCubesv3.0.3.jar HelloCube.java


How to execute?

java -cp SugarCubesv3.0.3.jar:. HelloCube


import inria.meije.rc.sugarcubes.SC;
import inria.meije.rc.sugarcubes.Program;
import inria.meije.rc.sugarcubes.Machine;
import inria.meije.rc.sugarcubes.LocalVariables;
import inria.meije.rc.sugarcubes.ReactiveEngine;
import inria.meije.rc.sugarcubes.JavaAction;
import inria.meije.rc.sugarcubes.StringID;
import inria.meije.rc.sugarcubes.JavaObjectExpression;

import java.awt.*;
import java.awt.event.*;

//This is a more complicated HelloWorld example which uses a cube.

class HelloCube
{
    public static Machine machine = SC.voidMachine();

    public static void main(String[] args){
        //First, one makes a program that will be the behavior of the cube.
        Program windowBehavior = SC.seq(SC.seq(SC.action(new HC_ShowWindow()),SC.await("hello")),
                SC.action(new HC_DisplayHello()),SC.halt());
        //One makes a cube of name 'HelloWorldWindow', which uses windowBehavior as behavior.
        //One adds this cube into the machine.

        machine.addProgram(SC.cube("HelloWorldWindow",new HC_BuildFrameExpression(),windowBehavior
                ,new HC_Dispose(),SC.NO_ACTION,SC.NO_ACTION));
        //As usual, one makes react the machine.
        int i = 1;
        do{
            try{Thread.sleep(100);}catch(Exception e){}
            System.out.println("instant "+(i++)+": ");
        }while(!machine.react());
        //exit
        System.exit(0);
    }
}

class MyFrame extends Frame
{
    Button generateHelloBtn = new Button("Generate 'hello'");
}

class HC_BuildFrameExpression implements JavaObjectExpression
{
    public Object evaluate(Object parent,LocalVariables vars,ReactiveEngine engine){
        MyFrame f = new MyFrame();
        f.addWindowListener(
            new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    HelloCube.machine.generateEvent(new StringID("HelloWorldWindow-destroy"),null);
                }
            }
            );
        f.generateHelloBtn.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    HelloCube.machine.generateEvent(new StringID("hello"),null);
                }
            }
            );
        f.add("Center",f.generateHelloBtn);
        return f;
    }
}

class HC_FrameAction implements JavaAction
{
    public void execute(Object self,LocalVariables vars,ReactiveEngine engine){
        execute((Frame)self);
    }
    public void execute(Frame self){}
}

class HC_ShowWindow extends HC_FrameAction
{
    public void execute(Frame self){
        self.setVisible(true);
    }
}

class HC_HideWindow extends HC_FrameAction
{
    public void execute(Frame self){
        self.setVisible(false);
    }
}

class HC_DisplayHello extends HC_FrameAction
{
    public void execute(Frame self){
        self.setTitle("Hello World!");
    }
}

class HC_Dispose extends HC_FrameAction
{
    public void execute(Frame self){
        self.dispose();
    }
}