import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.util.*;
import java.math.*;

/**
 * <p>Title: </p>
 * <p>Description: Home base USB serial code</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Kosala Kodikara
 * @version 1.0
 */

public class Frame1 extends JFrame {

  private float ixy[][];
  private boolean somethingtodraw=false;

  private JPanel contentPane;
  private JMenuBar jMenuBar1 = new JMenuBar();
  private JMenu jMenuFile = new JMenu();
  private JMenuItem jMenuFileExit = new JMenuItem();
  private JMenu jMenuHelp = new JMenu();
  private JMenuItem jMenuHelpAbout = new JMenuItem();
  private JLabel statusBar = new JLabel();
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel jPanel1 = new JPanel();
  private JPanel jPanel2 = new JPanel();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel3 = new JPanel();
  private JPanel jPanel4 = new JPanel();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private BorderLayout borderLayout3 = new BorderLayout();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JButton jButton5 = new JButton();
  private JButton jButton6 = new JButton();
  private JButton jButton7 = new JButton();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JPanel jPanel5 = new JPanel();
  private JButton jButton8 = new JButton();
  private JButton jButton9 = new JButton();
  private mappoint mappoint_panel = new mappoint();
  private TitledBorder titledBorder1;
  private JButton jButton10 = new JButton();
  private JButton jButton11 = new JButton();
  private GridLayout gridLayout1 = new GridLayout();
  private JPanel jPanel6 = new JPanel();
  private BorderLayout borderLayout5 = new BorderLayout();
  private JScrollPane jScrollPane1 = new JScrollPane();
  private JTextArea jTextArea1 = new JTextArea();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    titledBorder1 = new TitledBorder("");
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(468, 363));
    this.setTitle("NavBot 2004 v1.00 Data Display");
    statusBar.setText("Navbot");
    jMenuFile.setText("File");
    jMenuFileExit.setText("Exit");
    jMenuFileExit.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        jMenuFileExit_actionPerformed(e);
      }
    });
    jMenuHelp.setText("Help");
    jMenuHelpAbout.setText("About");
    jMenuHelpAbout.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        jMenuHelpAbout_actionPerformed(e);
      }
    });
    jPanel2.setLayout(borderLayout2);
    contentPane.setPreferredSize(new Dimension(200, 370));
    jButton1.setText("Zoom Out");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("Zoom IN");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jPanel3.setLayout(borderLayout3);
    jButton3.setToolTipText("");
    jButton3.setHorizontalTextPosition(SwingConstants.CENTER);
    jButton3.setMnemonic('0');
    jButton3.setText("Center");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton4.setText("down");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    });
    jButton5.setText("Left");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton5_actionPerformed(e);
      }
    });
    jButton6.setText("Right");
    jButton6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton6_actionPerformed(e);
      }
    });
    jButton7.setText("up");
    jButton7.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton7_actionPerformed(e);
      }
    });
    jPanel1.setLayout(borderLayout4);
    jButton8.setText("map points");
    jButton8.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton8_actionPerformed(e);
      }
    });
    jButton9.setText("open file");
    jButton9.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton9_actionPerformed(e);
      }
    });

    jButton10.setToolTipText("");
    jButton10.setText("Rotate CClock");
    jButton10.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton10_actionPerformed(e);
      }
    });
    jButton11.setText("Rotate Clock");
    jButton11.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton11_actionPerformed(e);
      }
    });
    jPanel4.setLayout(gridLayout1);
    gridLayout1.setColumns(2);
    gridLayout1.setRows(2);
    borderLayout3.setHgap(20);
    borderLayout3.setVgap(20);
    jPanel6.setLayout(borderLayout5);
    jTextArea1.setEditable(false);
    jTextArea1.setText("jTextArea1");
    jMenuFile.add(jMenuFileExit);
    jMenuHelp.add(jMenuHelpAbout);
    jMenuBar1.add(jMenuFile);
    jMenuBar1.add(jMenuHelp);
    this.setJMenuBar(jMenuBar1);
    contentPane.add(statusBar, BorderLayout.SOUTH);
    contentPane.add(jPanel1,  BorderLayout.CENTER);
    jPanel1.add(jPanel5,  BorderLayout.SOUTH);
    jPanel5.add(jButton9, null);
    jPanel5.add(jButton8, null);
    jPanel1.add(mappoint_panel, BorderLayout.CENTER);
    contentPane.add(jPanel2,  BorderLayout.EAST);
    jPanel2.add(jPanel3, BorderLayout.NORTH);
    jPanel2.add(jPanel4,  BorderLayout.SOUTH);
    jPanel4.add(jButton11, null);
    jPanel4.add(jButton10, null);
    jPanel4.add(jButton2, null);
    jPanel4.add(jButton1, null);
    jPanel2.add(jPanel6,  BorderLayout.CENTER);
    jPanel6.add(jScrollPane1,  BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTextArea1, null);
    jPanel3.add(jButton3, BorderLayout.CENTER);
    jPanel3.add(jButton4, BorderLayout.SOUTH);
    jPanel3.add(jButton5, BorderLayout.WEST);
    jPanel3.add(jButton6, BorderLayout.EAST);
    jPanel3.add(jButton7, BorderLayout.NORTH);
  }
  //File | Exit action performed
  public void jMenuFileExit_actionPerformed(ActionEvent e) {
    System.exit(0);
  }
  //Help | About action performed
  public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
    Frame1_AboutBox dlg = new Frame1_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.pack();
    dlg.show();
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      jMenuFileExit_actionPerformed(null);
    }
  }

  void jButton9_actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    String filename="";


    ixy=new float[100][2];
    // Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the Java 2 SDK, Standard Edition.


    int returnVal = chooser.showOpenDialog(this);
    if(returnVal == JFileChooser.APPROVE_OPTION) {

      filename=chooser.getSelectedFile().getAbsolutePath();
      System.out.println("You chose to open this file: " +filename );

      processFile pf=new processFile(filename);

      ixy=pf.getXYfromfile();
      this.jTextArea1.setText("N\tX\tY\n");
      for(int i=0;i<ixy.length/2;i++){
        this.jTextArea1.append(i+"\t"+ixy[i][0]+"\t"+ixy[i][1]+" \n ");

      }
      somethingtodraw=true;
    }


  }

  void jButton2_actionPerformed(ActionEvent e) {
    mappoint_panel.zoomin();

  }

  void jButton1_actionPerformed(ActionEvent e) {
    mappoint_panel.zoomout();

  }

  void jButton5_actionPerformed(ActionEvent e) {
    mappoint_panel.xshift_left();
  }

  void jButton6_actionPerformed(ActionEvent e) {
     mappoint_panel.xshift_right();
  }

  void jButton3_actionPerformed(ActionEvent e) {
    mappoint_panel.center();
  }

  void jButton4_actionPerformed(ActionEvent e) {
    mappoint_panel.yshift_down();
  }

  void jButton7_actionPerformed(ActionEvent e) {
    mappoint_panel.yshift_up();
  }

  void jButton8_actionPerformed(ActionEvent e) {

    if(somethingtodraw==true){
      mappoint_panel.mapthepoints(ixy);
      somethingtodraw=false;
    }
    else
      JOptionPane.showMessageDialog(this,"No New points to map .",
                                    "Missing Info error", JOptionPane.ERROR_MESSAGE);

  }

  void jButton11_actionPerformed(ActionEvent e) {
    mappoint_panel.rotate_c();
  }

  void jButton10_actionPerformed(ActionEvent e) {
    mappoint_panel.rotate_cc();
  }



}


