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 Gui.panels.db_panels.AddNewElectricalItems; import exceptions.BadFileTypeException; import Gui.panels.db_panels.ChoosingPanel; import DB_connection.FixValues; import DB_connection.DBConnection; import DB_data_loader.LoadDataFromDB; import DB_data_loader.StaticCachedData; 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 javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.table.DefaultTableModel; import org.apache.commons.io.FilenameUtils; import Gui.panels.error_panels.Popup; /** * * @author Paris */ public class LoadExcelDataGui extends javax.swing.JFrame { /** * Creates new form LoadExcelDataGui */ DBConnection dbconn; ChoosingPanel cp; File[] sheetfiles = new File[0]; SpreadSheetOpener sheetopener; public LoadExcelDataGui(DBConnection dbconn) { this.dbconn = dbconn; initComponents(); cp = new ChoosingPanel(); cp.setVisible(true); add(cp); 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() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); sample_data_table = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); open_sheet_button = new javax.swing.JButton(); sheet_number_spinner = new javax.swing.JSpinner(); jLabel2 = new javax.swing.JLabel(); pass_data_to_transformer_button = new javax.swing.JButton(); pass_data_to_breaker_button = new javax.swing.JButton(); add_new_electrical_items = new javax.swing.JButton(); progress_bar = new javax.swing.JProgressBar(); sheet_name_textfield = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); progress_textfield = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setLayout(new java.awt.BorderLayout()); sample_data_table .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[] { "Title 1", "Title 2", "Title 3", "Title 4" })); jScrollPane1.setViewportView(sample_data_table); jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER); jLabel1.setText("Sample Data"); open_sheet_button.setText("Open File"); open_sheet_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { open_sheet_buttonActionPerformed(evt); } }); sheet_number_spinner.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { sheet_number_spinnerStateChanged(evt); } }); jLabel2.setText("Sheet Number"); pass_data_to_transformer_button.setText("Pass to Transformer"); pass_data_to_transformer_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pass_data_to_transformer_buttonActionPerformed(evt); } }); pass_data_to_breaker_button.setText("Pass to Breaker"); pass_data_to_breaker_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pass_data_to_breaker_buttonActionPerformed(evt); } }); add_new_electrical_items.setText("add new items"); add_new_electrical_items.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { add_new_electrical_itemsActionPerformed(evt); } }); sheet_name_textfield.setEditable(false); jButton1.setText("AutoLoad(Beta)"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(add_new_electrical_items, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(pass_data_to_transformer_button) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pass_data_to_breaker_button) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sheet_name_textfield) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(open_sheet_button, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup().addComponent(jLabel2) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(sheet_number_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))))) .addGroup(layout.createSequentialGroup().addGap(20, 20, 20).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(progress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(progress_bar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(open_sheet_button) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(sheet_number_spinner, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(sheet_name_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(pass_data_to_transformer_button) .addComponent(pass_data_to_breaker_button).addComponent(add_new_electrical_items) .addComponent(jButton1)) .addGap(18, 18, 18) .addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(progress_bar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(progress_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents void load_temp_data() { String columnnames[] = sheetopener.getrow(0, 2, 0); String[][] data = sheetopener.getdata(0, 2, 1, 11); DefaultTableModel dtb = new DefaultTableModel(data, columnnames); sample_data_table.setModel(dtb); revalidate(); } 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]); sheet_name_textfield.setText(((ExcelSheetOpener) sheetopener).getsheetname()); } catch (NoSuchSheetException ex) { Logger.getLogger(LoadExcelDataGui.class.getName()).log(Level.SEVERE, null, ex); } } else if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("csv")) { try { sheetopener = new CSVSheetOpener(sheetfiles[0]); } catch (FileNotFoundException ex) { Logger.getLogger(LoadExcelDataGui.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; } } progress_bar.setValue(50); load_temp_data(); progress_bar.setValue(0); } setEnabled(true); }//GEN-LAST:event_open_sheet_buttonActionPerformed private void pass_data_to_transformer_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pass_data_to_transformer_buttonActionPerformed setEnabled(false); pass_data_to_Transformer(); StaticCachedData.cacheupdateevent(); progress_bar.setValue(0); setEnabled(true); }//GEN-LAST:event_pass_data_to_transformer_buttonActionPerformed private void sheet_number_spinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sheet_number_spinnerStateChanged if (sheetfiles[0] != null) { if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("xls")) { if ((int) sheet_number_spinner.getValue() < 0) { sheet_number_spinner.setValue(0); } else { try { sheetopener = new ExcelSheetOpener((int) sheet_number_spinner.getValue(), sheetfiles[0]); sheet_name_textfield.setText(((ExcelSheetOpener) sheetopener).getsheetname()); } catch (NoSuchSheetException ex) { sheet_number_spinner.setValue((int) sheet_number_spinner.getValue() - 1); return; } load_temp_data(); } } } else { sheet_number_spinner.setValue(0); } }//GEN-LAST:event_sheet_number_spinnerStateChanged private void pass_data_to_breaker_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pass_data_to_breaker_buttonActionPerformed setEnabled(false); pass_data_to_Breaker(); StaticCachedData.cacheupdateevent(); progress_bar.setValue(0); setEnabled(true); }//GEN-LAST:event_pass_data_to_breaker_buttonActionPerformed private void add_new_electrical_itemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_new_electrical_itemsActionPerformed JFrame parent = this; Thread addingwindow = new Thread() { @Override public void run() { AddNewElectricalItems a = new AddNewElectricalItems(); a.setDefaultCloseOperation(DISPOSE_ON_CLOSE); a.setVisible(true); a.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent windowEvent) { parent.setEnabled(true); parent.toFront(); parent.repaint(); } }); } }; setEnabled(false); addingwindow.start(); try { LoadDataFromDB.loadall(); } catch (InterruptedException ex) { Logger.getLogger(LoadExcelDataGui.class.getName()).log(Level.SEVERE, null, ex); } cp.refresh(); }//GEN-LAST:event_add_new_electrical_itemsActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed setEnabled(false); autoload(); }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton add_new_electrical_items; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton open_sheet_button; private javax.swing.JButton pass_data_to_breaker_button; private javax.swing.JButton pass_data_to_transformer_button; private javax.swing.JProgressBar progress_bar; private javax.swing.JTextField progress_textfield; private javax.swing.JTable sample_data_table; private javax.swing.JTextField sheet_name_textfield; private javax.swing.JSpinner sheet_number_spinner; // End of variables declaration//GEN-END:variables private void pass_data_to_Breaker() { if (sheetopener != null) { int errors = 0; boolean fatalerror = false; String data; if (sheetopener.getClass() == ExcelSheetOpener.class) { for (int i = 1; i < sheetopener.max_row; i++) { try { String[] breakerdata = sheetopener.getrow(0, 2, i); if (!breakerdata[1].equals("") && !breakerdata[2].equals("")) { data = "'" + FixValues.reversedate(breakerdata[1], '/', ':') + "'" + "," + breakerdata[2].replace(',', '.') + "," + cp.selected_breaker.id; StoreDatatoDB.store("Breaker_data", data); } else { errors++; } } catch (BadDateInputException ex) { errors++; } catch (BadTimeInputException ex) { errors++; } catch (CouldntStoreDataException ex) { errors++; } if (i % 100 == 0) { progress_bar.setValue((int) i / (sheetopener.max_row / 100)); progress_bar.update(progress_bar.getGraphics()); } } } if (sheetopener.getClass() == CSVSheetOpener.class) { for (int i = 1; i < sheetopener.max_row; i++) { try { String[] breakerdata = sheetopener.getrow(0, 2, i); if (!breakerdata[0].equals("") && !breakerdata[1].equals("")) { data = "'" + breakerdata[0] + ":00'" + "," + breakerdata[1].replace(',', '.') + "," + cp.selected_breaker.id; StoreDatatoDB.store("Breaker_data", data); } else { errors++; } } catch (CouldntStoreDataException ex) { errors++; } if (i % 100 == 0) { progress_bar.setValue((int) i / (sheetopener.max_row / 100)); } } } if (fatalerror == false) { System.out.println("query completed sucesfully"); System.out.println("errors #" + errors); System.out.println("sucesses #" + (sheetopener.max_row - errors)); Popup.popup( "Passed Succesfully " + (sheetopener.max_row - errors) + " from " + sheetopener.max_row); } } else { ErrorPopup.popup(new NoFileSelectedException()); } } private void pass_data_to_Transformer() { if (sheetopener != null) { int errors = 0; boolean fatalerror = false; String data; if (sheetopener.getClass() == ExcelSheetOpener.class) { for (int i = 1; i < sheetopener.max_row; i++) { try { String[] transformerdata = sheetopener.getrow(0, 2, i); if (!transformerdata[1].equals("") && !transformerdata[2].equals("")) { data = "'" + FixValues.reversedate(transformerdata[1], '/', ':') + "'" + "," + transformerdata[2].replace(',', '.') + "," + cp.selected_transformer.id; StoreDatatoDB.store("Transformer_data", data); } else { errors++; } } catch (BadDateInputException ex) { errors++; } catch (BadTimeInputException ex) { errors++; } catch (CouldntStoreDataException ex) { errors++; } if (i % 100 == 0) { progress_bar.setValue((int) i / (sheetopener.max_row / 100)); progress_bar.update(progress_bar.getGraphics()); } } } if (sheetopener.getClass() == CSVSheetOpener.class) { for (int i = 1; i < sheetopener.max_row; i++) { try { String[] transformerdata = sheetopener.getrow(0, 2, i); if (!transformerdata[0].equals("") && !transformerdata[1].equals("")) { data = "'" + transformerdata[0] + ":00'" + "," + transformerdata[1].replace(',', '.') + "," + cp.selected_transformer.id; StoreDatatoDB.store("Transformer_data", data); } else { errors++; } } catch (CouldntStoreDataException ex) { errors++; } if (i % 128 == 0) { progress_bar.setValue((int) i / (sheetopener.max_row / 100)); progress_bar.update(progress_bar.getGraphics()); } } } if (fatalerror == false) { System.out.println("querycompleted sucesfully"); System.out.println("errrors #" + errors); System.out.println("sucesses #" + (sheetopener.max_row - errors)); Popup.popup( "Passed Succesfully " + (sheetopener.max_row - errors) + " from " + sheetopener.max_row); } } else { ErrorPopup.popup(new NoFileSelectedException()); } } private void autoload() { Thread autoloader = new Thread() { public void run() { progress_textfield.setText("0/" + sheetfiles.length); progress_textfield.repaint(); if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("xls")) { 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(); } } else if (FilenameUtils.getExtension(sheetfiles[0].getPath()).equals("csv")) { for (int i = 0; i < sheetfiles.length; i++) { try { sheetopener = new CSVSheetOpener(sheetfiles[i]); pass_data_to_Breaker(); } catch (FileNotFoundException ex) { ErrorPopup.popup(ex); } 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(); } }