/*
 * JListagem.java
 *
 * Created on 12 de Outubro de 2004, 01:38
 */

package locadora.ui;

/**
 *
 * @author  rbarcelos
 */
import javax.swing.*;
import java.util.*;
import locadora.core.*;

public class JListagem extends javax.swing.JDialog {
    
    /** Creates new form JListagem */
    ChooseListModel modelo;
    IRecebeSelecao receptor;
    
    public JListagem(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.setSize(400,600);
        initComponents();
        modelo = new ChooseListModel();
        list.setModel(modelo);
        
        
       
    }
    
    public void setReceptor(IRecebeSelecao rec)
    {
        receptor = rec;
    }
    
    public void setObject(Vector vec)
    {
        for(int i=0;i<vec.size();i++)
        {
            modelo.add(vec.get(i));
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();

        getContentPane().setLayout(new java.awt.CardLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        list.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selecionado(evt);
            }
        });

        jScrollPane1.setViewportView(list);

        getContentPane().add(jScrollPane1, "card2");

        pack();
    }//GEN-END:initComponents

    private void selecionado(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selecionado
        Object selecionado;
        if(evt.getClickCount()==2)
        {
          selecionado = list.getSelectedValue();
            
          
          receptor.setSelecionado(selecionado);
          this.dispose();
          
        }
    }//GEN-LAST:event_selecionado
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JListagem(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JList list;
    // End of variables declaration//GEN-END:variables
    
}

class ChooseListModel extends AbstractListModel {
    Vector elementos;
    
    public ChooseListModel() {
        super();
        elementos = new Vector();
        
    }
    
    public int getSize() {
        return elementos.size();
    }
    
    public void clear() {
        elementos.clear();
    }
    
    public Object getElementAt(int i) {
        return elementos.elementAt(i);
    }
    
    public void add(Object obj) {
        elementos.add(obj);
    }
}
