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

public class ObjectFromFrame extends WizCard implements ItemListener
 {
  Choice frameChoice, colorChoice, clockChoice;
  TextField tText, xText, vText, properText;
  TextField name;
  Checkbox check;

  Universe u;
  PhysicalObject obj;
  int type;
  
  public ObjectFromFrame(Wizard w, Universe u, PhysicalObject obj, int type)
   {
    super(w);
    this.u = u;
    this.obj = obj;
    this.type = type;

    isFinal = true;  // Done! instead of next>>

    setLayout(new GridBagLayout());

    Util.add(this,
        new Label("Please specify the following parameters for the initial location of" + ((type == ObjectInit.LONG) ? " the center of " : " ") + obj.name + "."),
        Util.ENDL); 

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

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

    Util.add(this,
        new Label("Initial x-coordinate in frame F:"),
        Util.DEFAULT); 

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

    Util.add(this,
        new Label("Initial t-coordinate in frame F: "),
        Util.DEFAULT); 

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

    Util.add(this,
        new Label("Initial velocity in frame F: "),
        Util.DEFAULT); 

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

    Util.add(this,
        new Label(obj.name + "'s clock is initially"),
        Util.DEFAULT);

    Util.add(this,
        clockChoice = new Choice(),
        Util.DEFAULT);

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

    Util.add(this,
        check = new Checkbox("Check this box to create an event at " + obj.name + "'s initial location.",true),
        Util.ENDL);

    Util.add(this,
        new Label("      Name of event:"),
        Util.DEFAULT);

    Util.add(this,
        name = new TextField(obj.name + " created",10),
        Util.ENDL);

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

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

    int i;

    clockChoice.add("set to time t = ");
    clockChoice.add("synchronized with F");

    check.addItemListener(this);
    clockChoice.addItemListener(this);

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

  public void itemStateChanged(ItemEvent e)
   {
    name.setEditable(check.getState());
    properText.setEditable(clockChoice.getSelectedIndex() == 0);
   }

  public void doNext()
   {
    int i = frameChoice.getSelectedIndex();

    ReferenceFrame f = u.frameAt(i);
     
    double t = 0, x = 0, v = 0;

    try
     {
      t = Util.doubleValue(tText);
      x = Util.doubleValue(xText);
      v = Util.doubleValue(vText);
     }
    catch (NumberFormatException e)
      {
       System.out.println("Error: x,t,v must be numbers");
       return;
      }

    if (v >= 1 || v <= -1)
     {
      System.out.println("Error: objects cannot exceed the speed of light.");
      return;
     }

    obj.setInitialLocation(f.location(x,t));
    obj.setInitialVelocity(f.abs_v(v));

    double t0 = 0;

    switch (clockChoice.getSelectedIndex())
     {
      case 0: 
       try
        {
         t0 = Util.doubleValue(properText);
        }
       catch (NumberFormatException e)
        {
         System.out.println("Error: t must be a number");
         return;
        }
       break;
      case 1: t0 = t; break;
     }   

    obj.setInitialProperTime(t0);

    switch (type)
     {
      case ObjectInit.INERTIAL: u.addInertialObject((InertialObject)obj); break;
      case ObjectInit.VARIABLE: u.addVariableObject((VariableObject)obj); break;
      case ObjectInit.LONG:     u.addLongObject((LongObject)obj); break;
     }

    if (check.getState())
     {
      SpacetimeEvent e = new SpacetimeEvent(obj.creation());
      e.color = Util.colorOf(colorChoice);
      e.name = name.getText();
      u.addEvent(e);
     }

    w.reset();
   }
 }
