Newer
Older
COMP101_JDBC_demo / src / main / java / ui / WaterQualityReporter.java
/*
 * 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 ui;

import data.DatabaseUtilities;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.text.DefaultCaret;
import data.QueryCallback;

/**
 *
 * @author gdick
 */
public class WaterQualityReporter extends javax.swing.JFrame implements QueryCallback {

    /**
     * Creates new form WaterQualityReporter
     */
    public WaterQualityReporter() {
        initComponents();
        DefaultCaret caret = (DefaultCaret) txtReportArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        setLocationRelativeTo(null);
    }

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

        jMenu1 = new javax.swing.JMenu();
        buttonRunQuery = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtReportArea = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        menuItemExit = new javax.swing.JMenuItem();

        jMenu1.setText("jMenu1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JDBC Client Example - Water Quality Reporter");

        buttonRunQuery.setText("Run Query");
        buttonRunQuery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRunQueryActionPerformed(evt);
            }
        });

        txtReportArea.setEditable(false);
        txtReportArea.setColumns(20);
        txtReportArea.setFont(new java.awt.Font("Courier New", 1, 24)); // NOI18N
        txtReportArea.setRows(5);
        txtReportArea.setAutoscrolls(false);
        jScrollPane1.setViewportView(txtReportArea);

        menuFile.setText("File");

        menuItemExit.setText("Exit");
        menuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemExitActionPerformed(evt);
            }
        });
        menuFile.add(menuItemExit);

        jMenuBar1.add(menuFile);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonRunQuery)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonRunQuery)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemExitActionPerformed
        dispose();
    }//GEN-LAST:event_menuItemExitActionPerformed

    private void buttonRunQueryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRunQueryActionPerformed
        DatabaseUtilities.queryDB(this);
    }//GEN-LAST:event_buttonRunQueryActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws InterruptedException, InvocationTargetException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(WaterQualityReporter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(WaterQualityReporter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(WaterQualityReporter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(WaterQualityReporter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        java.awt.EventQueue.invokeAndWait(() -> {
            new LoginDialog(null, true).setVisible(true);
        });

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new WaterQualityReporter().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonRunQuery;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenuItem menuItemExit;
    private javax.swing.JTextArea txtReportArea;
    // End of variables declaration//GEN-END:variables

    Map<String, Map<String, Double>> reportingData = new TreeMap<>();

    @Override
    public void clearDetails() {
        txtReportArea.setText("Period    Alexandra   Dunedin   Tekapo\n");
        txtReportArea.append("-------   ---------   -------   ------\n");
        reportingData.clear();
    }

    @Override
    public void pushUpdate(String period, String region, double recording) {
        if (!reportingData.containsKey(period)) {
            reportingData.put(period, new HashMap<>());
        }
        reportingData.get(period).put(region, recording);
    }

    @Override
    public void finished() {
        reportingData.entrySet().forEach((entry) -> {
            String period = entry.getKey();
            double alexandra = entry.getValue().get("Alexandra");
            double dunedin = entry.getValue().get("Dunedin");
            double tekapo = entry.getValue().get("Tekapo");

            txtReportArea.append(String.format("%7s   %9.1f   %7.1f   %6.1f\n",
                    period, alexandra, dunedin, tekapo));
        });
    }
}