class processFile{

  char[] buffer;
  int num=0;
  String filename;

  public processFile(String filename){

    this.filename=filename;
    buffer=new char[10*1024];

    openfile();

  }

  public void openfile(){

    File inputFile = new File(filename);


    try{
      FileReader in = new FileReader(inputFile);

      num=in.read(buffer,0,10*1024);

      in.close();


    }
    catch(FileNotFoundException e){

      e.printStackTrace();

    }
    catch(IOException e){

      e.printStackTrace();
    }
  }


  private int[][] readMappoints(){
    int i=0,j=0;
    String str;
    int numArray[][];
    int numArraytemp[];

    numArray=new int[3][100];
    numArraytemp=new int[500];

    str=new String(buffer);
    StringTokenizer st = new StringTokenizer(str," ");
    while (st.hasMoreTokens()) {
      try{
        numArraytemp[i]=Integer.valueOf(st.nextToken()).intValue();

        i++;
      }
      catch(NumberFormatException  e){}

       //System.out.println(st.nextToken());
    }

    for(int k=0;k<=i/3;k++){
      numArray[0][k]=numArraytemp[3*k];
      numArray[1][k]=numArraytemp[3*k+1];
      numArray[2][k]=numArraytemp[k*3+2];
    }
    return numArray;
  }


  public float[][] getXYfromfile(){
    float ixy[][];
    int numArray[][];

    ixy=new float[100][2];


    numArray=readMappoints();

    for(int i=0;i<numArray[0].length;i++){

      ixy[i][0]=(float)(numArray[1][i]*Math.cos(2*Math.PI*numArray[2][i]/3600)*204/14);
      ixy[i][1]=(float)(numArray[1][i]*Math.sin(2*Math.PI*numArray[2][i]/3600)*204/14);
    }

    return ixy;
  }
}


