/* @(#)Methods.java 1.0  17 November 2004 */

/* Useful imports */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.regex.*;

// import java.text.*;

public class Methods extends JPF 
{
    
    public static void main(String[] args) { 
        new Methods();
    }
    
    /* Opens a frame for a 10 second timer test. */
    public void test() {
        TimerSample.test(10);
    }
}

class TimerSample extends DisplayPanel {
    
    private int totalseconds = 60;
    
    private volatile int seconds = 0;
    
    private volatile boolean isActive = false;
    
    
    /* The text field that is temporarily editable. */
    private TextFieldView editview = new TextFieldView("", 500);
    
    /* The message field that displays the seconds remaining. */
    private Annotation message = new Annotation("Waiting");
    
    
    /* The timer countdown action. */
    private SimpleAction countdown =
        new SimpleAction("Count Down") {
        	public void perform() { doCountDown(); }
    	};
    
    /* The wrapper to force the countdown into a new thread. */
    private ThreadedAction wrapper =
        new ThreadedAction(countdown);
    
    
    /* The table panel with text field, message, and button. */
    private TablePanel testPanel = new TablePanel
    	(new Object[] { editview, message, wrapper },
    	 VERTICAL, 10, 10, CENTER);
    
    
    /* Constructs default timer with a duration of 60 seconds. */
    public TimerSample() {
        add(testPanel);
        editview.setEditable(false);
    }
    
    /* Constructs timer with the given timer duration. */
    public TimerSample(int duration) {
        this(); // execute default constructor first
        
        setTimerDuration(duration);
    }
    
    /* Place a test timer of the given duration into a frame. */
    public static void test(int duration) {
        JPTFrame.createQuickJPTFrame
        	("timer test", new TimerSample(duration));
    }
    
    /* Set the duration for the next timer execution. */
    public void setTimerDuration(int duration) {
        totalseconds = duration;
    }
    
    /* Force the timer execution to stop within one second. */
    public void done() {
        seconds = 0;
    }
    
    
    /* The count down method. */
    private void doCountDown() {
        if (isActive)
            return;
        
        isActive = true;
        
        startup();
        
        seconds = totalseconds;
        
        while (seconds > 0) {
            message.setText("Seconds = " + seconds);
            seconds--;
            
            // 1 second thread pause: parameter in milliseconds
            JPTUtilities.pauseThread(1000);
        }
        
        message.setText("Time is up");
        
        finish();
        
        isActive = false;
    }
    
    /*
     * The specific startup behavior for this sample.
     * Should be changed for other examples.
     */
    private void startup() {
        editview.setViewState("");
        editview.setEditable(true);
    }
    
    /*
     * The specific closing behavior for this sample.
     * Should be changed for other examples.
     */
    private void finish() {
        editview.setEditable(false);
    }
    
}
