Packageardisia.components.calculator
Classpublic class Calculator
InheritanceCalculator Inheritance spark.components.supportClasses.SkinnableComponent
Implements mx.managers.IFocusManagerComponent

Simple Calculator class that evaluates simple expressions. Use the included CalculatorGridEditor to attach the calculator to numerical fields in the DataGrid.

View the technical documentation on the Ardisia Labs website for more information.



Public Properties
 PropertyDefined By
  animationDuration : Number = 150
The duration of each animation (animating out and animating in).
Calculator
  easer : IEaser
The easer to use for animations.
Calculator
  fractionalDigits : int = 5
The maximum number of digits after the decimal to display after an expression is evaluated.
Calculator
  value : Number
The last committed value.
Calculator
Public Methods
 MethodDefined By
  
dispose():void
Remove the component from the display list and prepare it for GC.
Calculator
Protected Methods
 MethodDefined By
  
animationHandler(event:EffectEvent):void
Handles the end of the first part of the animation.
Calculator
  
backspaceButtonPartHandler(event:Event):void
Handles events on the backspaceButtonPart.
Calculator
  
buttonsHandler(event:Event):void
Handles events on the skin part buttons.
Calculator
  
calculateButtonPartHandler(event:Event):void
Handles events on the calculateButtonPart.
Calculator
  
clear():void
Call to clear the value and display label.
Calculator
  
clearButtonPartHandler(event:Event):void
Handles events on the clearButtonPart.
Calculator
  
convertStringToArray(str:String):Array
Parses a string into an array by splitting it via the operators present.
Calculator
  
displayLabelPartHandler(event:Event):void
Handles events on the displayLabelPart.
Calculator
  
evaluateExpression(expression:String):Number
Takes the expression, evaluates it, and returns the number or NaN.
Calculator
  
evaluateOperator(values:Array, operatorsArray:Array):Array
Walks the expression parsed into an array, and evaluates left to right based on the passed operators.
Calculator
  
finalAnimationHandler(event:EffectEvent):void
Handles the end of the 2nd part of the animation.
Calculator
  
isDecimalMark(value:String):Boolean
Returns whether the passed symbol is a decimal mark.
Calculator
  
isOperand(value:Object):Boolean
Check if the passed value is an operand, or an Operator.
Calculator
  
keyDownHandler(event:KeyboardEvent):void
[override] Handles keyboard input when the calculator has focus.
Calculator
  
searchForOperators(haystack:String, needle:String, replacementNeedle:String, delimiter:String):String
Searches the expression for the passed operator and inserts the replacement string and a delimiter in its place.
Calculator
  
setValue(value:Number):void
Update the value property and any bindings.
Calculator
  
update():void
Call to evaluate the current expression and update the value.
Calculator
  
Update the display label skin part.
Calculator
  
validation():void
Validates the expression after each change to the DisplayLabelPart.
Calculator
Events
 Event Summary Defined By
  Dispatched when the "value" property changes via user interaction.Calculator
  Dispatched when the "value" property changes for any reason.Calculator
Styles
 Style Description Defined By
  
backgroundAlpha
Type: Number CSS Inheritance: no
The alpha of the background.
Calculator
  
backgroundColor
Type: uint Format: Color CSS Inheritance: no
The color of the background.
Calculator
Skin Parts

This component uses skins made up of skin parts. Do not set the skin parts directly. The component's skin sets the skin parts.


 Skin Part Description Defined By
  
addButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a + to the expression.
Calculator
  
backspaceButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will remove the last character. Made this a skin part to move the somewhat complex code out of the skin to keep the skin lightweight.
Calculator
  
button0Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 0 to the expression.
Calculator
  
button1Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 1 to the expression.
Calculator
  
button2Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 2 to the expression.
Calculator
  
button3Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 3 to the expression.
Calculator
  
button4Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 4 to the expression.
Calculator
  
button5Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 5 to the expression.
Calculator
  
button6Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 6 to the expression.
Calculator
  
button7Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 7 to the expression.
Calculator
  
button8Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 8 to the expression.
Calculator
  
button9Part:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a 9 to the expression.
Calculator
  
calculateButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will evaluate the current expression.
Calculator
  
clearButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will clear the expression.
Calculator
  
decimalButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a ./, to the expression.
Calculator
  
displayLabelPart:spark.components.RichEditableText
Required: true Part Type: Static
The skin part that displays the expression or value.
Calculator
  
displayLabelWrapperPart:spark.components.Group
Required: true Part Type: Static
The skin part that wraps the DisplayLabel. Required to ensure that the displayLabelPart is parented by a Group so clipping can be enabled for the animation to work.
Calculator
  
divideButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a / to the expression.
Calculator
  
minusButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a - to the expression.
Calculator
  
multiplyButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a to the expression.
Calculator
  
sqrtButtonPart:spark.components.Button
Required: false Part Type: Static
Optional skin part button that when clicked, will add a √ to the expression.
Calculator
Skin States

To skin the component, implement a skin that defines the following states. Although you must implement all skin states, a skin state can be empty. An empty skin state specifies no changes to the default skin state.


 Skin State Description Defined By
  
disabled
Calculator
  
