/* * frmSessionData.java * * Created on June 25, 2004, 9:44 AM */ package subjectData; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.JOptionPane; import javax.swing.JFileChooser; import java.io.File; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.FileInputStream; import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; /** * * @author jaetzel */ public class frmSessionData extends javax.swing.JInternalFrame { Connection conData; Statement stmSQL; private boolean bolAddingNew = false; static final int intBUFFER = 2048; //constant used for zip file access /** Creates new form frmSessionData */ public frmSessionData() { initComponents(); try { Class.forName("org.gjt.mm.mysql.Driver"); conData = SubjectData.getDatabaseConnection(); //gets connection to the correct database } catch (Exception e) { e.printStackTrace(); } FillComboBoxes(); //populate the GUI from the database DisableGUI(); //so can't edit the session info yet } private void FillComboBoxes() { //load the form after filling cboSubject & cboExperimenter String strNames = ""; int intSubID = 0; String strTemp = ""; cboSubject.removeAllItems(); cboSubject.addItem(""); cboExperimenter.removeAllItems(); 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"); 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); } //load into cboExperimenter stmSQL = conData.createStatement(); rssSubjects = stmSQL.executeQuery("SELECT boxChoice FROM comboBoxes WHERE boxID = 'Experimenter' ORDER BY boxChoice"); while(rssSubjects.next()) { cboExperimenter.addItem(rssSubjects.getString("boxChoice")); } //load into cboType stmSQL = conData.createStatement(); rssSubjects = stmSQL.executeQuery("SELECT boxChoice FROM comboBoxes WHERE boxID = 'ExperimentType' ORDER BY boxChoice"); while(rssSubjects.next()) { cboType.addItem(rssSubjects.getString("boxChoice")); } } catch(java.sql.SQLException e) { e.printStackTrace(); } } /** 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(); cmdAdd = new javax.swing.JButton(); jLabel21 = new javax.swing.JLabel(); cmdDelete = new javax.swing.JButton(); cmdEditData = new javax.swing.JButton(); cmdClose2 = new javax.swing.JButton(); cmdOKClose = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); lstSessions1 = new javax.swing.JList(); cmdCancel = new javax.swing.JButton(); fraSessionInfo = new javax.swing.JPanel(); txtAge = new javax.swing.JTextField(); lblDate = new javax.swing.JLabel(); txtDate = new javax.swing.JTextField(); lblAge = new javax.swing.JLabel(); lblType = new javax.swing.JLabel(); cboType = new javax.swing.JComboBox(); lblExperimenter = new javax.swing.JLabel(); cboExperimenter = new javax.swing.JComboBox(); txtTime = new javax.swing.JTextField(); lblTime = new javax.swing.JLabel(); txtComment = new javax.swing.JTextArea(); jLabel20 = new javax.swing.JLabel(); lblDataFile = new javax.swing.JLabel(); txtDataFile = new javax.swing.JTextField(); cmdSetData = new javax.swing.JButton(); cmdDelete1 = new javax.swing.JButton(); getContentPane().setLayout(new java.awt.GridBagLayout()); setClosable(true); setIconifiable(true); setMaximizable(true); setResizable(true); setTitle("Work With Sessions"); setPreferredSize(new java.awt.Dimension(800, 375)); lblSubject.setText("select subject:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0); getContentPane().add(lblSubject, gridBagConstraints); cboSubject.setPreferredSize(new java.awt.Dimension(250, 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 = 0; gridBagConstraints.gridwidth = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 0); getContentPane().add(cboSubject, gridBagConstraints); cmdAdd.setMnemonic('A'); cmdAdd.setText("add new"); cmdAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdAddActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 0); getContentPane().add(cmdAdd, gridBagConstraints); jLabel21.setText("sessions for this subject:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0); getContentPane().add(jLabel21, gridBagConstraints); cmdDelete.setMnemonic('D'); cmdDelete.setText("delete"); cmdDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdDeleteActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 3; gridBagConstraints.insets = new java.awt.Insets(5, 30, 5, 0); getContentPane().add(cmdDelete, gridBagConstraints); cmdEditData.setMnemonic('V'); cmdEditData.setText("edit"); cmdEditData.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdEditDataActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 3; gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 0); getContentPane().add(cmdEditData, gridBagConstraints); cmdClose2.setMnemonic('C'); cmdClose2.setText("close"); cmdClose2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdClose2ActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 4; gridBagConstraints.gridy = 5; gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0); getContentPane().add(cmdClose2, gridBagConstraints); cmdOKClose.setMnemonic('S'); cmdOKClose.setText("save"); cmdOKClose.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdOKCloseActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.insets = new java.awt.Insets(5, 20, 5, 0); getContentPane().add(cmdOKClose, gridBagConstraints); jScrollPane3.setMinimumSize(new java.awt.Dimension(300, 100)); jScrollPane3.setPreferredSize(new java.awt.Dimension(600, 100)); lstSessions1.setBorder(new javax.swing.border.EtchedBorder()); lstSessions1.setModel(new javax.swing.AbstractListModel() { String[] strings = { "select a session" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); lstSessions1.setMaximumSize(new java.awt.Dimension(200, 30)); lstSessions1.setMinimumSize(new java.awt.Dimension(200, 30)); lstSessions1.setPreferredSize(new java.awt.Dimension(360, 30)); lstSessions1.setVisibleRowCount(5); lstSessions1.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { lstSessions1ValueChanged(evt); } }); jScrollPane3.setViewportView(lstSessions1); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 5; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; getContentPane().add(jScrollPane3, gridBagConstraints); 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 = 4; gridBagConstraints.gridy = 3; gridBagConstraints.insets = new java.awt.Insets(5, 40, 5, 0); getContentPane().add(cmdCancel, gridBagConstraints); fraSessionInfo.setLayout(new java.awt.GridBagLayout()); fraSessionInfo.setBorder(new javax.swing.border.TitledBorder("session details")); txtAge.setPreferredSize(new java.awt.Dimension(150, 19)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(txtAge, gridBagConstraints); lblDate.setText("date (yyyy-mm-dd):"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(lblDate, gridBagConstraints); txtDate.setMinimumSize(new java.awt.Dimension(130, 20)); txtDate.setPreferredSize(new java.awt.Dimension(150, 19)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(txtDate, gridBagConstraints); lblAge.setText("subject age:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); fraSessionInfo.add(lblAge, gridBagConstraints); lblType.setText("experiment name: "); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(lblType, gridBagConstraints); cboType.setPreferredSize(new java.awt.Dimension(150, 19)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(cboType, gridBagConstraints); lblExperimenter.setText("experimenter:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 0; gridBagConstraints.ipady = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); fraSessionInfo.add(lblExperimenter, gridBagConstraints); cboExperimenter.setPreferredSize(new java.awt.Dimension(150, 19)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 0; gridBagConstraints.gridwidth = 2; gridBagConstraints.ipady = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(cboExperimenter, gridBagConstraints); txtTime.setText("00:00"); txtTime.setMinimumSize(new java.awt.Dimension(130, 26)); txtTime.setPreferredSize(new java.awt.Dimension(70, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(txtTime, gridBagConstraints); lblTime.setText("time (hh:mm, 00 to 24):"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 6, 0, 0); fraSessionInfo.add(lblTime, gridBagConstraints); txtComment.setFont(new java.awt.Font("Dialog", 0, 10)); txtComment.setLineWrap(true); txtComment.setRows(3); txtComment.setWrapStyleWord(true); txtComment.setBorder(new javax.swing.border.EtchedBorder()); txtComment.setMinimumSize(new java.awt.Dimension(400, 150)); txtComment.setPreferredSize(new java.awt.Dimension(480, 50)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 4; gridBagConstraints.ipady = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(txtComment, gridBagConstraints); jLabel20.setText("comment:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.ipady = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0); fraSessionInfo.add(jLabel20, gridBagConstraints); lblDataFile.setText("data file:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); fraSessionInfo.add(lblDataFile, gridBagConstraints); txtDataFile.setEditable(false); txtDataFile.setPreferredSize(new java.awt.Dimension(290, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0); fraSessionInfo.add(txtDataFile, gridBagConstraints); cmdSetData.setText("add data file"); cmdSetData.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdSetDataActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(3, 2, 3, 0); fraSessionInfo.add(cmdSetData, gridBagConstraints); cmdDelete1.setText("delete data file"); cmdDelete1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdDelete1ActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 4; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.insets = new java.awt.Insets(3, 0, 3, 0); fraSessionInfo.add(cmdDelete1, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = 5; getContentPane().add(fraSessionInfo, gridBagConstraints); pack(); }//GEN-END:initComponents private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteActionPerformed //delete the session highlighted in lstSessions int intAnswer = 0; String strTemp = ""; String strSubID = ""; String strSesID = ""; String strTime = ""; ResultSet rssSubjects; boolean bolMade = true; String strHour = ""; String strSQL = ""; //check if a subject and session are selected and get the IDs strTemp = (String)cboSubject.getSelectedItem(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the subject you want to delete from the box.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = (String)lstSessions1.getSelectedValue(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the session you want to delete from the list.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSesID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : //ask the user if they are sure they want to do the delete, exit if not intAnswer = JOptionPane.showConfirmDialog(null, "This session will be deleted from the database.\nAre you sure that you want to delete this session?", "File Delete Confirm", JOptionPane.YES_NO_OPTION); if (intAnswer == JOptionPane.NO_OPTION) { return; } try { //get the path to the previous data file for this session & subject & time stmSQL = conData.createStatement(); rssSubjects = stmSQL.executeQuery("SELECT dataFile FROM sessionData WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"); rssSubjects.next(); strTemp = rssSubjects.getString("dataFile"); if (!strTemp.equals("")) { JOptionPane.showMessageDialog(null, "Please delete the data file before the session.", "Error", JOptionPane.ERROR_MESSAGE); return; } } catch(java.sql.SQLException e) { e.printStackTrace(); } try { //delete the existing record from the session table stmSQL = conData.createStatement(); //stmSQL & conData are global stmSQL.executeUpdate("DELETE FROM sessionData WHERE subID = \"" + strSubID + "\" AND sessionID = \"" + strSesID + "\""); } catch(java.sql.SQLException e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "There has been an error deleting the session table database entry.", "Database Deletion Error", JOptionPane.ERROR_MESSAGE); return; } UpdateList(); //call sub to fill up the list of sessions for the current subject ClearGUI(); //call sub to blank out the bottom of the screen }//GEN-LAST:event_cmdDeleteActionPerformed private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed //call subs to update screen ClearGUI(); UpdateList(); DisableGUI(); cmdAdd.setEnabled(true); cmdDelete.setEnabled(true); }//GEN-LAST:event_cmdCancelActionPerformed private void cmdDelete1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDelete1ActionPerformed //delete the zip file associated with this session and clear out txtPath and txtTime for new info int intAnswer = 0; String strPath = ""; boolean bolMade = true; String strTemp = ""; String strSubID = ""; String strSesID = ""; ResultSet rssSubjects; String strSQL = ""; //get the subject and session ID strTemp = (String)cboSubject.getSelectedItem(); //get selected subject and session IDs if (strTemp.equals("") == true) { return; } if (lstSessions1.getSelectedIndex() == -1) { return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = (String)lstSessions1.getSelectedValue(); strSesID = strTemp.substring(0, strTemp.indexOf(":")); //session ID is first also try { //get the path to the previous data file for this session & subject & time stmSQL = conData.createStatement(); rssSubjects = stmSQL.executeQuery("SELECT dataFile FROM sessionData WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"); rssSubjects.next(); strPath = rssSubjects.getString("dataFile"); } catch(java.sql.SQLException e) { e.printStackTrace(); } File fileZipPath = new File(strPath); if (fileZipPath.exists() == true) { //file should already exist intAnswer = JOptionPane.showConfirmDialog(null, "The previous data file for this subject and session will be permanently deleted.\nIf you are replacing the existing file click yes, otherwise click no to cancel.", "File Delete Confirm", JOptionPane.YES_NO_OPTION); if (intAnswer == JOptionPane.NO_OPTION) { return; } else { bolMade = fileZipPath.delete(); //delete the existing file; bolMade shows if successful if (bolMade == false) { JOptionPane.showMessageDialog(null, "There has been an error deleting the previous file. The old file was not deleted.", "Serious File Deletion Error", JOptionPane.ERROR_MESSAGE); return; } } } //blank and enable so can update the path & time //update data in the database as needed strSQL = "UPDATE sessionData SET dataFile = '' WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"; try { stmSQL = conData.createStatement(); stmSQL.executeUpdate(strSQL); } catch (java.sql.SQLException e) { e.printStackTrace(); } txtDataFile.setText(""); cmdSetData.setEnabled(true); cmdDelete1.setEnabled(false); }//GEN-LAST:event_cmdDelete1ActionPerformed private void cmdSetDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSetDataActionPerformed //show a file chooser to let user pick a text file for compression. //JFileChooser comes with the ability to move around the directory structure int intC = 0; String strPath = ""; String strZipPath = ""; String strSubID = ""; String strSesID = ""; String strTemp = ""; String strType = ""; String strSQL = ""; boolean bolMade = true; File fileZipPath; File fileDrive; String str1 = ""; int count = 0; int intRow = 0; //check that the session info - subID, sessionID, type - on the screen is ok strTemp = (String)cboSubject.getSelectedItem(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the subject.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = (String)lstSessions1.getSelectedValue(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the session.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSesID = strTemp.substring(0, strTemp.indexOf(":")); strType = (String)cboType.getSelectedItem(); if (strType.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the type of experiment the subject performed on this date.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } //get the file(s) to put into the zip file and database JFileChooser frmFileOpen = new JFileChooser(); //declare a JFileChooser - the select file box frmFileOpen.setApproveButtonText("Select Text Files"); //have button say Select instead of Open frmFileOpen.setMultiSelectionEnabled(true); //pick multiple files intC = frmFileOpen.showOpenDialog(null); if (intC != JFileChooser.APPROVE_OPTION) { return; } //user canceled, so exit sub File fileChosen[] = frmFileOpen.getSelectedFiles(); //the files picked for (intC = 0; intC < fileChosen.length; intC++) { strPath = "|" + fileChosen[intC].getPath() + strPath; } fileZipPath = new File(SubjectData.getSavePlace() + File.separator + "subject_" + strSubID + File.separator + "session_" + strSesID + ".zip"); bolMade = fileZipPath.mkdirs(); /*if (fileZipPath.exists() == true) { //file already exists, so delete it. intC = JOptionPane.showConfirmDialog(null, "A file with for this subject and this session at this time already exists.\nIf you are replacing the existing file click yes, otherwise click no.", "File Delete Confirm", JOptionPane.YES_NO_OPTION); if (intC == JOptionPane.NO_OPTION) { return; } else { bolMade = fileZipPath.delete(); //delete the existing file; bolMade shows if successful if (bolMade == false) { JOptionPane.showMessageDialog(null, "There has been an error deleting the previous file. The old file was not deleted.", "File Deletion Error", JOptionPane.ERROR_MESSAGE); return; } } } */ try { //compress the file and resave it to strZipPath BufferedInputStream origin = null; strZipPath = fileZipPath.getAbsolutePath(); bolMade = fileZipPath.createNewFile() ; FileOutputStream dest = new FileOutputStream(strZipPath); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[intBUFFER]; if (strPath.substring(0,1).equals("|") == true) { //more than one file to put into zip strPath = strPath.substring(1); //trim off first | while (strPath.indexOf("|") > 0) { str1 = strPath.substring(0, strPath.indexOf("|")); //get next filename out strPath = strPath.substring(strPath.indexOf("|") + 1); //trim off filename just saved to str1 FileInputStream fi = new FileInputStream(str1); origin = new BufferedInputStream(fi, intBUFFER); ZipEntry entry = new ZipEntry(str1); out.putNextEntry(entry); while((count = origin.read(data, 0, intBUFFER)) != -1) { out.write(data, 0, count); } origin.close(); out.closeEntry(); } } //add last file, or only file if multiples FileInputStream fi = new FileInputStream(strPath); origin = new BufferedInputStream(fi, intBUFFER); ZipEntry entry = new ZipEntry(strPath); out.putNextEntry(entry); while((count = origin.read(data, 0, intBUFFER)) != -1) { out.write(data, 0, count); } origin.close(); out.close(); //close these things for good } catch(Exception e) { e.printStackTrace(); } //update data in the database as needed strZipPath = SubjectData.AddEscapeChars(strZipPath); strSQL = "UPDATE sessionData SET dataFile = '" + strZipPath + "' WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"; try { stmSQL = conData.createStatement(); stmSQL.executeUpdate(strSQL); } catch (java.sql.SQLException e) { e.printStackTrace(); } txtDataFile.setText(strZipPath); //show something on screen cmdSetData.setEnabled(false); //have a data file now, so can't set a new one }//GEN-LAST:event_cmdSetDataActionPerformed private void cmdOKCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOKCloseActionPerformed //adds or updates the database String strSubID = ""; String strSesID = ""; String strTemp = ""; String strComment = ""; String strType = ""; String strExperimenter = ""; String strDate = ""; String strSQL = ""; String strAge = ""; String strZipPath = ""; String strTime = ""; String strMinute = ""; String strHour = ""; String strPath = ""; boolean bolMade = true; int intAnswer = 0; File fileZipPath; File fileDrive; String strInPath = ""; String str1 = ""; int count = 0; int intRow = 0; //get the subject ID and session ID from the combo & list boxes strTemp = (String)cboSubject.getSelectedItem(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the subject.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : if (bolAddingNew == false) { //updating the data in the database strTemp = (String)lstSessions1.getSelectedValue(); if (strTemp.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the session.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } strSesID = strTemp.substring(0, strTemp.indexOf(":")); } //get the values from the user input and put into correct format strExperimenter = (String)cboExperimenter.getSelectedItem(); strDate = txtDate.getText(); strComment = txtComment.getText(); strType = (String)cboType.getSelectedItem(); //check for errors in the text boxes; show message box and exit sub if errors if (strExperimenter.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the experimenter for this session.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } if (strType.equals("") == true) { JOptionPane.showMessageDialog(null, "Please select the type of experiment the subject performed on this date.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return; } if (SubjectData.CheckDate(strDate, "the experiment date field") == false) { return; } //call fn to check date strComment = SubjectData.AddEscapeChars(strComment); //call sub to add / before ', /, and " strAge = txtAge.getText(); if (SubjectData.CheckTime(txtTime.getText(), "clip start time") == true) { strTime = txtTime.getText(); } else { return; } //update or add data in the database as needed if (bolAddingNew == false) { //updating the data in the database strSQL = "UPDATE sessionData SET experimenter = '" + strExperimenter + "', expDate = '" + strDate + "', comment = '" + strComment + "', expType='" + strType + "', age='" + strAge + "', timeStarted='" + strTime + "' WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"; } else if (bolAddingNew == true) { //adding a new row strSQL = "INSERT INTO sessionData VALUES (" + strSubID + ", NULL, '" + strDate + "', '" + strExperimenter + "', '" + strComment + "', '" + strType + "', '" + strAge + "', '" + strTime + "', '')"; } try { stmSQL = conData.createStatement(); stmSQL.executeUpdate(strSQL); } catch (java.sql.SQLException e) { e.printStackTrace(); } ClearGUI(); DisableGUI(); UpdateList(); //call subs to update screen cmdAdd.setEnabled(true); cmdDelete.setEnabled(true); }//GEN-LAST:event_cmdOKCloseActionPerformed private void cmdEditDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditDataActionPerformed String strTemp = ""; String strSubID = ""; String strSesID = ""; ResultSet rssSubjects; strTemp = (String)cboSubject.getSelectedItem(); //get selected subject and session IDs if (strTemp.equals("") == true) { return; } if (lstSessions1.getSelectedIndex() == -1) { return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = (String)lstSessions1.getSelectedValue(); strSesID = strTemp.substring(0, strTemp.indexOf(":")); //session ID is first also strTemp = ""; try { //get the path to the previous data file for this session & subject & time stmSQL = conData.createStatement(); rssSubjects = stmSQL.executeQuery("SELECT dataFile FROM sessionData WHERE subID = '" + strSubID + "' AND sessionID = '" + strSesID + "'"); rssSubjects.next(); strTemp = rssSubjects.getString("dataFile"); } catch(java.sql.SQLException e) { e.printStackTrace(); } if (strTemp.equals("")) { cmdSetData.setEnabled(true); } else { cmdDelete1.setEnabled(true); } EnableGUI(); bolAddingNew = false; //set flag - not adding a new session cmdAdd.setEnabled(false); cmdDelete.setEnabled(false); }//GEN-LAST:event_cmdEditDataActionPerformed private void cboSubjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSubjectItemStateChanged //call sub to fill up the list of sessions for this subject bolAddingNew = false; //switch flag - viewing, not adding if ((String)cboSubject.getSelectedItem() != "") { //only want to do this if clicked, not if loading if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) { UpdateList(); //call sub to fill up the list of movies for the current subject and session ClearGUI(); //clear the boxes in case were previously filled DisableGUI(); }} //end the ifs }//GEN-LAST:event_cboSubjectItemStateChanged private void ClearGUI() { //blank out the controls txtDate.setText("0000-00-00"); txtComment.setText(""); txtAge.setText(""); txtDataFile.setText(""); txtTime.setText("00:00"); cboType.setSelectedIndex(0); cboExperimenter.setSelectedIndex(0); lstSessions1.removeAll(); } private void UpdateList() { //show the sessions in the list box for the current subject String strSQL = ""; String strTemp = ""; String strID = ""; String strDate = ""; String strType = ""; String strSession = ""; int intID = 0; String strList[]; int intSize = 0; String strExperimenter = ""; strTemp = (String)cboSubject.getSelectedItem(); //get the date to parse out the parts strID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strSQL = "SELECT sessionID, expDate, experimenter, expType FROM sessionData WHERE subID = \"" + strID + "\" ORDER BY expDate"; try { ResultSet rssSubjects = stmSQL.executeQuery(strSQL); //make the resultset on the sql statement while(rssSubjects.next()) { intSize = intSize + 1; } //count the number of rows in the resultset rssSubjects.beforeFirst(); //move to the start of the resultset again strList = new String[intSize]; //get the array ready to hold the data intSize = 0; while(rssSubjects.next()) { //get the data out of the database intID = rssSubjects.getInt("sessionID"); strType = rssSubjects.getString("expType"); strDate = rssSubjects.getString("expDate"); strExperimenter = rssSubjects.getString("experimenter"); strList[intSize] = "" + intID + ": " + strDate + ", " + strType + " (" + strExperimenter + ")"; //store the list item intSize = intSize + 1; //update count for next time } lstSessions1.setListData(strList); //done with the result set, so add the data to the list } catch(java.sql.SQLException e) { e.printStackTrace(); } } private void lstSessions1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstSessions1ValueChanged //show the selected session's information in the boxes on the screen String strTemp = ""; String strSubID = ""; java.sql.Date dateTemp; String strSesID = ""; bolAddingNew = false; //switch flag - viewing, not adding strTemp = (String)cboSubject.getSelectedItem(); //get selected subject and session IDs if (strTemp.equals("") == true) { return; } if (lstSessions1.getSelectedIndex() == -1) { return; } strSubID = strTemp.substring(0, strTemp.indexOf(":")); //ID is always the first part before the : strTemp = (String)lstSessions1.getSelectedValue(); strSesID = strTemp.substring(0, strTemp.indexOf(":")); //session ID is first also try { //get all of the data from the database and put it up on the screen ResultSet rssSubjects = stmSQL.executeQuery("SELECT * FROM sessionData WHERE subID = " + strSubID + " AND sessionID = " + strSesID); rssSubjects.next(); //move forward so can get the data out of the database cboExperimenter.setSelectedItem(rssSubjects.getString("experimenter")); txtComment.setText(rssSubjects.getString("comment")); txtDate.setText(rssSubjects.getString("expDate")); cboType.setSelectedItem(rssSubjects.getString("expType")); txtAge.setText(rssSubjects.getString("age")); txtDataFile.setText(rssSubjects.getString("dataFile")); } catch(java.sql.SQLException e) { e.printStackTrace(); } DisableGUI(); cmdAdd.setEnabled(true); cmdDelete.setEnabled(true); }//GEN-LAST:event_lstSessions1ValueChanged private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddActionPerformed if (((String)cboSubject.getSelectedItem()).equals("")) { return; } ClearGUI(); EnableGUI(); cmdSetData.setEnabled(false); //have to add the session before can set a data file bolAddingNew = true; //set flag for adding a new session cmdAdd.setEnabled(false); cmdDelete.setEnabled(false); }//GEN-LAST:event_cmdAddActionPerformed private void cmdClose2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdClose2ActionPerformed try { this.setClosed(true); } catch(java.beans.PropertyVetoException e) { e.printStackTrace(); } }//GEN-LAST:event_cmdClose2ActionPerformed private void EnableGUI() { cboExperimenter.setEnabled(true); cboType.setEnabled(true); txtAge.setEditable(true); txtComment.setEditable(true); txtDate.setEditable(true); txtTime.setEditable(true); } private void DisableGUI() { cboExperimenter.setEnabled(false); cboType.setEnabled(false); txtAge.setEditable(false); txtComment.setEditable(false); txtDate.setEditable(false); txtTime.setEditable(false); cmdSetData.setEnabled(false); cmdDelete1.setEnabled(false); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox cboExperimenter; private javax.swing.JComboBox cboSubject; private javax.swing.JComboBox cboType; private javax.swing.JButton cmdAdd; private javax.swing.JButton cmdCancel; private javax.swing.JButton cmdClose2; private javax.swing.JButton cmdDelete; private javax.swing.JButton cmdDelete1; private javax.swing.JButton cmdEditData; private javax.swing.JButton cmdOKClose; private javax.swing.JButton cmdSetData; private javax.swing.JPanel fraSessionInfo; private javax.swing.JLabel jLabel20; private javax.swing.JLabel jLabel21; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JLabel lblAge; private javax.swing.JLabel lblDataFile; private javax.swing.JLabel lblDate; private javax.swing.JLabel lblExperimenter; private javax.swing.JLabel lblSubject; private javax.swing.JLabel lblTime; private javax.swing.JLabel lblType; private javax.swing.JList lstSessions1; private javax.swing.JTextField txtAge; private javax.swing.JTextArea txtComment; private javax.swing.JTextField txtDataFile; private javax.swing.JTextField txtDate; private javax.swing.JTextField txtTime; // End of variables declaration//GEN-END:variables }