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.classToEntity.*;


public class ProgressEB 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();
	EntityBeanExtractor ex;
	
	public ProgressEB(Vector ejbs)
	{
		this.ejbs = ejbs;
		setLayout(new BorderLayout());
		createListPanel();
		createProgressPanel();

	}

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

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

		DefaultListModel leftListModel;
		leftListModel = new DefaultListModel();
		leftListModel.addElement(ejbs.get(2));

		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 actionPerformed(ActionEvent ev)
	{

  		startProgressBar();
		
	}


	public void valueChanged(ListSelectionEvent e) {
	}
	
	
  	public EntityBeanExtractor execute() 
  	{
		return ex;  		
  	}
  	

	public void run()
	{
		Vector beanInfo = new Vector();
  		ejbs.remove(ejbs.lastElement());
  		beanInfo = ejbs;
	
		for(Enumeration e=beanInfo.elements();e.hasMoreElements();)
			System.out.println("beanInfo = " + e.nextElement());

		
		ex = new EntityBeanExtractor(beanInfo, this);
		
				
		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) {
		ProgressEB demo = new ProgressEB();
		JFrame frame = new JFrame();
		     frame.addWindowListener(new WindowAdapter() {
		         public void windowClosing(WindowEvent e) {
		             System.exit(0);
		         }
		     });

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


