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

/*
 * The MultipleClocksApplet uses four RoundClocks to show
 * clocks for different time zones simultanously.
 *
 * The actual drawing of the clock faces are done by the
 * RoundClock class. The RoundClock objects are controlled
 * by a ClockAdmin, which makes sure that the RoundClock
 * objects redraw themself at suitable intervals (4 times pr second).
 *
 * This applet starts a seperate thread in which ClockAdmin runs.
 */


public class MultipleClocksApplet extends Applet implements ItemListener{

  //Init admin
  ClockAdmin clock_admin = new ClockAdmin();

  Choice zones_choice = new Choice();

  // Labels to put under each clock.
  Label labels [] = {new Label("Oslo"),
		    new Label("Moscow"),
		    new Label("New York")};
  
  // Set time zone info.
  String zones [] = {"ECT", "NET", "EST", "GMT"};

  RoundClock clocks [] = new RoundClock[4];

  // Variable used for the extra thread.
  Thread clock_thread;

  final int MAX_CLOCKS = 4 - 1;

  public void init(){
    // --------------------------------
    // Initialize clocks and the admin.
    // --------------------------------
    

    // Add clocks to clock admin. All clocks can be controlled
    // by one ClockAdmin

    for(int i = 0; i <= MAX_CLOCKS; i++){
      clocks[i] = new RoundClock();
      clocks[i].setTimeZone(zones[i]);
      clock_admin.addClock(clocks[i]);
    }


    // --------------------
    // Build user interface
    // --------------------

    // Set label alignment.
    for(int i = 0; i <= MAX_CLOCKS - 1; i++)
      labels[i].setAlignment(Label.CENTER);

    // Use GridLayout since we have several clocks.
    // All cells in a GridLayout have the same size, which
    // means that there will be plenty of space around the labels.
    // Use GridBagLayout if you want a better looking interface

    setLayout(new GridLayout(2, 4));

    //will need later to arrange labels and choice in a proper way
    Panel p;

    // Add the clocks to the applet.
    // The add method fills the grid row-wise. Each row from
    // left to right.

    for(int i = 0; i <= MAX_CLOCKS; i++)
      add(clocks[i]);

    // Add labels to applet.
    for(int i = 0; i <= MAX_CLOCKS - 1; i++){
      p = new Panel();
      p.add(labels[i]);
      add(p);
    }

    //Add choice box with all available zones for the fourth clock.
    String z [] = TimeZone.getAvailableIDs();
    for(int i = 0; i < z.length; i++)
      zones_choice.add(z[i]);

    zones_choice.select("GMT");

    p = new Panel();
    p.add(zones_choice);
    add(p);

    zones_choice.addItemListener(this);
  }

  /*
   * Start a new thread in which clock_admin can run.
   */
  public void start(){
    if(clock_thread == null){
      clock_thread = new Thread(clock_admin);
      clock_thread.start();
    }
  }

  /*
   * Stop the clock_thread, if it is running.
   */
  public void stop(){
    if(clock_thread != null){
      clock_thread.stop();
      clock_thread = null;
    }
  }

  /*
   * This method should be empty. The painting is done by RoundClock.
   */

  public void paint(Graphics g){}

  //Change the time zone for the fourth clock.
  public void itemStateChanged(ItemEvent e){
    clocks[MAX_CLOCKS].setTimeZone(zones_choice.getSelectedItem());
  }
}

