/***************************************************************
 * Source: @(#)$Id$
 * Scope:  SetContr class
 *
 *  12/16/1999  GVT  First Implementation
 ***************************************************************/

package counter.swing.controllers;

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import gvt.mvc.*;

import counter.models.*;

/**
 * This class implements a panel 
 * with a text field and calls the set method
 * of the counter model
 *
 * This is a "pure" controller
 *
 * @author   Giuseppe Vitillaro
 * @version  $Id$
 */
public class SetContr extends JPanel
    implements ActionListener {

    private Counter counter;
    private JTextField set;

    private Toolkit toolkit = Toolkit.getDefaultToolkit();

    
    /**     
     * The Constructor: it build the textfield
     * save a reference of the model and hooks
     * the hander for actions in the text field
     */ 
    public SetContr ( Counter counter ) {
	this.counter = counter;

	set = new JTextField ( 10 );

	add ( set );

	set.addActionListener ( this );
    }


    /**
     *  The action event handler for
     *  the text field in this controller
     */
    public void actionPerformed ( ActionEvent e ) {
	try {
	    Integer n = new Integer ( set.getText() );
	    if ( n.intValue() < 0 )
		toolkit.beep();
	    else {
		counter.set ( n );
		set.setText ( "" );
	    }
	}
	catch ( NumberFormatException exc ) {
	    toolkit.beep();
	}
    }
}
