Puka - Software for Detection of Breaths in Strain Gauge Recordings 1.0.0

File: <base>/sourceCode/subjectData/SubjectData.java (18,593 bytes)
/*
 * SubjectData.java
 *
 * Created on March 18, 2002, 2:31 PM
 */

package subjectData;

/**
 *
 * @author  Joset A. Etzel
 */
import java.sql.*; import java.awt.*; import java.util.*; import java.lang.*;
import javax.swing.*; import java.awt.event.*; import subjectData.*; 

import java.awt.Toolkit; import javax.swing.ImageIcon;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import javax.swing.UIManager;

public class SubjectData extends javax.swing.JFrame {
  private static String strDBtoConnect = "";
  private static String strDBPassword = ""; private static String strDBUserID = "";
	private static String strSavePlace = "";
	
	private static ArrayList jcDBNames = new ArrayList();
    
    /** Creates new form SubjectData */
  public SubjectData() {
    initComponents();
    setIconImage(Toolkit.getDefaultToolkit().getImage("ParasolSmall.gif"));

	readConfigurationFile();  //get the database names from the config file
	mnuConnectToDatabase.doClick();  //and show the form
    
    desktopPane.setSize(new java.awt.Dimension(700, 600));
  }
    
    /** 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
    desktopPane = new javax.swing.JDesktopPane();
    menuBar = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    mnuComboBoxes = new javax.swing.JMenuItem();
    mnuConnectToDatabase = new javax.swing.JMenuItem();
    jSeparator1 = new javax.swing.JSeparator();
    exitMenuItem = new javax.swing.JMenuItem();
    editMenu = new javax.swing.JMenu();
    mnuAddSubject = new javax.swing.JMenuItem();
    mnuNewSession = new javax.swing.JMenuItem();
    viewMenu = new javax.swing.JMenu();
    mnuQuery = new javax.swing.JMenuItem();
    mnuDBKey = new javax.swing.JMenuItem();

    setTitle("SubDB");
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        exitForm(evt);
      }
    });

    desktopPane.setMinimumSize(new java.awt.Dimension(800, 800));
    getContentPane().add(desktopPane, java.awt.BorderLayout.CENTER);

    fileMenu.setMnemonic('F');
    fileMenu.setText("File");
    mnuComboBoxes.setText("Change Combobox Choices");
    mnuComboBoxes.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuComboBoxesActionPerformed(evt);
      }
    });

    fileMenu.add(mnuComboBoxes);

    mnuConnectToDatabase.setText("Connect to a database");
    mnuConnectToDatabase.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuConnectToDatabaseActionPerformed(evt);
      }
    });

    fileMenu.add(mnuConnectToDatabase);

    fileMenu.add(jSeparator1);

    exitMenuItem.setText("Exit");
    exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        exitMenuItemActionPerformed(evt);
      }
    });

    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    editMenu.setMnemonic('A');
    editMenu.setText("Alter Database");
    mnuAddSubject.setText("Work With Subjects");
    mnuAddSubject.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuAddSubjectActionPerformed(evt);
      }
    });

    editMenu.add(mnuAddSubject);

    mnuNewSession.setText("Work With Sessions");
    mnuNewSession.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuNewSessionActionPerformed(evt);
      }
    });

    editMenu.add(mnuNewSession);

    menuBar.add(editMenu);

    viewMenu.setMnemonic('V');
    viewMenu.setText("View");
    mnuQuery.setText("Query Database");
    mnuQuery.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuQueryActionPerformed(evt);
      }
    });

    viewMenu.add(mnuQuery);

    mnuDBKey.setText("Database Key");
    mnuDBKey.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        mnuDBKeyActionPerformed(evt);
      }
    });

    viewMenu.add(mnuDBKey);

    menuBar.add(viewMenu);

    setJMenuBar(menuBar);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-780)/2, (screenSize.height-600)/2, 780, 600);
  }//GEN-END:initComponents

  private void mnuNewSessionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuNewSessionActionPerformed
    //show frmConnectToDatabase	
	
	frmSessionData frmSessionData1 = new frmSessionData();
    desktopPane.add(frmSessionData1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    frmSessionData1.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
    frmSessionData1.setBounds(20, 20, 656, 445);
    frmSessionData1.show();
  }//GEN-LAST:event_mnuNewSessionActionPerformed

  private void mnuConnectToDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConnectToDatabaseActionPerformed
	//show frmConnectToDatabase	
	
	frmConnectToDatabase frmConnectToDatabase1 = new frmConnectToDatabase();
    desktopPane.add(frmConnectToDatabase1, javax.swing.JLayeredPane.DEFAULT_LAYER);
    frmConnectToDatabase1.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
    frmConnectToDatabase1.setBounds(50, 40, 400, 300);
    frmConnectToDatabase1.show();
  }//GEN-LAST:event_mnuConnectToDatabaseActionPerformed

	public static String AddEscapeChars(String strSQL) {
		//adds another \ in front of all \, ', and " so everything saved ok
    int intC = 0;
        
    StringBuffer strbNew = new StringBuffer(strSQL);
			for (intC = 0; intC < strSQL.length(); intC++) {   //parse string, looking for dangerous characters
				if (strbNew.charAt(intC) == '\\' | strbNew.charAt(intC) == '\"' | strbNew.charAt(intC) == '\'') {
					strbNew.insert(intC,'\\'); intC++; 
				}   //insert escape char, increment counter to pass \
      }
      String strNew = new String(strbNew);  //convert character array back to string
     return strNew; 
    }
	
	   public static boolean CheckTime(String strTemp, String strBox) {
      //returns true if string strTemp is in format hh:mm, military time.  
      String strTime = ""; String strMinute = ""; String strHour = ""; 
      int intHour = 0; int intMinute = 0;
      
      //check if zeros included, ok if they are
      if (strTemp.length() == 8 && strTemp.substring(5).equals(":00") == true) { strTime = strTemp.substring(0, 5); }
      else { strTime = strTemp; }
      
      if (strTime.equals("") == true) { JOptionPane.showMessageDialog(null, "A time must be entered in " + strBox + ".\nPlease enter the time the clips started exactly as hh:mm in " + strBox + ".\nThe hours should be in military time, 01 to 24 hours.", "Data Error", JOptionPane.ERROR_MESSAGE); return false; }        
      if (strTime.indexOf(":") != 2 | strTime.length() != 5) { JOptionPane.showMessageDialog(null, "Please enter the time the clips started exactly as hh:mm in " + strBox + ".\nThe hours should be in military time, 01 to 24 hours.", "Data Error", JOptionPane.ERROR_MESSAGE); return false; }        
      
      strHour = strTime.substring(0,2); strMinute = strTime.substring(3);  //parse out the string parts
      intMinute = Integer.parseInt(strMinute); intHour = Integer.parseInt(strHour);  //convert to int
      
      if (intMinute < 0 | intMinute > 60) { JOptionPane.showMessageDialog(null, "The minutes must be between 00 and 60.\nPlease enter the time the clips started exactly as hh:mm in " + strBox + ".\nThe hours should be in military time, 01 to 24 hours.", "Data Error", JOptionPane.ERROR_MESSAGE); return false; }        
      if (intHour < 0 | intHour > 24) { JOptionPane.showMessageDialog(null, "The hours must be between 00 and 24.\nPlease enter the time the clips started exactly as hh:mm in " + strBox + ".\nThe hours should be in military time, 01 to 24 hours.", "Data Error", JOptionPane.ERROR_MESSAGE); return false; }        

      return true;  //haven't exited, so string is ok
    }
		 
	 public static boolean CheckDate(String strTemp, String strBox) {
        //function checks if the date passed (strDate) is in a valid format returns true if so, false if not
        String strMonth = ""; String strDay = ""; String strYear = ""; boolean bolReturn = true;
        int intYear = 0; int intMonth = 0; int intDay = 0;
        
        if (strTemp.equals("0000-00-00") == true | strTemp.equals("") == true) { return true; }  //zero string is ok
        if (strTemp.indexOf("-") == -1) { JOptionPane.showMessageDialog(null, "The date in in " + strBox + " must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        strYear = strTemp.substring(0, strTemp.indexOf("-"));
        intYear = strTemp.indexOf("-") + 1;
        strTemp = strTemp.substring(intYear);
        if (strTemp.indexOf("-") == -1) { JOptionPane.showMessageDialog(null, "The date in in " + strBox + " must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        strMonth = strTemp.substring(0, strTemp.indexOf("-"));  //now can parse out the next part - the month
        intYear = strTemp.indexOf("-") + 1;
        strDay = strTemp.substring(intYear);  //take out the month, leaves day
        
        intYear = Integer.parseInt(strYear); intMonth = Integer.parseInt(strMonth); intDay = Integer.parseInt(strDay);
        if (strYear.length() != 4) { JOptionPane.showMessageDialog(null, "Please use four digits for the year in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        if (strMonth.length() != 2) { JOptionPane.showMessageDialog(null, "Please use two digits for the month (01 to 12) in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        if (strDay.length() != 2) { JOptionPane.showMessageDialog(null, "Please use two digits for the day (01 to 31) in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        if (intYear < 1800 | intYear > 2200) { JOptionPane.showMessageDialog(null, "Please use four digits for the year in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        if (intMonth < 1 | intMonth > 12) { JOptionPane.showMessageDialog(null, "Please use two digits for the month (01 to 12) in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        if (intDay < 1 | intDay > 31) { JOptionPane.showMessageDialog(null, "Please use two digits for the day (01 to 31) in " + strBox + ". \nThe date must be in the exact format yyyy-mm-dd.", "Missing Data Error", JOptionPane.ERROR_MESSAGE); return false; }
        
        return true;  //if haven't exited yet, date is ok so return true
    }
		 
		 
	private void mnuConnectEricaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConnectEricaActionPerformed
	}//GEN-LAST:event_mnuConnectEricaActionPerformed

	private void mnuConnectEduardoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConnectEduardoActionPerformed
	}//GEN-LAST:event_mnuConnectEduardoActionPerformed

  private static void readConfigurationFile() {
    //open subjectDataConfig.txt and read the database names contained in it, filling up jcDBNames.
	//tell the user if there is a problem with the file.
	BufferedReader config = null; String strLine = null; File fileConfig; String strPath = "";

	//check if the configuration file exists at the expected location, exit if it can not be read
	fileConfig = new File("subjectDataConfig.txt");  
	if (fileConfig.exists() == false) {
	  JOptionPane.showMessageDialog(null, "The configuration file, subjectDataConfig.txt, does not exist.", "Startup Error", JOptionPane.ERROR_MESSAGE);
	  System.exit(1); 
	}
	if (fileConfig.canRead() == false) {
	  JOptionPane.showMessageDialog(null, "The configuration file, subjectDataConfig.txt, can not be read.", "Startup Error", JOptionPane.ERROR_MESSAGE);
	  System.exit(1); 
	}
	 if (fileConfig.isFile() == false) {
	  JOptionPane.showMessageDialog(null, "The configuration file, subjectDataConfig.txt, file is not valid.", "Startup Error", JOptionPane.ERROR_MESSAGE);
	  System.exit(1); 
	} 

	//read in the configuration file
	try {
	  config = new BufferedReader(new FileReader("subjectDataConfig.txt"));
	  while ((strLine = config.readLine()) != null) {
		if (strLine.startsWith("dbname")) {
		  strPath = strLine.substring(strLine.indexOf("=")+1);
		  if (strPath.equals("")) {
			JOptionPane.showMessageDialog(null, "The subjectDataConfig.txt file must contain a valid dbname entry.", "Startup Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		  }
		  jcDBNames.add(strPath);
		}
		else if (strLine.startsWith("savePlace")) {
			strPath = strLine.substring(strLine.indexOf("=")+1);
		  if (!strPath.equals("")) { strSavePlace = strPath; }
		}
	}
		
	} catch (java.io.FileNotFoundException e) {
	  System.out.println("couldn't find subjectDataConfig.txt");
	  System.exit(1);
	} catch (java.io.IOException e) {
	  System.out.println("IOException: " + e.getMessage());
	  e.printStackTrace();
	  System.exit(1);
	}
  }
	
  
  public static ArrayList getDBNames() { return jcDBNames; }
	
	public static String getSavePlace() { return strSavePlace; }
  
  public static void setDBName(String strDBName) { strDBtoConnect = strDBName; }
    
  public static void setDBUserID(String strDBUserID1) { strDBUserID = strDBUserID1; }

  public static void setDBPassword(String strDBPassword1) { strDBPassword = strDBPassword1; }
  
  
    private void mnuDBKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuDBKeyActionPerformed
      //loads the class ViewDatabaseKey and shows it in the DeskTopPane (a JInternalFrame)
			
      ViewDatabaseKey frmKey = new ViewDatabaseKey();
      desktopPane.add(frmKey, javax.swing.JLayeredPane.DEFAULT_LAYER);
      frmKey.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
			frmKey.setBounds(50, 40, 650, 400);
        frmKey.show();
    }//GEN-LAST:event_mnuDBKeyActionPerformed

    private void mnuQueryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuQueryActionPerformed
      //loads the class TableTest and shows it in the DeskTopPane (a JInternalFrame)
      TableTest frmTable = new TableTest();
      desktopPane.add(frmTable, javax.swing.JLayeredPane.DEFAULT_LAYER);
      frmTable.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
      frmTable.setBounds(50, 40, 650, 450);
      frmTable.show();
    }//GEN-LAST:event_mnuQueryActionPerformed

    private void mnuComboBoxesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuComboBoxesActionPerformed
      //loads the class AlterCombos and shows it in the DeskTopPane (a JInternalFrame)
      AlterCombos frmCombos = new AlterCombos();
      desktopPane.add(frmCombos, javax.swing.JLayeredPane.DEFAULT_LAYER);
      frmCombos.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
      frmCombos.setBounds(50, 40, 600, 200);
      frmCombos.show();
    }//GEN-LAST:event_mnuComboBoxesActionPerformed
        
    private void mnuAddSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuAddSubjectActionPerformed
        //loads the class SubjectInfo and shows it in the DeskTopPane (a JInternalFrame)
        SubjectInfo frmSubject = new SubjectInfo();
        desktopPane.add(frmSubject, javax.swing.JLayeredPane.DEFAULT_LAYER);
        frmSubject.setFrameIcon(new ImageIcon("ParasolSmall.gif"));
        frmSubject.setBounds(10, 10, 750, 300);
        frmSubject.show();        
    }//GEN-LAST:event_mnuAddSubjectActionPerformed
        
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0); 
    }//GEN-LAST:event_exitMenuItemActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    	

	public static Connection getDatabaseConnection() {
	  //return a Connection to the database named in strDBtoConnect
	  
	  try {
        Class.forName("org.gjt.mm.mysql.Driver");
		Connection conData = DriverManager.getConnection("jdbc:mysql://localhost/" + strDBtoConnect, strDBUserID, strDBPassword);
		return conData;
	  } 
	  catch (Exception e) { e.printStackTrace(); return null; }
	}
		
	
    /**
     * @param args the command line arguments
     */
  public static void main(String args[]) {
	try {
	  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (Exception e) {
	  System.out.println("Couldn't use the native look and feel "); e.printStackTrace();
	}
	
    new SubjectData().show();
  }
    
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JDesktopPane desktopPane;
  private javax.swing.JMenu editMenu;
  private javax.swing.JMenuItem exitMenuItem;
  private javax.swing.JMenu fileMenu;
  private javax.swing.JSeparator jSeparator1;
  private javax.swing.JMenuBar menuBar;
  private javax.swing.JMenuItem mnuAddSubject;
  private javax.swing.JMenuItem mnuComboBoxes;
  private javax.swing.JMenuItem mnuConnectToDatabase;
  private javax.swing.JMenuItem mnuDBKey;
  private javax.swing.JMenuItem mnuNewSession;
  private javax.swing.JMenuItem mnuQuery;
  private javax.swing.JMenu viewMenu;
  // End of variables declaration//GEN-END:variables
    
}