Class ExpressionInput
- All Implemented Interfaces:
InputObject
,Value
,ImageObserver
,MenuContainer
,Serializable
,Accessible
An ExpressionInput will ordinarily be registered with a Controller in TWO ways: It's added to a Controller with the Controller's add() method. This makes the Contrller call the ExpressionInput's checkInput() method during the Controller's compute() method. Secondly, the Controller is set as the "onUserAction" property. This causes the Controller's compute() method to be called when the user presses return in the ExpressionInput box. This is optional-- you might, for example, only want the Controller to compute() when a Compute button is pressed. You can also set the ExpressionInput's onTextChange property to a Controller if you want it to compute every time the text in the box changes.
Use the function getFunction() if you want to use an ExpressionInput as a way of inputting a function.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
The expression associated with an ExpressionInput belongs to this class.Nested classes/interfaces inherited from class java.awt.TextField
TextField.AccessibleAWTTextField
Nested classes/interfaces inherited from class java.awt.TextComponent
TextComponent.AccessibleAWTTextComponent
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String
Error message from the most recent time the input was checked by a call to checkInput().protected ExpressionInput.EI
The Expression associate with this input box.protected Parser
A parser for parsing the user's input expression.protected String
protected boolean
True if an error should be thrown when checkInput() is called, but the content of the box is not a legal expression.Fields inherited from class java.awt.TextComponent
textListener
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new ExpressionFunction with no associated parser.ExpressionInput
(String initialValue, Parser p) Create an ExpressionInputBox with initial contents given by initialValue. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Get the expression from the box, maybe throw a JBCError if a ParseError occurs.Get error message from previous call to checkInput().Get the Expression associated with this ExpressionInput.Get a function of one variable whose value at a real number x is computed by assigning x to the variable v and then returning the value of the expression associated with this ExpressionInput.getFunction
(Variable[] v) Get a function of one or more variables whose value at arguments x1, x2, ...Return the Controller, if any, that is notified whenever the text in this input box changesReturn the Controller, if any, that is notified when the user presses return in this text-input box.boolean
Return the value of the throwErrors property, which determines whether errors can be thrown when checkInput() is called.double
getVal()
Return the current value of the expression associated with this ExpressionInput.void
Method required by InputObject interface; in this class, it simply calls setOnUserAction(c).void
Overridden to call onUserAction.compute() if onUserAction is non-null.void
Overridden to call onUserAction.compute() if onUserAction is non-null.void
If the parameter, c, is non-null, then its compute method will be called whenever the text in this input box changes.void
If the parameter c is non-null, then its compute method will be called whenever the user presses the return key while typing in this text-input box.void
Set the parser that is used to parse the user's input strings.void
Set the text displayed in this input box.void
setThrowErrors
(boolean throwErrors) Set the throwErrors property.Methods inherited from class java.awt.TextField
addActionListener, addNotify, echoCharIsSet, getAccessibleContext, getActionListeners, getColumns, getEchoChar, getListeners, getMinimumSize, getMinimumSize, getPreferredSize, getPreferredSize, minimumSize, minimumSize, paramString, preferredSize, preferredSize, processEvent, removeActionListener, setColumns, setEchoChar, setEchoCharacter
Methods inherited from class java.awt.TextComponent
addTextListener, enableInputMethods, getBackground, getCaretPosition, getInputMethodRequests, getSelectedText, getSelectionEnd, getSelectionStart, getText, getTextListeners, isEditable, removeNotify, removeTextListener, select, selectAll, setBackground, setCaretPosition, setEditable, setSelectionEnd, setSelectionStart
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, prepareImage, prepareImage, print, 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, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
-
Field Details
-
expr
The Expression associate with this input box. Class EI is a private nested class. -
parser
A parser for parsing the user's input expression. If this is null, a default parser will be used and only constant expressions will be allowed. -
previousContents
-
throwErrors
protected boolean throwErrorsTrue if an error should be thrown when checkInput() is called, but the content of the box is not a legal expression. Otherwise, the expression will become a constant expression with value Double.NaN. -
errorMessage
Error message from the most recent time the input was checked by a call to checkInput(). If this is null, then no error occurred.
-
-
Constructor Details
-
ExpressionInput
public ExpressionInput()Create a new ExpressionFunction with no associated parser. This can only be used to input constant expressions, unless you set a parser later with setParser(). -
ExpressionInput
Create an ExpressionInputBox with initial contents given by initialValue. (If initialValue is null, the empty string is used.) If p is not null, then p will be used to parse the contents of the box.- Parameters:
initialValue
- initial contents of ExpressionInputBox.p
- if non-null, this parser will be used to parse contents of the ExpressionInputBox.
-
-
Method Details
-
setParser
Set the parser that is used to parse the user's input strings. If this is null, then a default parser is used that will only allow constant expressions.- Parameters:
p
- parser to register with user's input strings.
-
getExpression
Get the Expression associated with this ExpressionInput. -
getFunction
Get a function of one variable whose value at a real number x is computed by assigning x to the variable v and then returning the value of the expression associated with this ExpressionInput. Of couse, v should be one of the variables registered with the Parser for this ExpressionInput, or else in can never occur in the expression. Note that the value of the variable v changes TEMPORARILY when the function is evaluated. (So you should not use a variable where setting the value has a side effect, such as the variable associated with a SliderVariable.)- Parameters:
v
- The function that is returned in a function of this variable.
-
getFunction
Get a function of one or more variables whose value at arguments x1, x2, ... is computed by assigning the x's to the variables and then returning the value of the expression associated with this ExpressionInput. Of couse, each v[i] should be one of the variables registered with the Parser for this ExpressionInput. Note that the value of the variables change TEMPORARILY when the function is evaluated.- Parameters:
v
- The function that is returned is a function of the variables in this array.
-
getVal
public double getVal()Return the current value of the expression associated with this ExpressionInput. -
setOnUserAction
If the parameter c is non-null, then its compute method will be called whenever the user presses the return key while typing in this text-input box. -
getOnUserAction
Return the Controller, if any, that is notified when the user presses return in this text-input box. -
notifyControllerOnChange
Method required by InputObject interface; in this class, it simply calls setOnUserAction(c). This is meant to be called by JCMPanel.gatherInputs().- Specified by:
notifyControllerOnChange
in interfaceInputObject
-
setOnTextChange
If the parameter, c, is non-null, then its compute method will be called whenever the text in this input box changes. Furthermore, the throwErrors property will be set to false, to avoid throwing multiple errors while the user is typing. (You can change it back to true if you want by calling setThrowErrors(true).) It would only rarely make sense to use this feature. -
getOnTextChange
Return the Controller, if any, that is notified whenever the text in this input box changes -
setThrowErrors
public void setThrowErrors(boolean throwErrors) Set the throwErrors property. When this is true, a JCMError can be thrown when checkInput() is called an a parse error is found in the contents of the input box. If throwErrors is false, no error is thrown. Instead, the expression is set to a constant expression with value Double.NaN. -
getThrowErrors
public boolean getThrowErrors()Return the value of the throwErrors property, which determines whether errors can be thrown when checkInput() is called. -
getErrorMessage
Get error message from previous call to checkInput(). Returns null if and only if there was no error. -
checkInput
public void checkInput()Get the expression from the box, maybe throw a JBCError if a ParseError occurs. This is meant to be called by a Controller, in general. The expression associated with this ExpressionInput can only change when this method is called; it DOES NOT change continuously as the user types.- Specified by:
checkInput
in interfaceInputObject
-
setText
Set the text displayed in this input box. This overrides TextField.setText to make sure that the expression will be recomputed the next time checkInput() is called. -
processActionEvent
Overridden to call onUserAction.compute() if onUserAction is non-null. This is not meant to be called directly- Overrides:
processActionEvent
in classTextField
-
processTextEvent
Overridden to call onUserAction.compute() if onUserAction is non-null. This is not meant to be called directly.- Overrides:
processTextEvent
in classTextComponent
-