import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL

class Context {
    State state
        
    Context(){
        this.state = new ConcreteStateA()
    }
    
    void setState(State state){
        this.state = state
    }
    
    def request(){
        return state.handle(this)
    }

}

interface State {
    def handle(Context context)
}

class ConcreteStateA implements State {
    def handle(Context context){
        // State change handled here
        context.setState(new ConcreteStateB())
        return "Handled with ConcreteStateA"
    }
}

class ConcreteStateB implements State {
    def handle(Context context){
        // State change handled here
        context.setState(new ConcreteStateA())
        return "Handled with ConcreteStateB"
    }
}


// Less Simple Example
Context context = new Context()

new SwingBuilder().edt {
  frame(title:'Frame', size:[300,300], show: true) {
    borderLayout()
    textlabel = label(text:"To begin, please click the button.", constraints: BL.NORTH)
    button(text:'Click Me',
         actionPerformed: {textlabel.text = context.request()},
         constraints:BL.SOUTH)
  }
}

println "Less Simple Example"
