/**
 * Title:         Seek Number Game
 * Description:   Applet for seek number game.
 * Copyright:     Copyright (c) 2001
 * Organization:  NO
 * @author noritan
 * @version 1.0
 */

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

/**
 * Seek Number Game applet.
 * This object acts as a Mediator and all buttons put on this
 * object act as Colleagues.
 * There is no additional interface between Mediator-Colleague
 * because the EventListener is used for the interface and it is
 * enough for the communication between Mediator and Colleagues.
 */
public class SeekNumberApplet extends Applet implements ActionListener {
  boolean isStandalone = false;
  int columns = 1;
  int rows = 1;
  int expected;
  Button[] buttonArray;
  Panel contentPane = new Panel();
  BorderLayout contentLayout = new BorderLayout();
  Panel messagePane = new Panel();
  Panel buttonPane = new Panel();
  Panel controlPane = new Panel();
  Button startButton = new Button();
  Label messageLabel = new Label();

  /**
   * Get a parameter value.
   */
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**
   * Constructor.
   */
  public SeekNumberApplet() {
  }

  /**
   * Initialization of the applet.
   */
  public void init() {
    try {
      columns = Integer.parseInt(this.getParameter("columns", "1"));
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    try {
      rows = Integer.parseInt(this.getParameter("rows", "1"));
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Initialization of the components.
   */
  private void jbInit() throws Exception {
    // Set-up contentPane on the applet.
    contentPane.setLayout(contentLayout);
    contentLayout.setHgap(20);
    contentLayout.setVgap(20);
    this.add(contentPane, null);
    // Set-up messagePane on the NORTH of the contentPane.
    messageLabel.setAlignment(Label.CENTER);
    messageLabel.setFont(new java.awt.Font("Dialog", 1, 16));
    contentPane.add(messagePane, BorderLayout.NORTH);
    messagePane.add(messageLabel, null);
    // Set-up controlPane on the SOUTH of the contentPane.
    startButton.setLabel("START");
    startButton.addActionListener(this);
    contentPane.add(controlPane, BorderLayout.SOUTH);
    controlPane.add(startButton, null);
    // Set-up buttonPane on the CENTER of the contentPane.
    // No buttons are put here.
    buttonPane.setLayout(new GridLayout(rows, columns, 10, 10));
    contentPane.add(buttonPane, BorderLayout.CENTER);
    // Make an array of buttons.
    buttonArray = new Button[rows * columns];
    for (int i = 0; i < buttonArray.length; i++) {
      buttonArray[i] = new Button();
      buttonArray[i].setLabel(String.valueOf(i + 1));
      buttonArray[i].addActionListener(this);
    }
  }

  /**
   * Start the applet.
   */
  public void start() {
    reorder();
  }

  /**
   * Listen to the ActionEvent issued by the buttons.
   * This method acts as the communication mthod from the Colleagues
   * to the Mediator.
   */
  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == startButton) {
      reorder();
    } else if (expected >= buttonArray.length) {
      return;
    } else if (source == buttonArray[expected]) {
      buttonArray[expected].setBackground(Color.yellow);
      expected++;
      if (expected >= buttonArray.length) {
        messageLabel.setText("Finished !!");
      }
    }
  }

  /**
   * Re-order the buttons in the array.
   */
  public void reorder() {
    setVisible(false);
    buttonPane.removeAll();
    for (int i = 0; i < buttonArray.length; i++) {
      int k;
      do {
        k = (int)(Math.random() * buttonArray.length);
      } while (buttonPane.isAncestorOf(buttonArray[k]));
      buttonArray[k].setBackground(getBackground());
      buttonPane.add(buttonArray[k]);
    }
    buttonPane.doLayout();
    expected = 0;
    messageLabel.setText("Seek Number Game");
    setVisible(true);
  }

  /**
   * Return information of the Applet.
   */
  public String getAppletInfo() {
    return "Applet Information";
  }

  /**
   * Return information of the parameters.
   */
  public String[][] getParameterInfo() {
    String[][] pinfo =
      {
      {"columns", "int", "Number of columns of buttons."},
      {"rows", "int", "Number of rows of buttons."},
      };
    return pinfo;
  }

  /**
   * main() method.
   */
  public static void main(String[] args) {
    SeekNumberApplet applet = new SeekNumberApplet();
    applet.isStandalone = true;
    Frame frame;
    frame = new Frame() {
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
      public synchronized void setTitle(String title) {
        super.setTitle(title);
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      }
    };
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(
      (d.width - frame.getSize().width) / 2,
      (d.height - frame.getSize().height) / 2
    );
    frame.setVisible(true);
  }
}

