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


public class SelectSBClass extends JPanel implements ListSelectionListener
{
	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 Vector resultSession = new Vector();
    	
	public SelectSBClass(Vector session)
	{
		setLayout(new BorderLayout());
	        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

		panel = new JPanel(new GridLayout(0, 2, 40, 10));
		
		panel.setPreferredSize(new Dimension(700, 300));
	        listModel = new DefaultListModel();
	        for(Enumeration e=session.elements();e.hasMoreElements();)
	        	listModel.addElement(e.nextElement());
	
		leftPanel = new JPanel(new BorderLayout());
		pList = new JPanel(new BorderLayout());
		makeLeftUI();	        
	        rightPanel = new JPanel(new BorderLayout());
        	makeRightUI();
	}

	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 : "));
	        panel.add(leftPanel);	        
	}
	
	private void makeRightUI()
	{
	
		labelPanel = new JPanel(new GridLayout(0,1,0,50));
	        labelPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
	        jlabels = new JLabel[4];
		jlabels[0] = new JLabel("Selected class:  ");
		jlabels[1] = new JLabel("Bean:  ");
		jlabels[2] = new JLabel("Home Interface:  ");
		jlabels[3] = new JLabel("Remote Interface:  ");
		labelPanel.add(jlabels[0]);
		labelPanel.add(jlabels[1]);
		labelPanel.add(jlabels[2]);
		labelPanel.add(jlabels[3]);
		
		textfieldPanel = new JPanel(new GridLayout(0,1, 0, 50));
		textfieldPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		metaInfos = new JTextField[4];
	        metaInfos[0] = new JTextField(20);
	        metaInfos[1] = new JTextField(20);
		metaInfos[2] = new JTextField(20);
		metaInfos[3] = new JTextField(20);
		textfieldPanel.add(metaInfos[0]);
		textfieldPanel.add(metaInfos[1]);
		textfieldPanel.add(metaInfos[2]);
		textfieldPanel.add(metaInfos[3]);
	        
	        rightPanel.add(labelPanel, BorderLayout.WEST);
	        rightPanel.add(textfieldPanel, BorderLayout.CENTER);
	        rightPanel.setBorder(BorderFactory.createTitledBorder("Session bean Meta Info : "));
	        panel.add(rightPanel);	   
	        this.add(panel, BorderLayout.CENTER);
	}
	
	
    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);

    		String val = metaInfos[0].getText() + "|" + metaInfos[1].getText() + "|" + metaInfos[2].getText() + "|" + metaInfos[3].getText();
    		if(!resultSession.contains(val))
    			resultSession.addElement(val);
            }
        }
    }	
    
    public Vector execute()
    {
    	return resultSession;
    }
    
    /*public static void main(String args[])
    {
       	Vector v = new Vector();
    	v.add("session class 1");
    	v.add("session class 2");
    	SelectSBClass s = new SelectSBClass(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);
    }    	
    */
}

