// RefDialog.java
//
// Copyright Henrik Björkman (www.stacken.kth.se/~bjorkman) 1999
//
// History:
//
// Created by Henrik Björkman 1999-08-13 using code from 
// DrawCircleDemo.java    
//

package chartplotter_package;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import se.beod.henrik.nav.*;

public class RefDialog extends Dialog 
{
  TextField tx;
  TextField ty;
  TextField tlat;
  TextField tlng;
  Frame parent;
  ReferencePoint r;
  Integer state;

  static void error(String s)
  {
    System.err.println("ChartThread: " + s);
    System.exit(0);
  }

  // Just for debugging.
  static public void debug(String s)
  {
    System.err.println("RefDialog: "+s);
  }

  public static void println(String s)
  {
    System.out.println(s);
  }


  

  void close() 
  {
    if ((r.x==-10000) && (r.y==-10000))
    {
      r.x=-10001;
      r.y=-10001;
    }

    setVisible(false);
    this.dispose();
  }


  void button() 
  {

    debug("Button");
    debug("X: "+tx.getText());
    debug("Y: "+ty.getText());
    debug("Lat: "+tlat.getText());
    debug("Lng: "+tlng.getText());
 
    try 
    {
      int x=Integer.parseInt(tx.getText());
      int y=Integer.parseInt(ty.getText());
      r.setLat(tlat.getText());
      r.setLng(tlng.getText());
      r.x=x;
      r.y=y;
      close();
    }
    catch (NumberFormatException e) 
    {
      //OkDialog d=new OkDialog(parent,"Could not parse input field",""+e);
      OkDialog.popUp(parent,"Could not parse input field",""+e);
    }

    debug(r.toString());
  }



  // The main constructor of this class.
  public RefDialog(Frame parent, String title, Point p, ReferencePoint r, Integer state)
  {
    super(parent,title,false);

    this.parent=parent;
    this.r=r;
    this.state=state;

    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { close(); }
    });


    {
    Panel xyp=new Panel();

    Label lx=new Label("x",Label.CENTER);
    xyp.add("North",lx);

    tx=new TextField(""+p.x,10);
    xyp.add("East",tx);

    Label ly=new Label("y",Label.CENTER);
    xyp.add("West",ly);

    ty=new TextField(""+p.y,10);
    xyp.add("South",ty);

    this.add("North",xyp);
    }

    {
    Panel p2=new Panel();

    Label l2=new Label("Latitude",Label.CENTER);
    p2.add("West",l2);

    tlat=new TextField("",10);
    p2.add("South",tlat);

    Label l1=new Label("Longitud",Label.CENTER);
    p2.add("North",l1);

    tlng=new TextField("",10);
    p2.add("East",tlng);

    this.add("Center",p2);
    }


    {
    Panel p3=new Panel();

    Button b=new Button("OK");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) { button(); }
    });
    p3.add("South",b);

    this.add("South",p3);
    }

    this.pack();
    debug("pack done");

    this.show();
    debug("show done");
  }

}


