/*
 * Created on 08/06/2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package Factory;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author testefuncional
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class Empresa extends JFrame implements ActionListener {
    JawtList FuncList;
    
    JTextField tx, txCod, txData, txNome, txProf;
    JLabel lblCod, lblData, lblNome, lblProf;
     
    JButton Get, Insert, Update, Delete;
    
    private static DAOFactory funcFactory;
    private static DAOFuncionario funcDAO;
    
    public Empresa() {
        super("Enumeration demo");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        JPanel jp = new JPanel();
        getContentPane().add(jp);
        jp.setLayout(new GridLayout(1, 2));
        JPanel left = new JPanel();
        JPanel right = new JPanel();
        jp.add(left);
        jp.add(right);
        left.setBorder(new EmptyBorder(5, 5, 5, 5));
        right.setBorder(new EmptyBorder(5, 5, 5, 5));
        FuncList = new JawtList(20);
        left.setLayout(new BorderLayout());
        left.add("Center", FuncList);

        right.setLayout(new BorderLayout());
        tx = new JTextField(10);
        
        lblCod = new JLabel();
        lblCod.setText("Código");
        txCod = new JTextField(17);
        
        lblNome = new JLabel();
        lblNome.setText("Nome");    
        txNome = new JTextField(17);
        
        lblProf = new JLabel();
        lblProf.setText("Profissão");
        txProf = new JTextField(17);
        
        lblData = new JLabel();
        lblData.setText("Data");
        txData = new JTextField(15);
        
        Get = new JButton("Get");
        Get.addActionListener(this);

        Insert = new JButton("Inserir");
        Insert.addActionListener(this);
        
        Update = new JButton("Atualizar");
        Update.addActionListener(this);
        
        Delete = new JButton("Excluir");
        Delete.addActionListener(this);        
        
        JPanel rtop = new JPanel();
        right.add("North", rtop);
        rtop.add(tx);
        rtop.add(Get);
        
        JPanel rcenter = new JPanel();        
        right.add("Center",rcenter);
        
        right.add("Center",rcenter);
        rcenter.add(lblCod);
        rcenter.add(txCod);
        rcenter.add(lblNome);
        rcenter.add(txNome);
        rcenter.add(lblProf);
        rcenter.add(txProf);
        rcenter.add(lblData);        
        rcenter.add(txData);
        
        JPanel rbottom = new JPanel();        
        right.add("South",rbottom);
        
        rbottom.add(Insert);
        rbottom.add(Update);
        rbottom.add(Delete);
        
        setSize(new Dimension(550, 300));
        setVisible(true);
        
        fillFuncList();
    }

    private void fillFuncList() {
    	FuncList.clear();
    	Funcionario f1 = new Funcionario();
    	f1.setAll(0, null, null, null);    	
    	
        try {   
        	List<Funcionario> funcs = funcDAO.searchMatching(f1);
            Iterator<Funcionario> itr = funcs.iterator();
            while (itr.hasNext()) {
                Funcionario f = itr.next();
                FuncList.add(f.getNome());
            }        	
        } catch (Exception e){}        
    }    
    
    public static void main(String[] args) {
    	funcFactory = new DAOFactory_BD();
        funcDAO = funcFactory.makeFuncionarioDAO();    	
        new Empresa();
    }    
    
    private void onInsertClick() { 
    	Funcionario func = new Funcionario();
    	func.setAll(Integer.parseInt(txCod.getText())
    			    , txNome.getText(), txProf.getText(), txData.getText());
    	try {
    		funcDAO.insert(func);
    	} catch (Exception e) {}
    }
    
    public void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("Inserir")) {
    		onInsertClick();
    	}	
    	else {
	        String club = tx.getText();
	        if (club.trim().length() > 0) {
	        }
    	}
    }    
}

interface awtList {
    public void add(String s);
    public void remove(String s);
    public String[] getSelectedItems();    
}

class JawtList extends JScrollPane implements ListSelectionListener, awtList {
    private JList listWindow;
    
    private JListData listContents;
    
    //-----------------------------------------
    public JawtList(int rows) {
        listContents = new JListData();
        listWindow = new JList(listContents);
        listWindow.setPrototypeCellValue("Abcdefg Hijkmnop");
        getViewport().add(listWindow);
        
    }
    
    //-----------------------------------------
    public void add(String s) {
        listContents.addElement(s);
    }
    
    //-----------------------------------------
    public void remove(String s) {
        listContents.removeElement(s);
    }
    
    //-----------------------------------------
    public void clear() {
        listContents.clear();
    }
    
    //-----------------------------------------
    public String[] getSelectedItems() {
        Object[] obj = listWindow.getSelectedValues();
        String[] s = new String[obj.length];
        for (int i = 0; i < obj.length; i++)
            s[i] = obj[i].toString();
        return s;
    }
    
    //-----------------------------------------
    public void valueChanged(ListSelectionEvent e) {
    }
    
}
//=========================================


class JListData extends AbstractListModel {
    private Vector data;
    
    //-----------------------------------------
    public JListData() {
        data = new Vector();
    }
    
    //-----------------------------------------
    public int getSize() {
        return data.size();
    }
    
    //-----------------------------------------
    public Object getElementAt(int index) {
        return data.elementAt(index);
    }
    
    //-----------------------------------------
    public void addElement(String s) {
        data.addElement(s);
        fireIntervalAdded(this, data.size() - 1, data.size());
    }
    
    //-----------------------------------------
    public void removeElement(String s) {
        data.removeElement(s);
        fireIntervalRemoved(this, 0, data.size());
    }
    
    //-----------------------------------------
    public void clear() {
        int size = data.size();
        data = new Vector();
        fireIntervalRemoved(this, 0, size);
    }
}
