import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.util.HashMap;
import java.util.Map;

public class CourseTree extends JFrame {
    private JTree tree;
    private JLabel selectedLabel;

    public CourseTree()
    {
        final Map<String,String> tab2URL = new HashMap<String,String>();
        tab2URL.put("List Iterators", "http://arch.umsl.edu/~siegel/CS4010/Lists/ListIterators.html");
        tab2URL.put("Some Classes", "http://arch.umsl.edu/~siegel/CS4010/Lists/commonstuff.html");
        tab2URL.put("Mixed Lists", "http://arch.umsl.edu/~siegel/CS4010/Lists/MixedLists.html");
        tab2URL.put("HTTP", "http://arch.umsl.edu/~siegel/CS4010/Servlets/http.html");
        //Build the JTree starting at the root.
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("CS 4010");
        ///////////////////////////////////////////
        DefaultMutableTreeNode pageNode = new DefaultMutableTreeNode("Lists");
        pageNode.add(new DefaultMutableTreeNode("List Iterators"));
        pageNode.add(new DefaultMutableTreeNode("Some Classes"));
        pageNode.add(new DefaultMutableTreeNode("Mixed Lists"));
        ////////////////////////////////////////
        DefaultMutableTreeNode tabNode = new DefaultMutableTreeNode("Servlets");
        tabNode.add(new DefaultMutableTreeNode("HTTP"));
        tabNode.add(new DefaultMutableTreeNode("student.java"));
        tabNode.add(new DefaultMutableTreeNode("New test2"));
        tabNode.add(new DefaultMutableTreeNode("The Get Client"));
        tabNode.add(new DefaultMutableTreeNode("The Post Client"));
        /////////////////////////
        root.add(pageNode);
        root.add(tabNode); 
        tree = new JTree(root);

        ImageIcon imageIcon1 = new ImageIcon(CourseTree.class.getResource("./duke1.jpg"));
        ImageIcon imageIcon2 = new ImageIcon(CourseTree.class.getResource("./duke2.jpg"));
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();       
        renderer.setLeafIcon(imageIcon2);
        renderer.setOpenIcon(imageIcon1);
        tree.setCellRenderer(renderer);
        tree.setShowsRootHandles(true);
        tree.setRootVisible(true);
        add(new JScrollPane(tree));    
        tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
                                                              @Override
                                                              public void valueChanged(TreeSelectionEvent e) {
                                                                  DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                                                                  String thisURL=tab2URL.get(selectedNode.getUserObject().toString());
                                                                  System.out.println(thisURL);
                                                                  if (thisURL!=null) {
                                                                      new Thread(new MyBrowser(2,thisURL)).start();
                                                                  }


                                                              }
                                                          });        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Course 4010");       
        this.setSize(300, 400);
        this.setVisible(true);
    }

    public static void main(String[] args)
    {    
        new CourseTree();
    }       
}