item.analysis.report.MainFrame.java Source code

Java tutorial

Introduction

Here is the source code for item.analysis.report.MainFrame.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 item.analysis.report;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractSeriesDataset;

/**
 *
 * @author Mikus
 */
public class MainFrame extends javax.swing.JFrame {
    private static ReportGenerator repGen;

    private static JFreeChart chart;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        filenameText1.setEditable(false);
        setLocationRelativeTo(null);
        repGen = null;
        fileChooser1.setCurrentDirectory(new File(System.getProperty("user.dir")));

    }

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

        fileChooser1 = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        filenameText1 = new javax.swing.JTextField();
        browseButton1 = new javax.swing.JButton();
        generateButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        passingGradeText1 = new javax.swing.JTextField();
        chartPanel = new javax.swing.JPanel();
        generateReportCheckbox = new javax.swing.JCheckBox();
        itemChartSelector = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Report Generator");
        setResizable(false);

        jLabel1.setText("Filename: ");

        browseButton1.setText("Browse");
        browseButton1.setActionCommand("browseFiles");
        browseButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButton1ActionPerformed(evt);
            }
        });

        generateButton.setText("Score Report");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Passing Grade:");

        chartPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        chartPanel.setToolTipText("");

        javax.swing.GroupLayout chartPanelLayout = new javax.swing.GroupLayout(chartPanel);
        chartPanel.setLayout(chartPanelLayout);
        chartPanelLayout.setHorizontalGroup(chartPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 581, Short.MAX_VALUE));
        chartPanelLayout.setVerticalGroup(chartPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 264, Short.MAX_VALUE));

        generateReportCheckbox.setSelected(true);
        generateReportCheckbox.setText("Generate PDF Report ");
        generateReportCheckbox.setToolTipText("Untick to only view the \"choice bargraph\"");
        generateReportCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateReportCheckboxActionPerformed(evt);
            }
        });

        itemChartSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemChartSelectorActionPerformed(evt);
            }
        });

        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(
                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(generateButton))
                                .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel2).addComponent(jLabel1))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(filenameText1,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 468,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(browseButton1))
                                                .addComponent(passingGradeText1,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 63,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 46, Short.MAX_VALUE))
                                .addGroup(layout
                                        .createSequentialGroup().addComponent(generateReportCheckbox)
                                        .addGap(0, 0, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(itemChartSelector,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 169,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(filenameText1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(browseButton1))
                                .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(passingGradeText1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(generateReportCheckbox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                        .addComponent(itemChartSelector, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chartPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(generateButton).addContainerGap()));

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

    private void browseButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton1ActionPerformed
        int returnVal = fileChooser1.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser1.getSelectedFile();
            filenameText1.setText(file.getAbsolutePath());
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_browseButton1ActionPerformed

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        //compute data for report
        String args[] = new String[2];
        args[0] = filenameText1.getText();
        args[1] = passingGradeText1.getText();

        if (repGen == null) {
            repGen = new ReportGenerator(args);
        } else {
            repGen.setArgs(args);
        }

        try {
            repGen.generateReport(generateReportCheckbox.isSelected(), itemChartSelector, chart, chartPanel);
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_generateButtonActionPerformed

    private void generateReportCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateReportCheckboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_generateReportCheckboxActionPerformed

    private void itemChartSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemChartSelectorActionPerformed
        if (itemChartSelector.getModel().getSize() < 1)
            return;

        //System.out.println("selected: " + itemChartSelector.getSelectedItem() + " (" + itemChartSelector.getSelectedIndex() + ")");
        int index = itemChartSelector.getSelectedIndex();
        CategoryDataset dataset = repGen.createDataset(index);
        chartPanel.removeAll();
        chart = repGen.createChart(dataset, index);

        ChartPanel chartPanelOuter = new ChartPanel(chart);
        chartPanelOuter.setFillZoomRectangle(true);
        chartPanelOuter.setMouseWheelEnabled(true);
        chartPanelOuter.setPreferredSize(new Dimension(585, 268));

        chartPanel.setLayout(new BorderLayout());
        chartPanel.add(chartPanelOuter, BorderLayout.CENTER);
        chartPanel.validate();
    }//GEN-LAST:event_itemChartSelectorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* 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 {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton1;
    private javax.swing.JPanel chartPanel;
    private javax.swing.JFileChooser fileChooser1;
    private javax.swing.JTextField filenameText1;
    private javax.swing.JButton generateButton;
    private javax.swing.JCheckBox generateReportCheckbox;
    private javax.swing.JComboBox itemChartSelector;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField passingGradeText1;
    // End of variables declaration//GEN-END:variables
}