UserInterface.EmployeeViewArea.ViewIssuesStatisticsJPanel.java Source code

Java tutorial

Introduction

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

import Business.EcoSystem;
import Business.Enterprise.Enterprise;
import Business.Network.City;
import Business.Network.Country;
import Business.Network.State;
import Business.Organization.ManagerOrganization;
import Business.Organization.Organization;
import Business.UserAccount.UserAccount;
import Business.WorkQueue.IssueWorkRequest;
import Business.WorkQueue.WorkRequest;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author Ekta
 */
public class ViewIssuesStatisticsJPanel extends javax.swing.JPanel {

    /**
     * Creates new form ViewIssuesStatisticsJPanel
     */
    private static String workingDir = System.getProperty("user.dir");
    private static int count = 1;
    private JPanel userProcessContainer;
    private EcoSystem system;
    private UserAccount userAccount;
    private static Random rand = new Random();
    private static String[] imageName = { "air", "ir", "mi", "sor", "mee", "clo", "red", "cra", "ark", "arc",
            "miri", "lori", "cres", "mur", "zer", "marac", "zoir", "slamar", "salmar", "urak", "d", "ed", "ark",
            "arc", "es", "er", "der", "tron", "med", "ure", "zur", "cred", "mur", "Kr", "Ca", "Ra", "Mrok", "Cru",
            "Ray", "Bre", "Zed", "Drak", "Mor", "Jag", "Mer", "Jar", "Mjol", "Zork", "Md", "Cry", "Zur", "Creo",
            "Azak", "Azur", "Rei", "Cro", "Mar", "Luk" };

    public ViewIssuesStatisticsJPanel(JPanel userProcessContainer, EcoSystem system, UserAccount userAccount) {
        initComponents();
        this.userProcessContainer = userProcessContainer;
        this.system = system;
        this.userAccount = userAccount;
        // REMOVE ANY ITEM CURRENTLY IN THE JCROLLPANE
        scrollPane.getViewport().removeAll();
        // BUILD AND ADD THE CHART TO THE JSCROLLPANE
        scrollPane.getViewport().add(createPiePanel());
    }

    public static void saveToFile(JFreeChart chart, String aFileName, int width, int height, double quality)
            throws FileNotFoundException, IOException {
        BufferedImage img = draw(chart, width, height);

        FileOutputStream fos = new FileOutputStream(aFileName);
        ImageIO.write(img, "jpg", fos);
        fos.close();
    }

    protected static BufferedImage draw(JFreeChart chart, int width, int height) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = img.createGraphics();

        chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));

        g2.dispose();
        return img;
    }

    /**
     * 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() {

        scrollPane = new javax.swing.JScrollPane();
        btnSaveImage = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        btnSendEmail = new javax.swing.JButton();
        lblHeader = new javax.swing.JLabel();

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

        scrollPane.setMaximumSize(new java.awt.Dimension(700, 700));
        scrollPane.setMinimumSize(new java.awt.Dimension(700, 700));
        add(scrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 66, 700, 577));

        btnSaveImage.setText("Save as Image");
        btnSaveImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveImageActionPerformed(evt);
            }
        });
        add(btnSaveImage, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 660, -1, -1));

        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });
        add(btnBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        btnSendEmail.setText("Send Email");
        btnSendEmail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendEmailActionPerformed(evt);
            }
        });
        add(btnSendEmail, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 660, 110, -1));

        lblHeader.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHeader.setText("Enterprise Level");
        add(lblHeader, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, 370, 26));
    }// </editor-fold>//GEN-END:initComponents

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

    private void btnSaveImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveImageActionPerformed
        // TODO add your handling code here:
        createPiePanel(500, 300, 100, "Reported Child Issues");
    }//GEN-LAST:event_btnSaveImageActionPerformed

    private void btnSendEmailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendEmailActionPerformed
        // TODO add your handling code here:

        CardLayout cardLayout = (CardLayout) userProcessContainer.getLayout();
        EmailJPanel ejp = new EmailJPanel(userProcessContainer);
        userProcessContainer.add("ejp", ejp);
        cardLayout.next(userProcessContainer);

    }//GEN-LAST:event_btnSendEmailActionPerformed

    public JPanel createPiePanel(int ring, int call, int off, String title) {
        JFreeChart pieChart = createPieChart(createPieDataset());
        try {
            String fileName = String.valueOf(count);
            fileName += imageName[rand.nextInt(imageName.length)];
            saveToFile(pieChart, workingDir + "\\src\\Images" + "\\" + fileName + "plot.jpg", ring, call, off);
            count++;
            JOptionPane.showMessageDialog(null, "Image saved successfully", "Warning", JOptionPane.WARNING_MESSAGE);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ChartPanel(pieChart);
    }

    // CREATE THE PIE CHART - 1
    public JPanel createPiePanel() {
        JFreeChart pieChart = createPieChart(createPieDataset());
        return new ChartPanel(pieChart);
    }

    // BUILD THE PIE CHART - 3
    private static JFreeChart createPieChart(PieDataset dataset) {
        // chart title   // data  // include legend
        JFreeChart chart = ChartFactory.createPieChart("Reported Child Issues", dataset, true, true, false);

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionOutlinesVisible(false);
        plot.setNoDataMessage("No data available");

        return chart;

    }

    // CREATE THE PIE CHART DATA - 2
    private PieDataset createPieDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        HashMap<String, Integer> hashmap = new HashMap<>();
        hashmap.put("Child Labour", 0);
        hashmap.put("Foster Care", 0);
        hashmap.put("Adoption and Rehabilitation", 0);
        hashmap.put("Donation of Basic Amenities", 0);
        for (Country c : system.getNetworkList()) {

            for (State s : c.getStateList()) {
                for (City city : s.getCityList()) {
                    for (Enterprise e : city.getEnterpriseDirectory().getEnterpriseList()) {

                        if (userAccount.getRole().getName().equals("System Type")) {

                            lblHeader.setText("EcoSystem Level");
                            for (Organization organization : e.getOrganizationDirectory().getOrganizationList()) {
                                if (organization instanceof ManagerOrganization) {
                                    for (WorkRequest request : organization.getWorkQueue().getWorkRequestList()) {
                                        if (!(request instanceof IssueWorkRequest)) {
                                        } else {
                                            String type = ((IssueWorkRequest) request).getType();
                                            if (type.equals("Child Labour")) {

                                                hashmap.put("Child Labour", hashmap.get("Child Labour") + 1);
                                            } else if (type.equals("Foster Care")) {
                                                hashmap.put("Foster Care", hashmap.get("Foster Care") + 1);
                                            } else if (type.equals("Adoption and Rehabilitation")) {
                                                hashmap.put("Adoption and Rehabilitation",
                                                        hashmap.get("Adoption and Rehabilitation") + 1);
                                            } else {
                                                hashmap.put("Donation of Basic Amenities",
                                                        hashmap.get("Donation of Basic Amenities") + 1);

                                            }

                                        }
                                    }
                                }
                            }
                        } else {

                            for (UserAccount ua : e.getUserAccountDirectory().getUserAccountList()) {
                                if (userAccount.getUsername().equals(ua.getUsername())) {
                                    lblHeader.setText("Enterprise Level");
                                    for (Organization organization : e.getOrganizationDirectory()
                                            .getOrganizationList()) {

                                        if (organization instanceof ManagerOrganization) {
                                            for (WorkRequest request : organization.getWorkQueue()
                                                    .getWorkRequestList()) {
                                                if (!(request instanceof IssueWorkRequest)) {
                                                } else {
                                                    String type = ((IssueWorkRequest) request).getType();
                                                    if (type.equals("Child Labour")) {

                                                        hashmap.put("Child Labour",
                                                                hashmap.get("Child Labour") + 1);
                                                    } else if (type.equals("Foster Care")) {
                                                        hashmap.put("Foster Care", hashmap.get("Foster Care") + 1);
                                                    } else if (type.equals("Adoption and Rehabilitation")) {
                                                        hashmap.put("Adoption and Rehabilitation",
                                                                hashmap.get("Adoption and Rehabilitation") + 1);
                                                    } else {
                                                        hashmap.put("Donation of Basic Amenities",
                                                                hashmap.get("Donation of Basic Amenities") + 1);

                                                    }
                                                }

                                            }
                                        }
                                    }
                                }
                            }

                        }

                    }

                }

            }
        }
        dataset.setValue("Child Labour", hashmap.get("Child Labour"));
        dataset.setValue("Foster Care", hashmap.get("Foster Care"));
        dataset.setValue("Adoption and Rehabilitation", hashmap.get("Adoption and Rehabilitation"));
        dataset.setValue("Donation of Basic Amenities", hashmap.get("Donation of Basic Amenities"));
        return dataset;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnSaveImage;
    private javax.swing.JButton btnSendEmail;
    private javax.swing.JLabel lblHeader;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}