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

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

  Universe u;
  PhysicalObject obj;
  int type;
 
// Change!
 
  public ObjectFromObject(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("x-coordinate of initial location in frame F:"),
        Util.DEFAULT); 

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

    Util.add(this,
        new Label("t-coordinate of initial location 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,
        check = new Checkbox("Check this box to create an event at the 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;

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

  public void itemStateChanged(ItemEvent e)
   {
    name.setEditable(check.getState());
   }

  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;
      }

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

   
    u.addObject(obj);

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

    w.reset();
   }
 }
