// ChartWindow.java
//
// Copyright (C) 1998 Henrik Bjorkman. 
//
// This is class opens a window to show the chart in.
//
//
//
// History
//
// 1998-05-22 
// Created by Henrik Bjorkman
//
// 1998-06-01 
// Added package. /Henrik
//
// 1998-06-04 
// Sometimes image method getWidth returned -1. Probably because the 
// image was not yet loaded. Now we wait until it is not -1. /Henrik
//
// 1998-06-15
// Doing flush on images. Otherwise we ran out of memory. /Henrik
//
// 1998-06-23
// Added a direction line. /Henrik
//
// 1998-06-24
// Made the mark red and added a blue trace. /henrik
//
// 1998-06-28
// Made the red circle thicker. /Henrik
//
// 1998-07-24
// Adding new functionality: a circle of 50 m radius encounters the current position  /Rikard
//
// 1998-10-23
// The radius of the circle added 24/7 is now set when loading a chart instead of
// at position update.
// /Henrik
//
// 1999-06-20
// Moved class ChartComp to its own file. /Henrik
//
// 1999-08-26
// Doing the trace a little better.
// Henrik
// 
// 2000-06-13
// Changed package name to chartplotter_package.
// Added a check so we get a warning if image file not found.
// Henrik
//


package chartplotter_package;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ChartWindow extends Frame
{
  ChartComp chart;
  static int nwindows=0;
  ScrollPane pane;
  int x,y;
  boolean track=false; /* true if we know the direction */
  boolean alwaysCenter=false;
  public boolean closed=false;

  static void debug(String s)
  {
    //System.out.println("ChartWindow: " + s);
  }

  public ChartWindow(boolean alwaysCenter, boolean trace, int thickness)
  {
    super(ChartVersion.name);

    this.alwaysCenter=alwaysCenter;
    nwindows++;

    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) { close(); }
    });

    pane=new ScrollPane();
    pane.setSize(400,300);

    this.add("Center", pane);
    debug("add pane done");

    chart = new ChartComp(this, trace, thickness);
    debug("chart Created");

    pane.add(chart);
    debug("add done");

    this.pack();
    debug("pack done");

    this.show();
    debug("show done");
  }

  /** Close a window. If this is the last open window, just quit. **/
  void close() 
  {
    debug("close");
    setVisible(false);
    nwindows--;
    closed=true;
    /*if (nwindows==0) System.exit(0);
    else this.dispose();*/
  }

  public ChartComp getChartComp()
  {
    return(chart);
  }

  /*public void tracePos(Point pos)
  {
    chart.tracePos(pos);
  }*/

  public void updatePos(Point pos, Point pred)
  {
    chart.updatePos(pos,pred);

    Point p=pane.getScrollPosition();
    Dimension d=pane.getViewportSize();
    Point p2=new Point(p);
    //debug("scroll pos "+p+" size "+d);

    if ((track) && (!alwaysCenter))
    {
      if ((pos.y<this.y) && (pos.y<p.y+d.height/4)) {p2.y=pos.y-(2*d.height)/3;}
      if ((pos.x<this.x) && (pos.x<p.x+d.width/4)) {p2.x=pos.x-(2*d.width)/3;}
      if ((pos.y>this.y) && (pos.y>p.y+d.height-d.height/4)) {p2.y=pos.y-(d.height)/3;}
      if ((pos.x>this.x) && (pos.x>p.x+d.width-d.width/4)) {p2.x=pos.x-(d.width)/3;}
    }
    else
    {
      p2.y=pos.y-(d.height)/2;
      p2.x=pos.x-(d.width)/2;
    }

    if (!p2.equals(p))
    {
      pane.setScrollPosition(p2);
    }

    this.x=pos.x;
    this.y=pos.y;
    track=true;
  }

  public void changeChart(String chartName, int scale, int precision)
  {
    if (!(new File(chartName)).exists()) 
    {
      System.err.println("Could not find file: "+chartName);
    }

    chart.loadChart(chartName, scale, precision);
    this.show();
    track=false;
  }

  public void timeOut()
  {
    chart.timeOut();
  }
}




