/*  TeachVAL.java
 *
 *  Created on June 10, 2005, 6:22 PM
 *
 *  This is the main class that runs the robot control software.
 *  It initilized all other classes and acts as a junction point
 *  to those other classes.
 *
 *  Author: Michael A. Piskuric (2537405) Summer 2005
 */

import Robot.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.beans.*; 

public class TeachVAL extends JFrame{
	
    private Menu            menu;               // the menu
    private StatusBar       statusBar;          // the statusbar
    private EditPanel       editPanel;          // the editor panel
    private PointPanel      pointPanel;         // the point panel
    private Compiler        compiler;           // the compiler
    private RobotComm       port = null;        // the communications port
    private int             speed = Microbot.FAST;
    private Location        currentLocation = Microbot.HOME;
    private String          whatPort = null;
    private File            cwd = new File(System.getProperty("user.dir"));
    
    // main constructor that initilized the entire thing
    public TeachVAL() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(screenSize.width, screenSize.height-40);
        setExtendedState (JFrame.MAXIMIZED_BOTH);
        setLayout(new FlowLayout());
        setTitle("TeachVAL II");
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exit();                
            }});
        menu = new Menu(this);
        editPanel = new EditPanel(this);
        pointPanel = new PointPanel(this);
        compiler = new Compiler(this);
        statusBar = new StatusBar(this);
        new Splash();
        setVisible(true);      
    }
    
    // exits the program and cleans out the CWD
    public void exit() {
        if (confirm("Do you want to exit?")) {
            if(port != null) port.close();
            try{
                Process proc = Runtime.getRuntime().exec("bin/cleanup.bat");
                proc.waitFor();
            }catch(Exception e) {new TVException(e.getMessage());};
            dispose();
            System.exit(0);
        }
    }
    
    public boolean confirm(String theMessage){
        int result = JOptionPane.showConfirmDialog((Component)null, theMessage, "Are you sure?", JOptionPane.YES_NO_OPTION);
        return result == JOptionPane.YES_OPTION;
    }
    
    // sets the working port
    public void setPort(RobotComm p, String wp) {
        port = p;
        whatPort = wp.trim();
        pointPanel.toggleButtons(port.isOpen());
        if(port.isOpen()) {
            port.reset();
            getStatusBar().setText("Port opened successfully.");
        }
    }
    
    // what port are we using?
    public RobotComm getPort() {
        return port;
    }
    
    // get the editor panel
    public EditPanel getEditPanel() {
        return editPanel;
    }
    
    // get the statusbar
    public StatusBar getStatusBar() {
        return statusBar;
    }
    
    // get the point panel
    public PointPanel getPointPanel() {
        return pointPanel;
    }
    
    // the the menu
    public Menu getMenu() {
        return menu;
    }
    
    // get the compiler
    public Compiler getCompiler(){
        return compiler;
    }
    
    // get the current speed
    public int getSpeed() {
        return speed;
    }
    
    // set the speed
    public void setSpeed(int sp) {
        speed = sp;
    }
    
    // get the current working directory
    public File getCWD() {
        return cwd;
    }

    // reopen a port after it was temporary 
    // closed for program execution
    public void reOpenPort() {
        if(whatPort != null){
            port = new RobotComm(whatPort);
            pointPanel.toggleButtons(port.isOpen());
        }
    }
    
    // temportaliy closes the communications port so
    // that a program can execute
    public void tempClosePort() {
        if(port!=null && port.isOpen()) {
            port.close();
            port=null;
        }
    }
    
    // get the curernt robot location
    public Location getCurrentLocation(){
        return currentLocation;
    }
    
    public void setCurrentLocation(Location loc) {
        currentLocation = loc;
    } 
    
    public static void main(String args[]) {new TeachVAL();}
}   // end main TeachVAL
