package ;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class BuscaTicket extends JFrame {
private Operador op;
private JPanel contentPane;
private JTable jtTabla;
private JTextField tfTicket;
private JScrollPane scTicket;
public DefaultTableModel model = new DefaultTableModel();
private TableRowSorter<TableModel> modeloOrdenado;
private DefaultTableCellRenderer alinearCentro, alinearDerecha, alinearIzquierda;
private JTextField tfFiltro;
private ListadoTicket lt;
public int idTicket;
/***************************/
private Timer timer;
/*
* 1000ms ---- 1s
* xms ---- 60sx2m/s
* xms = 120 x 1000 = 120.000ms
* */
private int delay = 120000; // every 1 second = 1000 milisegundos
/***************************/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BuscaTicket frame = new BuscaTicket(new ListadoTicket(-1));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BuscaTicket(ListadoTicket lt) throws SQLException {
setAlwaysOnTop(true);
setResizable(false);
this.lt = lt;
idTicket = -1;
setTitle("Ticket");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 325, 320);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnCancelar = new JButton("Cancelar");
btnCancelar.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnCancelar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cancelar();
}
});
btnCancelar.setBounds(230, 258, 83, 23);
contentPane.add(btnCancelar);
JButton btnAceptar = new JButton("Aceptar");
btnAceptar.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnAceptar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
botonAceptar();
}
});
btnAceptar.setBounds(146, 258, 83, 23);
contentPane.add(btnAceptar);
scTicket = new JScrollPane();
scTicket.setBounds(10, 39, 300, 200);
contentPane.add(scTicket);
jtTabla = new JTable();
jtTabla.setFont(new Font("Tahoma", Font.PLAIN, 12));
scTicket.setViewportView(jtTabla);
JLabel lblIDTicket = new JLabel("Nro. Ticket");
lblIDTicket.setBounds(10, 262, 83, 14);
contentPane.add(lblIDTicket);
tfTicket = new JTextField();
tfTicket.setEditable(false);
tfTicket.setBounds(80, 259, 56, 20);
contentPane.add(tfTicket);
tfTicket.setColumns(10);
JLabel lblFiltro = new JLabel("Filtro");
lblFiltro.setBounds(10, 14, 56, 14);
contentPane.add(lblFiltro);
tfFiltro = new JTextField();
tfFiltro.setBounds(67, 11, 110, 20);
contentPane.add(tfFiltro);
tfFiltro.setColumns(10);
JButton btnActualizar = new JButton("Actualizar");
btnActualizar.setFont(new Font("Tahoma", Font.PLAIN, 11));
btnActualizar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
initTabla();
}
});
btnActualizar.setBounds(200, 10, 83, 23);
contentPane.add(btnActualizar);
this.initAlineacionTablas();
this.initTabla();
this.setVisible(true);
this.autoFiltro();
this.correr();
}
public void cerrar(){
this.detenerTIMER();
this.dispose();
}
public void botonAceptar(){
try {
if (idTicket > 0 )
{
this.lt.cargaTicket(idTicket, this);
this.setVisible(false);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Datos mal ingresados");
}
}
public void cancelar(){
this.detenerTIMER();
this.dispose();
}
/**CARGA TABLAS LISTA DE TICKETS */
public void initTabla(){
model = new DefaultTableModel(
new Object[][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
},
new String[] {
"ID", "Ticket", "Fecha", "Art. Distintos"
}
) {
Class[] columnTypes = new Class[] {
String.class, String.class, String.class, String.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
};
jtTabla.setModel(model);
jtTabla.getColumnModel().getColumn(0).setPreferredWidth(40);
jtTabla.getSelectionModel().addListSelectionListener(new RowListener());
List<Venta>list = null;
list = FisicaVenta.listVenta();
if(list.size() != 0){
model.setRowCount(0);
int art;
String fecha;
for (Venta x : list) {
art = x.size();
fecha = Util.getDate(x.getFecha());
model.addRow(new Object[] {
x.getId(),
x.getNumeroVenta(),
fecha,
art
});
}
}
jtTabla.getColumnModel().getColumn(0).setCellRenderer(alinearCentro);
jtTabla.getColumnModel().getColumn(1).setCellRenderer(alinearCentro);
jtTabla.getColumnModel().getColumn(2).setCellRenderer(alinearCentro);
jtTabla.getColumnModel().getColumn(3).setCellRenderer(alinearCentro);
jtTabla.setToolTipText("Lista de ticket");
}
/*Inicializo y cargo alineacinn de conlumnas en tablas*/
public void initAlineacionTablas(){
alinearCentro = new DefaultTableCellRenderer();
alinearDerecha = new DefaultTableCellRenderer();
alinearIzquierda = new DefaultTableCellRenderer();
alinearCentro.setHorizontalAlignment(SwingConstants.CENTER);
alinearDerecha.setHorizontalAlignment(SwingConstants.RIGHT);
alinearIzquierda.setHorizontalAlignment(SwingConstants.LEFT);
}
//METODOS PARA OBTENER LAS FILAS SELECIONADAS
private int outputSelection() {
int row = jtTabla.getSelectedRow();
return row;
}
private class RowListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
try {
if (event.getValueIsAdjusting()) {
return;
}
int x = outputSelection();
String padron, nroTicket;
int nro;
idTicket = (Integer) jtTabla.getValueAt(x, 0);
tfTicket.setText(Integer.toString(idTicket));
} catch (Exception e) {
System.out.println("error de filtro y selección");
}
}
}
private void filtrar(String filtrar) {
//codigo de filtro
modeloOrdenado = new TableRowSorter(model);
tfFiltro.setText(filtrar);
String fil = filtrar;
if (fil.length() != 0){
modeloOrdenado.setRowFilter (RowFilter.regexFilter(".*"+fil+"*."));
}else{
modeloOrdenado.setRowFilter (RowFilter.regexFilter(""));
}
jtTabla.setRowSorter (modeloOrdenado);
}
/**********Evento temporal para actualizar la tabla*****************/
public void correr(){
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
initTabla();
System.out.println("inicializo tabla");
}
};
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start();
}
});
}
public void detenerTIMER(){
timer.stop();
}
}