×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:06 PM
Modified: Jan 4, 2017 10:26 PM
Views: 2204
Tags: no tags
  1.  
  2. package inventorysystem.gui;
  3.  
  4. import com.sun.glass.events.KeyEvent;
  5. import interfaces.Switchable;
  6. import inventorysystem.data.NumberFormater;
  7. import inventorysystem.data.Product;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.Vector;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.DefaultComboBoxModel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16.  
  17. /**
  18.  *
  19.  * @author Epic
  20.  */
  21. public class Products extends javax.swing.JPanel implements Switchable {
  22.  
  23.     /**
  24.      * Creates new form Products
  25.      */
  26.     public Products() {
  27.         initComponents();
  28.         this.setName("Product");
  29.         this.fillSupplierList();
  30.        
  31.        
  32.     }
  33.    
  34.    
  35.     /**
  36.      * This method is called from within the constructor to initialize the form.
  37.      * WARNING: Do NOT modify this code. The content of this method is always
  38.      * regenerated by the Form Editor.
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  42.     private void initComponents() {
  43.  
  44.         jLabel1 = new javax.swing.JLabel();
  45.         txtName = new javax.swing.JTextField();
  46.         jLabel2 = new javax.swing.JLabel();
  47.         jScrollPane1 = new javax.swing.JScrollPane();
  48.         taDescription = new javax.swing.JTextArea();
  49.         jLabel3 = new javax.swing.JLabel();
  50.         jLabel4 = new javax.swing.JLabel();
  51.         jLabel5 = new javax.swing.JLabel();
  52.         txtQuantity = new javax.swing.JTextField();
  53.         jLabel6 = new javax.swing.JLabel();
  54.         cbmSupplier = new javax.swing.JComboBox<>();
  55.         jLayeredPane1 = new javax.swing.JLayeredPane();
  56.         btnSave = new javax.swing.JButton();
  57.         btnCancel = new javax.swing.JButton();
  58.         jLabel7 = new javax.swing.JLabel();
  59.         cbmManufactureDate = new datechooser.beans.DateChooserCombo();
  60.         cbmExpiryDate = new datechooser.beans.DateChooserCombo();
  61.         jLabel8 = new javax.swing.JLabel();
  62.         jLabel9 = new javax.swing.JLabel();
  63.         txtBatchNo = new javax.swing.JTextField();
  64.         txtCost = new javax.swing.JFormattedTextField();
  65.         txtSales = new javax.swing.JFormattedTextField();
  66.         costError = new javax.swing.JLabel();
  67.         salesError = new javax.swing.JLabel();
  68.  
  69.         jLabel1.setText("Name:");
  70.  
  71.         jLabel2.setText("Description:");
  72.  
  73.         taDescription.setColumns(20);
  74.         taDescription.setRows(5);
  75.         jScrollPane1.setViewportView(taDescription);
  76.  
  77.         jLabel3.setText("Cost Price:");
  78.  
  79.         jLabel4.setText("Sale Price:");
  80.  
  81.         jLabel5.setText("Quantity:");
  82.  
  83.         jLabel6.setText("Supplier:");
  84.  
  85.         jLayeredPane1.setLayout(new java.awt.GridLayout(1, 0));
  86.  
  87.         btnSave.setText("Save");
  88.         btnSave.addActionListener(new java.awt.event.ActionListener() {
  89.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  90.                 btnSaveActionPerformed(evt);
  91.             }
  92.         });
  93.         jLayeredPane1.add(btnSave);
  94.  
  95.         btnCancel.setText("Cancel");
  96.         btnCancel.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 btnCancelActionPerformed(evt);
  99.             }
  100.         });
  101.         jLayeredPane1.add(btnCancel);
  102.  
  103.         jLabel7.setText("Manufacture Date:");
  104.  
  105.         cbmManufactureDate.setCurrentView(new datechooser.view.appearance.AppearancesList("Swing",
  106.             new datechooser.view.appearance.ViewAppearance("custom",
  107.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  108.                     new java.awt.Color(222, 222, 222),
  109.                     new java.awt.Color(0, 0, 255),
  110.                     false,
  111.                     true,
  112.                     new datechooser.view.appearance.swing.ButtonPainter()),
  113.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  114.                     new java.awt.Color(222, 222, 222),
  115.                     new java.awt.Color(0, 0, 255),
  116.                     true,
  117.                     true,
  118.                     new datechooser.view.appearance.swing.ButtonPainter()),
  119.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  120.                     new java.awt.Color(0, 0, 255),
  121.                     new java.awt.Color(0, 0, 255),
  122.                     false,
  123.                     true,
  124.                     new datechooser.view.appearance.swing.ButtonPainter()),
  125.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  126.                     new java.awt.Color(128, 128, 128),
  127.                     new java.awt.Color(0, 0, 255),
  128.                     false,
  129.                     true,
  130.                     new datechooser.view.appearance.swing.LabelPainter()),
  131.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  132.                     new java.awt.Color(222, 222, 222),
  133.                     new java.awt.Color(0, 0, 255),
  134.                     false,
  135.                     true,
  136.                     new datechooser.view.appearance.swing.LabelPainter()),
  137.                 new datechooser.view.appearance.swing.SwingCellAppearance(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 11),
  138.                     new java.awt.Color(222, 222, 222),
  139.                     new java.awt.Color(255, 0, 0),
  140.                     false,
  141.                     false,
  142.                     new datechooser.view.appearance.swing.ButtonPainter()),
  143.                 (datechooser.view.BackRenderer)null,
  144.                 false,
  145.                 true)));
  146.  
  147.     jLabel8.setText("Expiry Date:");
  148.  
  149.     jLabel9.setText("Batch Number:"// </editor-fold>                        
  150.  
  151.     private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
  152.         // TODO add your handling code here:
  153.         Product product = new Product(0, txtName.getText(),
  154.                 Integer.parseInt(txtQuantity.getText()),
  155.                 Float.valueOf(txtSales.getText()),
  156.                 "", taDescription.getText(),
  157.                 cbmManufactureDate.getText(),
  158.                 cbmExpiryDate.getText(),
  159.                  
  160.                 txtBatchNo.getText(),
  161.                 Float.valueOf(txtCost.getText()));
  162.        
  163.                
  164.        
  165.        
  166.         try {
  167.             // since the login class has access to the currently logged in employee
  168.             if(product.addProduct(
  169.                     ((String)cbmSupplier.getSelectedItem()),//supplier from the combo selection
  170.                     login.getCurrentEmployee().getEmployeeId() //currently logged in employee in the system
  171.             )){
  172.                 JOptionPane.showMessageDialog(null, "New Product saved..");
  173.             }
  174.         } catch (SQLException ex) {
  175.             Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  176.         }
  177.     }                                      
  178.  
  179.     private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
  180.         // TODO add your handling code here:
  181.     }                                        
  182.  
  183.     private void txtCostActionPerformed(java.awt.event.ActionEvent evt) {                                        
  184.         // TODO add your handling code here:
  185.     }                                      
  186.  
  187.     private void txtCostKeyTyped(java.awt.event.KeyEvent evt) {                                
  188.         // TODO add your handling code here:
  189.         NumberFormater.integerOnlyField(evt, costError);
  190.        
  191.     }                                
  192.  
  193.     private void txtSalesKeyTyped(java.awt.event.KeyEvent evt) {                                  
  194.         // TODO add your handling code here:
  195.         NumberFormater.integerOnlyField(evt, salesError);
  196.     }                                
  197.  
  198.  
  199.     // Variables declaration - do not modify                    
  200.     private javax.swing.JButton btnCancel;
  201.     private javax.swing.JButton btnSave;
  202.     private datechooser.beans.DateChooserCombo cbmExpiryDate;
  203.     private datechooser.beans.DateChooserCombo cbmManufactureDate;
  204.     private javax.swing.JComboBox<String> cbmSupplier;
  205.     private javax.swing.JLabel costError;
  206.     private javax.swing.JLabel jLabel1;
  207.     private javax.swing.JLabel jLabel2;
  208.     private javax.swing.JLabel jLabel3;
  209.     private javax.swing.JLabel jLabel4;
  210.     private javax.swing.JLabel jLabel5;
  211.     private javax.swing.JLabel jLabel6;
  212.     private javax.swing.JLabel jLabel7;
  213.     private javax.swing.JLabel jLabel8;
  214.     private javax.swing.JLabel jLabel9;
  215.     private javax.swing.JLayeredPane jLayeredPane1;
  216.     private javax.swing.JScrollPane jScrollPane1;
  217.     private javax.swing.JLabel salesError;
  218.     private javax.swing.JTextArea taDescription;
  219.     private javax.swing.JTextField txtBatchNo;
  220.     private javax.swing.JFormattedTextField txtCost;
  221.     private javax.swing.JTextField txtName;
  222.     private javax.swing.JTextField txtQuantity;
  223.     private javax.swing.JFormattedTextField txtSales;
  224.     // End of variables declaration                  
  225.  
  226.     @Override
  227.     public void OnPanelSwitched(JPanel panelToSwitch) {
  228.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  229.     }
  230.    
  231.     /**
  232.      * Get a list of suppliers and fill the suppler comboList
  233.      */
  234.     private void fillSupplierList(){
  235.         inventorysystem.data.Suppler s = new inventorysystem.data.Suppler();
  236.         Vector supplierList = new Vector(); // since a DefaultComboBoxModel takes a vector for initialization
  237.         supplierList.add(0,"Select Suppler");
  238.         try {
  239.             ArrayList<inventorysystem.data.Suppler> suppliers =  s.getSupplier("", true);
  240.             for (int i = 0; i < suppliers.size(); i++) {
  241.                
  242.                 supplierList.add(suppliers.get(i).getSupplerBusinessName());
  243.                 cbmSupplier.setModel(new DefaultComboBoxModel<>(supplierList));
  244.              }
  245.         } catch (SQLException ex) {
  246.             Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  247.         }
  248.     }
  249. }
  250.  

1 comment

Israel Edet 6 years ago
Hello Prof, on line 291 i needed the ID of the currently logged in user for log purposes, instead of going back to the database to get that data. i remembered that during login in the login.java class the employee object was populated and still persist. i now made the variable static so that i can use it in this class.
I tried to do this earlier by creating an instance of the login class but the variable (employee) returned null every time, but making it static solved the problem.
Question: was i right to have done this, and is there a better way? thanks sir

Write a comment