import jcontrol.ui.wombat.Button;
import jcontrol.ui.wombat.Container;
import jcontrol.ui.wombat.Frame;
import jcontrol.ui.wombat.Label;
import jcontrol.ui.wombat.event.ActionEvent;
import jcontrol.ui.wombat.event.ActionListener;

/**
 * <p>This example demonstrates how to handle
 * events within the GUI framework JControl/Wombat.
 * This program needs the image resource
 * 'mouse.jcif'.</p>
 *
 * <p>(C) DOMOLOGIC Home Automation GmbH 2003-2005</p>
 */
public class WombatEventHandlingExample 
             extends Frame implements ActionListener {
  // the Label
  Label label;
  // the right Button
  Button button_right;

  /**
   * Create two buttons and a label and add an ActionListener.
   */
  public WombatEventHandlingExample() {
    // create the Buttons
    Button b1 = new Button("Left Button", 2, 10, 60, 13);
    button_right = new Button("Right Button", 66, 10, 60, 13);
    
    // add the ActionListener
    b1.setActionListener(this);
    button_right.setActionListener(this);

	// create a content pane
	Container content = new Container();
    this.setContent( content);
    
    // add the Buttons to the Frame
    content.add(b1);
    content.add(button_right);
    
    // create the Label
    label = new Label("Please press a button!", 0, 30, 128, 10, 
    						Label.STYLE_ALIGN_CENTER);
    content.add(label);
  }
  
  /**
   * This is the event handler. When a component fires an 
   * ActionEvent for us, this method is invoked.
   */
  public void onActionEvent(ActionEvent event) {
    // check whether this is a BUTTON_PRESSED event
    if (event.type == ActionEvent.BUTTON_PRESSED) {
      
      // recognize event's source by using getActionCommand()
      if ( "Left Button".equals(event.command))
        label.setText("You pressed the left button!");
        
      // recognize event's source by using getSource()
      if (event.source == button_right)
        label.setText("The right button was hit!");
    }
  }

  /**
   * Instantiate and show the main frame.
   */
  public static void main(String[] args) {
    new WombatEventHandlingExample().setVisible(true);
  }
}
