#--
# Copyright (C) Swiby Committers. All rights reserved.
# 
# The software in this package is published under the terms of the BSD
# style license a copy of which has been included with this distribution in
# the LICENSE.txt file.
#
#++

#
# JRuby version of 'Using the Actor pattern in GUIs' 
# from http://www.andreweland.org/code/gui-actor
#

require 'swiby/form'

include_class 'java.awt.EventQueue'

class TeaMaker

  attr_accessor :listener
  
  def brew()
    
    sleep 5
    
    @listener.handle_brewing_complete
    
  end
  
end

class Actor
  
  include java.lang.Runnable
  
  def initialize delegate
    @delegate = delegate
  end
  
  def run
    @delegate.send(@method, *@args)
  end
  
  def method_missing method, *args

    @method = method
    @args = args
    
    EventQueue.invokeLater self
    
  end
  
end

tea_maker = TeaMaker.new

f = form {
  
  title 'Tee Maker'
  
  width 200
  height 100
  
  content {
    label ' ', :name => :state
    button('Brew', :name => :brew) {
      
      context[:brew].enabled = false
      context[:state].text = 'Brewing started'
      
      Thread.new do
        tea_maker.brew
      end
      
    }
    button('Quit') {
      exit
    }
  }
  
  visible true
  
}

def f.handle_brewing_complete()
  context[:state].text = 'Brewing complete'
  context[:brew].enabled = true
end

tea_maker.listener = Actor.new(f)

