first commit
This commit is contained in:
207
CS1322/p6/Extra Credit/EventHandler.java
Normal file
207
CS1322/p6/Extra Credit/EventHandler.java
Normal file
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* EventHandler.java
|
||||
*
|
||||
* Revisions: 1.1 Mar. 14, 2001
|
||||
* Added minor modifications to event handling behavior. Added
|
||||
* some documentation.
|
||||
* 1.0 Jan. 13, 2001
|
||||
* Created the EventHandler class
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:demox@cc.gatech.edu">Luke A. Olbrish</A>
|
||||
* @version Version 1.0, Jan. 13, 2001
|
||||
*/
|
||||
|
||||
import java.awt.event.*;
|
||||
|
||||
public class EventHandler implements ActionListener
|
||||
{
|
||||
// Data Block
|
||||
|
||||
/**
|
||||
* The actual gui that will be the view for this program.
|
||||
*/
|
||||
private CalculatorGUI gui = null;
|
||||
|
||||
/**
|
||||
* This is the model that will actually perform calculations.
|
||||
*/
|
||||
private Calculator calc = null;
|
||||
|
||||
/**
|
||||
* This state variable will be used to keep track of whether the number
|
||||
* on the display needs to be replaced by new digits.
|
||||
*/
|
||||
private boolean isNumberStarted = false;
|
||||
|
||||
// Constructors
|
||||
|
||||
/**
|
||||
* Creates a new event handler that is bound by the gui view thats passed
|
||||
* as a parameter.
|
||||
*
|
||||
* @param gui the view that this control connects to.
|
||||
*/
|
||||
public EventHandler( CalculatorGUI gui )
|
||||
{
|
||||
// If the gui is null, then this class is being used improperly and should
|
||||
// exit.
|
||||
if( gui == null )
|
||||
System.exit( 0 );
|
||||
|
||||
// Add the model and view to the control.
|
||||
this.gui = gui;
|
||||
this.calc = new Calculator();
|
||||
}// end of constructor( CalculatorGUI )
|
||||
|
||||
// Action Listener
|
||||
|
||||
/**
|
||||
* This function recieves an action event and interprets the event to take
|
||||
* the proper action. It receives all the button actions from
|
||||
* CalculatorGUI.
|
||||
*
|
||||
* @param ae the action event that needs to be processed.
|
||||
*/
|
||||
public void actionPerformed( ActionEvent ae )
|
||||
{
|
||||
// No action to process.
|
||||
if(( ae == null ) ||
|
||||
( ae.getActionCommand() == null ))
|
||||
return;
|
||||
|
||||
// Clear the calculator screen and model.
|
||||
if( ae.getActionCommand().equals( "Clr" ) )
|
||||
doClearCalculator();
|
||||
|
||||
// Find out if an operation was selected and handle it.
|
||||
switch( ae.getActionCommand().charAt(0) )
|
||||
{
|
||||
case '=':
|
||||
doCalculation();
|
||||
break;
|
||||
case '+':
|
||||
doSetOp( Calculator.ADD_OP );
|
||||
break;
|
||||
case '-':
|
||||
doSetOp( Calculator.SUB_OP );
|
||||
break;
|
||||
case '*':
|
||||
doSetOp( Calculator.MUL_OP );
|
||||
break;
|
||||
case '/':
|
||||
doSetOp( Calculator.DIV_OP );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}// end of switch
|
||||
|
||||
// Find out if a digit was pressed
|
||||
if( Character.isDigit( ae.getActionCommand().charAt(0) ) )
|
||||
doAddDigit( ae.getActionCommand() );
|
||||
}// end of actionPerformed( ActionEvent )
|
||||
|
||||
// Operational Functions
|
||||
|
||||
/**
|
||||
* This function assists the EventHandler by doing the actions needed to
|
||||
* add a digit to the display.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
* @param digit a string that should have a single digit contents.
|
||||
*/
|
||||
private void doAddDigit( String digit )
|
||||
{
|
||||
// If diplay needs to be cleared before the digit is added.
|
||||
if( !isNumberStarted )
|
||||
{
|
||||
gui.setDisplay( digit );
|
||||
isNumberStarted = true;
|
||||
}
|
||||
else
|
||||
gui.setDisplay( gui.getDisplay() + digit );
|
||||
}// end of doAddDigit( String )
|
||||
|
||||
/**
|
||||
* Clears the gui display and resets the calculator model.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
private void doClearCalculator()
|
||||
{
|
||||
isNumberStarted = false;
|
||||
gui.setDisplay( " " );
|
||||
calc.clearCalulator();
|
||||
return;
|
||||
}// end of doClearCalculator()
|
||||
|
||||
/**
|
||||
* Attempts to do a calculation. GUI is updated based on whether a
|
||||
* calculation occurs and returns the calculation to the gui.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
public void doCalculation()
|
||||
{
|
||||
// holds the parsed operator #2.
|
||||
int val = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to parse an int from the gui.
|
||||
val = Integer.parseInt( gui.getDisplay() );
|
||||
|
||||
// If preconditions are met, then attempt a calculation.
|
||||
if(( calc.isOperator1Set() ) &&
|
||||
( calc.isOperationSet() ))
|
||||
{
|
||||
// Calculate and update gui.
|
||||
calc.setOperator2( val );
|
||||
gui.setDisplay( "" + calc.getCalculation() );
|
||||
// Reset display to take in numbers.
|
||||
isNumberStarted = false;
|
||||
}// end of if
|
||||
}// end of try
|
||||
catch( NumberFormatException nfe )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Error!" );
|
||||
return;
|
||||
}// end of catch( NumberFormatException )
|
||||
catch( ArithmeticException ae )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Calc. Error!" );
|
||||
return;
|
||||
}// end of catch( ArithmeticException )
|
||||
}// end of doCalculation()
|
||||
|
||||
/**
|
||||
* Set the operation for the calculation. If the operation is already set,
|
||||
* then do a calculation then set the operation.
|
||||
*
|
||||
* @see #actionPerformed
|
||||
*/
|
||||
public void doSetOp( int operation )
|
||||
{
|
||||
try
|
||||
{
|
||||
// Do calculation if operation is already set.
|
||||
if( calc.isOperationSet() )
|
||||
doCalculation();
|
||||
|
||||
// Set the first operator and set the operation.
|
||||
calc.setOperator1( Integer.parseInt( gui.getDisplay() ));
|
||||
calc.setOperation( operation );
|
||||
isNumberStarted = false;
|
||||
}// end of try.
|
||||
catch( NumberFormatException nfe )
|
||||
{
|
||||
calc.clearCalulator();
|
||||
gui.setDisplay( "Error!" );
|
||||
}// catch( NumberFormatException )
|
||||
}// end of doSetOp( int )
|
||||
|
||||
}// end of class EventHandler
|
||||
Reference in New Issue
Block a user