namedatabasescraper.MainWindow.java Source code

Java tutorial

Introduction

Here is the source code for namedatabasescraper.MainWindow.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 namedatabasescraper;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Erik Norvelle <erik.norvelle@cyberlogos.co>
 */
public class MainWindow extends javax.swing.JFrame {

    private NameDatabaseScraper parent;
    private DefaultTableModel model;
    private Collection<File> htmlFiles;
    private List<PageScraper> scrapers;
    private int totalNames;
    private String dirname;

    /**
     * Creates new form MainWindow
     *
     * @param parent
     */
    public MainWindow(NameDatabaseScraper parent) {
        this.parent = parent;
        initComponents();
        String oldDir = this.parent.getProps().getProperty("last_directory");
        if (oldDir != null) {
            this.jDirectoryNameTextField.setText(oldDir);
            this.populateNamesColumn();
        }
        String oldSelector = this.parent.getProps().getProperty("selector");
        if (oldSelector != null) {
            this.jSelectorField.setText(oldSelector);
        }
        String oldCharset = this.parent.getProps().getProperty("charset", "UTF-8");
        if (oldCharset != null) {
            this.jCharsetField.setText(oldCharset);
        }
    }

    /**
     * A single-access point for reporting exceptions to the user.
     *
     * @param message
     */
    public void reportException(String message) {
        JOptionPane.showMessageDialog(this, Utils.wordWrapString(message, 50), "Program error",
                JOptionPane.ERROR_MESSAGE);
    }

    private void populateNamesColumn() {
        String[] extensions = { "html", "htm" };
        File dir = new File(this.jDirectoryNameTextField.getText());
        if (dir.exists()) {
            this.htmlFiles = FileUtils.listFiles(dir, extensions, false);
            this.model = new DefaultTableModel();
            model.setNumRows(htmlFiles.size());
            model.setColumnCount(2);
            String[] columnNames = { "File Names", "Number of Words Extracted" };
            model.setColumnIdentifiers(columnNames);
            int rowCount = 0;
            for (File htmlFile : this.htmlFiles) {
                model.setValueAt(htmlFile.getName(), rowCount, 0);
                model.setValueAt("0", rowCount, 1);
                rowCount++;
            }
            this.jResultsTable.setModel(model);
            this.dirname = dir.getName();
        }
    }

    private void runScraper() {
        this.scrapers = new ArrayList<>();
        final String selector = this.jSelectorField.getText();
        final String charset = this.jCharsetField.getText();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                int rowCount = 0;
                for (File htmlFile : htmlFiles) {
                    try {
                        PageScraper scraper = new PageScraper(htmlFile, dirname, selector, charset);
                        model.setValueAt(scraper, rowCount, 1);
                        scrapers.add(scraper);
                        totalNames += scraper.getNames().size();
                    } catch (IOException ex) {
                        scrapers.add(null);
                        model.setValueAt(ex.getMessage(), rowCount, 1);
                    }
                    rowCount++;
                }
                jStoreToDatabaseButton.setEnabled(true);
            }
        });
    }

    private void saveToDb() {
        StringBuilder builder = new StringBuilder();
        final File csvFile = new File(this.jDirectoryNameTextField.getText() + File.separator + "names.txt");

        for (PageScraper scraper : scrapers) {
            builder.append(scraper.storeToCsv());
        }
        try {
            FileUtils.writeStringToFile(csvFile, builder.toString());
        } catch (IOException ex) {
            NameDatabaseScraper.reportException(ex);
            return;
        }
        jStoreToDatabaseButton.setEnabled(false);
        this.jStatusBar.setText(String.format("Wrote %d names to %s", this.totalNames, csvFile.getAbsolutePath()));
    }

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

        jFileChooser = new javax.swing.JFileChooser();
        jLabel1 = new javax.swing.JLabel();
        jDirectoryNameTextField = new javax.swing.JTextField();
        jSelectDirectoryButton = new javax.swing.JButton();
        jRunScraperButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jResultsTable = new javax.swing.JTable();
        jStoreToDatabaseButton = new javax.swing.JButton();
        jSelectorField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jStatusBar = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCharsetField = new javax.swing.JTextField();

        jFileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Directory:");

        jSelectDirectoryButton.setText("Select Directory");
        jSelectDirectoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSelectDirectoryButtonActionPerformed(evt);
            }
        });

        jRunScraperButton.setText("Run Scraper");
        jRunScraperButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRunScraperButtonActionPerformed(evt);
            }
        });

        jResultsTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { { null, null } },
                new String[] { "File Name", "Names Extracted" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Integer.class };
            boolean[] canEdit = new boolean[] { false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jResultsTable);
        if (jResultsTable.getColumnModel().getColumnCount() > 0) {
            jResultsTable.getColumnModel().getColumn(0).setResizable(false);
            jResultsTable.getColumnModel().getColumn(1).setResizable(false);
        }

        jStoreToDatabaseButton.setText("Write to CSV");
        jStoreToDatabaseButton.setEnabled(false);
        jStoreToDatabaseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jStoreToDatabaseButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Selector");

        jStatusBar.setText("Nothing saved");
        jStatusBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setText("Charset");

        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).addComponent(jScrollPane1)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1).addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jSelectorField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 231,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jRunScraperButton).addGap(18, 18, 18)
                                                .addComponent(jLabel3)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jCharsetField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        134, Short.MAX_VALUE)
                                                .addComponent(jStoreToDatabaseButton))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jDirectoryNameTextField)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jSelectDirectoryButton)))))
                        .addContainerGap())
                .addComponent(jStatusBar, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(jDirectoryNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSelectDirectoryButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jRunScraperButton).addComponent(jStoreToDatabaseButton)
                                .addComponent(jSelectorField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2).addComponent(jLabel3).addComponent(jCharsetField,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jStatusBar)));

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

    private void jSelectDirectoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSelectDirectoryButtonActionPerformed
        File file;
        int returnVal = this.jFileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = this.jFileChooser.getSelectedFile();
        } else {
            return;
        }
        this.jDirectoryNameTextField.setText(file.getAbsolutePath());
        this.populateNamesColumn();
    }//GEN-LAST:event_jSelectDirectoryButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.parent.getProps().setProperty("last_directory", this.jDirectoryNameTextField.getText());
        this.parent.getProps().setProperty("selector", this.jSelectorField.getText());
        this.parent.getProps().setProperty("charset", this.jCharsetField.getText());
        this.parent.shutdown();
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosing

    private void jRunScraperButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRunScraperButtonActionPerformed
        this.runScraper();
    }//GEN-LAST:event_jRunScraperButtonActionPerformed

    private void jStoreToDatabaseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jStoreToDatabaseButtonActionPerformed
        this.saveToDb();
    }//GEN-LAST:event_jStoreToDatabaseButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jCharsetField;
    private javax.swing.JTextField jDirectoryNameTextField;
    private javax.swing.JFileChooser jFileChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTable jResultsTable;
    private javax.swing.JButton jRunScraperButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jSelectDirectoryButton;
    private javax.swing.JTextField jSelectorField;
    private javax.swing.JLabel jStatusBar;
    private javax.swing.JButton jStoreToDatabaseButton;
    // End of variables declaration//GEN-END:variables

}