UserInterface.CentreForDiseaseControl.AddDiseaseJPanel.java Source code

Java tutorial

Introduction

Here is the source code for UserInterface.CentreForDiseaseControl.AddDiseaseJPanel.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.CentreForDiseaseControl;

import Business.Enterprise.CDCEnterprise;
import Business.Enterprise.PHDEnterprise;
import Business.Enterprise.VaccineManufacturerEnterprise;
import Business.Order.Order;
import Business.Order.OrderItem;
import Business.Vaccine.Disease;
import Business.Vaccine.Vaccine;
import java.awt.CardLayout;
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.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author Rishika Idnani
 */
public class AddDiseaseJPanel extends javax.swing.JPanel {

    /**
     * Creates new form AddDiseaseJPanel
     */
    private JPanel userProcessContainer;
    private CDCEnterprise enterprise;

    public AddDiseaseJPanel(JPanel userProcessContainer, CDCEnterprise enterprise) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.enterprise = enterprise;
        structureInternalFrame();
        populateDiseaseTable();
    }

    public void displayPieChart(String diseaseName) {
        String sName = "";
        int winnerState = 0;
        int totalQuantityConsumedInState = 0;
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        for (PHDEnterprise phdEnterprise : enterprise.getPhdList()) {
            String stateName = phdEnterprise.getStateName();
            for (Order order : enterprise.getMasterOrderCatalog().getOrderList()) {
                if (order.getSite().getStateName().equals(stateName)) {
                    for (OrderItem orderItem : order.getOrderItemList()) {
                        if (orderItem.getVaccine().getDisease().getDiseaseName().equals(diseaseName)
                                && orderItem.getIsOrderItemApprovedByCdc().equals("Approved")) {
                            totalQuantityConsumedInState = totalQuantityConsumedInState
                                    + orderItem.getTotalQuantity();
                            if (totalQuantityConsumedInState > winnerState) {
                                winnerState = totalQuantityConsumedInState;
                                sName = stateName;
                            }
                        }
                    }
                }
            }
            pieDataset.setValue(stateName, totalQuantityConsumedInState);
            totalQuantityConsumedInState = 0;
        }

        JFreeChart chart = ChartFactory.createPieChart("Pie Chart", pieDataset, true, true, true);
        PiePlot p = (PiePlot) chart.getPlot();
        ChartFrame frame = new ChartFrame("Disease Summary", chart);
        frame.setVisible(true);
        frame.setSize(450, 500);

        stateNameJTextField.setText(sName);
    }

    public void structureInternalFrame() {
        javax.swing.plaf.InternalFrameUI diseaseFrameUI = diseaseJInternalFrame.getUI();
        ((javax.swing.plaf.basic.BasicInternalFrameUI) diseaseFrameUI).setNorthPane(null);
    }

    public void populateDiseaseTable() {
        DefaultTableModel model = (DefaultTableModel) diseaseJTable.getModel();
        model.setRowCount(0);

        for (Disease disease : enterprise.getDiseaseCatalog().getDiseaseList()) {
            Object row[] = new Object[1];
            row[0] = disease;
            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() {

        panelDescriptionJLabel = new javax.swing.JLabel();
        backJButton = new javax.swing.JButton();
        displayJLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        diseaseJTable = new javax.swing.JTable();
        removeDiseaseJButton = new javax.swing.JButton();
        diseaseJInternalFrame = new javax.swing.JInternalFrame();
        nameJLabel = new javax.swing.JLabel();
        nameJTextField = new javax.swing.JTextField();
        addDiseaseJButton = new javax.swing.JButton();
        vaccineSummaryJLabel = new javax.swing.JLabel();
        viewGraphJButton = new javax.swing.JButton();
        stateJLabel = new javax.swing.JLabel();
        stateNameJTextField = new javax.swing.JTextField();

        panelDescriptionJLabel.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        panelDescriptionJLabel.setText("Add Disease");

        backJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        backJButton.setText("<< Back");
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        displayJLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        displayJLabel.setText("Display of Disease");

        diseaseJTable.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        diseaseJTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null }, { null }, { null }, { null } }, new String[] { "Disease Name" }));
        diseaseJTable.setRowHeight(24);
        jScrollPane1.setViewportView(diseaseJTable);

        removeDiseaseJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        removeDiseaseJButton.setText("Remove Disease");
        removeDiseaseJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeDiseaseJButtonActionPerformed(evt);
            }
        });

        diseaseJInternalFrame.setVisible(true);

        nameJLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        nameJLabel.setText("Name");

        nameJTextField.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        addDiseaseJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        addDiseaseJButton.setText("Add Disease");
        addDiseaseJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDiseaseJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout diseaseJInternalFrameLayout = new javax.swing.GroupLayout(
                diseaseJInternalFrame.getContentPane());
        diseaseJInternalFrame.getContentPane().setLayout(diseaseJInternalFrameLayout);
        diseaseJInternalFrameLayout.setHorizontalGroup(
                diseaseJInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(diseaseJInternalFrameLayout.createSequentialGroup().addContainerGap()
                                .addComponent(nameJLabel).addGap(18, 18, 18)
                                .addComponent(nameJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 99,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18).addComponent(addDiseaseJButton)
                                .addContainerGap(22, Short.MAX_VALUE)));
        diseaseJInternalFrameLayout.setVerticalGroup(diseaseJInternalFrameLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(diseaseJInternalFrameLayout.createSequentialGroup().addContainerGap()
                        .addGroup(diseaseJInternalFrameLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(nameJLabel)
                                .addComponent(nameJTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(addDiseaseJButton))
                        .addContainerGap(67, Short.MAX_VALUE)));

        vaccineSummaryJLabel.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        vaccineSummaryJLabel.setText("Vaccine summary in states wrt Disease");

        viewGraphJButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        viewGraphJButton.setText("View Graphically ");
        viewGraphJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewGraphJButtonActionPerformed(evt);
            }
        });

        stateJLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        stateJLabel.setText("State in which maximum people are suffering from this disease");

        stateNameJTextField.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(panelDescriptionJLabel).addComponent(displayJLabel)
                        .addComponent(diseaseJInternalFrame, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup().addComponent(vaccineSummaryJLabel)
                                .addGap(18, 18, 18).addComponent(viewGraphJButton))
                        .addComponent(backJButton)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        310, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(removeDiseaseJButton))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup().addComponent(stateJLabel).addGap(18, 18, 18)
                                                .addComponent(stateNameJTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 108,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(137, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(panelDescriptionJLabel)
                        .addGap(18, 18, 18)
                        .addComponent(diseaseJInternalFrame, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28).addComponent(displayJLabel).addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(removeDiseaseJButton))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(vaccineSummaryJLabel).addComponent(viewGraphJButton))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(stateJLabel).addComponent(stateNameJTextField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29).addComponent(backJButton).addContainerGap(436, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void addDiseaseJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDiseaseJButtonActionPerformed
        // TODO add your handling code here:
        String diseaseName = nameJTextField.getText();

        if (diseaseName.trim().equals("")) {
            JOptionPane.showMessageDialog(null, "Please enter disease name!");
            return;
        }

        Disease d = enterprise.getDiseaseCatalog().addAndCreateDisease(diseaseName);
        if (d == null) {
            JOptionPane.showMessageDialog(null, "Disease already present!");
            return;
        }
        nameJTextField.setText("");
        populateDiseaseTable();
    }//GEN-LAST:event_addDiseaseJButtonActionPerformed

    private void removeDiseaseJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeDiseaseJButtonActionPerformed
        // TODO add your handling code here:
        int selectedRow = diseaseJTable.getSelectedRow();

        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row!");
            return;
        }

        Disease disease = (Disease) diseaseJTable.getValueAt(selectedRow, 0);
        enterprise.getDiseaseCatalog().removeDisease(disease);
        populateDiseaseTable();

        for (VaccineManufacturerEnterprise e : enterprise.getVaccineManufacturerList()) {
            for (Vaccine v : e.getVaccineCatalog().getVaccineList()) {
                if (v.getDisease() == disease) {
                    e.getVaccineCatalog().removeVaccine(v);
                }
            }
        }
    }//GEN-LAST:event_removeDiseaseJButtonActionPerformed

    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed
        // TODO add your handling code here:
        userProcessContainer.remove(this);
        CardLayout layout = (CardLayout) userProcessContainer.getLayout();
        layout.previous(userProcessContainer);
    }//GEN-LAST:event_backJButtonActionPerformed

    private void viewGraphJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewGraphJButtonActionPerformed
        // TODO add your handling code here:
        int selectedRow = diseaseJTable.getSelectedRow();

        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Please select a row!");
            return;
        }

        String diseaseName = String.valueOf(diseaseJTable.getValueAt(selectedRow, 0));
        displayPieChart(diseaseName);
    }//GEN-LAST:event_viewGraphJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addDiseaseJButton;
    private javax.swing.JButton backJButton;
    private javax.swing.JInternalFrame diseaseJInternalFrame;
    private javax.swing.JTable diseaseJTable;
    private javax.swing.JLabel displayJLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameJLabel;
    private javax.swing.JTextField nameJTextField;
    private javax.swing.JLabel panelDescriptionJLabel;
    private javax.swing.JButton removeDiseaseJButton;
    private javax.swing.JLabel stateJLabel;
    private javax.swing.JTextField stateNameJTextField;
    private javax.swing.JLabel vaccineSummaryJLabel;
    private javax.swing.JButton viewGraphJButton;
    // End of variables declaration//GEN-END:variables
}