<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/***************************************************************
 * Source: @(#)$Id$
 * Scope:  BasicContr class
 *
 *  12/16/1999  GVT  First Implementation
 ***************************************************************/

package counter.swing.controllers;

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

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 JPanel
    implements View, ActionListener {
    
    private Counter counter;
    
    private JButton incr;
    private JButton decr;
    private JButton 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 JButton ( "+" );
	decr   =  new JButton ( "-" );
	reset  =  new JButton ( "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() &lt;= 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();
	}
    }
}




</pre></body></html>