package extractor.ui;

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

import extractor.classToEjb.classToSession.*;
import extractor.classToEjb.classToEntity.*;
import ui.*;
import javax.swing.table.DefaultTableModel;


public class SelectEBClass extends JPanel implements ListSelectionListener, ActionListener
{
	private JPanel panel;
	private JList list;
	private JPanel leftPanel;
	private JPanel pList;
	private JPanel rightPanel;
	private DefaultListModel listModel;
	private JPanel labelPanel;
    	private JLabel[] jlabels;
    	private JPanel textfieldPanel;
    	private JTextField[] metaInfos;
    	private JScrollPane scrollPane;
    	private JPanel rightMergePanel;
    	private JPanel finderPanel;
	private JButton addButton;
	private JPanel buttonPanel;
	private Vector entity = new Vector();
	private FinderUI finderUI = null;
	private ExtractResultsPanel ejbName = null;
	JDialog finderD = new JDialog();
	JList finderList;
	
	public SelectEBClass(Vector entity, ExtractResultsPanel ejbName)
	{
		this.entity = entity;
		this.ejbName = ejbName;
		int sizeOfEntity = entity.size();
		setLayout(new BorderLayout());
	        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

		panel = new JPanel(new GridLayout(0, 2, 40, 10));
		
		panel.setPreferredSize(new Dimension(700, 400));
	        listModel = new DefaultListModel();
	        for(Enumeration e=entity.elements();e.hasMoreElements();)
	        	listModel.addElement(e.nextElement());
	
		leftPanel = new JPanel(new BorderLayout());
		pList = new JPanel(new BorderLayout());
		makeLeftUI();		
	        rightPanel = new JPanel(new BorderLayout());
        	makeRightUI();       	
        	makeFinderPanel();
        	
        	//add
        	rightMergePanel = new JPanel(new BorderLayout());
        	rightMergePanel.add(rightPanel, BorderLayout.NORTH);
        	rightMergePanel.add(finderPanel, BorderLayout.CENTER);
        	
	        panel.add(leftPanel);	        
	        panel.add(rightMergePanel);	   
	        this.add(panel, BorderLayout.CENTER);        	
	}

	private void makeLeftUI()
	{
	        list = new JList(listModel);

	        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        list.setSelectedIndex(0);
	        list.addListSelectionListener(this);	        
	        pList.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	        pList.add(list, BorderLayout.CENTER);
	        leftPanel.add(pList, BorderLayout.CENTER);
	        leftPanel.setBorder(BorderFactory.createTitledBorder("Select classes : "));

	}
	
	private void makeRightUI()
	{
		labelPanel = new JPanel(new GridLayout(0,1,0,10));
	        labelPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	        jlabels = new JLabel[5];
		jlabels[0] = new JLabel("Selected class :  ");
		jlabels[1] = new JLabel("Bean :  ");
		jlabels[2] = new JLabel("Home Interface :  ");
		jlabels[3] = new JLabel("Remote Interface :  ");
		jlabels[4] = new JLabel("Value Object Name :  ");
		labelPanel.add(jlabels[0]);
		labelPanel.add(jlabels[1]);
		labelPanel.add(jlabels[2]);
		labelPanel.add(jlabels[3]);
		labelPanel.add(jlabels[4]);
		
		textfieldPanel = new JPanel(new GridLayout(0,1, 0, 10));
		textfieldPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		metaInfos = new JTextField[5];
	        metaInfos[0] = new JTextField(10);
	        metaInfos[1] = new JTextField(10);
		metaInfos[2] = new JTextField(10);
		metaInfos[3] = new JTextField(10);
		metaInfos[4] = new JTextField(10);
		
		if(!entity.isEmpty())
		{
			String name = (String)entity.get(0);
	            	metaInfos[0].setText(name);
	            	metaInfos[1].setText(name + "Bean");
		 	metaInfos[2].setText(name + "Home");
			metaInfos[3].setText(name);
			metaInfos[4].setText(name + "Data bData");
		}
	
		textfieldPanel.add(metaInfos[0]);
		textfieldPanel.add(metaInfos[1]);
		textfieldPanel.add(metaInfos[2]);
		textfieldPanel.add(metaInfos[3]);
		textfieldPanel.add(metaInfos[4]);
	        
	        rightPanel.add(labelPanel, BorderLayout.WEST);
	        rightPanel.add(textfieldPanel, BorderLayout.CENTER);
	        rightPanel.setBorder(BorderFactory.createTitledBorder("entity bean Meta Info : "));
	}
	
	
	public void makeFinderPanel()
	{
		finderPanel = new JPanel(new BorderLayout());
		finderPanel.setBorder(BorderFactory.createTitledBorder("Finder methods : "));
		addButton = new JButton("Add");
		addButton.addActionListener(this);
			
		buttonPanel = new JPanel();
		buttonPanel.add(addButton);
		
	        DefaultListModel listModel = new DefaultListModel();
	        finderList = new JList(listModel);
	        finderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        finderList.setSelectedIndex(0);
	        finderList.addListSelectionListener(this);
	        JScrollPane listScrollPane = new JScrollPane(finderList);

		finderPanel.add(listScrollPane, BorderLayout.CENTER);
		finderPanel.add(buttonPanel, BorderLayout.SOUTH);
	}
	

    public void actionPerformed(ActionEvent e)
    {
    	// Create new object of FinderUI class
    	System.out.println("I'm an addButton.");

	finderUI = new FinderUI(new JFrame(), ejbName, this);
	//finderD.getContentPane().add(finderUI);
    }
    
    			
    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() == false) {

            if (list.getSelectedIndex() == -1) {
            //No selection, disable fire button.
               metaInfos[0].setText("");

            } else {
            //Selection, update text field.
                String name = list.getSelectedValue().toString();
                metaInfos[0].setText(name);
                metaInfos[1].setText(name + "Bean");
	 	metaInfos[2].setText(name + "Home");
		metaInfos[3].setText(name);
		metaInfos[4].setText(name + "Data bData");
            }
        }
    }	
    
    /*public static void main(String args[])
    {
       	Vector v = new Vector();
    	v.add("entity class 1");
    	v.add("entity class 2");
    	SelectEBClass s = new SelectEBClass(v);
        JFrame frame = new JFrame();
        frame.getContentPane().add(s);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }    	*/

  	public Vector execute() 
  	{//OK
  		entity.clear();
		entity.addElement(metaInfos[1].getText()); //bean class
		entity.addElement(metaInfos[2].getText()); //home interface
		entity.addElement(metaInfos[3].getText()); //remote interface
		entity.addElement(metaInfos[4].getText()); //value object
		
		entity.addElement(finderList); // finder methods
		
		return entity;
  	}
    
}

