import java.awt.*;
import WizCard;
import Util;

public class EventFromFrame extends WizCard
 {
  Choice frameChoice;
  TextField xText, tText;
  TextField name;
  Choice colorChoice;

  Universe u;
  
  public EventFromFrame(Wizard w, Universe u)
   {
    super(w);
    this.u = u;
    isFinal = true;  // Done! instead of next>>

    setLayout(new GridBagLayout());

    Util.add(this,
        new Label("Please specify the following parameters."),
        Util.ENDL); 

    Util.add(this,
        new Label("Reference frame: "),
        Util.DEFAULT); 

    Util.add(this,
        frameChoice = new Choice(),
        Util.ENDL);

    Util.add(this,
        new Label("Frame x-coordinate: "),
        Util.DEFAULT); 

    Util.add(this,
        xText = new TextField("0.0",10),
        Util.ENDL);

    Util.add(this,
        new Label("Frame t-coordinate: "),
        Util.DEFAULT); 

    Util.add(this,
        tText = new TextField("0.0",10),
        Util.ENDL);

    Util.add(this,
        new Label("Event name: "),
        Util.DEFAULT); 

    Util.add(this,
        name = new TextField("Event " + Integer.toString(u.events.size()),40),
        Util.ENDL);

    Util.add(this,
        new Label("Display color: "),
        Util.DEFAULT);

    Util.add(this,
        colorChoice = Util.colorChoice(),
        Util.ENDL);

    int i;

    for (i = 0; i < u.frames.size(); i++)
      frameChoice.add(u.frameAt(i).name);
   }

  public void doNext()
   {
    ReferenceFrame f = u.frameAt(frameChoice.getSelectedIndex());
    double x,t;

    try
     {
      x = Util.doubleValue(xText);
      t = Util.doubleValue(tText);
     }
    catch (NumberFormatException e)
      {
       System.out.println("Error: (x,t) co-ordinates have to be numbers");
       return;
      }
 
    SpacetimeEvent e = new SpacetimeEvent(f,x,t);
    e.name = name.getText();
    e.color = Util.colorOf(colorChoice);
    u.addEvent(e);
    w.reset();
   }
 }
