package extractor.ui;

import javax.swing.JTree;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeSelectionModel;
import java.net.URL;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.*;
import java.util.*;

public class ResultSessionBean extends JPanel {

	Vector resultSession = new Vector();
	String className;
	String beanName;
	String homeName;
	String remoteName;
	
    public ResultSessionBean(Vector resultSession) {
    	this.resultSession = resultSession;
    	
   	
        //Create the nodes.
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Result of Session beans");
        createNodes(top);

        //Create a tree that allows one selection at a time.
        final JTree tree = new JTree(top);
        tree.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);

        //Listen for when the selection changes.
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                                   tree.getLastSelectedPathComponent();

                if (node == null) return;

                Object nodeInfo = node.getUserObject();
                if (node.isLeaf()) {
                        System.out.print("I'm a leaf. ");
                } else {
                    
                }
            }
        });

	DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
	renderer.setLeafIcon(new ImageIcon("images/class.gif"));
	tree.setCellRenderer(renderer);

        JScrollPane treeView = new JScrollPane(tree);
        treeView.setPreferredSize(new Dimension(500, 300));
	treeView.setBorder(BorderFactory.createTitledBorder("session bean ÃßÃâ°á°ú : "));		
	
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        add(treeView, BorderLayout.CENTER);
    }


    private void createNodes(DefaultMutableTreeNode top) {

        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode book = null;
    	
		for(Enumeration e=resultSession.elements();e.hasMoreElements();)
		{
			String str = (String)e.nextElement();
			StringTokenizer st = new StringTokenizer(str, "|");
			while(st.hasMoreTokens())
			{
				className = st.nextToken();
				System.out.println(className);
				beanName = st.nextToken();
				System.out.println(beanName);
				homeName = st.nextToken();
				System.out.println(homeName);
				remoteName = st.nextToken();
				System.out.println(remoteName);
			}
			
	        category = new DefaultMutableTreeNode(className);
	        top.add(category);
		book = new DefaultMutableTreeNode(beanName);
		category.add(book);
		book = new DefaultMutableTreeNode(homeName);
		category.add(book);
		book = new DefaultMutableTreeNode(remoteName);
		category.add(book);
		}
    }

/*    public static void main(String[] args) {
        ResultSessionBean r = new ResultSessionBean();
        JFrame frame = new JFrame();        
 
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });  
	frame.getContentPane().add(r);
 
        frame.pack();
        frame.setVisible(true);
    }*/
}
