com.numobjects.mathbeans.awt
Class CalcPanel

com.numobjects.mathbeans.awt.CalcPanel

public class CalcPanel
implements java.io.Serializable, java.awt.event.ActionListener

Class CalcPanel (Calculator panel bean) provides the following functionality: based solely on a string representing a mathematical expression with any number of variables, it automatically constructs and displays a mini-calculator panel for this expression. The panel will have input fields for all variables in the expression, each with the label representing its name, and similar fields and labels for results. The user enters any numbers into the input fields and presses Enter in any of these fields. The result is immediately computed and displayed in the result field. The user can set parameters that control the format of the output. If desired, the appearance of the panel can be further customized. For instance, one can add a button to the panel to control the start of calculation (in addition to pressing Enter). To do so just register the panel as this button's ActionListener. See the java.awt package docs for information how to do this.

This bean provides a unique possibility of creating various calculators, static or dynamic, avoiding the hassle of setting up GUI, adding logic, dispatching events, etc. The user's only input is the formula set itself, all the rest is figured out by the bean.

The formulas must have the form

Result1=expression1;...ResultN=expressionN

Here "ResultX" is any string providing the name for the result, e.g. "Total amount". The "expressionX" must be a valid non-constant mathematical expression (i.e. must contain variables). Variable names in the expression cannot contain spaces; if there is a need for the name to consist of several words, use quotes around the name in the formula. Here is one simple example:

"Total tax"=Quantity*"Item price"*"Tax rate"/100;
"Total amount"=Quantity*"Item price"*(1+"Tax rate"/100)+Shipping

The resulting calculator panel would contain six fields labeled "Quantity", "Item price", "Tax rate", "Shipping", "Total tax" and "Total amount". As soon as one enters values in the first four fields and presses Enter, the fields labeled "Total tax" and "Total amount" will display the computed value. Missing values are treated as zero.

Here is the code that you have to include in your program:

import com.numobjects.mathbeans.awt.CalcPanel;
...
String formula = "\"Total tax\"=Quantity*\"Item price\"*\"Tax rate\"/100;\"Total amount\"=Quantity*\"Item price\"*(1+\"Tax rate\"/100)+Shipping";
CalcPanel panel = new CalcPanel ();
panel.setFormula (formula);

The calculator panel is ready to be added to your container.

The parameter entry fields can be any subclasses of java.awt.TextField, but the default entry fields are in fact CalcFields, so it is safe to cast them to this type if desired as well as use them to compute constant expressions.

The new feature in version 2 is the ability to support several formulas.

See Also:
CalcField, CalcGraphPanel, Serialized Form

Constructor Summary
CalcPanel()
          Constructs new CalcPanel
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Handles action events
 int getFieldHeight()
          Gets entry field height
 int getFieldWidth()
          Gets entry field width
 java.lang.String getFormulae()
          Gets formula set
 int getHorizontalSpacing()
          Gets horizontal spacing between fields
 java.awt.Insets getInsets()
          Gets insets
 java.awt.Dimension getMinimumSize()
          Gets minimum size for the panel
 java.awt.TextField getParameterField(java.lang.String parameterName)
          Gets parameter entry field for given parameter name
 int getPrecision()
          Gets precision
 java.awt.TextField getResultField(java.lang.String resultName)
          Gets field for the result
 java.lang.String getResultName(int index)
          Gets name (label) for the result field
 java.lang.String[] getResultNames()
          Gets result names
 java.lang.String getResultNames(int i)
          Gets i-th result name
 int getRounding()
          Gets rounding
 java.lang.String getValue(java.lang.String parameterName)
          Gets parameter value
 int getVerticalSpacing()
          Gets vertical spacing between fields
 boolean isDisplayBorder()
          Gets border display flag
 boolean isDisplayLogo()
          Gets logo display flag
 void paint(java.awt.Graphics g)
          Paints the panel
 void setBackground(java.awt.Color c)
          Sets background
 void setDisplayLogo(boolean flag)
          Sets logo display flag
 void setFieldHeight(int value)
          Sets entry field height
 void setFieldWidth(int value)
          Sets entry field width
 void setFormulae(java.lang.String formulaSet)
          Sets formula set
 void setHorizontalSpacing(int value)
          Sets horizontal spacing
 void setInsets(java.awt.Insets insets)
          Sets insets for the panel
 void setParameterError(java.lang.String parameterName)
          Sets parameter error
 void setParameterField(java.lang.String parameterName, java.awt.TextField field)
          Tags a text field as a parameter field for given parameter name
 void setPrecision(int value)
          Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
 void setResultField(java.lang.String resultName, java.awt.TextField field)
          Tags a text field as the result field
 void setResultNames(int i, java.lang.String resultName)
          Sets result labels
 void setResultNames(java.lang.String[] resultNames)
          Sets result labels
 void setResultValue(int index, java.lang.String result)
          Sets result value for given index
 void setRounding(int value)
          Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
 void setVerticalSpacing(int value)
          Sets vertical spacing
 

