/** ModifiedRadioButton
 *	@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 ModifiedRadioButton extends JRadioButton 
implements ItemListener
{
	
	private static boolean selected = false;
	private boolean originalState = false;
	private boolean firstClick = true;
	private int selectedstate = 0;
	private boolean toggle = true;
	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 ModifiedRadioButton(String filename, String text) 
   	{
    	super(text);
    	dynamicCount++;    	
		count = dynamicCount;
    	setForeground(Color.RED);
		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(true);
      	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 += ("Radio " + countString + " - start: " + startTime + "\n");
      		firstTime = false;
		}
		
		clicked++;
		
		if(clicked >= 3)
		{
			setForeground(Color.BLACK);
			setSelected(false);
			endTime = getSeconds();  
       		data += ("Radio " + 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 ModifiedRadioButton
