package programming101.calculBeanz;

import FR.essi.sander.graphix.mvc.ControlEvent;
import FR.essi.sander.graphix.mvc.ControlListener;
import FR.essi.sander.graphix.mvc.ModelEvent;
import FR.essi.sander.graphix.mvc.ModelImpl;
import FR.essi.sander.graphix.mvc.ModelListener;


/**
	* Defines a very simple calculator which combines a cumulative numeric
	* value with a new numeric value according to some operation.
	* The calculator as a whole rigorously applies the MVC (Model-View-Control)
	* "design pattern", where the calculator state is the model (that's this component),
	* the various buttons provide the control, and the display region is the
	* display (really!).  Communication is by way of events, e.g., the
	* calculator (M component) registers itself as interested by button events
	* (the C components), and when a button is pushed the appropriate event gets sent to the
	* calculator which then calculates and posts an event to the display (the V component).
	* This has the advantage of being modular, with each component in fact being a JavaBean.
	* <P>
	* This class
	* <UL>
	* <LI><CODE>extends FR.essi.sander.graphix.mvc.ModelImpl</CODE><BR>
	* superclass supplies management for <CODE>ModelListener</CODE>s so we don't have to
	* <LI><CODE>implements FR.essi.sander.graphix.mvc.ControlListener</CODE><BR>
	* guarantees code for the <CODE>update</CODE> method which is invoked by control events
	* <LI><CODE>implements java.io.Serializable</CODE><BR>
	* needed for JavaBeans
	* </UL>
	*
	* @author © 1998 Peter T. Sander
	* @version 1.2 (6/04/98)
	* @since 18/03/98
	*/

public class Calculator extends ModelImpl 
							implements ControlListener, java.io.Serializable {
	// visible attributes	
	//  none
	// other variables
	private double cumulator;
	private double entry;
	private CalculatorButton lastButton;
	
	/**
		* No-args constructor (needed for JavaBeans).
		*/
	public Calculator() {
		super();
	}

	/**
		* Method guaranteed by implementing the <CODE>ControlListener</CODE>
		* interface.  Invoked by any controls which have registered this
		* calculator as interested by their events.
		*
		* @param ce a control button event
		*/
	public void update(ControlEvent ce) {
		// determines what type of control event invoked this update
		if (ce.getControl() instanceof CalculatorButton) {  // button pushed
			// determines the new cumulator value from the button that was clicked
			cumulator = ((CalculatorButton)ce.getControl()).op(cumulator, entry, lastButton);
			// fires an event at the view component (the display/entry field) to display it
			fireModelEvent(new ModelEvent(this));
			// stores the button that was just clicked
			lastButton = (CalculatorButton)ce.getControl();
		} else if (ce.getControl() instanceof DisplayField) {  // new value entered
			entry = Double.valueOf(((DisplayField)ce.getControl()).getText()).doubleValue();
		}
	}
	
	/**
		* Getter method for cumulator attribute.
		*
		* @return cumulator value
		*/
	public double getCumulator() {
		return cumulator;
	}
}
