package extractor.ui.servletUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.tree.*;

public class ServletSelectUI extends JDialog implements ActionListener
{
	
	//the center panel
	JPanel 	centerPn;
	JPanel	treePn;	
	JTree 	listTree;
	JPanel	btnPn;
	JButton allBtn;
	JButton clearBtn;
	
	//the bottom panel
	JPanel 	bottomPn;
	JButton nextBtn;
	JButton cancelBtn;
	JButton finishBtn;
	

	Vector treeItems;
	Vector selected;
	JFrame frame;
			
	public ServletSelectUI(JFrame f, Vector servlets)
	{
		super(f, true);
		frame = f;
		treeItems = servlets;
		
		setTitle("서블릿을 선택하세요.");
		makeCenterPanel(); // the center panel including a tree and two buttons
		makeBottomPanel(); 	 // the bottom panel including three buttons
		
		BorderLayout bl = (BorderLayout)getContentPane().getLayout();
		bl.setVgap(30);
		getContentPane().add(centerPn, BorderLayout.CENTER);
		getContentPane().add(bottomPn, BorderLayout.SOUTH);
		
		setSize(600, 500);		
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					dispose();
				}
			}
		);		
		setVisible(true);
	}
	
	private void makeCenterPanel()	
	{
		centerPn = new JPanel(new BorderLayout());		
		centerPn.setBorder(BorderFactory.createTitledBorder("The List of Servlets : "));
		
		treePn = new JPanel(new BorderLayout());
		treePn.setBorder(new EmptyBorder(10,10,10,10));

		
		makeTree(treeItems);
		treePn.add(listTree, BorderLayout.CENTER);
				
		btnPn = new JPanel();
		allBtn = new JButton("All");
		clearBtn = new JButton("Clear");		
		allBtn.addActionListener(this);
		clearBtn.addActionListener(this);
		btnPn.add(allBtn);
		btnPn.add(clearBtn);		
		
		centerPn.add(treePn, BorderLayout.CENTER);
		centerPn.add(btnPn, BorderLayout.SOUTH);
	}
	
	private void makeTree(Vector values)
	{		
		DefaultMutableTreeNode top = new DefaultMutableTreeNode("Target Servlets");
		DefaultMutableTreeNode classNode = null;
  		for(int i=0; i<values.size();i++)
  		{
  			classNode = new DefaultMutableTreeNode(values.get(i));
  			top.add(classNode);
  		}
   		listTree = new JTree(top);
   		listTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
		renderer.setLeafIcon(new ImageIcon("images/class.gif"));
		listTree.setCellRenderer(renderer);		
	}
	
	private void makeBottomPanel()
	{
		bottomPn = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 0));
		nextBtn = new JButton("Next");
		cancelBtn = new JButton("Cancel");
		finishBtn = new JButton("Finish");
		
		nextBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		finishBtn.addActionListener(this);
		
		bottomPn.add(nextBtn);
		bottomPn.add(cancelBtn);
		bottomPn.add(finishBtn);		
		
	}

	public Vector getSelectedNodes()
	{
  		TreePath[] tps = listTree.getSelectionPaths();
  		selected = new Vector();
  		
  		if(tps != null)
  		{
	  		for(int i = 0; i<tps.length; i++)
	  		{
	  			System.out.println(tps[i].getLastPathComponent().toString());
	    		selected.addElement(tps[i].getLastPathComponent().toString());
	  		}  
  		}
  		
  		return selected;
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		Object obj = e.getSource();
		if(obj == nextBtn)
		{
			System.out.println("I'm next button.");
			getSelectedNodes();
			if(selected.size() > 0)
			{
				new ServletMainTFUI(this, selected);
			}
			else
				JOptionPane.showMessageDialog(null,"선택된 서블릿이 없습니다.",
												"warning message", JOptionPane.WARNING_MESSAGE);
					
		}
		else if(obj == allBtn)
		{
			System.out.println("I'm all button.");
			listTree.expandRow(0);
			listTree.setSelectionInterval(1, listTree.getRowCount());
		}
		else if(obj == clearBtn)
		{
			System.out.println("I'm cancel button.");
			listTree.clearSelection();
		}
		else if(obj == cancelBtn)
		{
			dispose();
		}
		else if(obj == finishBtn)
		{
			dispose();
		}
	}
	
	/*public static void main(String[] arg)
	{

		Vector servlets = new Vector();
		servlets.add("BoardList_db");
		servlets.add("BoardSearch_db");
		servlets.add("BoardUpdate_db");
		
		new ServletSelectUI(servlets);	
	}*/	
}