/** ModifiedCheckBox
 *	@author: <a href="mailto:gsafko@mail.camdencc.edu">Gregory Safko</a>
 *	@version 1.0 Fall 2004
 *	Date: November, 2004
 *  Nova Southeastern University
 *  Course DISS 720: Human Computer Interaction
 *	Instructor: Maxine Cohen, PhD
 *	Purpose: A modified GUI control used to test 
 *  interaction proficiency among subjects with Musical
 *  Intelligence and those without Musical Intelligence
 *  (Base on H. Gardner's Seven Intelligences)
 */

 
 // Java core packages
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;
import javax.swing.event.*;

public class ModifiedCheckBox extends JCheckBox 
implements ItemListener
{
	
	private boolean selected = false;
	private boolean originalState = false;
	private int selectedstate = 0;
	private int clicked = 0;
	private int startTime, endTime;
	private PrintWriter hcifile = null;
	private static String data = "";
	private boolean firstTime = true;
	private static int dynamicCount = 0;
	private int count;
	private final int MAXTIMES = 35;
	

    
	// set up the object
	public ModifiedCheckBox(String filename, String text) 
   	{
    	super(text);
    	dynamicCount++;    	
		count = dynamicCount;
    	try 
		{
			hcifile = new PrintWriter(new FileOutputStream(filename));
		}
		catch(FileNotFoundException ex)
		{
			System.out.println("Error opening the output file: " + filename);
			System.exit(0);	
		}
    	selectedstate = (int) (Math.random() * 100);
    	if (selectedstate % 2 == 0)
    		{
    			selected = true;
    			originalState = true;
    		}
    	else 
      		{
      			selected = false;
      			originalState = false;
      		}
      	setSelected(selected);
    	
      	this.addItemListener( this );
    	     
    }
    public void itemStateChanged( ItemEvent ie )
	{
		String countString = "";
		if(count == 1)
			countString += (MAXTIMES - count + 1);
		else 
			countString += " " + (MAXTIMES - count + 1);
		if(firstTime)
		{
			startTime = getSeconds();  
      		data += ("Check Box " + countString + " - start: " + startTime + "\n");
      		firstTime = false;
		}
		
		this.clicked++;
		
		
		if(this.clicked >= 2)
		{
			this.setVisible(false);
			endTime = getSeconds();  
      		data += ("Check Box " + countString + " - end:   " + endTime + "\n");
			hcifile.println(data);
			hcifile.close();
		}
			
		
		      	
	}
	public int getSeconds()
   	{
   		GregorianCalendar cal = new GregorianCalendar();
   		int hours 	= (int) cal.get(GregorianCalendar.HOUR_OF_DAY);
   		int minutes = (int) cal.get(GregorianCalendar.MINUTE);
   		int seconds = (int) cal.get(GregorianCalendar.SECOND);
		int milli 	= (int) cal.get(GregorianCalendar.MILLISECOND);
 	
		return ((hours * 3600) + (minutes * 60) + seconds)*1000 + milli;   	
	}
} // end class ModifiedCheckBox

  	