/* * ButtonHandler Class * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1997 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package Calculator; /** * This class is the "smarts" for a Calculator. There should be one * for each Calculator. Creating it sets up all of the other * infrastructure required to handle button presses; it is an * interactive control loop and runs asynchronously with the * Calculator GUI.

* * The heart of a ButtonHandler instance is an interactive control * loop that operates in an object-oriented polymorphic-dispatch mode: * that is, each button pressed is associated with an intelligent * ButtonObj(ect), and that button object is asked to handle itself. * There's also a single calculator state object that each of the * button objects can use. * * @author: Lynn Andrea Stein, las@ai.mit.edu * @author: Luis F. G. Sarmenta, lfgs@mit.edu * @version: $$ * * @see Calculator, CalculatorState, ButtonObj */ public class ButtonHandler implements Runnable { /** * This object's animacy.

* Initialized by constructor, doesn't change. */ private Thread spirit; /** * This object's gui.

* Used to getButton(), getText(), setText(S). Received by * constructor and recorded here. Doesn't change. */ private Calculator gui; /** * This object's state information. Start with a fresh one. */ private CalculatorState stateObj = new CalculatorState(); /** * The translation between buttons pressed and smart button objects. * buttonObjs[BUTTON_ID] is the ButtonObj that handles BUTTON_ID.

* Initialized by constructor; doesn't change. */ private ButtonObj[] buttonObjs = new ButtonObj[Calculator.LAST]; /** * This constructor sets up a button handler control loop and the * myriad objects that will actually do the work.

* * @param realGUI The Calculator whose button presses this object * is handling. */ public ButtonHandler( Calculator realGUI ) { this.gui = realGUI; // hang on to the GUI for later (getButton()ing) // also set up a textGUI for the various ButtonObjs to use. CalcTextGUI textGUI = new CalcTextGUI( realGUI ); // Set up the buttonObjs array: who handles which button? // First, set up the number buttons. for ( int i = 0; i < 10; i++ ) { // Numbers are handled by NumButtonObjs that know their numbers this.buttonObjs[i] = new NumButtonObj( i, textGUI, this.stateObj ); } // Next, set up the Dot button... this.buttonObjs[Calculator.DOT] = new DotButtonObj( this.buttonObjs[0], textGUI, this.stateObj ); //...and the equals button. ButtonObj equalButton = this.buttonObjs[Calculator.EQUALS] = new EqualButtonObj( textGUI, this.stateObj ); // Finally, the operators: this.buttonObjs[Calculator.OP_DIV] = new OpButtonObj( Calculator.OP_DIV, equalButton, textGUI, this.stateObj ); this.buttonObjs[Calculator.OP_MUL] = new OpButtonObj( Calculator.OP_MUL, equalButton, textGUI, this.stateObj ); this.buttonObjs[Calculator.OP_ADD] = new OpButtonObj( Calculator.OP_ADD, equalButton, textGUI, this.stateObj ); this.buttonObjs[Calculator.OP_SUB] = new OpButtonObj( Calculator.OP_SUB, equalButton, textGUI, this.stateObj ); this.buttonObjs[Calculator.CLEAR] = new ClearButtonObj( textGUI, this.stateObj ); // Since the ButtonHandler is self-animating, create a Thread for // it and start up the Thread. this.spirit = new Thread ( this ); this.spirit.start(); } /** Called by the ButtonHandler's Thread, this is how this object * behaves on an ongoing/independent basis. Gets a button, * handles it, repeat.

* *How to handle a button? Ask the associated "smart object" to *handle itself! (Typical middle management :o) ). */ public void run () { while (true) // forever.... { int buttonID = this.gui.getButton(); // get a button... this.buttonObjs[buttonID].handleButton(); // ...and handle it. } } } /* * $Log: ButtonHandler.java,v $ * Revision 1.1.1.1 2002/06/05 21:56:25 root * CS101 comes to Olin finally. * * Revision 1.2 1997/10/24 22:20:40 las * Documented all of the code files, but didn't write the user manual (yet). * */