Constructor Detail

CalcPanel

public CalcPanel()
Constructs new CalcPanel
Method Detail

setPrecision

public void setPrecision(int value)
Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
Parameters:
value - precision to set

getPrecision

public int getPrecision()
Gets precision
Returns:
precision value (the number of significant digits in the calculation)

setRounding

public void setRounding(int value)
Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
Parameters:
value - rounding

getRounding

public int getRounding()
Gets rounding
Returns:
rounding value (the number of digits displayed after the decimal dot)

setInsets

public void setInsets(java.awt.Insets insets)
Sets insets for the panel
Parameters:
insets - insets

getInsets

public java.awt.Insets getInsets()
Gets insets
Returns:
insets

setVerticalSpacing

public void setVerticalSpacing(int value)
Sets vertical spacing
Parameters:
value - value to set

getVerticalSpacing

public int getVerticalSpacing()
Gets vertical spacing between fields
Returns:
vertical spacing

setHorizontalSpacing

public void setHorizontalSpacing(int value)
Sets horizontal spacing
Parameters:
value - value to set

getHorizontalSpacing

public int getHorizontalSpacing()
Gets horizontal spacing between fields
Returns:
horizontal spacing

setFieldWidth

public void setFieldWidth(int value)
Sets entry field width
Parameters:
value - value to set

getFieldWidth

public int getFieldWidth()
Gets entry field width
Returns:
field width

setFieldHeight

public void setFieldHeight(int value)
Sets entry field height
Parameters:
value - value to set

getFieldHeight

public int getFieldHeight()
Gets entry field height
Returns:
field height

setDisplayLogo

public void setDisplayLogo(boolean flag)
Sets logo display flag
Parameters:
flag - value to set

isDisplayLogo

public boolean isDisplayLogo()
Gets logo display flag
Returns:
logo flag

isDisplayBorder

public boolean isDisplayBorder()
Gets border display flag
Returns:
border flag

getFormulae

public java.lang.String getFormulae()
Gets formula set
Returns:
formula set string

setFormulae

public void setFormulae(java.lang.String formulaSet)
Sets formula set
Parameters:
formulaSet - formula set string

setResultNames

public void setResultNames(java.lang.String[] resultNames)
Sets result labels
Parameters:
resultNames - result names to set

getResultNames

public java.lang.String[] getResultNames()
Gets result names
Returns:
result names

setResultNames

public void setResultNames(int i,
                           java.lang.String resultName)
Sets result labels
Parameters:
resultNames - result names to set

getResultNames

public java.lang.String getResultNames(int i)
Gets i-th result name
Parameters:
i - index
Returns:
result names

getValue

public java.lang.String getValue(java.lang.String parameterName)
Gets parameter value
Parameters:
parameterName - parameter name
Returns:
value

setResultValue

public void setResultValue(int index,
                           java.lang.String result)
Sets result value for given index
Parameters:
index - formula index
value - value to set

getResultName

public java.lang.String getResultName(int index)
Gets name (label) for the result field
Returns:
result label name

setParameterField

public void setParameterField(java.lang.String parameterName,
                              java.awt.TextField field)
Tags a text field as a parameter field for given parameter name
Parameters:
parameterName - parameter name
field - text field to tag

getParameterField

public java.awt.TextField getParameterField(java.lang.String parameterName)
Gets parameter entry field for given parameter name
Parameters:
parameterName - parameter name
Returns:
parameter field

setResultField

public void setResultField(java.lang.String resultName,
                           java.awt.TextField field)
Tags a text field as the result field
Parameters:
field - text field to tag

getResultField

public java.awt.TextField getResultField(java.lang.String resultName)
Gets field for the result
Returns:
result field

getMinimumSize

public java.awt.Dimension getMinimumSize()
Gets minimum size for the panel
Returns:
minimum size

paint

public void paint(java.awt.Graphics g)
Paints the panel
Parameters:
g - graphics

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Handles action events
Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - event

setBackground

public void setBackground(java.awt.Color c)
Sets background
Parameters:
c - color

setParameterError

public void setParameterError(java.lang.String parameterName)
Sets parameter error
Parameters:
parameterName - parameter name