/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PrinterGui.java
 *
 * Created on 26-11-2008, 01:11:31 PM
 */
package BodegaPOS;

import BodegaPOS.printer.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;



import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Daniel
 */
public class PrinterGui extends javax.swing.JFrame {

    private ArrayList<Solicitud> solicitud = new ArrayList<Solicitud>();
    private WSThread WS;
    public PrinterGui() {
        initComponents();
        ListSelectionModel listSelectionModel = Lista.getSelectionModel();
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    }
    public int indicelista = -1;

    class SharedListSelectionHandler implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            if (lsm.isSelectionEmpty()) {
            } else {
                // Find out which indexes are selected.
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                for (int i = minIndex; i <= maxIndex; i++) {
                    if ((lsm.isSelectedIndex(i)) && (indicelista != i)) {


                        DefaultTableModel modelo = (DefaultTableModel) Tabla.getModel();
                        for (int z = 0; z <= Tabla.getModel().getRowCount(); z++) {
                            modelo.setRowCount(0);// .removeRow(z);
                        }


                        /////ingresar nuevos datos a la tabla
                        indicelista = i;
                        DefaultListModel modelo2 = (DefaultListModel) Lista.getModel();
                        String id = modelo2.getElementAt(indicelista).toString().split("-", 2)[0];
                        System.out.println("ID" + id);
                        System.out.println("Size Sol : " + solicitud.size());
                       for (int z = 0; z < solicitud.get(indicelista).nombreitem.size(); z++) {
                            System.out.println("Itemo:" + solicitud.get(indicelista).nombreitem.get(z));
                                                        modelo.insertRow(Tabla.getRowCount(),
                                        new Object[]{
                                            solicitud.get(indicelista).iditem.get(z),
                                            solicitud.get(indicelista).nombreitem.get(z),
                                            solicitud.get(indicelista).cantidaditem.get(z)
                                        }
                                    );
                       }
                    }
                }
            }
        }
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Borrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        Lista = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        Imprimir = new javax.swing.JButton();
        Conectar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(469, 323));

        Borrar.setText("Borrar Solicitud");
        Borrar.setEnabled(false);
        Borrar.setMaximumSize(new java.awt.Dimension(113, 23));
        Borrar.setMinimumSize(new java.awt.Dimension(113, 23));
        Borrar.setPreferredSize(new java.awt.Dimension(113, 23));
        Borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BorrarActionPerformed(evt);
            }
        });

        Lista.setEnabled(false);
        Lista.setMaximumSize(new java.awt.Dimension(446, 128));
        Lista.setName("Lista"); // NOI18N
        jScrollPane1.setViewportView(Lista);
        Lista.getAccessibleContext().setAccessibleName("jLista");
        Lista.setModel(new DefaultListModel());

        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "ID", "Nombre", "Numero"
            }
        ));
        Tabla.setEnabled(false);
        jScrollPane2.setViewportView(Tabla);

        Imprimir.setText("Imprimir Solicitud");
        Imprimir.setEnabled(false);
        Imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ImprimirActionPerformed(evt);
            }
        });

        Conectar.setText("Conectar");
        Conectar.setMaximumSize(new java.awt.Dimension(113, 23));
        Conectar.setMinimumSize(new java.awt.Dimension(113, 23));
        Conectar.setPreferredSize(new java.awt.Dimension(113, 23));
        Conectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConectarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Conectar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(Imprimir, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                        .addGap(49, 49, 49)
                        .addComponent(Borrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Borrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Imprimir)
                    .addComponent(Conectar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BorrarActionPerformed

        /////eliminar datos previos en la tabla
        DefaultTableModel modelo = (DefaultTableModel) Tabla.getModel();
        for (int z = 0; z <= Tabla.getModel().getRowCount(); z++) {
            modelo.setRowCount(0);// .removeRow(z);
        }
        //// eliminar elemento de la lista
        DefaultListModel modelo2 = (DefaultListModel) Lista.getModel();
        modelo2.removeElementAt(indicelista);
        //// eliminar solicitud;
        solicitud.remove(indicelista);
        Lista.setSelectedIndex(-1);
        indicelista = -1;
        this.validate();


}//GEN-LAST:event_BorrarActionPerformed

    private void ImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ImprimirActionPerformed
        new Thread() {

            @Override
            public void run() {

                List<String> texto = new ArrayList<String>();
                texto.add("ID Solicitud      : " + solicitud.get(indicelista).id);
                texto.add("Nombre de  Usuario: \n" + solicitud.get(indicelista).nombre);
                texto.add("[ID, Nombre Item , Cantidad]");

                DefaultTableModel modelo = (DefaultTableModel) Tabla.getModel();
                /////Imprimir datos de la tabla
                if (Tabla.getModel().getRowCount() > 0) {
                    for (int z = 0; z < Tabla.getModel().getRowCount(); z++) {
                        texto.add(modelo.getDataVector().get(z).toString());
                    }
                    Print print = new Print();
                    print.setLista(texto);
                    print.start();
                }
            }
        }.start();
    }//GEN-LAST:event_ImprimirActionPerformed

private void ConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConectarActionPerformed

    Lista.setSelectedIndex(-1);
    indicelista = -1;
    Lista.setEnabled(true);
    Imprimir.setEnabled(true);
    
    WS= new WSThread((DefaultListModel)Lista.getModel(),solicitud,this);
    WS.start();
    

}//GEN-LAST:event_ConectarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Borrar;
    private javax.swing.JButton Conectar;
    private javax.swing.JButton Imprimir;
    public javax.swing.JList Lista;
    private javax.swing.JTable Tabla;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}

