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 converttooem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Calendar; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JTextField; import javax.swing.LayoutStyle; import javax.swing.WindowConstants; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NotOLE2FileException; /** * * @author Ralph Landon */ public class ConvertToOemFrame extends javax.swing.JFrame { File selectedFile; /** * Creates new form ConvertToOemFrame */ public ConvertToOemFrame() { initComponents(); selectedFile = 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() { selectFileButton = new JButton(); fileLabel = new JLabel(); progressBar = new JProgressBar(); convertButton = new JButton(); jLabel1 = new JLabel(); yellowMaxField = new JTextField(); jLabel3 = new JLabel(); jLabel4 = new JLabel(); jLabel5 = new JLabel(); yellowMinField = new JTextField(); jLabel6 = new JLabel(); blueMinField = new JTextField(); redMinField = new JTextField(); greenMinField = new JTextField(); jLabel2 = new JLabel(); jLabel7 = new JLabel(); jLabel8 = new JLabel(); blueMaxField = new JTextField(); greenMaxField = new JTextField(); redMaxField = new JTextField(); oxyButton = new JButton(); standardButton = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("Convert to OEM"); selectFileButton.setText("Select File"); selectFileButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { selectFileButtonActionPerformed(evt); } }); fileLabel.setText("No File Selected"); convertButton.setText("Convert"); convertButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { convertButtonActionPerformed(evt); } }); jLabel1.setText("Yellow"); yellowMaxField.setText("15"); yellowMaxField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { yellowMaxFieldActionPerformed(evt); } }); jLabel3.setText("Blue"); jLabel4.setText("Green"); jLabel5.setText("Red"); yellowMinField.setText("0"); jLabel6.setText("-"); blueMinField.setText("16"); redMinField.setText("51"); greenMinField.setText("31"); jLabel2.setText("-"); jLabel7.setText("-"); jLabel8.setText("-"); blueMaxField.setText("30"); blueMaxField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { blueMaxFieldActionPerformed(evt); } }); greenMaxField.setText("50"); redMaxField.setText("100"); oxyButton.setText("Oxy"); oxyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { oxyButtonActionPerformed(evt); } }); standardButton.setText("Standard"); standardButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { standardButtonActionPerformed(evt); } }); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(progressBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(convertButton)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jLabel5).addComponent(jLabel4)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(redMinField, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)) .addComponent(selectFileButton)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(18, 38, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(fileLabel) .addGroup(layout.createSequentialGroup() .addGap(14, 14, 14) .addGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING, false).addComponent(blueMaxField) .addComponent(yellowMaxField, GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE) .addComponent(greenMaxField) .addComponent(redMaxField))))) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(jLabel8).addComponent(jLabel7)) .addGap(0, 0, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(greenMinField, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE) .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jLabel3).addComponent(jLabel1)) .addGroup(layout .createParallelGroup(GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGap(4, 4, 4) .addComponent(yellowMinField)) .addGroup(layout.createSequentialGroup() .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(blueMinField, GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE))))) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(jLabel6, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(0, 0, Short.MAX_VALUE))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(standardButton) .addComponent(oxyButton, GroupLayout.Alignment.TRAILING)))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(39, 39, 39) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(selectFileButton).addComponent(fileLabel)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel1) .addComponent(yellowMaxField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(yellowMinField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6).addComponent(oxyButton)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup( layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel3) .addComponent(blueMinField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2) .addComponent(blueMaxField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(standardButton)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel4) .addComponent(greenMinField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7).addComponent(greenMaxField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(9, 9, 9) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(jLabel5) .addComponent(redMinField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8).addComponent(redMaxField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE) .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(convertButton) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void selectFileButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_selectFileButtonActionPerformed JFileChooser fc = new JFileChooser("Select a file"); fc.showDialog(this, "Open File"); selectedFile = fc.getSelectedFile(); if (selectedFile != null && selectedFile.exists()) { fileLabel.setText(selectedFile.getAbsolutePath()); pack(); } }//GEN-LAST:event_selectFileButtonActionPerformed private void convertButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed progressBar.setValue(0); if (selectedFile != null && selectedFile.exists()) { String outFileName = selectedFile.getAbsolutePath().replace(".xls", ".dat"); try { //Open the xls file HSSFWorkbook wb = null; try { wb = new HSSFWorkbook(new FileInputStream(selectedFile)); } catch (NotOLE2FileException e) { JOptionPane.showMessageDialog(this, "That file doesn't appear to be a '.xls' file!", "Error", JOptionPane.ERROR_MESSAGE); } if (wb == null) { return; } //Open the second(1) sheet HSSFSheet sheet = wb.getSheetAt(1); //Pull scan meta data ScanData sd = new ScanData(); sd.scanner = sheet.getRow(11).getCell(8).getStringCellValue(); sd.mileage = sheet.getRow(37).getCell(4).getNumericCellValue(); sd.standby = sheet.getRow(38).getCell(4).getNumericCellValue(); sheet = wb.getSheetAt(3); sd.scanPrice = sheet.getRow(32).getCell(4).getNumericCellValue(); sd.totalPrice = sd.scanPrice + sd.mileage + sd.standby + 50.0; //Open the first(0) sheet sheet = wb.getSheetAt(0); sd.numberOfJoints = (int) sheet.getRow(107).getCell(15).getNumericCellValue(); sd.company = sheet.getRow(2).getCell(9).getStringCellValue(); System.out.println("Company: " + sd.company); sd.well = sheet.getRow(2).getCell(5).getStringCellValue(); sd.companyMan = sheet.getRow(36).getCell(15).getStringCellValue(); sd.size = "" + sheet.getRow(36).getCell(2).getNumericCellValue(); Calendar c = Calendar.getInstance(); c.setTimeInMillis(selectedFile.lastModified()); sd.date = c.getTime(); sd.grade = sheet.getRow(36).getCell(11).getStringCellValue(); sd.pullingUnit = sheet.getRow(36).getCell(7).getStringCellValue(); System.out.println(sd); progressBar.setValue(10); PrintWriter out = new PrintWriter(new FileWriter(new File(outFileName))); out.println("2"); out.println(sd.numberOfJoints); out.println("3"); out.println("2"); //Yellow Range int yellowMin = getInt(yellowMinField); int yellowMax = getInt(yellowMaxField); out.println(yellowMin); out.println(yellowMax); //Blue Range int blueMin = getInt(blueMinField); int blueMax = getInt(blueMaxField); out.println(blueMin); out.println(blueMax); //Green Range int greenMin = getInt(greenMinField); int greenMax = getInt(greenMaxField); out.println(greenMin); out.println(greenMax); //Red Range int redMin = getInt(redMinField); int redMax = getInt(redMaxField); out.println(redMin); out.println(redMax); //Date String dateString = String.format("%s-%s-%s", "" + c.get(Calendar.YEAR), (sd.date.getMonth() + 1 < 10 ? "0" + (sd.date.getMonth() + 1) : "" + sd.date.getMonth() + 1), (sd.date.getDate() < 10 ? "0" + sd.date.getDate() : "" + sd.date.getMonth())); System.out.println(c.get(Calendar.YEAR)); System.out.println(c.get(Calendar.MONTH + 1)); System.out.println(c.get(Calendar.DATE)); System.out.println(dateString); out.println(dateString); //Name out.println(selectedFile.getName().replace(".xls", "")); out.println(); out.println(); out.println(sd.grade); out.println(); out.println(sd.company); out.println(sd.well); out.println("Company man: " + sd.companyMan); out.println(); out.println(); out.println("Stealth Oilwell Services"); out.println("7668 W. 42ND Odessa, TX 79764"); out.println("Office: 432-333-3600"); out.println("Scan Tech: " + sd.scanner); out.println(); out.println("Scan: " + sd.scanPrice); out.println("Mileage: " + sd.mileage); out.println("Standby: " + sd.standby); out.println("Rubber Charge: $50.00"); out.println("Total: " + sd.totalPrice); out.println(); out.println("THIS IS NOT AN INVOICE"); for (int i = 0; i < 15; i++) { out.println(); } out.println("HOLE-PIT"); out.println("RODWEAR-SPLIT"); out.println("COLOR"); for (int i = 0; i < 4; i++) { out.println(); } progressBar.setValue(20); System.out.println(sd.numberOfJoints); int onRow = 113; for (int i = 0; i < sd.numberOfJoints; i++) { int pit = ((int) (sheet.getRow(onRow).getCell(2).getNumericCellValue() * 100.0)); int wear = ((int) (sheet.getRow(onRow).getCell(11).getNumericCellValue() * 100.0)); int worst = Math.max(pit, wear); out.println(pit); out.println(wear); if (worst >= yellowMin && worst <= yellowMax) { out.println("YELLOW"); } else if (worst >= blueMin && worst <= blueMax) { out.println("BLUE"); } else if (worst >= greenMin && worst <= greenMax) { out.println("GREEN"); } else if (worst >= redMin && worst <= redMax) { out.println("RED"); } else { out.println("UNDEFINED"); } for (int j = 0; j < 4; j++) { out.println(); } onRow++; switch (onRow) { case 168: onRow = 175; break; case 225: onRow = 232; break; case 282: onRow = 289; break; case 339: onRow = 346; break; case 396: onRow = 403; break; case 453: onRow = 460; break; default: break; } progressBar.setValue(20 + ((int) ((i / sd.numberOfJoints) * 75))); } wb.close(); out.flush(); out.close(); progressBar.setValue(100); System.out.println("Done"); } catch (IOException ex) { Logger.getLogger(ConvertToOemFrame.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_convertButtonActionPerformed private void yellowMaxFieldActionPerformed(ActionEvent evt) {//GEN-FIRST:event_yellowMaxFieldActionPerformed // TODO add your handling code here: }//GEN-LAST:event_yellowMaxFieldActionPerformed private void blueMaxFieldActionPerformed(ActionEvent evt) {//GEN-FIRST:event_blueMaxFieldActionPerformed // TODO add your handling code here: }//GEN-LAST:event_blueMaxFieldActionPerformed private void oxyButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_oxyButtonActionPerformed yellowMinField.setText("0"); yellowMaxField.setText("22"); blueMinField.setText("0"); blueMaxField.setText("0"); greenMinField.setText("23"); greenMaxField.setText("50"); redMinField.setText("51"); redMaxField.setText("100"); }//GEN-LAST:event_oxyButtonActionPerformed private void standardButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_standardButtonActionPerformed yellowMinField.setText("0"); yellowMaxField.setText("15"); blueMinField.setText("16"); blueMaxField.setText("30"); greenMinField.setText("31"); greenMaxField.setText("50"); redMinField.setText("51"); redMaxField.setText("100"); }//GEN-LAST:event_standardButtonActionPerformed /** * @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 { 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(ConvertToOemFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ConvertToOemFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ConvertToOemFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ConvertToOemFrame.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 ConvertToOemFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private JTextField blueMaxField; private JTextField blueMinField; private JButton convertButton; private JLabel fileLabel; private JTextField greenMaxField; private JTextField greenMinField; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JLabel jLabel4; private JLabel jLabel5; private JLabel jLabel6; private JLabel jLabel7; private JLabel jLabel8; private JButton oxyButton; private JProgressBar progressBar; private JTextField redMaxField; private JTextField redMinField; private JButton selectFileButton; private JButton standardButton; private JTextField yellowMaxField; private JTextField yellowMinField; // End of variables declaration//GEN-END:variables private int getInt(JTextField field) { String s = field.getText(); try { return Integer.parseInt(s); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Please nter valid integers for color ranges.", "Error", JOptionPane.ERROR_MESSAGE); } return -1; } }