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

package counter.awt.controllers;

import java.awt.*;
import java.awt.event.*;

import gvt.mvc.*;

import counter.models.*;


/**
 * This class implements a panel
 * of buttons to control a counter
 *
 * @author   Giuseppe Vitillaro
 * @version  $Id$
 */
public class BasicContr extends Panel
    implements View, ActionListener {
    
    private Counter counter;
    
    private Button incr;
    private Button decr;
    private Button reset;

    
    /**
     * The Constructor: it build the needed buttons
     * save a reference of the model and hooks
     * the handler for actions on the buttons
     */
    public BasicContr ( Counter counter ) {
	this.counter = counter;
	
	incr   =  new Button ( "+" );
	decr   =  new Button ( "-" );
	reset  =  new Button ( "rst" );

	add ( incr );
	add ( decr );
	add ( reset );

	incr.addActionListener ( this );
	decr.addActionListener ( this );
	reset.addActionListener ( this );
    }


    /**
     * This controller is also a view
     * when called from the model it sets
     * the enabled status of the decr button
     */
    public void updateView ( Model model ) {
	Counter m = (Counter)model;

	if ( m.get().intValue() <= 0 )
	    decr.setEnabled ( false );
	else
	    decr.setEnabled ( true );
    }

    
    /**
     *  The action event handler for
     *  the buttons in this controller
     */
    public void actionPerformed ( ActionEvent e ) {
	Object source = e.getSource();

	if ( source == incr ) {
	    counter.incr();
	}

	if ( source == decr ) {
	    counter.decr();
	}

	if ( source == reset ) {
	    counter.reset();
	}
    }
}




