package vista;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import base.Cliente;
import base.ClienteDTO;
import javax.swing.JComboBox;
import javax.swing.BorderFactory;
import javax.swing.border.SoftBevelBorder;
public class AbmClienteS<M, I> extends PanelGeneral {
private TableRowSorter<TableModel> modeloOrdenado;
private static final long serialVersionUID = 1L;
TablaCliente table = new TablaCliente();
DefaultTableModel dtm= (DefaultTableModel)table.getModel();
JScrollPane scrollPane = new JScrollPane(table);
String[] prueba = new String[8];
private JPanel panelFiltro,
panelSeleccion,
panelTabla,
panelBotonera;
private JLabel etiquetaNombre = null,
etiquetaNyA = null,
etiquetaTipoId,
etiquetaIdFiscal,
etiquetaTelefono,
jLabel = null,
jLabel1 = null,
jLabel2 = null,
etiquetaId;
private JButton botonEliminar,
botonNuevo,
botonModificar,
botonSalir;
public JComboBox selectorTipoId = null;
private JTextField textoNombre = null,
textoNyA = null,
textoTipoId,
textoIdFiscal,
textoTelefono,
jTextField = null,
textoId,
textoIdF;
static JDialog dialog=new JDialog();
static private AbmCliente singletonAbmCliente = null;
/**
* This is the default constructor
*/
public AbmCliente() {
super();
initialize();
inicializarTabla();
this.getEtiquetaRutaNavegacion().setText(" Inicio/Administraci�n Clientes");
this.add(botonSalir, null);
}
static public AbmCliente getSingletonAbmCliente() {
if (singletonAbmCliente == null) {
singletonAbmCliente = new AbmCliente();
}else{
singletonAbmCliente.limpiar(singletonAbmCliente);
singletonAbmCliente.cargarTablaClientes();
}
return singletonAbmCliente;
}
public void termino(){
this.removeAll();
this.add(getPanelFiltro(), null);
this.add(getJPanelSeleccion(), null);
this.add(getPanelBotonera(), null);
this.add(getPanelRutaNavegacion());
this.add(botonSalir, null);
this.repaint();
cargarTablaClientes();
}
public void inicializarTabla(){
dtm.addColumn("Id cliente");
dtm.addColumn("Nombre y apellido");
dtm.addColumn("Tipo de Id fiscal");
dtm.addColumn("Id fiscal ");
dtm.addColumn("Tel�fono");
scrollPane.setVisible(true);
//scrollPane.setBounds(new Rectangle(10, 30, 600, 400));
table.setCellSelectionEnabled(false);
modeloOrdenado = new TableRowSorter<TableModel>(dtm);
table.setRowSorter(modeloOrdenado);
table.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(java.awt.event.MouseEvent e)
{
clickEnTablaClientes();
}
});
table.setRowSelectionAllowed(true); //realiza la seleccion de toda la fila
// this.add(scrollPane);
cargarTablaClientes();
textoNombre.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
LinkedList<RowFilter> lista = new LinkedList<RowFilter>();
lista.add(RowFilter.regexFilter(textoNombre.getText(), 1));
lista.add(RowFilter.regexFilter(textoIdF.getText(), 3));
RowFilter filtroAnd = RowFilter.andFilter((Iterable<? extends RowFilter<? super M, ? super I>>) lista); // and de ambos filtros
modeloOrdenado.setRowFilter(filtroAnd);
}
});
textoIdF.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
LinkedList<RowFilter> lista = new LinkedList<RowFilter>();
lista.add(RowFilter.regexFilter(textoNombre.getText(), 1));
lista.add(RowFilter.regexFilter(textoIdF.getText(), 3));
RowFilter filtroAnd = RowFilter.andFilter((Iterable<? extends RowFilter<? super M, ? super I>>) lista); // and de ambos filtros
modeloOrdenado.setRowFilter(filtroAnd);
}
});
}
public void insertarFila(Cliente cliente)
{
prueba[0] = cliente.getIdCliente().toString();
prueba[1] = cliente.getNombreApellido();
prueba[2] = cliente.getTipoIdFiscal();
prueba[3] = cliente.getIdFiscal();
prueba[4] = cliente.getTelefono();
dtm.addRow(prueba);
scrollPane.setBounds(new Rectangle(10,20, 380, 250));
//scrollPane.setBorder(new TitledBorder( new LineBorder(Color.BLACK) ,"",0, 0, new Font("TimesRoman", Font.BOLD, 18), Color.BLACK));
scrollPane.setVisible(true);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize()
{
etiquetaNombre = new JLabel();
etiquetaNombre.setText("Nombre y apellido");
etiquetaNombre.setBounds(new Rectangle(15, 25, 120, 20));
this.setToolTipText("Administracion de clientes");
this.setBackground(new Color(209, 217, 237));
this.add(getPanelFiltro(), null);
this.add(getJPanelSeleccion(), null);
this.add(getPanelBotonera(), null);
}
/**
* This method initializes textoNombre
*
* @return javax.swing.JTextField
*/
private JTextField getTextoNombre()
{
if (textoNombre == null) {
textoNombre = new JTextField();
textoNombre.setBounds(new Rectangle(152, 25, 200, 20));
}
return textoNombre;
}
/**
* This method initializes panelFiltro
*
* @return javax.swing.JPanel
*/
private JPanel getPanelFiltro() {
if (panelFiltro == null) {
panelFiltro = new JPanel();
panelFiltro.setLayout(null);
panelFiltro.setBounds(new Rectangle(35, 35, 420, 430));
panelFiltro.add(etiquetaNombre, null);
panelFiltro.add(getTextoNombre(), null);
panelFiltro.setBorder(BorderFactory.createTitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 16), Color.black));
panelFiltro.setOpaque(false);
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(17, 79, 115, 16));
jLabel2.setText("Id fiscal");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(15, 52, 117, 20));
jLabel1.setText("Tipo id fiscal");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(384, 26, 26, 23));
jLabel.setText("JLabel");
jLabel.setText("");
jLabel.setIcon(new ImageIcon(getClass().getResource("/imagenes/search.png")));
selectorTipoId = new JComboBox();
selectorTipoId.setBounds(new Rectangle(154, 53, 99, 19));
panelFiltro.add(jLabel, null);
panelFiltro.add(getJPanelTabla(), null);
panelFiltro.add(jLabel1, null);
panelFiltro.add(jLabel2, null);
panelFiltro.add(selectorTipoId, null);
panelFiltro.add(getTextoIdF(), null);
}
return panelFiltro;
}
/**
* This method initializes panelSeleccion
*
* @return javax.swing.JPanel
*/
private JPanel getJPanelSeleccion() {
if (panelSeleccion == null) {
etiquetaId= new JLabel();
etiquetaId.setBounds(new Rectangle(15, 40, 110, 20));
etiquetaId.setText("Id cliente:");
textoId = new JTextField();
textoId.setBounds(new Rectangle(140, 40, 117, 20));
etiquetaNyA = new JLabel();
etiquetaNyA.setBounds(new Rectangle(15, 80, 117, 20));
etiquetaNyA.setText("Nombre y apellido:");
textoNyA = new JTextField();
textoNyA.setBounds(new Rectangle(140, 80, 189, 20));
etiquetaTipoId= new JLabel();
etiquetaTipoId.setBounds(new Rectangle(15, 120, 115, 20));
etiquetaTipoId.setText("Tipo de Id:");
textoTipoId = new JTextField();
textoTipoId.setBounds(new Rectangle(140,120, 117, 20));
etiquetaIdFiscal= new JLabel();
etiquetaIdFiscal.setBounds(new Rectangle(15, 160, 110, 20));
etiquetaIdFiscal.setText("IdFiscal:");
textoIdFiscal = new JTextField();
textoIdFiscal.setBounds(new Rectangle(140, 160, 117, 20));
etiquetaTelefono= new JLabel();
etiquetaTelefono.setBounds(new Rectangle(15, 200, 111, 20));
etiquetaTelefono.setText("Telefono:");
textoTelefono= new JTextField();
textoTelefono.setBounds(new Rectangle(140, 200, 117, 20));
panelSeleccion = new JPanel();
panelSeleccion.setLayout(null);
panelSeleccion.setBounds(new Rectangle(470, 35, 360, 358));
panelSeleccion.setBorder(BorderFactory.createTitledBorder(new SoftBevelBorder(SoftBevelBorder.RAISED), "Cliente Seleccionado", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Tahoma", Font.PLAIN, 14), Color.black));
panelSeleccion.setOpaque(false);
panelSeleccion.add(etiquetaId, null);
panelSeleccion.add(etiquetaNyA, null);
panelSeleccion.add(etiquetaTipoId, null);
panelSeleccion.add(etiquetaIdFiscal, null);
panelSeleccion.add(etiquetaTelefono, null);
panelSeleccion.add(textoId, null);
panelSeleccion.add(textoNyA, null);
panelSeleccion.add(textoTipoId, null);
panelSeleccion.add(textoIdFiscal, null);
panelSeleccion.add(textoTelefono, null);
}
return panelSeleccion;
}
/**
* This method initializes panelTabla
*
* @return javax.swing.JPanel
*/
private JPanel getJPanelTabla(){
if (panelTabla == null) {
panelTabla = new JPanel();
panelTabla.setLayout(null);
panelTabla.setBounds(new Rectangle(6, 146, 406, 277));
panelTabla.add(scrollPane);
panelTabla.setBorder(new TitledBorder( new LineBorder(Color.BLACK) ,"",0, 0, new Font("TimesRoman", Font.BOLD, 18), Color.BLACK));
panelTabla.setOpaque(true);
panelTabla.setBackground(Color.lightGray);
}
return panelTabla;
}
/**
* This method initializes panelBotonera
*
* @return javax.swing.JPanel
*/
private JPanel getPanelBotonera() {
if (panelBotonera == null) {
panelBotonera = new JPanel();
panelBotonera.setLayout(null);
panelBotonera.setOpaque(false);
panelBotonera.setBounds(new Rectangle(470, 393, 358, 72));
panelBotonera.setBorder(new TitledBorder( new LineBorder(Color.BLACK) ,"",0, 0, new Font("TimesRoman", Font.BOLD, 18), Color.BLACK));
botonEliminar = new JButton();
botonEliminar.setText("Eliminar");
botonEliminar.setBounds(new Rectangle(237, 15, 100, 20));
//botonEliminar.setIcon(new ImageIcon(getClass().getResource("/imagenes/eliminar.gif")));
botonEliminar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Cliente c = new Cliente();
c.setIdCliente(Integer.valueOf(textoId.getText()));
Cliente cliente=new Cliente();
for(int i=0;i<ClienteDTO.listaClientes().size();i++ ){
cliente =(Cliente)ClienteDTO.listaClientes().get(i);
if (cliente.getIdCliente().equals(c.getIdCliente()))
break;
}
int opcion = JOptionPane.showConfirmDialog(null,"�Est� seguro que desea eliminar al clientes?");
if(opcion == JOptionPane.YES_OPTION)
{
ClienteDTO.EliminaCliente(c);
cargarTablaClientes();
}
}
});
botonNuevo = new JButton();
//botonNuevo.setIcon(new ImageIcon(getClass().getResource("/imagenes/agregar.gif")));
botonNuevo.setBounds(new Rectangle(10, 15, 100, 20));
botonNuevo.setText("Nuevo");
botonNuevo.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
AltaCliente a = new AltaCliente(AbmCliente.this);
AbmCliente.this.removeAll();
AbmCliente.this.add(a);
AbmCliente.this.repaint();
}
});
botonModificar = new JButton();
botonModificar.setText("Modificar");
botonModificar.setBounds(new Rectangle(124,15, 100, 20));
//botonModificar.setIcon(new ImageIcon(getClass().getResource("/imagenes/modificar.gif")));
botonModificar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Cliente c=new Cliente();
c.setIdCliente(Integer.valueOf(textoId.getText()));
Cliente cliente=new Cliente();
for(int i=0;i<ClienteDTO.listaClientes().size();i++ ){
cliente =(Cliente)ClienteDTO.listaClientes().get(i);
if (cliente.getIdCliente().equals(c.getIdCliente()))
break;
}
ModificarCliente m=new ModificarCliente(AbmCliente.this,cliente);
AbmCliente.this.removeAll();
AbmCliente.this.repaint();
AbmCliente.this.add(m);
}
});
botonSalir = new JButton();
botonSalir.setText("Salir");
botonSalir.setBounds(new Rectangle(731, 481, 100, 20));
botonSalir.setSelected(true);
botonSalir.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
}
});
panelBotonera.add(botonNuevo, null);
panelBotonera.add(botonModificar, null);
panelBotonera.add(botonEliminar, null);
}
return panelBotonera;
}
/**
* This method initializes textoIdF
*
* @return javax.swing.JTextField
*/
private JTextField getTextoIdF() {
if (textoIdF == null) {
textoIdF = new JTextField();
textoIdF.setBounds(new Rectangle(152, 81, 142, 17));
}
return textoIdF;
}
public void cargarTablaClientes()
{
dtm.setNumRows(0);
Cliente cliente = new Cliente();
for(int i=0;i<ClienteDTO.listaClientes().size();i++ )
{
cliente =(Cliente)ClienteDTO.listaClientes().get(i);
insertarFila(cliente);
}
modeloOrdenado.setRowFilter(RowFilter.regexFilter(textoNombre.getText(), 1));
cliente = null;
}
private void clickEnTablaClientes()
{
int id = Integer.parseInt((String) table.getValueAt(table.getSelectedRow(), 0));
Cliente cliente = ClienteDTO.buscarCliente(id);
textoId.setText(String.valueOf(cliente.getIdCliente()));
textoNyA.setText(cliente.getNombreApellido());
textoTipoId.setText(cliente.getTipoIdFiscal());
textoIdFiscal.setText(cliente.getIdFiscal());
textoTelefono.setText(cliente.getTelefono());
cliente = null;
}
}