/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GPL License. You should have
* received a copy of the GPL License along with this program. If
* not, a copy is available at
* <a href="http://opensource.org/licenses/GPL-2.0">
* opensource.org</a>.
*
* @author rbruno <robert.alexander.bruno@gmail.com>
*/
package net.sourceforge.barcodegen.views;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sourceforge.barcodegen.app.Main;
import net.sourceforge.barcodegen.model.DB;
import net.sourceforge.barcodegen.model.Model;
import net.sourceforge.barcodegen.util.Config;
import net.sourceforge.barcodegen.util.Debug;
public class PrinView extends JDialog implements ActionListener
{
private static final long serialVersionUID = 1L;
private ArrayList<Object> listas = new ArrayList<Object>();
private JButton btnCancelar = new JButton("Cancelar", new ImageIcon(Main.imgPath("cancel.png")));
private JButton btnAceptar = new JButton("Aceptar",new ImageIcon(Main.imgPath("acept.png")));
protected JComboBox combo = new JComboBox();
private NumericTextField fieldCantidad = new NumericTextField();
private JTextField fieldFinal = new JTextField("0");
public PrinView(){
JPanel panelCentral = new JPanel();
panelCentral.setLayout(new GridLayout(6,1));
getContentPane().add(panelCentral);
panelCentral.add(new JLabel("Lista de Tickes"));
panelCentral.add(combo);
panelCentral.add(new JLabel("última etiqueta generada"));
panelCentral.add(fieldFinal);
fieldFinal.setEditable(false);
fieldFinal.setFont(new Font("arial",1,24));
//fieldFinal.setEditable(false);
panelCentral.add(new JLabel("cantidad"));
panelCentral.add(fieldCantidad);
fieldCantidad.setText("3");
fieldCantidad.setFont(new Font("arial",1,24));
JPanel southPanel = new JPanel();
getContentPane().add(southPanel, BorderLayout.SOUTH);
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
southPanel.add(btnAceptar);
btnAceptar.addActionListener(this);
southPanel.add(btnCancelar);
btnCancelar.addActionListener(this);
/*
* Consulta las listas disponibles
*/
try
{
ResultSet rs = Main.lista.find("id=id order by id desc");
int i = 0;
while (rs.next())
{
listas.add( i,rs.getString("id"));
combo.insertItemAt(rs.getString("nombre"), i);
combo.setSelectedIndex(0);
fieldFinal.setText(rs.getString("ultimoStiker"));
}
rs.close();
} catch (Exception e) {
Debug.log(e);
}
combo.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent arg0)
{
JComboBox cmb = (JComboBox) arg0.getSource();
int i = cmb.getSelectedIndex();
try
{
ResultSet rs = Main.lista.find("id="+listas.get(i));
while (rs.next())
{
fieldFinal.setText(rs.getString("ultimoStiker"));
}
rs.close();
} catch (SQLException e) {
Debug.log(e);
}
}
});
setSize(350, 250);
setLocationRelativeTo(getParent());
//setAlwaysOnTop(true);
setResizable(false);
setVisible(true);
}
public void enableComponents(boolean enabled){
this.btnAceptar.setEnabled(enabled);
this.btnCancelar.setEnabled(enabled);
this.combo.setEnabled(false);
this.combo.setEditable(enabled);
this.fieldCantidad.setEnabled(enabled);
this.fieldCantidad.setEditable(enabled);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource().equals(btnCancelar)){
this.dispose();
}
if(arg0.getSource().equals(btnAceptar)){
try
{
this.enableComponents(false);
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
int index = Integer.parseInt((String) listas.get(combo.getSelectedIndex()));
int cantidad = Integer.parseInt(fieldCantidad.getText());
int lasStiker = Integer.parseInt(fieldFinal.getText()) +1;
String banner = "";
ResultSet rs = Main.lista.find("id=" + index);
while (rs.next()){
banner = rs.getString("banner");
}
rs.close();
boolean printed = this.generar(index,lasStiker,cantidad,banner);
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
this.enableComponents(true);
if(printed){
this.dispose();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "error: "+ e.getLocalizedMessage());
Debug.log(e);
}
}
}
/**
* Genera x cantidad de stikers de la lista indicada
*
* @param listId
* @param cantidad
*/
public boolean generar (int listId, int desde, int cantidad, String title){
boolean printed = false;
try
{
Main.frame.showProgress("Imprimiendo...");
Main.frame.table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
Model stikers = new Model("Stikers");
stikers.delete("");
String[] columns = {"numero","lista_id"};
String[] values = new String[2];
values[1] = ""+listId;
for (int i = desde; i < desde + cantidad; i++){
values[0] = ""+i;
stikers.insert( columns, values);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("code_title", title);
JasperReport report = JasperCompileManager.compileReport(Config.getValue("app.reportTemplatePath"));
Debug.log("Compiling report...");
JasperPrint print = JasperFillManager.fillReport(report, params, DB.getInstance().getConn());
Debug.log("Filling report...");
//JasperViewer.viewReport(print, false);
DocumentViewer documentViewer = DocumentViewer.show(print);
Debug.log("Show report...");
if(documentViewer.isPrintExecuted()){
printed = true;
Main.lista.update("ultimoStiker="+ (desde + cantidad -1), "id="+listId);
Model strip = new Model("strip");
String[] columns1 = {"inicio","cantidad","lista_id"};
String[] valuesList = {""+desde, ""+cantidad,""+listId};
strip.insert(columns1, valuesList);
}else{
Main.showMsg("No se imprimió el listado");
}
Main.frame.filtrar(Calendar.getInstance());
} catch (Exception e){
Debug.log(e);
}finally{
Main.frame.hideProgress();
Main.frame.table.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
return printed;
}
}