vista.promocion.DiagArticuloPromocion.java Source code

Java tutorial

Introduction

Here is the source code for vista.promocion.DiagArticuloPromocion.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vista.promocion;

import entidades.articulo.Articulo;
import entidades.promocion.Promocion;
import entidades.promocion.PromocionArticulo;
import includes.Comunes;
import java.math.BigDecimal;
import javax.swing.JOptionPane;
import org.apache.commons.validator.routines.BigDecimalValidator;
import vista.articulos.DiagBuscarArticulo;

/**
 *
 * @author franco
 */
public class DiagArticuloPromocion extends javax.swing.JDialog {

    private PromocionArticulo promocionArticulo;
    private Articulo articulo;
    private Promocion promocion;
    private String tipoOperacion;

    public PromocionArticulo getPromocionArticulo() {
        return promocionArticulo;
    }

    public void setPromocionArticulo(PromocionArticulo promocionArticulo) {
        this.promocionArticulo = promocionArticulo;
    }

    public Promocion getPromocion() {
        return promocion;
    }

    public void setPromocion(Promocion promocion) {
        this.promocion = promocion;
    }

    public DiagArticuloPromocion(java.awt.Frame parent, boolean modal, String tipoOperacion, Promocion promocion) {
        super(parent, modal);
        this.tipoOperacion = tipoOperacion;
        this.promocion = promocion;
        initComponents();
        inicializarComponentes();
    }

    public DiagArticuloPromocion(java.awt.Frame parent, boolean modal, String tipoOperacion,
            PromocionArticulo promocionArticulo) {
        super(parent, modal);
        this.tipoOperacion = tipoOperacion;
        this.promocionArticulo = promocionArticulo;
        initComponents();
        inicializarComponentes();
    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfArticulo = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        tfValor = new javax.swing.JTextField();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle
                .getMessage(DiagArticuloPromocion.class, "DiagArticuloPromocion.jPanel1.border.title"))); // NOI18N

        jLabel1.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.jLabel1.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.jLabel2.text")); // NOI18N

        tfArticulo.setEditable(false);
        tfArticulo.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.tfArticulo.text")); // NOI18N

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/buscar2.png"))); // NOI18N
        btnBuscar.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.btnBuscar.text")); // NOI18N
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        tfValor.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.tfValor.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addGap(18, 18, 18).addComponent(tfArticulo).addGap(18, 18, 18).addComponent(btnBuscar))
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(23, 23, 23).addComponent(jLabel2)
                        .addGap(18, 18, 18).addComponent(tfValor, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(tfArticulo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnBuscar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(tfValor, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(23, Short.MAX_VALUE)));

        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/accept.png"))); // NOI18N
        btnAceptar.setText(org.openide.util.NbBundle.getMessage(DiagArticuloPromocion.class,
                "DiagArticuloPromocion.btnAceptar.text")); // NOI18N
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup().addContainerGap()
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())
                .addGroup(layout.createSequentialGroup().addGap(214, 214, 214).addComponent(btnAceptar)
                        .addContainerGap(256, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAceptar)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        seleccionarArticulo();
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        agregar();
    }//GEN-LAST:event_btnAceptarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tfArticulo;
    private javax.swing.JTextField tfValor;
    // End of variables declaration//GEN-END:variables

    private void seleccionarArticulo() {
        DiagBuscarArticulo buscarArticulo = new DiagBuscarArticulo(null, true);
        buscarArticulo.setLocation(Comunes.centrarDialog(buscarArticulo));
        buscarArticulo.setVisible(true);
        if (buscarArticulo.getArticulo() != null) {
            articulo = buscarArticulo.getArticulo();
            tfArticulo.setText(articulo.getCodigoBarra() + " - " + articulo.getDescripcion());
        } else {
            JOptionPane.showMessageDialog(null, "No selecciono ningn Articulo", "Advertencia",
                    JOptionPane.WARNING_MESSAGE);
        }
    }

    private void agregar() {
        if (articulo != null) {
            if (BigDecimalValidator.getInstance().isValid(tfValor.getText().replace(",", "."))) {
                switch (tipoOperacion) {
                case "Alta":
                    promocionArticulo = new PromocionArticulo();
                    break;
                }
                //                promocionArticulo.setPromocion(promocion);
                promocionArticulo.setValor(new BigDecimal(tfValor.getText().replace(",", ".")));
                promocionArticulo.setArticulo(articulo);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Debe Ingresar un valor correcto");
                tfValor.requestFocus();
            }
        } else {
            JOptionPane.showMessageDialog(null, "No selecciono ningn Articulo");
            tfValor.requestFocus();
        }
    }

    private void inicializarComponentes() {
        if (tipoOperacion.equals("Modificacin")) {
            cargarDatosPromocion();
        }
    }

    private void cargarDatosPromocion() {
        articulo = promocionArticulo.getArticulo();
        //        promocion = promocionArticulo.getPromocion();
        tfArticulo.setText(articulo.getCodigoBarra() + " - " + articulo.getDescripcion());
        tfValor.setText(promocionArticulo.getValor().toString());
    }
}