UserInterface.WaterSupplyAdmin.UsagePatternJPanel.java Source code

Java tutorial

Introduction

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

import Business.Customer.Customer;
import Business.Customer.WaterUsage;
import Business.Employee.Employee;
import Business.Enterprise.Enterprise;
import Business.Organization.CustomerOrganization;
import Business.Organization.Organization;
import Business.WaterSupplyAdmin.SensorValue;
import java.awt.CardLayout;
import java.awt.Color;
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.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author Bhavesh Patel
 */
public class UsagePatternJPanel extends javax.swing.JPanel {

    /**
     * Creates new form UsagePatternJPanel
     */
    private Enterprise enterprise;
    private JPanel container;
    private CustomerOrganization customerOrganization;

    public UsagePatternJPanel(JPanel container, Enterprise enterprise) {
        initComponents();
        this.enterprise = enterprise;
        this.container = container;

        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof CustomerOrganization) {
                this.customerOrganization = (CustomerOrganization) organization;
                break;
            }
        }
        populateCustomers();
    }

    private void populateCustomers() {
        DefaultTableModel model = (DefaultTableModel) customerTable.getModel();

        model.setRowCount(0);
        if (customerOrganization != null) {

            for (Employee employee : customerOrganization.getEmployeeDirectory().getEmployeeList()) {
                Object[] row = new Object[4];
                Customer cust = (Customer) employee;
                row[0] = cust.getId();
                row[1] = cust;
                row[2] = cust.getContactNo();
                row[3] = cust.getEmailId();
                model.addRow(row);
            }
            note.setText("");
        } else {
            note.setText("*Note : No Customers Available");
        }
    }

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

        jLabel1 = new javax.swing.JLabel();
        backJButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        customerTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        note = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Usage Pattern");

        backJButton.setText("<< Back");
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        customerTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null },
                        { null, null, null, null } },
                new String[] { "Customer ID", "Name", "Contact", "Email ID" }));
        customerTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                customerTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(customerTable);

        jLabel2.setText("Customer Details");

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("Water Usage");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton2.setText("Flow Rate");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton3.setText("Pie Chart");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("*Water Usage trend over a period of time specific to the customer");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("*Variation in flow rate over a period of time specific to the customer");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("*Summarized view of total Water Usage per customer");

        note.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGap(385, 385, 385).addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup().addGap(38, 38, 38).addComponent(backJButton))
                        .addGroup(layout.createSequentialGroup().addGap(65, 65, 65)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 873,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(
                                                note, javax.swing.GroupLayout.PREFERRED_SIZE, 211,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup().addGap(223, 223, 223)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3).addComponent(jLabel4).addComponent(jLabel5))
                                .addGap(32, 32, 32).addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 111,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(99, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(41, 41, 41).addComponent(jLabel1).addGap(18, 18, 18)
                        .addComponent(jLabel2).addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(note, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addGroup(layout.createSequentialGroup().addComponent(jButton1).addGap(40, 40, 40)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton2).addComponent(jLabel4))))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3).addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
                        .addComponent(backJButton).addGap(49, 49, 49)));
    }// </editor-fold>//GEN-END:initComponents

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

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

    private void customerTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_customerTableMouseClicked

    }//GEN-LAST:event_customerTableMouseClicked

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

        int selectedRow = customerTable.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Select a customer first");
            return;
        }

        Customer customer = (Customer) customerTable.getValueAt(selectedRow, 1);
        if (customer.getSourceSensor() == null) {
            JOptionPane.showMessageDialog(null, "Water Supply not available yet for this customer");
            return;
        }
        if (customer.getWaterUsageHistory().size() == 0) {
            JOptionPane.showMessageDialog(null, "No water usage yet");
            return;
        }
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (WaterUsage waterUsage : customer.getWaterUsageHistory()) {
            dataset.setValue(waterUsage.getUsageVolume(), waterUsage.getDate(), "Usage Volume");
        }

        JFreeChart chart = ChartFactory.createBarChart("Customer Water Usage Trends over a period of time",
                "Time of Usage", "Gallons", dataset, PlotOrientation.VERTICAL, true, true, true);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.cyan);
        ChartFrame frame = new ChartFrame("Bar Char for Weight", chart);

        frame.setVisible(true);
        frame.setSize(450, 350);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int selectedRow = customerTable.getSelectedRow();
        if (selectedRow < 0) {
            JOptionPane.showMessageDialog(null, "Select a customer first");
            return;
        }

        Customer customer = (Customer) customerTable.getValueAt(selectedRow, 1);
        if (customer.getSourceSensor() == null) {
            JOptionPane.showMessageDialog(null, "Water Supply not available yet for this customer");
            return;
        }
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (SensorValue sensorValue : customer.getTargetSensor().getSensorValueList()) {
            dataset.addValue(sensorValue.getFlowrate(), "Date", sensorValue.getDate());
        }
        if (customer.getTargetSensor().getSensorValueList().size() == 1) {
            JFreeChart chart = ChartFactory.createBarChart(
                    "Customer's water flowrate variation over a period of time", "Time of Usage",
                    "FlowRate(gallons/sec)", dataset, PlotOrientation.VERTICAL, true, true, true);
            CategoryPlot p = chart.getCategoryPlot();
            p.setRangeGridlinePaint(Color.cyan);
            ChartFrame frame = new ChartFrame("Bar Char for Weight", chart);

            frame.setVisible(true);
            frame.setSize(450, 350);
        } else {
            JFreeChart chart = ChartFactory.createLineChart(
                    "Customer's water flowrate variation over a period of time", "Time of Usage",
                    "FlowRate(gallons/sec)", dataset, PlotOrientation.VERTICAL, true, true, true);
            CategoryPlot p = chart.getCategoryPlot();
            p.setRangeGridlinePaint(Color.cyan);
            ChartFrame frame = new ChartFrame("Bar Char for Weight", chart);
            RefineryUtilities.centerFrameOnScreen(frame);

            frame.setVisible(true);
            frame.setSize(450, 350);
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int total = 0;

        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof CustomerOrganization) {
                for (Employee employee : organization.getEmployeeDirectory().getEmployeeList()) {
                    Customer customer = (Customer) employee;
                    total += customer.getTotalUsageVolume();
                }
            }
        }
        if (total == 0) {
            JOptionPane.showMessageDialog(null, "No Customer has used water yet");
            return;
        }
        DefaultPieDataset dataset = new DefaultPieDataset();
        for (Organization organization : enterprise.getOrganizationDirectory().getOrganizationList()) {
            if (organization instanceof CustomerOrganization) {
                for (Employee employee : organization.getEmployeeDirectory().getEmployeeList()) {
                    Customer customer = (Customer) employee;
                    dataset.setValue(customer.getName(), customer.getTotalUsageVolume());
                }
            }
        }

        JFreeChart chart = ChartFactory.createPieChart3D("Customer Water Usage", dataset, true, true, true);
        ChartFrame frame = new ChartFrame("Pie Chart demonstrating customer water usage ", chart);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(200);
        plot.setForegroundAlpha(0.50f);
        RefineryUtilities.centerFrameOnScreen(frame);
        frame.setVisible(true);
        frame.setSize(650, 550);
    }//GEN-LAST:event_jButton3ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backJButton;
    private javax.swing.JTable customerTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel note;
    // End of variables declaration//GEN-END:variables
}