normal
Calculator
Property Detail
animationDurationproperty
public var animationDuration:Number = 150

The duration of each animation (animating out and animating in). Set to 0 to skip the animation.

This animationDuration is applied twice in a sequence. E.G. the total animation duration is double this value.

The default value is 150.

easerproperty 
public var easer:IEaser

The easer to use for animations.

The default value is Sine(0.5).

fractionalDigitsproperty 
public var fractionalDigits:int = 5

The maximum number of digits after the decimal to display after an expression is evaluated.

The default value is 5.

valueproperty 
value:Number

The last committed value. Commits occur when an expression is evaluated.

This property is bindable.

The default value is 0.

This property can be used as the source for data binding.


Implementation
    public function get value():Number
    public function set value(value:Number):void
Method Detail
animationHandler()method
protected function animationHandler(event:EffectEvent):void

Handles the end of the first part of the animation. Starts the 2nd half of the cash register animation.

Parameters

event:EffectEvent


Events
the — that triggered the handler
backspaceButtonPartHandler()method 
protected function backspaceButtonPartHandler(event:Event):void

Handles events on the backspaceButtonPart.

Parameters

event:Event — the Event that triggered the handler

buttonsHandler()method 
protected function buttonsHandler(event:Event):void

Handles events on the skin part buttons.

Parameters

event:Event — the Event that triggered the handler

calculateButtonPartHandler()method 
protected function calculateButtonPartHandler(event:Event):void

Handles events on the calculateButtonPart.

Parameters

event:Event — the Event that triggered the handler

clear()method 
protected function clear():void

Call to clear the value and display label.

clearButtonPartHandler()method 
protected function clearButtonPartHandler(event:Event):void

Handles events on the clearButtonPart.

Parameters

event:Event — the Event that triggered the handler

convertStringToArray()method 
protected function convertStringToArray(str:String):Array

Parses a string into an array by splitting it via the operators present.

Parameters

str:String — the String to parse

Returns
Array — the parsed array
displayLabelPartHandler()method 
protected function displayLabelPartHandler(event:Event):void

Handles events on the displayLabelPart.

Parameters

event:Event — the Event that triggered the handler

dispose()method 
public function dispose():void

Remove the component from the display list and prepare it for GC.

evaluateExpression()method 
protected function evaluateExpression(expression:String):Number

Takes the expression, evaluates it, and returns the number or NaN.

Parameters

expression:String — the expression as a String

Returns
Number
evaluateOperator()method 
protected function evaluateOperator(values:Array, operatorsArray:Array):Array

Walks the expression parsed into an array, and evaluates left to right based on the passed operators.

Parameters

values:Array — the expression parsed as an array
 
operatorsArray:Array — the array of Operators to evaluate

Returns
Array — the modified passed values array
finalAnimationHandler()method 
protected function finalAnimationHandler(event:EffectEvent):void

Handles the end of the 2nd part of the animation.

Parameters

event:EffectEvent


Events
the — that triggered the handler
isDecimalMark()method 
protected function isDecimalMark(value:String):Boolean

Returns whether the passed symbol is a decimal mark. E.G. whether it is a "." in the anglosphere (is the USA part of the anglosphere? -> YES, but the USA is not in the Commonwealth) or "," everywhere else.

Parameters

value:String — the String to test

Returns
Boolean
isOperand()method 
protected function isOperand(value:Object):Boolean

Check if the passed value is an operand, or an Operator.

Parameters

value:Object — the Object to check

Returns
Boolean — a Boolean
keyDownHandler()method 
override protected function keyDownHandler(event:KeyboardEvent):void

Handles keyboard input when the calculator has focus. Handles the basic calculator keys like ENTER, BACKSPACE, ESCAPE, "=" and the basic symbols.

Parameters

event:KeyboardEvent — the KeyboardEvent that triggered the handler

searchForOperators()method 
protected function searchForOperators(haystack:String, needle:String, replacementNeedle:String, delimiter:String):String

Searches the expression for the passed operator and inserts the replacement string and a delimiter in its place.

Parameters

haystack:String — the String to search
 
needle:String — the String to search for
 
replacementNeedle:String — the String to replace the needle with
 
delimiter:String — the String to wrap the replacement needle around

Returns
String — the new String
setValue()method 
protected function setValue(value:Number):void

Update the value property and any bindings. Does not invalidate properties.

Parameters

value:Number — the Number to set

update()method 
protected function update():void

Call to evaluate the current expression and update the value.

updateDisplayLabelPart()method 
protected function updateDisplayLabelPart():void

Update the display label skin part.

validation()method 
protected function validation():void

Validates the expression after each change to the DisplayLabelPart.

Checks that the expression makes sense. Also, applies the emptyString as needed and clears the expression based on internal flags.

Event Detail
change Event
Event Object Type: ardisia.components.calculator.events.CalculatorEvent
CalculatorEvent.type property = ardisia.components.calculator.events.CalculatorEvent

Dispatched when the "value" property changes via user interaction.

valueCommit Event  
Event Object Type: mx.events.FlexEvent
FlexEvent.type property = mx.events.FlexEvent

Dispatched when the "value" property changes for any reason.