import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;

public class TVHelp extends JFrame implements TreeSelectionListener { 
    
    private JTextArea   textArea = new JTextArea("Select an item from the list to the left.");
    private JTree       tree;
    private String      theCommands[] = {"APPRO", "APPROS", "CLOSE", "CLOSEI", "DELAY", "DEPART", 
                                        "DEPARTS","DRAW", "GRASP", "HERE", "MOVE", "MOVES", 
                                        "MOVET", "MOVEST", "OPEN", "OPENI", "READY", "SHIFT", 
                                        "SIGNAL", "SPEED", "TYPE", "WAIT"};
    private String      theHowTo[] = {"Starting TeachVAL II", "File Menu", "Project Menu",
                            "Adding a New Point", "Creating a Point Using the Keyboard",
                            "Creating a Point Using the TeachControl", "Deleting or Editing Points",
                            "Opening Ports", "Exiting TeachVAL II"};
                            
    private String theCommandText[] = new String[22];
    private String theHowToText[] = new String[9];
    
    public TVHelp() {
        initComponents();
        getText("bin/helpCommands.txt", theCommandText);
        getText("bin/howTo.txt", theHowToText);
        setVisible(true);   
    }
    
    private void initComponents() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
        setTitle("TeachVAL II Help");
        setSize(750,500);
        this.setResizable(false);
        int x = ((int)screenSize.getWidth()/2)-(getWidth()/2);
        int y = ((int)screenSize.getHeight()/2)-(getHeight()/2);
        setLocation(x,y);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        tree = getTree();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane leftScrollPane = new JScrollPane(tree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JScrollPane rightScrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftScrollPane, rightScrollPane);
        splitPane.setDividerLocation(250);   
        add(splitPane);
    }
    
    private JTree getTree() {
        JTree theTree = new JTree();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("TeachVAL II Help");
        DefaultMutableTreeNode commands = new DefaultMutableTreeNode("TeachVAL II Commands");
        DefaultMutableTreeNode howTo = new DefaultMutableTreeNode("How to Use TeachVAL II");
        DefaultMutableTreeNode manip = new DefaultMutableTreeNode("Manipulating Points");
        for(int i=0; i<theCommands.length; i++) commands.add(new DefaultMutableTreeNode(theCommands[i]));
        howTo.add(new DefaultMutableTreeNode(theHowTo[0]));
        howTo.add(new DefaultMutableTreeNode(theHowTo[1]));
        howTo.add(new DefaultMutableTreeNode(theHowTo[2]));
        manip.add(new DefaultMutableTreeNode(theHowTo[3]));
        manip.add(new DefaultMutableTreeNode(theHowTo[4]));
        manip.add(new DefaultMutableTreeNode(theHowTo[5]));
        manip.add(new DefaultMutableTreeNode(theHowTo[6]));
        howTo.add(manip);
        howTo.add(new DefaultMutableTreeNode(theHowTo[7]));
        howTo.add(new DefaultMutableTreeNode(theHowTo[8]));
        
        
        root.add(commands);
        root.add(howTo);
        theTree = new JTree(root);
        theTree.addTreeSelectionListener(this);        
        return theTree;
    }
    
    private void getText(String filename, String array[]) {
        String text = "";
        int counter = 0;
        try{
            BufferedReader in = new BufferedReader(new FileReader(filename));
            while(true) {
                String temp = in.readLine();
                if(temp.compareTo("~~~") == 0) {
                    array[counter] = text;
                    text = "";
                    counter ++;
                }
                else if (temp == null) break;
                else text += temp + "\n";
            }
            in.close();
        }
        catch(Exception e) {};
    }
    
    public void valueChanged(TreeSelectionEvent event) {
        for(int i=0; i<theCommands.length; i++) {
            if(tree.getLastSelectedPathComponent().toString().compareTo(theCommands[i]) == 0) {
                textArea.setText(theCommandText[i]);
            }
        }
        for(int j=0; j<theHowTo.length; j++) {
            if(tree.getLastSelectedPathComponent().toString().compareTo(theHowTo[j]) == 0) {
                textArea.setText(theHowToText[j]);
            }
        }
        String test = "HELLO\nGOODBYE\nGO TO HELL";
        
    }
    
}
