package extractor.ui.servletUI;

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

import java.util.*;

public class ServletPkgTFUI extends JPanel implements ActionListener, ListSelectionListener
{
	private JPanel textFieldPane;
	private JPanel deletePane;
	private JScrollPane deleteFieldPane;
	private JComboBox deleteCombo;
	private JList deleteList;
	private JScrollPane addFieldPane;
	private JTextArea addField;
	private JPanel pkgFieldPane;
	private JTextField pkgField;
	private JScrollPane resultFieldPane;
	private JList resultList;
	private JPanel classNameFieldPane;
	private JTextField classNameField;
	private JButton okBtn;	
	private JPanel okBtnPane;
	
	private DefaultListModel listModel;
	private DefaultListModel resultListModel;
	
	
	public ServletPkgTFUI()
	{
		listModel = new DefaultListModel();
		resultListModel = new DefaultListModel();
		
		setLayout(new BorderLayout());		
		
		addComponents();
		
		add(textFieldPane, BorderLayout.CENTER);
		setVisible(true);
	}


	private void addComponents()
	{
		textFieldPane = new JPanel();
		textFieldPane.setLayout(new BoxLayout(textFieldPane, BoxLayout.Y_AXIS));
		textFieldPane.setBorder(BorderFactory.createTitledBorder("서블릿의 패키지정보를 바꿉니다"));		



		pkgField = new JTextField();
		pkgField.addActionListener(this);
		pkgFieldPane = new JPanel(new BorderLayout());
		pkgFieldPane.add(pkgField);
		pkgFieldPane.setPreferredSize(new Dimension(200, 60));
		pkgFieldPane.setBorder(BorderFactory.createTitledBorder("클래스 패키지 이름 : "));

	//for test		
		Vector listValues = new Vector();
		listValues.add("value 1");
		listValues.add("value 2");
		listValues.add("value 3");
		listValues.add("value 4");
		listValues.add("value 5"); 
		listValues.add("value 6");
		listValues.add("value 7");
		listValues.add("value 8");

		deleteCombo = new JComboBox(listValues);
		deleteCombo.addActionListener(this);
		deleteList = new JList(listModel);
		deleteList.addListSelectionListener(this);
		deletePane = new JPanel(new BorderLayout(0, 10));
		deleteFieldPane = new JScrollPane(deleteList);
		deleteFieldPane.setPreferredSize(new Dimension(200, 250));
		deletePane.add(deleteCombo, BorderLayout.NORTH);
		deletePane.add(deleteFieldPane, BorderLayout.CENTER);
		deletePane.setBorder(BorderFactory.createTitledBorder("패키지 삭제 : "));		
		
		addField = new JTextArea();
		addFieldPane = new JScrollPane(addField);
		addFieldPane.setPreferredSize(new Dimension(200, 250));
		addFieldPane.setBorder(BorderFactory.createTitledBorder("패키지 추가 : "));		
		
		resultList = new JList(resultListModel);
		resultFieldPane = new JScrollPane(resultList);	
		resultFieldPane.setPreferredSize(new Dimension(200, 500));
		resultFieldPane.setBorder(BorderFactory.createTitledBorder("패키지 변환 결과 : "));
		
		classNameField = new JTextField();
		classNameField.addActionListener(this);
		classNameFieldPane = new JPanel(new BorderLayout());
		classNameFieldPane.add(classNameField);
		classNameFieldPane.setPreferredSize(new Dimension(200, 60));
		classNameFieldPane.setBorder(BorderFactory.createTitledBorder("클래스 이름 : "));

		
		okBtnPane = new JPanel();
		okBtn = new JButton("OK");
		okBtn.addActionListener(this);
		okBtnPane.add(okBtn);
		    
		textFieldPane.add(pkgFieldPane);
		textFieldPane.add(Box.createRigidArea(new Dimension(0,5)));
		textFieldPane.add(deletePane);
		textFieldPane.add(Box.createRigidArea(new Dimension(0,5)));
		textFieldPane.add(addFieldPane);
		textFieldPane.add(Box.createRigidArea(new Dimension(0,5)));
		//textFieldPane.add(resultFieldPane);		
		textFieldPane.add(classNameFieldPane);
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == deleteCombo)
		{
			System.out.println("Hi, deleteCombo");
	        		String listValue = (String)deleteCombo.getSelectedItem();
	   	        	if(!listModel.contains(listValue))
	   	        	{
			        	listModel.addElement(listValue);
				deleteList.setSelectedIndex(listModel.size()-1);
			}
	   	}
		if(e.getSource() == pkgField)
		{
			addPkgCode(pkgField.getText());
		}
	}
	
	public void valueChanged(ListSelectionEvent e) {
		//ListModel lm = deleteList.getModel();
		//addImportPkgCode(lm);
	}
	
	private void addPkgCode(String pkg)
	{
		if(!resultListModel.contains(pkg))
		{
			if(resultListModel.size() > 0)
				resultListModel.setElementAt(pkg, 0);
			else
				resultListModel.add(0, pkg);
	}	}

	private void addImportPkgCode(ListModel lm)
	{
		String value = "";
		for(int i=0; i<lm.getSize();i++)
		{
			value = (String)lm.getElementAt(i);
			if(!resultListModel.contains(value))
			{
				if(resultListModel.size() > i+1)
					resultListModel.setElementAt(value, i+1);
				else
					resultListModel.add(i+1, value);	
			}
		}
	}	
	
	public static void main(String[] arg)
	{
		JFrame f = new JFrame();
		ServletPkgTFUI sName = new ServletPkgTFUI();
		f.getContentPane().add(sName);
		f.setVisible(true);
		f.pack();
	}
	
}