package extractor.ui;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;
import javax.swing.colorchooser.*;
import javax.swing.filechooser.*;
import javax.accessibility.*;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;

import extractor.classToEjb.classToSession.*;

public class ProgressSB extends JPanel implements ListSelectionListener, ActionListener, Runnable
{

	public JProgressBar progress;
	JPanel progressPanel;
	
	JPanel mainListPanel;
	JPanel leftListPanel;
	JPanel rightListPanel;
	
	JList leftList;
	JList rightList;
	Vector ejbs = new Vector();
	SessionBeanExtractor ex;
	Vector resultSession = new Vector();
	
	public ProgressSB(Vector ejbs, Vector resultSession)
	{
		this.ejbs = ejbs;
		this.resultSession = resultSession;
		
		setLayout(new BorderLayout());
		createListPanel();
		createProgressPanel();
	}

	public void createListPanel()
	{
		mainListPanel = new JPanel(new GridLayout(0,2));
		mainListPanel.setBorder(BorderFactory.createTitledBorder("Extracting session beans : "));		

		leftListPanel = new JPanel(new BorderLayout());
		rightListPanel = new JPanel(new BorderLayout());
		leftListPanel.setBorder(BorderFactory.createTitledBorder("추출될 session bean 목록 : "));		
		rightListPanel.setBorder(BorderFactory.createTitledBorder("추출된 session bean 목록 : "));

		DefaultListModel leftListModel;
		leftListModel = new DefaultListModel();
		for(Enumeration e=ejbs.elements();e.hasMoreElements();)
			leftListModel.addElement(e.nextElement());
		leftList = new JList(leftListModel);
		leftList.setPreferredSize(new Dimension(300, 200));		
		leftList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		leftList.setSelectedIndex(0);
		leftList.addListSelectionListener(this);		
		leftListPanel.add(leftList);
		
		
		DefaultListModel rightListModel;
		rightListModel = new DefaultListModel();
		rightList = new JList(rightListModel);
		rightList.setPreferredSize(new Dimension(300, 200));
		rightList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		rightList.setSelectedIndex(0);
		rightList.addListSelectionListener(this);
		rightListPanel.add(rightList);
		
		mainListPanel.add(leftListPanel);
		mainListPanel.add(rightListPanel);		
		add(mainListPanel, BorderLayout.CENTER);
	}
		    
	public void createProgressPanel() {
		progressPanel = new JPanel();
		JLabel minLabel = new JLabel("0 %");		
		JLabel maxLabel = new JLabel("100 %");
		
		progress = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100){
		    public Dimension getPreferredSize() {
			return new Dimension(300, super.getPreferredSize().height);
		    }
		};
		
		JButton b = new JButton("Extracting");
		b.addActionListener(this);
				
		progressPanel.add(minLabel);
		progressPanel.add(progress);		
		progressPanel.add(maxLabel);
		progressPanel.add(b);

		add(progressPanel, BorderLayout.SOUTH);
	}

	public void valueChanged(ListSelectionEvent e) {
		progress.setValue(0);
	}

	public void actionPerformed(ActionEvent ev)
	{
  		startProgressBar();
		
	}

  	public SessionBeanExtractor execute() 
  	{
		return ex;  		
  	}


	public void run()
	{
		progress.setValue(0);
		for(Enumeration e=resultSession.elements();e.hasMoreElements();)
		{
			String str = (String)e.nextElement();
			StringTokenizer st = new StringTokenizer(str, "|");
			String className = null;
			while(st.hasMoreTokens())
			{
				className = st.nextToken();
				System.out.println(className);
			}
			
			String selected = (String)leftList.getSelectedValue();
			if(className.equals(selected))
			{
				ex = new SessionBeanExtractor(str, this);
				progress.setValue(progress.getMaximum());
				DefaultListModel rightM = (DefaultListModel)rightList.getModel();
				rightM.addElement(leftList.getSelectedValue());		
				DefaultListModel leftM =  (DefaultListModel)leftList.getModel();
				leftM.removeElement(leftList.getSelectedValue());
			}
		}					
	}

	public void delayTime(long tm)
	{
		try
		{
			Thread.sleep(tm);
		}
		catch(InterruptedException x)
		{
		}
	}
  	
	public void startProgressBar()
	{
		new Thread(this).start();
	}
	
/*	public static void main(String[] args) {
		ProgressSB demo = new ProgressSB();
		JFrame frame = new JFrame();
		     frame.addWindowListener(new WindowAdapter() {
		         public void windowClosing(WindowEvent e) {
		             System.exit(0);
		         }
		     });

		frame.getContentPane().add(demo);
		frame.pack();
		frame.show();	
	}*/
}


