/* * frmLoadFromDB.java * * Created on March 25, 2003, 1:17 PM */ /** * * @author Joset A. Etzel */ import javax.swing.JOptionPane; import java.awt.event.ItemEvent; import java.sql.*; import java.util.zip.*; import java.io.File; import java.util.*; import javax.swing.ImageIcon; public class frmLoadFromDB extends javax.swing.JInternalFrame { private static String strSubjectID = ""; private static String strSessionID = ""; private Connection conData; private Statement stmSQL; private boolean bolIsZip = false; /** Creates new form frmLoadFromDB */ public frmLoadFromDB() { initComponents(); setBounds(60, 30, 450, 200); setFrameIcon(new ImageIcon(frmPreferences.getInstallPath() + "iconSmall.gif")); //connect to the database try { Class.forName("org.gjt.mm.mysql.Driver"); conData = frmMain.getDatabaseConnection(); } catch (Exception e) { e.printStackTrace(); } FillCombos(); } /** 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. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; lblSubject = new javax.swing.JLabel(); cboSubject = new javax.swing.JComboBox(); cboSession = new javax.swing.JComboBox(); lblSession = new javax.swing.JLabel(); cmdCancel = new javax.swing.JButton(); cmdOK = new javax.swing.JButton(); lblPath = new javax.swing.JLabel(); txtPath = new javax.swing.JTextField(); getContentPane().setLayout(new java.awt.GridBagLayout()); setIconifiable(true); setMaximizable(true); setResizable(true); setTitle("Load Data from Database"); lblSubject.setText("Select subject: "); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5); getContentPane().add(lblSubject, gridBagConstraints); cboSubject.setMaximumSize(new java.awt.Dimension(400, 20)); cboSubject.setMinimumSize(new java.awt.Dimension(300, 20)); cboSubject.setPreferredSize(new java.awt.Dimension(300, 20)); cboSubject.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cboSubjectItemStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); getContentPane().add(cboSubject, gridBagConstraints); cboSession.setMaximumSize(new java.awt.Dimension(400, 20)); cboSession.setMinimumSize(new java.awt.Dimension(200, 20)); cboSession.setPreferredSize(new java.awt.Dimension(300, 20)); cboSession.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cboSessionActionPerformed(evt); } }); cboSession.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cboSessionItemStateChanged(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); getContentPane().add(cboSession, gridBagConstraints); lblSession.setText("Select session:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5); getContentPane().add(lblSession, gridBagConstraints); cmdCancel.setMnemonic('C'); cmdCancel.setText("Cancel"); cmdCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdCancelActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; getContentPane().add(cmdCancel, gridBagConstraints); cmdOK.setMnemonic('O'); cmdOK.setText("OK"); cmdOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdOKActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 4; getContentPane().add(cmdOK, gridBagConstraints); lblPath.setText("Path to file or archive:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 5); getContentPane().add(lblPath, gridBagConstraints); txtPath.setEditable(false); txtPath.setMaximumSize(new java.awt.Dimension(500, 20)); txtPath.setPreferredSize(new java.awt.Dimension(300, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 2; gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0); getContentPane().add(txtPath, gridBagConstraints); pack(); }//GEN-END:initComponents private void cboSessionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboSessionActionPerformed // Add your handling code here: }//GEN-LAST:event_cboSessionActionPerformed private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed try { this.setClosed(true); } //close the form catch(java.beans.PropertyVetoException e) { e.printStackTrace(); } }//GEN-LAST:event_cmdCancelActionPerformed private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOKActionPerformed //send the path, filename, and file type over to frmLoadData String strPath = ""; File jcFile; strPath = txtPath.getText(); //get the indicated path to the file frmLoadData.setPath(strPath); frmLoadData.setIsZip(bolIsZip); try { this.setClosed(true); } //send the info over to frmLoadData and close this form catch(java.beans.PropertyVetoException e) { e.printStackTrace(); } }//GEN-LAST:event_cmdOKActionPerformed private void cboSessionItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSessionItemStateChanged //show the path to the indicated data file, and the subfiles if the data file is a zipped archive String strTemp = ""; String strSQL = ""; File jcFile; ZipFile jcZipFile; //only want to do this if clicked, not if loading so make sure a real selection if ( (String)cboSession.getSelectedItem() != "" ) { if ( evt.getStateChange() == ItemEvent.SELECTED ) { strTemp = (String)cboSession.getSelectedItem(); //get the session to parse out the parts if ( strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "You must select a session before saving the data to the database.", "Selection Error", JOptionPane.ERROR_MESSAGE); return; } strSessionID = strTemp.substring(0, strTemp.indexOf(":")); //sessionID is always the first part before the : try { //get the path and show it in txtPath; show files in lstData if present strSQL = "SELECT dataFile FROM sessionData WHERE subID = '" + strSubjectID + "' AND sessionID = '" + strSessionID + "'"; ResultSet rssSubjects = conData.createStatement().executeQuery(strSQL); rssSubjects.next(); strTemp = rssSubjects.getString("dataFile"); //get the path out of the database // if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "No data path was found in the database.", "Selection Error", JOptionPane.ERROR_MESSAGE); return; } jcFile = new File(strTemp); // if (jcFile.exists() == false) { JOptionPane.showMessageDialog(null, "The file indicated in the database (" + strTemp + ") does not exist.", "File Error", JOptionPane.ERROR_MESSAGE); return; } // if (jcFile.canRead() == false) { JOptionPane.showMessageDialog(null, "The file indicated in the database (" + strTemp + ") can not be read.", "File Error", JOptionPane.ERROR_MESSAGE); return; } txtPath.setText(jcFile.getAbsolutePath()); //show path in txtPath jcZipFile = new ZipFile(jcFile); //check if file is a zipped archive or just a file Enumeration jcEnum = jcZipFile.entries(); if ((jcEnum == null) | (jcEnum.hasMoreElements() == false)) { bolIsZip = false; } else { bolIsZip = true; } } catch (Exception e) { e.printStackTrace(); } } } }//GEN-LAST:event_cboSessionItemStateChanged private void cboSubjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSubjectItemStateChanged //call sub to fill up the list of sessions for this subject if ( (String)cboSubject.getSelectedItem() != "" ) { //only want to do this if clicked, not if loading if ( evt.getStateChange() == ItemEvent.SELECTED ) { GetSessions(); } } }//GEN-LAST:event_cboSubjectItemStateChanged private void FillCombos() { //fills cboSubject from the database String strNames = ""; String strTemp = ""; cboSubject.removeAllItems(); //clear in case ran previously try { stmSQL = conData.createStatement(); //stmSQL is global - have to get it ready ResultSet rssSubjects = stmSQL.executeQuery("SELECT subID, PPG, firstName, middleInitial, lastName FROM subject ORDER BY lastName, PPG"); cboSubject.addItem(""); //on top while(rssSubjects.next()) { //fill up cboSubject w/subject names & PPG numbers strTemp = rssSubjects.getString("PPG"); if ( strTemp.equals("") == false ) { strNames = rssSubjects.getInt("subID") + ": " + strTemp; } else { strNames = rssSubjects.getInt("subID") + ": " + rssSubjects.getString("lastName") + ", " + rssSubjects.getString("firstName") + " " + rssSubjects.getString("middleInitial") + "."; } cboSubject.addItem(strNames); } } catch(java.sql.SQLException e) { e.printStackTrace(); } } private void GetSessions() { //show the sessions in the list box for the current subject String strTemp = ""; String strDate = ""; String strType = ""; int intID = 0; String strExperimenter = ""; strTemp = (String)cboSubject.getSelectedItem(); //get the subject to parse out the parts if ( strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "You must select a subject before selecting a session.", "Selection Error", JOptionPane.ERROR_MESSAGE); return; } strSubjectID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = "SELECT sessionID, expDate, experimenter, expType FROM sessionData WHERE subID = \"" + strSubjectID + "\" ORDER BY expDate"; cboSession.addItem(""); //add a blank line to the top of the box try { ResultSet rssSubjects = stmSQL.executeQuery(strTemp); //make the resultset on the sql statement while( rssSubjects.next() ) { //get the data out of the database & put into cboSession intID = rssSubjects.getInt("sessionID"); strType = rssSubjects.getString("expType"); strDate = rssSubjects.getString("expDate"); strExperimenter = rssSubjects.getString("experimenter"); cboSession.addItem("" + intID + ": " + strDate + ", " + strType + " (" + strExperimenter + ")"); } } catch(java.sql.SQLException e) { e.printStackTrace(); } } /** the subjectID selected, for use in saving the data */ public static String getSubjectID() { return strSubjectID; } /** the sessionID selected, for use in saving the data */ public static String getSessionID() { return strSessionID; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox cboSession; private javax.swing.JComboBox cboSubject; private javax.swing.JButton cmdCancel; private javax.swing.JButton cmdOK; private javax.swing.JLabel lblPath; private javax.swing.JLabel lblSession; private javax.swing.JLabel lblSubject; private javax.swing.JTextField txtPath; // End of variables declaration//GEN-END:variables }