package extractor.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import extractor.classToEjb.*;
import extractor.classToEjb.classToEntity.*;
//import extractor.ui.expected.*;
import ui.*;
import javax.swing.event.*;


public class FinderUI extends JDialog implements ListSelectionListener, ActionListener
{
  	JLabel jLabel1 = new JLabel();
  	JTextField jTextField1 = new JTextField();
  	JLabel jLabel2 = new JLabel();
  	JTextField jTextField2 = new JTextField();
  	JLabel jLabel3 = new JLabel();
  	JTextField jTextField3 = new JTextField();
  	JLabel jLabel4 = new JLabel();
  	JTextField jTextField4 = new JTextField();
  	JList jList1 = new JList();
  	Border border1;
  	JButton jButton1 = new JButton();
  	JButton jButton2 = new JButton();
  	JButton jButton3 = new JButton();
  	Vector v = new Vector();
  	ExtractResultsPanel ejbName = null;

	JPanel textfieldPanel;
	JPanel rightPanel;
	JScrollPane listScrollPane;
	JPanel buttonP;
  	JList list;
  	JButton addButton;
  	JButton okButton;
	JTextField[] metaInfos;
	SelectEBClass selectEB;
	
  	/**Construct the frame*/
  	public FinderUI(JFrame f, ExtractResultsPanel ejbName, SelectEBClass selectEB) 
  	{
  		super(f, "Finder Method", true);
  		
  		this.ejbName = ejbName;
  		this.selectEB = selectEB;
    		try 
    		{
      			jbInit();
    		}
    		catch(Exception e) 
    		{
      			e.printStackTrace();
    		}
  	}
  
  
  	/**Component initialization*/
  	private void jbInit() throws Exception  
  	{
    		
    		getContentPane().setLayout(new BorderLayout());
    		textfieldPanel = new JPanel(new GridLayout(0,1, 0, 10));
		textfieldPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		makeInputFields();
    		makeList();
		makeButton();
		getContentPane().add(rightPanel, BorderLayout.NORTH);
		getContentPane().add(listScrollPane, BorderLayout.CENTER);
		getContentPane().add(buttonP, BorderLayout.SOUTH);

		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					dispose();
				}
			}
		);

		setSize(500, 500);
		setVisible(true);    				
  	}
  
  	void jButton1_actionPerformed(ActionEvent e) 
  	{
    		String str = "public " + jTextField2.getText() + " " + jTextField1.getText() + "(" 
    			+ jTextField3.getText() + ")";
    		v.addElement(str);
    		jList1.setListData(v);
  	}
  
  
  	public void execute()
  	{ //ok
		HomeExtractor he = new HomeExtractor();
		String pk = jTextField4.getText();
		HomeExtractor.dicName = EntityBeanExtractor.dicName;
		he.makeFinderMethods(pk, v);
		Vector session = new Vector();
		session = ExpectedUI.sess;	
	
//		JOptionPane joption = new JOptionPane();
		//joption.setValue("session");
		//if(joption.getValue() == "session") {
  		//	new SessionMain(session, ejbName);
  		//}	
  	}
  	
  	/////////////////////////////////////////////////////////////////////////////
  	public Vector getSession()
  	{
  		return ExpectedUI.sess;	
  	}
  
  	private void makeInputFields()
  	{
		JPanel labelPanel = new JPanel(new GridLayout(0,1,0,10));
	        labelPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	        JLabel[] jlabels = new JLabel[4];
		jlabels[0] = new JLabel("finder method name :  ");
		jlabels[1] = new JLabel("return type :  ");
		jlabels[2] = new JLabel("parameters :  ");
		jlabels[3] = new JLabel("primary key :  ");
		labelPanel.add(jlabels[0]);
		labelPanel.add(jlabels[1]);
		labelPanel.add(jlabels[2]);
		labelPanel.add(jlabels[3]);

		metaInfos = new JTextField[4];
	        metaInfos[0] = new JTextField(10);
	        metaInfos[1] = new JTextField(10);
		metaInfos[2] = new JTextField(10);
		metaInfos[3] = new JTextField(10);
		textfieldPanel.add(metaInfos[0]);
		textfieldPanel.add(metaInfos[1]);
		textfieldPanel.add(metaInfos[2]);
		textfieldPanel.add(metaInfos[3]);

		rightPanel = new JPanel(new BorderLayout());		
	        rightPanel.add(labelPanel, BorderLayout.WEST);
	        rightPanel.add(textfieldPanel, BorderLayout.CENTER);
	        rightPanel.setBorder(BorderFactory.createTitledBorder("Finder method : "));
	}
	
	private void makeList()
	{
	        DefaultListModel listModel = new DefaultListModel();
	
	        //Create the list and put it in a scroll pane
	        list = new JList(listModel);
	        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        list.setSelectedIndex(0);
	        list.addListSelectionListener(this);
	        listScrollPane = new JScrollPane(list);
	}
	
	private void makeButton()
	{
		buttonP = new JPanel(new FlowLayout());
		addButton = new JButton("ADD");
		okButton = new JButton("OK");		
		
		addButton.addActionListener(this);
		okButton.addActionListener(this);
		
		buttonP.add(addButton);		
		buttonP.add(okButton);
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == addButton)
		{
			System.out.println("I'm an addBtn");
	    		String str = "public " + metaInfos[1].getText() + " " + metaInfos[0].getText() + "(" 
	    			+ metaInfos[2].getText() + ")";
	    		v.addElement(str);
	    		list.setListData(v);
		}
		if(e.getSource() == okButton)
		{
			System.out.println("I'm an okBtn");
			selectEB.finderList.setListData(v);
			dispose();			
		}

	}

  	void b_actionPerformed(ActionEvent e) 
  	{
  	}

    public void valueChanged(ListSelectionEvent e) {}	
}