userinterface.AdministratorWorkArea.TopMedicineJPanel.java Source code

Java tutorial

Introduction

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

import Business.Enterprise.Enterprise;
import Business.Organization.AdminOrganization;
import Business.Organization.Organization;
import Business.UserAccount.UserAccount;
import Business.WorkQueue.MedicineWorkRequest;
import Business.WorkQueue.WorkRequest;
import java.awt.CardLayout;
import java.awt.Color;
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 Neelu
 */
public class TopMedicineJPanel extends javax.swing.JPanel {

    private JPanel userProcessContainer;
    private Enterprise enterprise;
    private UserAccount userAccount;
    private AdminOrganization adminOrganization;
    private Organization org;

    /**
     * Creates new form TopMedicineJPanel
     */
    public TopMedicineJPanel(JPanel upc, Enterprise e, UserAccount ua) {
        initComponents();
        userProcessContainer = upc;
        userAccount = ua;
        enterprise = e;

        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof AdminOrganization) {
                org = organization;
                break;
            }
        }
        if (org != null) {
            String maxMed = null;
            int maxQaunt = 0;
            for (WorkRequest request : org.getWorkQueue().getWorkRequestList()) {
                if (request instanceof MedicineWorkRequest) {
                    int quant = ((MedicineWorkRequest) request).getQuantityOrdered();
                    if (quant > maxQaunt) {
                        maxQaunt = quant;
                        maxMed = ((MedicineWorkRequest) request).getMedicineName();

                    }

                }
            }
            DefaultTableModel dtm = (DefaultTableModel) productTable.getModel();

            int rowcount = productTable.getRowCount();

            for (int i = rowcount - 1; i >= 0; i--) {
                dtm.removeRow(i);
            }

            Object row[] = new Object[2];

            row[0] = maxMed == null ? "No Medicines Ordered Yet" : maxMed;
            row[1] = maxQaunt == 0 ? "No Medicines Ordered Yet" : maxQaunt;
            dtm.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();
        productTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        backJButton = new javax.swing.JButton();

        productTable.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        productTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Medicine Name", "Quantity" }));
        jScrollPane1.setViewportView(productTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setText("Most Popular Medicine");

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jButton1.setText("Bar Chart");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(255, 255, 255).addComponent(jLabel1)
                        .addContainerGap(297, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 154,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 592,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup().addGap(51, 51, 51).addComponent(backJButton)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(96, 96, 96)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(93, 93, 93).addComponent(jButton1).addGap(77, 77, 77).addComponent(backJButton)
                        .addContainerGap(87, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        int i = 1;

        for (WorkRequest request : org.getWorkQueue().getWorkRequestList()) {

            dataSet.setValue(((MedicineWorkRequest) request).getQuantityOrdered(), "Number Of Medicines Ordered",
                    ((MedicineWorkRequest) request).getMedicineName());

            i++;

        }

        JFreeChart chart = ChartFactory.createBarChart("Popular Medicines in Hospital Graph", "Medicine Name",
                "Quantity", dataSet, PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.BLACK);
        ChartFrame frame = new ChartFrame("Bar Chart for Patient", chart);
        frame.setVisible(true);
        frame.setSize(800, 550);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed

        userProcessContainer.remove(this);
        CardLayout layout = (CardLayout) userProcessContainer.getLayout();
        layout.previous(userProcessContainer);
    }//GEN-LAST:event_backJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backJButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable productTable;
    // End of variables declaration//GEN-END:variables
}