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.BioRecyclingRole; import Business.Enterprise.Enterprise; import Business.Organization.BioRecyclingOrganization; import Business.Organization.Organization; import Business.UserAccount.UserAccount; import Business.WorkQueue.BioRecyclingWorkRequest; import Business.WorkQueue.BioDegradableFactoryOutletTransportationWorkRequest; import Business.Organization.BioDegradableFactoryOutletTransportationOrganization; import Business.WorkQueue.WorkRequest; import java.awt.Dimension; import javax.swing.BorderFactory; 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.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; /** * * @author pranalinarkar */ public class BioRecyclingJPanel extends javax.swing.JPanel { private JPanel userProcessContainer; private BioRecyclingOrganization organization; private Enterprise enterprise; private UserAccount userAccount; /** * Creates new form BioRecyclingJPanel */ public BioRecyclingJPanel(JPanel userProcessContainer, UserAccount userAccount, BioRecyclingOrganization organization, Enterprise enterprise) { initComponents(); this.userProcessContainer = userProcessContainer; this.organization = organization; this.enterprise = enterprise; this.userAccount = userAccount; populateRequestTable(); populateBioFactoryOutlet(); jChart1.setBorder(BorderFactory.createEmptyBorder()); jChart1.setContentAreaFilled(false); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); jBioRecycling = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); jRefresh = new javax.swing.JButton(); jProcess = new javax.swing.JButton(); jFactoryOutlet = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); jTotalMoney = new javax.swing.JTextField(); jTotal = new javax.swing.JButton(); jChart1 = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); setBackground(new java.awt.Color(255, 255, 204)); setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jBioRecycling.setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null } }, new String[] { "Message", "Sender", "Bio Quantity", "Quantity Produced(number)", "Money Earned(in $)", "Temperature in Degree c", "Time(hrs)", "SentTo" }) { boolean[] canEdit = new boolean[] { false, false, false, false, false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); jScrollPane1.setViewportView(jBioRecycling); add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 160, 770, 91)); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("BioDegradable Recycling Work Area"); add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 20, -1, 24)); jRefresh.setText("Refresh"); jRefresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRefreshActionPerformed(evt); } }); add(jRefresh, new org.netbeans.lib.awtextra.AbsoluteConstraints(740, 270, -1, -1)); jProcess.setText("Process"); jProcess.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jProcessActionPerformed(evt); } }); add(jProcess, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 270, -1, -1)); jFactoryOutlet.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); add(jFactoryOutlet, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 270, -1, -1)); jLabel3.setText("FactoryOutletransporter"); add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 270, 120, 23)); jTotalMoney.setEditable(false); jTotalMoney.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTotalMoneyActionPerformed(evt); } }); add(jTotalMoney, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 360, 50, 30)); jTotal.setText("TotalMoney"); jTotal.setEnabled(false); jTotal.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTotalActionPerformed(evt); } }); add(jTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 360, -1, 30)); jChart1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/graphicon.jpg"))); // NOI18N jChart1.setText("Chart 1"); jChart1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jChart1ActionPerformed(evt); } }); add(jChart1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 330, 110, 60)); jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/recyclingicon.jpg"))); // NOI18N add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 50, -1, 100)); }// </editor-fold>//GEN-END:initComponents public void populateBioFactoryOutlet() { Organization org = null; for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) { if (organization instanceof BioDegradableFactoryOutletTransportationOrganization) { org = organization; break; } } if (org != null) { jFactoryOutlet.removeAllItems(); for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) { jFactoryOutlet.addItem(ua); } } } public void populateRequestTable() { DefaultTableModel model = (DefaultTableModel) jBioRecycling.getModel(); model.setRowCount(0); for (WorkRequest request : userAccount.getWorkQueue().getWorkRequestList()) { Object[] row = new Object[3]; row[0] = request; row[2] = ((BioRecyclingWorkRequest) request).getSegregatedDegradable(); row[1] = ((BioRecyclingWorkRequest) request).getReceiver(); model.addRow(row); } } private void jRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshActionPerformed DefaultTableModel model = (DefaultTableModel) jBioRecycling.getModel(); model.setRowCount(0); for (WorkRequest request : userAccount.getWorkQueue().getWorkRequestList()) { Object[] row = new Object[8]; row[0] = request; row[1] = ((BioRecyclingWorkRequest) request).getReceiver(); row[2] = ((BioRecyclingWorkRequest) request).getSegregatedDegradable(); row[3] = ((BioRecyclingWorkRequest) request).getQuantityProduced(); row[4] = ((BioRecyclingWorkRequest) request).getMoneyEarned(); row[5] = ((BioRecyclingWorkRequest) request).getbTemperature(); row[6] = ((BioRecyclingWorkRequest) request).getbTime(); row[7] = ((BioRecyclingWorkRequest) request).getSentTo(); model.addRow(row); } jTotal.setEnabled(true); }//GEN-LAST:event_jRefreshActionPerformed private void jProcessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProcessActionPerformed int selectedRow = jBioRecycling.getSelectedRow(); if (selectedRow < 0) { JOptionPane.showMessageDialog(null, "Please select a row"); return; } UserAccount ua = (UserAccount) jFactoryOutlet.getSelectedItem(); BioRecyclingWorkRequest gcwr = (BioRecyclingWorkRequest) jBioRecycling.getValueAt(selectedRow, 0); if (gcwr.getMessage().equalsIgnoreCase("Ready to be sent to outlet")) { JOptionPane.showMessageDialog(null, "Already Assigned"); return; } gcwr.setSentTo(ua); gcwr.setMessage("Ready to be sent to outlet"); int quantity = gcwr.getSegregatedDegradable(); double price = 1.5; if (quantity > 0 && quantity < 20) { gcwr.setQuantityProduced(70); int q = gcwr.getQuantityProduced(); gcwr.setMoneyEarned((int) (price * q)); gcwr.setbTemperature(100); gcwr.setbTime(5); } else if ((quantity > 20 && quantity < 40) || quantity == 20) { gcwr.setQuantityProduced(140); int q = gcwr.getQuantityProduced(); gcwr.setMoneyEarned((int) (price * q)); gcwr.setbTemperature(200); gcwr.setbTime(10); } else if ((quantity > 40 && quantity < 60) || quantity == 40) { gcwr.setQuantityProduced(200); int q = gcwr.getQuantityProduced(); gcwr.setMoneyEarned((int) (price * q)); gcwr.setbTemperature(400); gcwr.setbTime(30); } else if (quantity > 60 || quantity == 60) { gcwr.setQuantityProduced(350); int q = gcwr.getQuantityProduced(); gcwr.setMoneyEarned((int) (price * q)); gcwr.setbTemperature(600); gcwr.setbTime(50); } BioDegradableFactoryOutletTransportationWorkRequest mrwk = new BioDegradableFactoryOutletTransportationWorkRequest(); mrwk.setMessage("Received by BioFactoryOutletTransportation"); mrwk.setSender(userAccount); mrwk.setQuantityProduced(gcwr.getQuantityProduced()); mrwk.setRequestDate(gcwr.getRequestDate()); Organization org = null; for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) { if (organization instanceof BioDegradableFactoryOutletTransportationOrganization) { org = organization; break; } } if (org != null) { ua.getWorkQueue().getWorkRequestList().add(mrwk); JOptionPane.showMessageDialog(null, "Work request added."); } }//GEN-LAST:event_jProcessActionPerformed private void jTotalMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTotalMoneyActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTotalMoneyActionPerformed public int getSum() { int rowcount = jBioRecycling.getRowCount(); int sum = 0; for (int i = 0; i < rowcount; i++) { sum = sum + Integer.parseInt(jBioRecycling.getValueAt(i, 4).toString()); } return sum; } private void jTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTotalActionPerformed int selectedRow = jBioRecycling.getRowCount(); if (selectedRow <= 0) { JOptionPane.showMessageDialog(null, "No Data"); return; } jTotalMoney.setText(Integer.toString(getSum())); }//GEN-LAST:event_jTotalActionPerformed private void jChart1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jChart1ActionPerformed DefaultCategoryDataset dataset = new DefaultCategoryDataset(); DefaultTableModel dtm = (DefaultTableModel) jBioRecycling.getModel(); int selectedRow = jBioRecycling.getSelectedRow(); if (selectedRow < 0) { JOptionPane.showMessageDialog(null, "Please select a row"); return; } BioRecyclingWorkRequest gcwr = (BioRecyclingWorkRequest) jBioRecycling.getValueAt(selectedRow, 0); dataset.setValue(gcwr.getQuantityProduced(), "Items Produced(number)", "" + "Items Produced(number)"); dataset.setValue(gcwr.getSegregatedDegradable(), "Bio Quantity", "" + "Bio Quantity(kgs)"); JFreeChart chart = ChartFactory.createBarChart("Bar Chart Demo", "Category", "Value", dataset, PlotOrientation.VERTICAL.VERTICAL, true, true, false); ChartPanel chartPanel = new ChartPanel(chart, false); chartPanel.setPreferredSize(new Dimension(500, 270)); ChartFrame cFrame = new ChartFrame("Name", chart); cFrame.pack(); cFrame.setVisible(true); }//GEN-LAST:event_jChart1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTable jBioRecycling; private javax.swing.JButton jChart1; private javax.swing.JComboBox jFactoryOutlet; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JButton jProcess; private javax.swing.JButton jRefresh; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton jTotal; private javax.swing.JTextField jTotalMoney; // End of variables declaration//GEN-END:variables }