Java tutorial
/* * 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 UserInterface.SupplierRole; import Business.Product.Product; import Business.Employee.Supplier; import java.awt.CardLayout; import java.awt.Color; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.table.DefaultTableModel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author Praneeth Reddy */ public class ProductReportJPanel extends javax.swing.JPanel { JPanel userProcessContainer; Supplier supplier; /** * Creates new form ReviewProductPerformance */ public ProductReportJPanel(JPanel userProcessContainer, Supplier supplier) { initComponents(); this.userProcessContainer = userProcessContainer; this.supplier = supplier; supplierName.setText(supplier.getName() + "-- Sales Performance"); try { Product product = supplier.getProductsSorted(supplier); productName.setText(product.getProdName()); quanitytxt.setText(String.valueOf(product.getSoldQuantity())); populateProducts(); } catch (IndexOutOfBoundsException ai) { errorMsg.setText("Sales Report Not Generated...!!!"); JOptionPane.showMessageDialog(null, "The Sales Report will only be generated after the customer's checkout"); } } /** * 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() { supplierName = new javax.swing.JLabel(); btnPictorialView = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); productName = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); quanitytxt = new javax.swing.JTextField(); backButton4 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); salestbl = new javax.swing.JTable(); errorMsg = new javax.swing.JLabel(); supplierName.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N btnPictorialView.setText("View Sales"); btnPictorialView.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnPictorialViewActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N jLabel1.setText("Top Sold Product"); jLabel2.setText("Product Name"); jLabel3.setText("Quantity"); quanitytxt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { quanitytxtActionPerformed(evt); } }); backButton4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N backButton4.setText("<< Back"); backButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backButton4ActionPerformed(evt); } }); salestbl.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Product Name", "Sold Quantity", "Unit Price", "Generated Revenue" }) { Class[] types = new Class[] { java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class }; boolean[] canEdit = new boolean[] { false, false, false, false }; public Class getColumnClass(int columnIndex) { return types[columnIndex]; } public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(salestbl); if (salestbl.getColumnModel().getColumnCount() > 0) { salestbl.getColumnModel().getColumn(0).setResizable(false); salestbl.getColumnModel().getColumn(1).setResizable(false); salestbl.getColumnModel().getColumn(2).setResizable(false); salestbl.getColumnModel().getColumn(3).setResizable(false); } errorMsg.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N errorMsg.setForeground(new java.awt.Color(204, 0, 0)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(200, 200, 200).addComponent(jLabel1)) .addGroup(layout.createSequentialGroup().addGap(49, 49, 49).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(31, 31, 31).addComponent(backButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(203, 203, 203).addComponent( btnPictorialView, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(148, 148, 148).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2).addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(productName).addComponent(quanitytxt, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(errorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(182, 182, 182).addComponent(supplierName, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(81, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(supplierName, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel1) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(productName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(quanitytxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(errorMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnPictorialView).addGap(31, 31, 31) .addComponent(backButton4).addContainerGap(37, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void quanitytxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quanitytxtActionPerformed // TODO add your handling code here: }//GEN-LAST:event_quanitytxtActionPerformed private void backButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButton4ActionPerformed // TODO add your handling code here: userProcessContainer.remove(this); CardLayout layout = (CardLayout) userProcessContainer.getLayout(); layout.previous(userProcessContainer); }//GEN-LAST:event_backButton4ActionPerformed private void btnPictorialViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPictorialViewActionPerformed // TODO add your handling code here: DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset(); for (Product product : supplier.getProductCatalog().getProductCatalog()) { categoryDataset.setValue(product.getSoldQuantity(), "Units", product.getProdName()); } JFreeChart freeChart = ChartFactory.createBarChart("Sales Report", "Product", "Units", categoryDataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot categoryPlot = freeChart.getCategoryPlot(); categoryPlot.setRangeGridlinePaint(Color.BLACK); ChartFrame cf = new ChartFrame("Sales Report", freeChart); cf.setVisible(true); cf.setSize(450, 300); }//GEN-LAST:event_btnPictorialViewActionPerformed // private Product getTopSold(){ // // Product product = Collections.max(supplier.getProductCatalog().getProductCatalog(), new Comparator<Product>(){ // // public int compare(Product o1, Product o2) { // if(o1.getSoldQuantity() > o2.getSoldQuantity()) // return 1; // else if(o1.getSoldQuantity() < o2.getSoldQuantity()) // return -1; // return 0; // } // }); // // return product; // } private void populateProducts() { // Collections.sort(supplier.getProductCatalog().getProductCatalog(), new Comparator<Product>(){ // // public int compare(Product o1, Product o2) { // if(o1.getSoldQuantity() < o2.getSoldQuantity()) // return 1; // else if(o1.getSoldQuantity() > o2.getSoldQuantity()) // return -1; // return 0; // } // }); DefaultTableModel dtm = (DefaultTableModel) salestbl.getModel(); dtm.setRowCount(0); for (Product p : supplier.getProductCatalog().getProductCatalog()) { Object row[] = new Object[4]; row[0] = p; row[1] = p.getSoldQuantity(); row[2] = p.getPrice(); row[3] = (p.getSoldQuantity() * p.getPrice()); dtm.addRow(row); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton backButton4; private javax.swing.JButton btnPictorialView; private javax.swing.JLabel errorMsg; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField productName; private javax.swing.JTextField quanitytxt; private javax.swing.JTable salestbl; private javax.swing.JLabel supplierName; // End of variables declaration//GEN-END:variables }