Package org.lsmp.djepExamples
Class Console
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
org.lsmp.djepExamples.Console
- All Implemented Interfaces:
KeyListener
,ImageObserver
,MenuContainer
,Serializable
,EventListener
,Accessible
- Direct Known Subclasses:
BlockStatments
,GroupConsole
,RpeConsole
,VectorConsole
,XJepConsole
This class implements a simple command line utility for evaluating
mathematical expressions.
Usage: java org.lsmp.djepExamples.Console [expression]If an argument is passed, it is interpreted as an expression and evaluated. Otherwise, a prompt is printed, and the user can enter expressions to be evaluated.
This class and its subclasses can also be run as a java applet which displays a textarea for interactive input.
This class has been designed to be sub classed to allow different consol applications. The methods
public void initialise() public void processEquation(Node node) throws Exception public boolean testSpecialCommands(String command) public void printPrompt() public void printIntroText() public void printHelp()can all be overwritten.
Furthermore main should be overwritten. For example
public static void main(String args[]) { Console c = new DJepConsole(); c.run(args); }
The main input loop is approximately
initialise(); printIntroText(); print(getPrompt()); String command; while((command = getCommand()) != null) { if(command.equals("quit") || command.equals("exit")) break; if(!testSpecialCommands(command)) continue; try { Node n = j.parse(command); processEquation(n); } catch(Exception e) {} print(getPrompt()); }
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.applet.Applet
Applet.AccessibleApplet
Nested classes/interfaces inherited from class java.awt.Panel
Panel.AccessibleAWTPanel
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JEP
Main JEP objectprotected TextArea
Text area for user input in applets.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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
Get a command from the input.Prints the prompt string.boolean
Handle an error in the parse and evaluate routines.void
init()
Applet initializationvoid
sets up all the needed objects.void
The main input loop for interactive operation.void
keyPressed
(KeyEvent arg0) void
keyReleased
(KeyEvent event) Handles keyRelease eventsvoid
static void
Creates a new Console object and calls run()void
Prints a line of text no newline.void
Prints a list of defined functions.void
Print help message.void
Prints introductory text.void
Prints a line of text no newline.void
printOps()
Prints a list of defined operators.final void
Prints a standard help message.void
Prints a list of variable.boolean
processCommand
(String command) Process a single command.void
processEquation
(Node node) Performs the required operation on a node.void
The main entry point with command line argumentsString[]
Splits a string on spaces.boolean
testSpecialCommands
(String command) Checks for special commands used by subclasses.Methods inherited from class java.applet.Applet
destroy, getAccessibleContext, getAppletContext, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, isValidateRoot, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
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, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, 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, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, revalidate, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
j
Main JEP object -
ta
Text area for user input in applets.
-
-
Constructor Details
-
Console
public Console()Constructor
-
-
Method Details
-
init
public void init()Applet initialization -
initialise
public void initialise()sets up all the needed objects. -
main
Creates a new Console object and calls run() -
run
The main entry point with command line arguments -
inputLoop
public void inputLoop()The main input loop for interactive operation. Repeatedly calls getCommand() and processCommand(). -
processCommand
Process a single command.- Tests for exit, quit, and help.
- Tests for any special commands used by sub classes.
testSpecialCommands(String)
- Parses the command.
- Processes the node.
processEquation(Node)
- Checks for errors.
handleError(Exception)
- Parameters:
command
- The line to be processed- Returns:
- false if un-recoverable error or 'quit' or 'exit'
-
processEquation
Performs the required operation on a node. Typically evaluates the node and prints the value.- Parameters:
node
- Node representing expression- Throws:
ParseException
- if a Parse or evaluation error
-
getCommand
Get a command from the input.- Returns:
- null if an IO error or EOF occurs.
-
getPrompt
Prints the prompt string. -
printStdHelp
public final void printStdHelp()Prints a standard help message. Type 'quit' or 'exit' to quit, 'help' for help. -
printHelp
public void printHelp()Print help message. -
printIntroText
public void printIntroText()Prints introductory text. -
printFuns
public void printFuns()Prints a list of defined functions. -
printOps
public void printOps()Prints a list of defined operators. -
printVars
public void printVars()Prints a list of variable. -
testSpecialCommands
Checks for special commands used by subclasses. For example a subclass may have a verbose mode switched on of off using the commandverbose on
This method can be used detected this input, perform required actions and skip normal processing by returning true.- Parameters:
command
-- Returns:
- true indicates normal processing should continue (default) false if the command is special and no further processing should be performed (parsing and evaluating)
- See Also:
-
handleError
Handle an error in the parse and evaluate routines.- Parameters:
e
-- Returns:
- false if the error cannot be recovered and the program should exit
-
split
Splits a string on spaces.- Parameters:
s
- the input string- Returns:
- an array of the tokens in the string
-
print
Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places. -
println
Prints a line of text no newline. Subclasses should call this method rather than System.out.print to allow for output to different places. -
keyReleased
Handles keyRelease events- Specified by:
keyReleased
in interfaceKeyListener
-
keyPressed
- Specified by:
keyPressed
in interfaceKeyListener
-
keyTyped
- Specified by:
keyTyped
in interfaceKeyListener
-
getAppletInfo
- Overrides:
getAppletInfo
in classApplet
-