Package eclc

Source Code of eclc.Vendors

/*
* @(#)Vendors.java   13/09/26
*
* Copyright (c) 2013 DieHard Development
*
* All rights reserved.
Released under the FreeBSD  license
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*
*
*
*/


/**
*
*/
package eclc;

//~--- non-JDK imports --------------------------------------------------------

import com.xzq.osc.JocTable;

import eclc.Exceptions.exceptions.PreexistingEntityException;

//~--- JDK imports ------------------------------------------------------------

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
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.WindowConstants;
import javax.swing.border.EmptyBorder;

/**
* @author TJ
*
*/
public class Vendors {
    private static int _vendorID = 0;
    private static TableCellListener tcl = null;
    private static String _vendorName, _vendorContact, _vendorPhone, _vendorLotSize, _vendorDeposit, _vendorBalance;
    private final String[] lotSize = { "Lot Size", "20x20", "50x50", "100x100", "200x200" };
    private Action action = new AbstractAction() {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            tcl = (TableCellListener) e.getSource();

            if (tcl.getColumn() == 5) {
                deductPayment();
            } else {
                getTableValues();
                createVendorEdit(buildEntity());
            }
        }
    };
    private final JFrame jf = new JFrame();
    private JocTable jt;
    private javax.persistence.EntityManager entityManager;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    private javax.persistence.Query vendorQuery;
    private java.util.List<eclc.Vendor> vendorList;
    private javax.swing.JScrollPane js;
    private javax.swing.JButton createVendor, deductPayment;
    private javax.swing.JTextField vendorName, vendorContact, vendorPhone, vendorLotSize, vendorDeposit;
    private javax.swing.JLabel vName, vContact, vPhone, vLot, vDeposit;
    @SuppressWarnings("rawtypes")
    private org.jdesktop.swingbinding.JTableBinding jTableBinding;
    @SuppressWarnings("rawtypes")
    private javax.swing.JComboBox chooseLot;

    /**
     * Method description
     *
     */
    public void showVendors() {
        bindTable();
        setupComp();
        buildGui(jt);
    }

    /**
     *
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void bindTable() {
        jt = new JocTable();
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        entityManager = java.beans.Beans.isDesignTime()
                        ? null
                        : javax.persistence.Persistence.createEntityManagerFactory("ShowManager").createEntityManager();
        vendorQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT e FROM Vendor e");
        vendorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : vendorQuery.getResultList();
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(
            org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vendorList, jt);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding =
            jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${vendorid}"));

        columnBinding.setColumnName("Vendor ID");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${vendor}"));
        columnBinding.setColumnName("Vendor Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${contact}"));
        columnBinding.setColumnName("Contact");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${phone}"));
        columnBinding.setColumnName("Phone");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lotsize}"));
        columnBinding.setColumnName("Lotsize");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${deposit}"));
        columnBinding.setColumnName("Deposit");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${balance}"));
        columnBinding.setColumnName("Balance");
        columnBinding.setEditable(false);
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        js = new JScrollPane();
        jt.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jt.getColumnModel().getColumn(0).setMinWidth(70);
        jt.getColumnModel().getColumn(0).setPreferredWidth(70);
        jt.getColumnModel().getColumn(0).setMaxWidth(70);
        jt.getColumnModel().getColumn(1).setMinWidth(150);
        jt.getColumnModel().getColumn(1).setPreferredWidth(150);
        jt.getColumnModel().getColumn(1).setMaxWidth(150);
        jt.getColumnModel().getColumn(2).setMinWidth(150);
        jt.getColumnModel().getColumn(2).setPreferredWidth(150);
        jt.getColumnModel().getColumn(2).setMaxWidth(150);
        jt.getColumnModel().getColumn(3).setMinWidth(70);
        jt.getColumnModel().getColumn(3).setPreferredWidth(70);
        jt.getColumnModel().getColumn(3).setMaxWidth(70);
        jt.getColumnModel().getColumn(4).setMinWidth(70);
        jt.getColumnModel().getColumn(4).setPreferredWidth(70);
        jt.getColumnModel().getColumn(4).setMaxWidth(70);
        jt.getColumnModel().getColumn(5).setMinWidth(70);
        jt.getColumnModel().getColumn(5).setPreferredWidth(70);
        jt.getColumnModel().getColumn(5).setMaxWidth(70);
        jt.getColumnModel().getColumn(6).setMinWidth(70);
        jt.getColumnModel().getColumn(6).setPreferredWidth(70);
        jt.getColumnModel().getColumn(6).setMaxWidth(70);
        jt.setEditable(true);
        jt.setPreferredSize(new Dimension(700, 200));
        tcl = new TableCellListener(jt, getAction());
        js.setBounds(5, 20, 720, 220);
        js.setViewportView(jt);
    }

    private void createVendorEdit(Vendor vendor) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowManager");
        VendorJpaController vjc = new VendorJpaController(emf);

        try {
            vjc.edit(vendor);
            jf.getContentPane().remove(js);
            jf.getContentPane().repaint();
            jf.getContentPane().validate();
            bindTable();
            jf.getContentPane().add(js);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getTableValues() {
        _vendorID = (Integer) jt.getModel().getValueAt(tcl.getRow(), 0);
        _vendorName = (String) jt.getModel().getValueAt(tcl.getRow(), 1);
        _vendorContact = (String) jt.getModel().getValueAt(tcl.getRow(), 2);
        _vendorPhone = (String) jt.getModel().getValueAt(tcl.getRow(), 3);
        _vendorLotSize = (String) jt.getModel().getValueAt(tcl.getRow(), 4);
        _vendorDeposit = (String) jt.getModel().getValueAt(tcl.getRow(), 5);
        _vendorBalance = (String) jt.getModel().getValueAt(tcl.getRow(), 6);
    }

    private void createVendor(Vendor vendor) throws PreexistingEntityException {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("ShowManager");
        VendorJpaController vjc = new VendorJpaController(emf);

        try {
            vjc.create(vendor);
            jf.getContentPane().remove(js);
            jf.getContentPane().repaint();
            jf.getContentPane().validate();
            bindTable();
            jf.getContentPane().add(js);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void setupComp() {
        vendorName = new JTextField();
        vendorName.setBounds(10, 300, 150, 24);
        vendorName.setToolTipText("Enter Vendor Name");
        vendorContact = new JTextField();
        vendorContact.setBounds(170, 300, 150, 24);
        vendorContact.setToolTipText("Enter Vendor Contact Person");
        vendorPhone = new JTextField();
        vendorPhone.setBounds(330, 300, 100, 24);
        vendorPhone.setToolTipText("Enter the Vendor's Phone Number");
        vendorLotSize = new JTextField();
        vendorLotSize.setBounds(440, 300, 100, 24);
        vendorLotSize.setToolTipText("Vendor's Lot Size requested");
        vendorLotSize.setEditable(false);
        vendorDeposit = new JTextField();
        vendorDeposit.setBounds(550, 300, 100, 24);
        vendorDeposit.setToolTipText("Enter the amount of deposit that Vendor submitted");
        createVendor = new JButton("Add Vendor");
        createVendor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!vendorName.getText().equals("") &&!vendorContact.getText().equals("")
                        &&!vendorLotSize.getText().equals("") &&!vendorLotSize.equals("Lot Size")
                        &&!vendorDeposit.getText().equals("")) {
                    setVendorVars();

                    try {
                        initVendorBalance();
                        createVendor(buildEntity());
                    } catch (PreexistingEntityException e1) {
                        e1.printStackTrace();
                    }

                    clearVendorInput();
                } else {
                    JOptionPane.showMessageDialog(null, "Some values needed are missing please check input");
                }
            }
        });
        createVendor.setBounds(10, 340, 100, 24);
        deductPayment = new JButton("Deduct Payment");
        deductPayment.setBounds(120, 340, 140, 24);
        deductPayment.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deductPayment();
            }
        });
        vName = new JLabel("Vendor Name");
        vName.setBounds(10, 280, 100, 24);
        vContact = new JLabel("Vendor Contact");
        vContact.setBounds(170, 280, 100, 24);
        vPhone = new JLabel("Phone #");
        vPhone.setBounds(330, 280, 100, 24);
        vLot = new JLabel("Lot Size");
        vLot.setBounds(440, 280, 100, 24);
        vDeposit = new JLabel("Deposit");
        vDeposit.setBounds(550, 280, 100, 24);
        chooseLot = new JComboBox(lotSize);
        chooseLot.setToolTipText("Choose the Lot Size that the Vendor requests");
        chooseLot.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!chooseLot.getSelectedItem().equals("") &&!chooseLot.getSelectedItem().equals("Lot Size")) {
                    vendorLotSize.setText(chooseLot.getSelectedItem().toString());

                    String size = chooseLot.getSelectedItem().toString();

                    setBalance(size);
                }

                if (chooseLot.getSelectedItem().equals("Lot Size")) {
                    vendorLotSize.setText("");
                    _vendorBalance = "0";
                }
            }
        });
        chooseLot.setBounds(440, 250, 100, 24);
    }

    private void buildGui(JTable jt) throws HeadlessException {
        JPanel contentPane = new JPanel();

        jf.setContentPane(contentPane);
        contentPane.setBorder(new EmptyBorder(1, 1, 1, 1));
        contentPane.setLayout(null);
        buildPanel(contentPane);
        jf.setPreferredSize(new Dimension(738, 400));
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);
        jf.setTitle("ECLC Vendors");
        jf.pack();
        jf.setVisible(true);
    }

    /**
     * @param contentPane
     */
    private void buildPanel(JPanel contentPane) {
        contentPane.add(js);
        contentPane.add(vendorName);
        contentPane.add(vendorContact);
        contentPane.add(vendorPhone);
        contentPane.add(vendorLotSize);
        contentPane.add(vendorDeposit);
        contentPane.add(createVendor);
        contentPane.add(vName);
        contentPane.add(vContact);
        contentPane.add(vPhone);
        contentPane.add(vLot);
        contentPane.add(vDeposit);
        contentPane.add(chooseLot);

        // contentPane.add(deductPayment);
    }

    /**
     *
     */
    private void setVendorVars() {
        _vendorID = jt.getModel().getRowCount() + 1;
        _vendorName = vendorName.getText();
        _vendorContact = vendorContact.getText();

        if (vendorPhone.getText().equals("")) {
            _vendorPhone = "NONE";
        } else {
            _vendorPhone = vendorPhone.getText();
        }

        _vendorLotSize = vendorLotSize.getText();
        _vendorDeposit = vendorDeposit.getText();
    }

    private Vendor buildEntity() {
        Vendor vn = new Vendor();

        vn.setVendorid(_vendorID);
        vn.setVendor(_vendorName);
        vn.setContact(_vendorContact);
        vn.setPhone(_vendorPhone);
        vn.setLotsize(_vendorLotSize);
        vn.setDeposit(_vendorDeposit);
        vn.setBalance(_vendorBalance);

        return vn;
    }

    /**
     * @throws NumberFormatException
     */
    private void initVendorBalance() throws NumberFormatException {
        if (Integer.valueOf(_vendorDeposit) > 0) {
            int tmpB = Integer.valueOf(_vendorBalance);
            int tmpD = Integer.valueOf(_vendorDeposit);
            int tmpCalc = (tmpB - tmpD);

            _vendorBalance = String.valueOf(tmpCalc);
        }
    }

    /**
     * @throws NumberFormatException
     */
    private void deductPayment() throws NumberFormatException {
        int tmp = tcl.getRow();

        _vendorDeposit = (String) jt.getModel().getValueAt(tmp, 5);
        _vendorBalance = (String) jt.getModel().getValueAt(tmp, 6);

        if (Integer.valueOf(_vendorDeposit) > 0) {
            int tmpD = Integer.valueOf(_vendorDeposit);
            int tmpB = Integer.valueOf(_vendorBalance);

            if ((tmpB > 0) && (tmpD > 0)) {
                _vendorBalance = String.valueOf(tmpB - tmpD);
                jt.getModel().setValueAt(_vendorBalance, tmp, 6);
                getTableValues();
                createVendorEdit(buildEntity());
            } else {
                _vendorBalance = String.valueOf(tmpB);
                jt.getModel().setValueAt(_vendorBalance, tmp, 6);
                getTableValues();
                createVendorEdit(buildEntity());
            }
        }
    }

    /**
     *
     */
    private void clearVendorInput() {
        vendorName.setText("");
        vendorContact.setText("");
        vendorPhone.setText("");
        vendorLotSize.setText("");
        vendorDeposit.setText("");
    }

    /**
     * @param size
     */
    private void setBalance(String size) {
        switch (size) {
         case "20x20" :
             _vendorBalance = "50";

             break;

         case "50x50" :
             _vendorBalance = "100";

             break;

         case "100x100" :
             _vendorBalance = "200";

             break;

         case "200x200" :
             _vendorBalance = "300";

             break;

         default :
             _vendorBalance = "0";

             break;
        }
    }

    /**
     * @return the action
     */
    public Action getAction() {
        return action;
    }

    /**
     * @param action the action to set
     */
    public void setAction(Action action) {
        this.action = action;
    }
}


//~ Formatted in DD Std on 13/09/26
TOP

Related Classes of eclc.Vendors

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.