/*  LocationDialog.java
 *
 *  This class is a dialog that is used to allow users to
 *  add new points to the system memory.
 *
 *  Author: Michael A. Piskuric (2537405) Summer 2005
 */

import Robot.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LocationDialog extends JDialog {
	
    private TeachVAL        parent;                             // parent window
    private ManualPanel     manual = new ManualPanel(this);     // panel for manually adding points
    private KeyboardPanel   keyboard = new KeyboardPanel(this); // panel for adding points using kbd
    private CardLayout      cardlayout = new CardLayout();      // layout for switching
    private Location        startLocation = null;
    private boolean         edit = false;
    
    // create new instance of LocationDialog for creating NEW points
    public LocationDialog(TeachVAL tv, String title) {
        parent = tv;
        setLocation((parent.getWidth()/2)-100, (parent.getHeight()/2)-100);
        setTitle(title);        
        setSize(200, 250);
        getContentPane().setLayout(cardlayout);
        getContentPane().add("manual", manual);
        getContentPane().add("keyBoard",keyboard);
        cardlayout.show(getContentPane(), "keyBoard"); 
        //parent.getPort().read();
        //startLocation = parent.getCurrentLocation();
        setVisible(true);
    }
    
    // create new instance of LocationDialog for EDITING points
    public LocationDialog(TeachVAL tv, Location loc) {
        this(tv, "Edit Point");
        manual.populate(loc);
        keyboard.populate(loc);
        edit = true;
    }
    
    // used for getting parent from the child panels
    public TeachVAL getParent() {
        return parent;
    }
    
    // flips between the 2 input panels
    public void change(String which) {
        cardlayout.show(getContentPane(), which); 
    }
    
    public boolean isEdit() {
        return edit;
    }
    
    // this internal class is a panel that contains the widgets
    // needed to create a point using the keyboard
    private class KeyboardPanel extends JPanel implements ActionListener {
        
        private JButton         ok,cancel,manButton;
        private LocationDialog  parent;
        private JTextField      X,Y,Z,P,R,NAME;
        private String          originalName = null;
        
        // new instance of KeyboardPanel and place the widgets
        private KeyboardPanel(LocationDialog p) {
            parent = p;
            
            JPanel panel1 = new JPanel();
            manButton = new JButton("Using TeachControl");
            manButton.addActionListener(this);
            panel1.add(manButton);
        
            JPanel panel2 = new JPanel(); 
            ok = new JButton("OK");
            cancel = new JButton("Cancel");
            ok.addActionListener(this);
            cancel.addActionListener(this);
            ok.setEnabled(false);
            panel2.add(ok);
            panel2.add(cancel);
                          
            JPanel panel3 = new JPanel();
            panel3.setLayout(new GridLayout(6,2));
            panel3.add(new JLabel("X:"));
            panel3.add(X = new JTextField("0",6));
            panel3.add(new JLabel("Y:"));
            panel3.add(Y = new JTextField("0",6));
            panel3.add(new JLabel("Z:"));
            panel3.add(Z = new JTextField("0",6));
            panel3.add(new JLabel("PITCH:"));
            panel3.add(P = new JTextField("0",6));
            panel3.add(new JLabel("ROLL:"));
            panel3.add(R = new JTextField("0",6));
            panel3.add(new JLabel("NAME:"));
            panel3.add(NAME = new JTextField(6));
            NAME.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent evt) {
                    if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }
                public void keyReleased(KeyEvent evt){
                    if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }
                public void keyPressed(KeyEvent evt){
                    if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }});       
            add(panel1);
            add(panel3);
            add(panel2);
        }
        
        // if editing the point populate the fields
        public void populate(Location loc) {
            X.setText(parent.getParent().getPointPanel().truncate(loc.getX()));
            Y.setText(parent.getParent().getPointPanel().truncate(loc.getY()));
            Z.setText(parent.getParent().getPointPanel().truncate(loc.getZ()));
            P.setText(parent.getParent().getPointPanel().truncate(loc.getPitch()));
            R.setText(parent.getParent().getPointPanel().truncate(loc.getRoll()));
            NAME.setText(loc.getName());
            if(NAME.getText().trim().length() > 0) ok.setEnabled(true);
            originalName = loc.getName();
        }
        
        // do proper action when OK or Cancel pressed
        public void actionPerformed(ActionEvent e) {
            JButton temp = (JButton)e.getSource();
            String theName = NAME.getText().trim(); 
            boolean isValid = true;
            if(theName.length() > 0) {
                char theChar = theName.charAt(0);
                isValid = (((theChar >='a')&& (theChar <='z')) | ((theChar >='A')&& (theChar <='Z')));
            }
            if(isValid && temp.getText() == "OK"){
                if(parent.isEdit())parent.getParent().getPointPanel().removePoint(originalName);
                double x = Double.parseDouble(X.getText());
                double y = Double.parseDouble(Y.getText());
                double z = Double.parseDouble(Z.getText());
                double p = Double.parseDouble(P.getText());
                double r = Double.parseDouble(R.getText());
                parent.getParent().getPointPanel().addNewPoint(new Location(x,y,z,p,r,theName));
                parent.getParent().getStatusBar().setText("Point Added");
                dispose();
            }
            else if(temp.getText() == "Using TeachControl"){
                parent.change("manual");
            }
            else if(temp.getText() == "Cancel"){
                parent.dispose();
            }
            else 
                new TVException("Name must start with a letter (a-z) or (A-Z)");
        }
    }   // end KeyboardPanel 
    
    // this internal class gives all the nescessary wigets for
    // creating a point using the TeachControl
    private class ManualPanel extends JPanel implements ActionListener {
        
        private JButton         ok,cancel,keyButton;
        private LocationDialog  parent;
        private JTextField      NAME;
        private String          originalName = null;
        
        // new instance of ManualPanel and initialize the wigets
        private ManualPanel(LocationDialog p) {
            parent = p;
            JPanel panel1 = new JPanel();
            keyButton = new JButton("Using Keyboard");
            keyButton.addActionListener(this);
            panel1.add(keyButton);
                
            JPanel panel2 = new JPanel(); 
            ok = new JButton("OK");
            cancel = new JButton("Cancel");
            ok.addActionListener(this);
            cancel.addActionListener(this);
            ok.setEnabled(false);
            panel2.add(ok);
            panel2.add(cancel); 
        
            JPanel panel3 = new JPanel();  
            panel3.setLayout(new GridLayout(2,1));
            JTextArea text = new JTextArea("Use the Teach Control pendant \n       to locate the new point. \n          Press OK then done.");
            text.setBackground(getBackground());
            text.setEditable(false);        
            panel3.add(text);
        
            JPanel panel4 = new JPanel();
            panel4.add(new JLabel("NAME:"));
            panel4.add(NAME = new JTextField(6));
            NAME.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent evt) {
                    if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }
                public void keyReleased(KeyEvent evt){
                    if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }
                public void keyPressed(KeyEvent evt){
                if(NAME.getText().trim().length() > 0)ok.setEnabled(true);
                    else if(NAME.getText().trim().length() == 0)ok.setEnabled(false);
                }}); 
            panel3.add(panel4);
        
            add(panel1);
            add(panel3);
            add(panel2);        
        }
        
        // populate the field if editing the point
        public void populate(Location loc) {
            NAME.setText(loc.getName());
            if(NAME.getText().length() > 0) ok.setEnabled(true);
            originalName = loc.getName();
        }
        
        // do proper action id OK or CANCEl is pressed
        public void actionPerformed(ActionEvent event) {
            JButton temp = (JButton)event.getSource(); 
            String theName = NAME.getText().trim();     
            boolean isValid = true;
            if(theName.length() > 0) {
                char theChar = theName.charAt(0);
                isValid = (((theChar >='a')&& (theChar <='z')) | ((theChar >='A')&& (theChar <='Z')));
            }
            if(isValid && (temp.getText() == "OK")){
                if(parent.isEdit())parent.getParent().getPointPanel().removePoint(originalName);
                parent.getParent().getPort().read();
                int data[] = parent.getParent().getPort().getAllData();
                if(data[0] == parent.getParent().getPort().SUCCESSFUL){
                    int b = 0 + data[1];
                    int s = -499 + data[2];
                    int e = 1165 + data[3];
                    int rw = 384 + data[4];
                    int lw = 384 + data[5];
                    int g = 0 + data[6];
                    Location tmp = new Location(b,s,e,rw,lw,g,theName);
                    parent.getParent().getPointPanel().addNewPoint(tmp);  
                }
                dispose();
            }
            else if(temp.getText() == "Using Keyboard"){
                parent.change("keyBoard");
            }
            else if(temp.getText() == "Cancel"){
                parent.dispose();
            }
            else 
                new TVException("Name must start with a letter (a-z) or (A-Z)");
        }        
    }   // end manual panel class
}   // end LocationDialog