001 /* 002 * Calculator Default ButtonHandler 003 * 004 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 005 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 006 * CS101 homepage</a> or email <las@ai.mit.edu>. 007 * 008 * Copyright (C) 1999 Massachusetts Institute of Technology. 009 * Please do not redistribute without obtaining permission. 010 */ 011 package calculator; 012 import cs101.io.Console; 013 import cs101.lang.Animate; 014 import cs101.lang.AnimatorThread; 015 016 /** 017 * This class provides the smarts for a basic four-function 018 * calculator. It repeatedly calls its Calculator(GUI)'s getButton() 019 * to consume the buttonIDs that the Calculator object produces. <p> 020 * 021 * <P>Copyright (c) 1999 Massachusetts Institute of Technology 022 * 023 * @author Todd C. Parnell, tparnell@ai.mit.edu 024 * @author Emil Sit, sit@mit.edu 025 * @author Lynn Andrea Stein, las@ai.mit.edu 026 * @version $Id: ButtonHandler.java,v 1.1.1.1 2002/06/05 21:56:34 root Exp $ 027 * 028 * @see Calculator 029 * @see cs101.util.semaphore.IntBuffer 030 */ 031 032 public class ButtonHandler implements Animate 033 { 034 035 protected Calculator gui; 036 protected CalculatorState brains; 037 protected AnimatorThread spirit; 038 039 /** 040 * Build a ButtonHandler object from a Calculator gui passed in as 041 * an argument. 042 */ 043 protected ButtonHandler( CalculatorGUI myGUI ) 044 { 045 this.gui = myGUI; 046 this.brains = new CalculatorState( this.gui ); 047 this.brains.resetCalc(); 048 this.spirit = new AnimatorThread(this); 049 this.spirit.start(); 050 } 051 052 protected ButtonHandler( ) 053 { 054 this( new CalculatorGUI() ); 055 } 056 057 /** 058 * Provides the interactive control loop of this animate object. 059 * @see cs101.lang.Animate 060 */ 061 public void act () 062 { 063 int buttonID = this.gui.getButton(); 064 065 // Deal with number buttons -- add to display text 066 if ( buttonID < 10 && buttonID >= 0) 067 { 068 this.brains.handleNumKey( buttonID ); 069 return; 070 } 071 072 // deal with everything else 073 switch ( buttonID ) 074 { 075 case Calculator.DOT: 076 this.brains.handleDecimal(); 077 break; 078 079 case Calculator.OP_MUL: 080 case Calculator.OP_DIV: 081 case Calculator.OP_ADD: 082 case Calculator.OP_SUB: 083 this.brains.handleOperation( buttonID ); 084 break; 085 086 case Calculator.EQUALS: 087 this.brains.handleEquals(); 088 break; 089 090 case Calculator.CLEAR: 091 this.brains.resetCalc(); 092 break; 093 094 default: 095 Console.println("ERROR: unmatched buttonID."); 096 Console.println( this.brains.toString() ); 097 } 098 } 099 } 100 101 102 /* Comments: 103 * 104 * History: 105 * $Log: ButtonHandler.java,v $ 106 * Revision 1.1.1.1 2002/06/05 21:56:34 root 107 * CS101 comes to Olin finally. 108 * 109 * Revision 1.3 2000/05/01 06:25:19 mharder 110 * Changed javadoc to agree with new cs101 packages. 111 * 112 * Revision 1.2 1999/10/17 05:40:59 jsmthng 113 * Created constructor which took in CalculatorGUI (because it was 114 * complaining that there wasn't one, before) -- no-parameters 115 * constructor still exists. 116 * 117 * Revision 1.1 1999/10/08 15:09:23 las 118 * This pset replaces the old Calculator pset. However, not everything 119 * has been transferred. At the moment, it's just java, doc, and index. 120 * The rest are still in the repository under Calculator-Old. 121 * 122 * Revision 1.5 1998/07/24 16:37:13 tparnell 123 * Placate new javadoc behavior 124 * 125 * Revision 1.4 1998/07/23 14:28:26 tparnell 126 * made class public 127 * 128 * Revision 1.3 1998/07/06 20:22:56 tparnell 129 * changed from while (true)... to while (!this.stopped)... in support of 130 * JDK1.2 deprecation of Thread.stop() 131 * 132 * Revision 1.2 1998/06/05 05:19:26 craigh 133 * added getButtonLabel() to Calculator interface. Implemented the 134 * method in CalculatorGUI, and made use of it in ButtonHandler. 135 * 136 * Revision 1.1 1998/02/26 17:25:43 tparnell 137 * Reconstruction from hard drive failure. Everything appears intact. 138 * 139 * Revision 1.3 1997/10/05 21:11:18 shong 140 * Updated for fall97, to Java 1.1 141 * changed GUI, using 1.1 Event Model 142 * 143 * Revision 1.2 1997/07/16 14:15:19 tparnell 144 * *** empty log message *** 145 * 146 * Revision 1.2 1996/10/04 16:20:17 las 147 * Transformed Calculator into an application and made it a package. See 148 * STAFF_SETUP for which files are public. To run, use Calculator.Main. 149 * 150 * Specifics: 151 * Added Main.java, which starts the calculator program (both 152 * CalculatorGUI and ButtonHandler); 153 * Made Calculator an interface; 154 * Moved GUI implementation (previously in Calculator) to 155 * CalculatorGUI. 156 * Added clear button, which looks pretty gross right now. (It can 157 * be deleted in a single line, though.) 158 * 159 * 160 */ 161