Java tutorial
/* * 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 Gui.loading_frames; import exceptions.BadFileTypeException; import DB_connection.FixValues; import DB_connection.DBConnection; import DB_data_loader.StoreDatatoDB; import ExcelComponents.CSVSheetOpener; import ExcelComponents.ExcelAutoLoader; import ExcelComponents.ExcelSheetOpener; import ExcelComponents.FileOpener; import ExcelComponents.SpreadSheetOpener; import exceptions.BadDateInputException; import exceptions.BadTimeInputException; import exceptions.CouldntStoreDataException; import Gui.panels.error_panels.ErrorPopup; import exceptions.NoFileSelectedException; import exceptions.NoSuchSheetException; import java.io.File; import java.io.FileNotFoundException; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FilenameUtils; import Gui.panels.error_panels.Popup; /** * * @author Paris */ public class AutoLoadExcelDataGui extends javax.swing.JFrame { /** * Creates new form LoadExcelDataGui */ DBConnection dbconn; File[] sheetfiles = new File[0]; SpreadSheetOpener sheetopener; public AutoLoadExcelDataGui(DBConnection dbconn) { this.dbconn = dbconn; initComponents(); setLocation(400, 0); } /** * 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() { open_sheet_button = new javax.swing.JButton(); progress_bar = new javax.swing.JProgressBar(); progress_textfield = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); open_sheet_button.setText("Load Excel File"); open_sheet_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { open_sheet_buttonActionPerformed(evt); } }); progress_textfield.setEditable(false); progress_textfield.setText("Waiting"); 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(progress_bar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(progress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(open_sheet_button)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(22, 22, 22).addComponent(open_sheet_button) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(progress_bar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(progress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void open_sheet_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_open_sheet_buttonActionPerformed setEnabled(false); sheetfiles = FileOpener.openfiles(); if (sheetfiles != null && sheetfiles[0] != null) { if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("xls")) { try { sheetopener = new ExcelSheetOpener(0, sheetfiles[0]); autoload(); } catch (NoSuchSheetException ex) { Logger.getLogger(AutoLoadExcelDataGui.class.getName()).log(Level.SEVERE, null, ex); } } else if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("csv")) { try { sheetopener = new CSVSheetOpener(sheetfiles[0]); throw new Exception("Operation not yet supported"); } catch (FileNotFoundException ex) { Logger.getLogger(AutoLoadExcelDataGui.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(AutoLoadExcelDataGui.class.getName()).log(Level.SEVERE, null, ex); } } else { try { throw new BadFileTypeException(); } catch (BadFileTypeException ex) { ErrorPopup.popup("File selected was not of excel format"); setEnabled(true); return; } } } setEnabled(true); }//GEN-LAST:event_open_sheet_buttonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton open_sheet_button; private javax.swing.JProgressBar progress_bar; private javax.swing.JTextField progress_textfield; // End of variables declaration//GEN-END:variables private void autoload() { Thread autoloader = new Thread() { public void run() { progress_textfield.setText("0/" + sheetfiles.length); progress_textfield.repaint(); for (int i = 0; i < sheetfiles.length; i++) { ExcelAutoLoader al = new ExcelAutoLoader(sheetfiles[i], progress_bar); progress_textfield.setText(i + 1 + "/" + sheetfiles.length); progress_textfield.repaint(); } progress_bar.setValue(0); progress_textfield.setText("Waiting"); progress_textfield.repaint(); setEnabled(true); } }; autoloader.start(); } }