userinterface.PlasticRecyclingServiceRole.PlasticRecyclingJPanel.java Source code

Java tutorial

Introduction

Here is the source code for userinterface.PlasticRecyclingServiceRole.PlasticRecyclingJPanel.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 userinterface.PlasticRecyclingServiceRole;

import Business.Enterprise.Enterprise;

import Business.Organization.PlasticFactoryOutletTransportationOrganization;
import Business.Organization.Organization;

import Business.Organization.PlasticRecyclingOrganization;
import Business.UserAccount.UserAccount;

import Business.WorkQueue.PlasticFactoryOutletTransportationWorkRequest;
import Business.WorkQueue.PlasticRecyclingWorkRequest;
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 PlasticRecyclingJPanel extends javax.swing.JPanel {
    private JPanel userProcessContainer;
    private PlasticRecyclingOrganization organization;
    private Enterprise enterprise;
    private UserAccount userAccount;

    /**
     * Creates new form PlasticRecyclingJPanel
     */
    public PlasticRecyclingJPanel(JPanel userProcessContainer, UserAccount userAccount,
            PlasticRecyclingOrganization organization, Enterprise enterprise) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.organization = organization;
        this.enterprise = enterprise;
        this.userAccount = userAccount;
        populatePlasticFactoryOutlet();
        populateRequestTable();
        jChart1.setBorder(BorderFactory.createEmptyBorder());
        jChart1.setContentAreaFilled(false);
    }

    public void populateRequestTable() {
        DefaultTableModel model = (DefaultTableModel) jPlasticRecycling.getModel();

        model.setRowCount(0);
        for (WorkRequest request : userAccount.getWorkQueue().getWorkRequestList()) {
            Object[] row = new Object[3];
            row[0] = request;

            row[2] = ((PlasticRecyclingWorkRequest) request).getSegregatedPlastic();

            row[1] = ((PlasticRecyclingWorkRequest) request).getReceiver();

            model.addRow(row);
        }
    }

    /**
     * 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();
        jPlasticRecycling = 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());

        jPlasticRecycling.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", "Plastic Quantity", "Items 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(jPlasticRecycling);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 200, 761, 91));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Plastic Recycling Work Area");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 20, -1, 37));

        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(730, 330, -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(370, 330, -1, -1));

        jFactoryOutlet.setModel(
                new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jFactoryOutlet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFactoryOutletActionPerformed(evt);
            }
        });
        add(jFactoryOutlet, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 330, -1, -1));

        jLabel3.setText("FactoryOutletransporter");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 330, 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(559, 416, 50, 34));

        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(642, 416, -1, 34));

        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(164, 382, 115, 68));

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/recyclingicon.jpg"))); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 80, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void jRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshActionPerformed
        DefaultTableModel model = (DefaultTableModel) jPlasticRecycling.getModel();

        model.setRowCount(0);
        for (WorkRequest request : userAccount.getWorkQueue().getWorkRequestList()) {
            Object[] row = new Object[8];
            row[0] = request;
            row[1] = ((PlasticRecyclingWorkRequest) request).getReceiver();
            row[2] = ((PlasticRecyclingWorkRequest) request).getSegregatedPlastic();

            row[3] = ((PlasticRecyclingWorkRequest) request).getNumberOfItemsProduced();
            row[4] = ((PlasticRecyclingWorkRequest) request).getpMoneyEarned();
            row[5] = ((PlasticRecyclingWorkRequest) request).getpTemperature();
            row[6] = ((PlasticRecyclingWorkRequest) request).getpTime();
            row[7] = ((PlasticRecyclingWorkRequest) 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 = jPlasticRecycling.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row");
            return;
        }
        UserAccount ua = (UserAccount) jFactoryOutlet.getSelectedItem();
        PlasticRecyclingWorkRequest gcwr = (PlasticRecyclingWorkRequest) jPlasticRecycling.getValueAt(selectedRow,
                0);
        if (gcwr.getMessage().equalsIgnoreCase("Ready to be sent to outlet")) {
            JOptionPane.showMessageDialog(null, "Already Assigned");
            return;

        }

        int quantity = gcwr.getSegregatedPlastic();
        gcwr.setSentTo(ua);
        gcwr.setMessage("Ready to be sent to outlet");
        int price = 2;
        if (quantity > 0 && quantity < 20) {
            gcwr.setNumberOfItemsProduced(70);
            int q = gcwr.getNumberOfItemsProduced();

            gcwr.setpMoneyEarned(price * q);
            gcwr.setpTemperature(100);
            gcwr.setpTime(5);

        } else if ((quantity > 20 && quantity < 40) || quantity == 20) {
            gcwr.setNumberOfItemsProduced(140);
            int q = gcwr.getNumberOfItemsProduced();

            gcwr.setpMoneyEarned(price * q);
            gcwr.setpTemperature(200);
            gcwr.setpTime(10);
        }

        else if ((quantity > 40 && quantity < 60) || quantity == 40) {
            gcwr.setNumberOfItemsProduced(200);
            int q = gcwr.getNumberOfItemsProduced();

            gcwr.setpMoneyEarned(price * q);
            gcwr.setpTemperature(400);
            gcwr.setpTime(30);
        } else if (quantity > 60 || quantity == 60) {
            gcwr.setNumberOfItemsProduced(350);
            int q = gcwr.getNumberOfItemsProduced();

            gcwr.setpMoneyEarned(price * q);
            gcwr.setpTemperature(600);
            gcwr.setpTime(50);
        }
        PlasticFactoryOutletTransportationWorkRequest mrwk = new PlasticFactoryOutletTransportationWorkRequest();
        mrwk.setMessage("Received by PlasticFactoryOutletTransportation");
        mrwk.setSender(userAccount);
        mrwk.setNumberOfItemsProduced(gcwr.getNumberOfItemsProduced());
        mrwk.setRequestDate(gcwr.getRequestDate());
        Organization org = null;
        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof PlasticFactoryOutletTransportationOrganization) {
                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 = jPlasticRecycling.getRowCount();
        int sum = 0;
        for (int i = 0; i < rowcount; i++) {
            sum = sum + Integer.parseInt(jPlasticRecycling.getValueAt(i, 4).toString());

        }

        return sum;

    }

    private void jTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTotalActionPerformed
        int selectedRow = jPlasticRecycling.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) jPlasticRecycling.getModel();
        int selectedRow = jPlasticRecycling.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row");
            return;
        }
        PlasticRecyclingWorkRequest gcwr = (PlasticRecyclingWorkRequest) jPlasticRecycling.getValueAt(selectedRow,
                0);
        dataset.setValue(gcwr.getNumberOfItemsProduced(), "Items Produced(number)", "" + "Items Produced(number)");
        dataset.setValue(gcwr.getSegregatedPlastic(), "Plastic Quantity", "" + "Plastic 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

    private void jFactoryOutletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFactoryOutletActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFactoryOutletActionPerformed

    public void populatePlasticFactoryOutlet() {

        Organization org = null;
        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof PlasticFactoryOutletTransportationOrganization) {

                org = organization;
                break;
            }
        }
        if (org != null) {
            jFactoryOutlet.removeAllItems();
            for (UserAccount ua : org.getUserAccountDirectory().getUserAccountList()) {
                jFactoryOutlet.addItem(ua);
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    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.JTable jPlasticRecycling;
    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
}