/** ModifiedSlider
 *	@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 ModifiedSlider extends JSlider 
implements ChangeListener
{
	
	private int min = 0;
	private int max = 200, start = 0;	
	private boolean maxHit = false, minHit = false;
	private int orientation = 0;
	private int temp = 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 = 10;
    
	// set up the object
	public ModifiedSlider(String filename) 
   	{
    	super();
		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);	
		}
		
    	temp = (int) (Math.random() * 100);
    	if (temp % 2 == 0)
    		orientation = SwingConstants.HORIZONTAL;
    	else 
      		orientation = SwingConstants.VERTICAL;
      	setOrientation(orientation);
    	max = (int) (Math.random() * 201);
    	start = (int) (Math.random() * max);
    	setMaximum(max);
    	setValue(start);
      	super.setMajorTickSpacing( 10 );
      	super.setPaintTicks( true );
      	this.addChangeListener( this );
      	
		
    	     
    }
    public void stateChanged( ChangeEvent e )
	{
		String countString = "";
		if(count == 1)
			countString += (MAXTIMES - count + 1);
		else 
			countString += " " + (MAXTIMES - count + 1);
		if(firstTime)
		{
			startTime = getSeconds();  
      		data += ("Slider " + countString + " - start: " + startTime + "\n");
      		firstTime = false;
		}
		if( this.getValue() == max)
			maxHit = true;
      	if( this.getValue() == min)
			minHit = true;
		if(maxHit && minHit)
		{
			maxHit = minHit = false;
			this.setVisible(false);
			endTime = getSeconds();  
      		data += ("Slider " + 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 ModifiedSlider

  	