edu.hws.jcm.awt
Class DataTableInput
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
edu.hws.jcm.awt.DataTableInput
- All Implemented Interfaces:
- MathObject, ParserExtension, ImageObserver, MenuContainer, Serializable, Accessible
public class DataTableInput
- extends Panel
- implements ParserExtension
A DataTableInput lets the user input a grid of real numbers arranged
in rows and columns. Each column has a name, and rows are numberd
starting from 1. The column names and row numbers can be
displayed, optionally. By default, a new row is added automatically
if the user moves down out of the last row by pressing return
or down-arrow, provided the last row is non-empty. Rows can also be
added programmatically. Columns are usually added in the constructor,
but they can also be added later. If the user leaves a cell
at a time when the content of that cell does not represent a legal
number, then the message "bad input" will be displayed in the cell.
A DataTableInput can be given a name and can then be added to a parser.
The parser will then recognize the name of the table. In an expression, the table name
can be followed by a ".", then one of the column names from table, then
an expression in parentheses. This represents a reference to the number in
one of the cells in the table. The expression gives the number of
the row, where rows are numbered starting from 1. For example:
"data.A(3)" represents the third number in column "A" in a table
named data. The table name and "." can also be followed by the word "sum" and
an expression in parentheses. An expression used in this way
can include references to the column names in the table and to
the special value "rowNumber". The value of expression is computed
for each row in the table and the sum of the values is computed. In the expression that
is being summed, a column name represents the
number in that column and rowNumber represents the number of
the row. For example: "data.sum(A^2)". Finally, the "." can be
followed by the word "count". "count" can, optionally be followed
by an empty pair of parentheses. This represents the number of
rows. For example: "data.count". Empty rows at the bottom
of the table are ignored in both "sum" and "count".
Note that rows are numbered starting from 1 because row numbers
can be visible to the user, who shouldn't be subjected to zero-based
numbering. However, columns are always numbered from zero.
- See Also:
- Serialized Form
Method Summary |
int |
addColumn()
Add a column at the right side of the table, with all cells initially
empty. |
int |
addColumn(String name)
Add a column with the specified name at the right side of the table, with all cells initially
empty. |
void |
addRows(int rowCt)
Add the specified number of empty rows at the bottom of the table. |
void |
addVariablesToParser(Parser p)
Add a row number variable (from the getRowNumberVariable() method) and
a column variable for each column (from the getColumnVariable() method)
to the parser. |
void |
clear()
Remove all rows from the table, leaving just one empty row. |
void |
deleteCurrentRow()
Delete the row that contains the cell that the user is editing. |
void |
doParse(Parser parser,
ParserContext context)
Required by the ParserExtension interface and not meant to be called directly. |
boolean |
getAutoAddRows()
Get the value of the autoAddRows property, which determines whether empty
rows are automatically added to the bottom of the table when needed. |
Color |
getBlankBackground()
Returns the color that is used for blank areas in the table, below the
rows of cells. |
Color |
getCellBackground()
Returns the color that is used as a background for cells in the table. |
double |
getCellContents(int row,
int col)
Get the number in the specified row and column. |
int |
getColumnCount()
Get the number of columns in the table. |
String |
getColumnName(int i)
Get the name of column number i, where columns are numbered starting from zero. |
protected Variable |
getColumnVariable(int columnNum)
Return a column variable for the specified column, where columns are
numbered starting from 1. |
int |
getCurrentRowNumber()
Return the current row number. |
double |
getEmptyCellValue()
Get the value that is represented by an empty cell. |
Color |
getGridColor()
Returns the color that is used for the lines between cells in the table. |
Color |
getLabelBackground()
Returns the color that is used as a background for row numbers and column titles. |
String |
getName()
Get the name of the DataInputTable (which might be null). |
int |
getNonEmptyRowCount()
Return the number of rows in the table, ignoring empty rows at the bottom
of the table. |
protected Variable |
getRowNumberVariable()
Get a variable that represents the current row number in the table,
as set by the setCurrentRowNumber() method. |
long |
getSerialNumber()
Get the serial number of the table. |
boolean |
getShowColumnTitles()
Test whether the column name is shown at the top of each column. |
boolean |
getShowRowNumbers()
Test whether row numbers are shown. |
boolean |
getThrowErrors()
Get the value of the throwErrors property, which determines whether an error
is thrown when an attempt is made to refer to the value of a cell that
contains an invalid string. |
void |
insertRow()
Insert a row before the row that contains the cell that the user is editing. |
boolean |
readFromStream(Reader in)
Read data for table from the specified Reader. |
void |
setAutoAddRows(boolean auto)
If the value of autoAddRows is true, then an empty row is added to the table
automatically when the user attempts to move down from the last row of
the table, provided that the last row is non-empty (so there can only be
one auto-added row at a time). |
void |
setBlankBackground(Color color)
Get the color to be used as a background blank areas in the table, below the
rows of cells. |
void |
setCellBackground(Color color)
Set the color to be used as a background for cells in the table. |
void |
setCellContents(int row,
int col,
double val)
Put the given real number, val, in the cell in the specified row
and column, where rows are numbered starting from 1 and columns are
numbered starting from zero. |
void |
setColumnName(int i,
String name)
Set the name of column number i, where columns are numbered starting
from zero. |
void |
setCurrentRowNumber(int i)
Set the current row in the table. |
void |
setEmptyCellValue(double val)
Set the value that should be returned when the value of an empty cell is
requested. |
void |
setGridColor(Color color)
Get the color to be used for the lines between cells in the table. |
void |
setLabelBackground(Color color)
Set the color to be used as a background for row numbers and column titles. |
void |
setName(String name)
Set the name of this DataTableInput. |
void |
setShowColumnTitles(boolean show)
If set to true, then the column name is shown at the top of each column. |
void |
setShowRowNumbers(boolean show)
If set to true, then the row number is shown at the left of each row. |
void |
setThrowErrors(boolean throwErr)
Set the throwErrors property. |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree |
Methods inherited from class java.awt.Component |
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle |
DataTableInput
public DataTableInput()
- Create a DataTableInput with no columns. Columns can be added later
using the addColumn() methods. The table initially has no name.
DataTableInput
public DataTableInput(String name,
String[] columnNames)
- Create a table with the specified column names. If columnNames
is null, the number of columns is zero. The name can be null, if you
don't need a name for the table. The length of the array determines
the number of columns in the table.
DataTableInput
public DataTableInput(String name,
int columns)
- Create a table with the specified number of columns,
named "A", "B", etc. The name can be null, if you
don't need a name for the table. The number of columns can be zero.
doParse
public void doParse(Parser parser,
ParserContext context)
- Required by the ParserExtension interface and not meant to be called directly.
This is called by a parser if it encounters the name of the table in an
expression. It parses the complete table reference, such as "data.A(3)"
or "data.sum(A^2)".
- Specified by:
doParse
in interface ParserExtension
getNonEmptyRowCount
public int getNonEmptyRowCount()
- Return the number of rows in the table, ignoring empty rows at the bottom
of the table. Note that an empty row that precedes some non-empty row
is included in the count.
getCellContents
public double getCellContents(int row,
int col)
- Get the number in the specified row and column. Rows are numberd starting
from 1, but columns are numbered starting from zero. If the specified
cell does not exist in the table, Double.NaN is returned. If the cell is empty,
emptyCellValue is returned. If the content of the cell does not define
a legal real number, then the action depends on the value of the missingValueIsError
property: If this property is true, then a JCMError is thrown; if it is
false, then Double.NaN is returned.
setCellContents
public void setCellContents(int row,
int col,
double val)
- Put the given real number, val, in the cell in the specified row
and column, where rows are numbered starting from 1 and columns are
numbered starting from zero. This is ignored if the specified row
and column do not exist in the table.
setCurrentRowNumber
public void setCurrentRowNumber(int i)
- Set the current row in the table. If the parameter is less than 1, the current
row is set to 1.
(The table keeps track of a "current row number", which is always greater than
or equal to 1. This row number is used when a column variable or row number
variable is evaluated. These variables can be added to a parser using the
addVariablesToParser method, and can then be used in expressions parsed by
that parser. When the row number variable, which is named rowNumber, is
evaluated, its value is the current row number in the table. When a column
variable is evaluated, its value is the number in the cell in the associated
column and in the current row. The setCurrentRowNumber() method, in combination
with the getNonEmptyRowCount() method allow you to iterate through the rows
of the table and evaluate the expression for each row.)
getCurrentRowNumber
public int getCurrentRowNumber()
- Return the current row number.
getColumnVariable
protected Variable getColumnVariable(int columnNum)
- Return a column variable for the specified column, where columns are
numbered starting from 1. The value of this variable is the number
in the specified column and in the current row of the table (as set
by the setCurrentRowNumber() method.) The name of the variable is
the name of the column. This method is protected since variables are
not meant to be used as regular variables. But they can be added to
a Parser by calling the addVariablesToParser() method.)
getRowNumberVariable
protected Variable getRowNumberVariable()
- Get a variable that represents the current row number in the table,
as set by the setCurrentRowNumber() method. The name of the
variable is rowNumber.
addVariablesToParser
public void addVariablesToParser(Parser p)
- Add a row number variable (from the getRowNumberVariable() method) and
a column variable for each column (from the getColumnVariable() method)
to the parser. The parser will then be able to parse expressions that
refer to these variables. The value of such an expression depends on
the current row number, as set by setCurrentRowNumber().
getSerialNumber
public long getSerialNumber()
- Get the serial number of the table. This is incremented each time the
table changes in any way.
setThrowErrors
public void setThrowErrors(boolean throwErr)
- Set the throwErrors property. If this is true, then a JCMError is thrown when
an attempt is made to use the value of a cell that contains an invalid String.
Note that referring to an empty cell is not an error. The default value is true.
getThrowErrors
public boolean getThrowErrors()
- Get the value of the throwErrors property, which determines whether an error
is thrown when an attempt is made to refer to the value of a cell that
contains an invalid string.
setEmptyCellValue
public void setEmptyCellValue(double val)
- Set the value that should be returned when the value of an empty cell is
requested. The default value is Double.NaN. Another plausible value, in
some circumstances, would be zero.
getEmptyCellValue
public double getEmptyCellValue()
- Get the value that is represented by an empty cell.
setAutoAddRows
public void setAutoAddRows(boolean auto)
- If the value of autoAddRows is true, then an empty row is added to the table
automatically when the user attempts to move down from the last row of
the table, provided that the last row is non-empty (so there can only be
one auto-added row at a time). If the user leaves this row while it is
still empty, it will automatically be deleted. The default value is true.
getAutoAddRows
public boolean getAutoAddRows()
- Get the value of the autoAddRows property, which determines whether empty
rows are automatically added to the bottom of the table when needed.
setName
public void setName(String name)
- Set the name of this DataTableInput. This is only needed if the table is
to be added to a parser. The name should be a legal identifier.
- Specified by:
setName
in interface MathObject
- Overrides:
setName
in class Component
getName
public String getName()
- Get the name of the DataInputTable (which might be null).
- Specified by:
getName
in interface MathObject
- Overrides:
getName
in class Component
setColumnName
public void setColumnName(int i,
String name)
- Set the name of column number i, where columns are numbered starting
from zero. If column variables are to be used or if
the DataTableInput itself is to be added to a parser, then the name should
be a legal identifier. If the showColumnTitles property is set to true,
then column names are shown at the top of the table.
getColumnName
public String getColumnName(int i)
- Get the name of column number i, where columns are numbered starting from zero.
addRows
public void addRows(int rowCt)
- Add the specified number of empty rows at the bottom of the table. If you
want a table with a fixed number of rows, add them with this method and
set the autoAddRows property to false.
insertRow
public void insertRow()
- Insert a row before the row that contains the cell that the user is editing.
deleteCurrentRow
public void deleteCurrentRow()
- Delete the row that contains the cell that the user is editing. However,
if that is the only row in the table, just make the row empty.
clear
public void clear()
- Remove all rows from the table, leaving just one empty row.
getColumnCount
public int getColumnCount()
- Get the number of columns in the table.
addColumn
public int addColumn()
- Add a column at the right side of the table, with all cells initially
empty. The name of the columns will be single letter such as 'A', 'B', ...
addColumn
public int addColumn(String name)
- Add a column with the specified name at the right side of the table, with all cells initially
empty. This is inefficient if the table already contains a bunch of non-empty rows.
getShowColumnTitles
public boolean getShowColumnTitles()
- Test whether the column name is shown at the top of each column.
setShowColumnTitles
public void setShowColumnTitles(boolean show)
- If set to true, then the column name is shown at the top of each column. The
default value is false. This is meant to be called before the table has been
shown on the screen, such as in the init() method of an applet. If you call it
after the table has already been shown, you will have to validate the panel
yourself.
getShowRowNumbers
public boolean getShowRowNumbers()
- Test whether row numbers are shown.
setShowRowNumbers
public void setShowRowNumbers(boolean show)
- If set to true, then the row number is shown at the left of each row. The
default value is false. This is meant to be called before the table has been
shown on the screen, such as in the init() method of an applet. If you call it
after the table has already been shown, you will have to validate the panel
yourself.
getLabelBackground
public Color getLabelBackground()
- Returns the color that is used as a background for row numbers and column titles.
setLabelBackground
public void setLabelBackground(Color color)
- Set the color to be used as a background for row numbers and column titles.
The default is a very light gray.
getCellBackground
public Color getCellBackground()
- Returns the color that is used as a background for cells in the table.
setCellBackground
public void setCellBackground(Color color)
- Set the color to be used as a background for cells in the table.
The default is a light yellow.
getBlankBackground
public Color getBlankBackground()
- Returns the color that is used for blank areas in the table, below the
rows of cells.
setBlankBackground
public void setBlankBackground(Color color)
- Get the color to be used as a background blank areas in the table, below the
rows of cells. The default is a gray.
getGridColor
public Color getGridColor()
- Returns the color that is used for the lines between cells in the table.
setGridColor
public void setGridColor(Color color)
- Get the color to be used for the lines between cells in the table.
The default is a blue.
readFromStream
public boolean readFromStream(Reader in)
- Read data for table from the specified Reader. One row is filled
from each non-empty line of input. The line should contain
numbers separated by spaces/tabs/commas. The word "undefined"
can be used to represent an empty cell. Otherwise, if a non-number is
encountered, an error occurs. If not enough numbers are
found on a line, the extra columns are filled with empties. After
filling all columns, extra data on the line is ignored.
Data currently in the table is removed and replaced (if no error
occurs during reading). In the case of an error, if throwErrors is
true, then a JCMError is thrown; if throwErrors is false, no
error is thrown, but the return value is false. If no error occurs,
the return value is true. If an error occurs, the previous data
in the table is left unchanged.