userinterface.MetalRecyclingServiceRole.MetalRecyclingServiceJPanel.java Source code

Java tutorial

Introduction

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

import Business.Enterprise.Enterprise;
import Business.Organization.MetalFactoryOutletTransortationOrganization;
import Business.Organization.MetalRecyclingOrganization;
import Business.Organization.Organization;
import Business.UserAccount.UserAccount;
import Business.WorkQueue.MetalRecyclingWorkRequest;
import Business.WorkQueue.WorkRequest;
import Business.WorkQueue.MetalFactoryOutletTransportationWorkRequest;
import java.awt.Dimension;
import java.awt.Font;
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 MetalRecyclingServiceJPanel extends javax.swing.JPanel {

    private JPanel userProcessContainer;
    private MetalRecyclingOrganization organization;
    private Enterprise enterprise;
    private UserAccount userAccount;

    /**
     * Creates new form MetalRecyclingServiceJPanel
     */
    public MetalRecyclingServiceJPanel(JPanel userProcessContainer, UserAccount userAccount,
            MetalRecyclingOrganization organization, Enterprise enterprise) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.organization = organization;
        this.enterprise = enterprise;
        this.userAccount = userAccount;
        populateRequestTable();

        populateMetalFactoryOutlet();
        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();
        jMetalRecycling = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jRefresh = new javax.swing.JButton();
        jProcess = new javax.swing.JButton();
        jFactoryOutlet = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jChart1 = new javax.swing.JButton();
        jTotalMoney = new javax.swing.JTextField();
        jTotal = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 204));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jMetalRecycling.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", "metal 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(jMetalRecycling);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 910, 91));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Metal Recycling Work Area");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 0, 250, -1));
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 208, 56, -1));

        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(600, 250, -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(440, 250, -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(150, 250, -1, -1));

        jLabel3.setText("FactoryOutletransporter");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 280, 120, -1));

        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(160, 310, 110, 70));

        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(400, 350, 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(480, 350, -1, 30));

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

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

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

            row[3] = ((MetalRecyclingWorkRequest) request).getQuantityProduced();
            row[4] = ((MetalRecyclingWorkRequest) request).getMoneyEarned();
            row[5] = ((MetalRecyclingWorkRequest) request).getmTemperature();
            row[6] = ((MetalRecyclingWorkRequest) request).getmTime();
            row[7] = ((MetalRecyclingWorkRequest) 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 = jMetalRecycling.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row");
            return;
        }
        UserAccount ua = (UserAccount) jFactoryOutlet.getSelectedItem();
        MetalRecyclingWorkRequest gcwr = (MetalRecyclingWorkRequest) jMetalRecycling.getValueAt(selectedRow, 0);

        if (gcwr.getMessage().equalsIgnoreCase("Ready to be sent to outlet")) {
            JOptionPane.showMessageDialog(null, "Already Assigned");
            return;

        }
        int quantity = gcwr.getSegregatedMetal();
        int price = 1;
        //        int previousmoneyearned=organization.getWk().getTotalMoneyEarned();
        //        organization.getWk().setTotalMoneyEarned(price);
        //        

        gcwr.setSentTo(ua);
        gcwr.setMessage("Ready to be sent to outlet");

        if (quantity > 0 && quantity < 20) {
            gcwr.setQuantityProduced(50);
            int q = gcwr.getQuantityProduced();

            gcwr.setMoneyEarned(price * q);

            gcwr.setmTemperature(200);
            gcwr.setmTime(10);

        } else if ((quantity > 20 && quantity < 40) || quantity == 20) {
            gcwr.setQuantityProduced(100);
            int q = gcwr.getQuantityProduced();

            gcwr.setMoneyEarned(price * q);
            gcwr.setmTemperature(400);
            gcwr.setmTime(20);

        }

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

            gcwr.setMoneyEarned(price * q);
            gcwr.setmTemperature(600);
            gcwr.setmTime(40);
        } else if (quantity > 60 || quantity == 60) {
            gcwr.setQuantityProduced(400);
            int q = gcwr.getQuantityProduced();

            gcwr.setMoneyEarned(price * q);
            gcwr.setmTemperature(1000);
            gcwr.setmTime(60);
        }
        MetalFactoryOutletTransportationWorkRequest mrwk = new MetalFactoryOutletTransportationWorkRequest();
        mrwk.setMessage("Received by MetalFactoryOutletTransportation");
        mrwk.setSender(userAccount);
        mrwk.setReceiver(ua);
        mrwk.setQuantityProduced(gcwr.getQuantityProduced());
        mrwk.setRequestDate(gcwr.getRequestDate());
        System.out.println(mrwk.getRequestDate());
        Organization org = null;
        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof MetalFactoryOutletTransortationOrganization) {
                org = organization;
                break;
            }
        }
        if (org != null) {

            ua.getWorkQueue().getWorkRequestList().add(mrwk);

            JOptionPane.showMessageDialog(null, "Work request added.");
        }

    }//GEN-LAST:event_jProcessActionPerformed

    public int getSum() {
        int rowcount = jMetalRecycling.getRowCount();
        int sum = 0;
        for (int i = 0; i < rowcount; i++) {
            sum = sum + Integer.parseInt(jMetalRecycling.getValueAt(i, 4).toString());

        }

        return sum;

    }

    private void jChart1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jChart1ActionPerformed
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        DefaultTableModel dtm = (DefaultTableModel) jMetalRecycling.getModel();
        int selectedRow = jMetalRecycling.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row");
            return;
        }
        MetalRecyclingWorkRequest gcwr = (MetalRecyclingWorkRequest) jMetalRecycling.getValueAt(selectedRow, 0);
        dataset.setValue(gcwr.getQuantityProduced(), "Items Produced(number)", "" + "Items Produced(number)");
        dataset.setValue(gcwr.getSegregatedMetal(), "Metal Quantity", "" + "Metal 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 jTotalMoneyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTotalMoneyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTotalMoneyActionPerformed

    private void jTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTotalActionPerformed
        int selectedRow = jMetalRecycling.getRowCount();
        if (selectedRow <= 0) {
            JOptionPane.showMessageDialog(null, "No Data");
            return;
        }

        jTotalMoney.setText(Integer.toString(getSum()));

    }//GEN-LAST:event_jTotalActionPerformed

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

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

            row[2] = ((MetalRecyclingWorkRequest) request).getSegregatedMetal();

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

            model.addRow(row);
        }
    }

    public void populateMetalFactoryOutlet() {

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

                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.JLabel jLabel4;
    private javax.swing.JTable jMetalRecycling;
    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
}