Package edu.wisc.ssec.mcidasv.jython
Class Console
java.lang.Object
edu.wisc.ssec.mcidasv.jython.Console
- All Implemented Interfaces:
KeyListener
,Runnable
,EventListener
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Maps a "jython action" to a keystroke.static class
static enum
private class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int[]
Offset array used when actual offsets cannot be determined.private static final String
Not used yet.private ConsoleCallback
A hook that allows external classes to respond to events.private Document
textPane
's internal representation.private final boolean
private static final Font
All text will appear in this font.Jython statements entered by the user.private Runner
Thread that handles Jython command execution.private static final org.slf4j.Logger
Logger
object for Jython consoles.private MenuWrangler
private JPanel
Panel that holdstextPane
.private static final String
Normal jython prompt.private static final String
Prompt that indicates more input is needed.private JTextPane
Where the user interacts with the Jython interpreter.protected static final Color
Color of text coming from "stderr".protected static final Color
Color of text coming from "stdout".protected static final Color
Color of the Jython text as it is being entered.protected static final Color
Not used just yet...private static final String
ActualString
of whitespace to insert for blocks and whatnot.private String
Title of the console window. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addPretendHistory
(String line) private boolean
canInsertAt
(int position) Determines whether or notposition
is an acceptable place to insert text.void
ejectObject
(org.python.core.PyObject pyObject) void
ejectObjectByName
(String name) protected void
Inserts a newline character at the end of the input.void
Displays an error.void
generatedError
(String text) Displays error output.void
generatedOutput
(String text) Displays non-error output that was not the result of an "associated"Command
.static int
getBlockDepth
(String line, String whitespace) Returns the "block depth" of a given line of Jython.int
Returns the Jython statements as entered by the user, ordered from first to last.Returns a subset of Jython's local namespace containing only variables that are "pure" Java objects.org.python.core.PyObject
getJythonObject
(String var) Retrieves the specified Jython variable from the interpreters local namespace.int
getLineJython
(int lineNumber) Returns the line of Jython that occupies a specified line number.int
getLineOffsetEnd
(int lineNumber) int[]
getLineOffsets
(int lineNumber) int
getLineOffsetStart
(int lineNumber) getLineText
(int lineNumber) Returns the line of text that occupies the specified line number.Returns a copy of Jython's local namespace.int
getOffsetLine
(int offset) Returns the line number that contains the specified offset.getPanel()
Returns the panel containing the various UI components.static int
getPromptLength
(String line) protected JTextPane
Returns theJTextPane
used by the console.static String
getUserPath
(String[] args) void
void
void
Moves the caret to the end of the line it is currently on, rather than the end of the document.void
Handles the user pressing enter by basically grabbing the line of jython under the caret.void
Handles the user hitting theHome
key.void
void
Inserts the contents ofWHITESPACE
wherever the cursor is located.void
handleUp()
private static boolean
void
injectObject
(String name, Object object) Inserts the specified object into Jython's local namespace using the specified name.protected void
Does the actual work of displaying color-coded messages intextPane
.protected void
insertAtCaret
(Color color, String text) void
Noop.void
Noop.void
private int[]
Returns the offsets of the beginning and end of the last line.static void
void
Shows the prompt that indicates more input is needed.void
moreInput
(int blockLevel) private boolean
Determines whether or not the caret is on the last line.void
prompt()
Shows the normal Jython prompt.void
queueBatch
(String name, List<String> commands) Sends a batch of Jython commands to the interpreter.void
Sends a line of Jython to the interpreter viajythonRunner
and saves it to the history.void
Displays non-error output.void
run()
Puts together the GUI once EventQueue has processed all other pending events.void
Runs the file specified bypath
in theInterpreter
.void
setCallbackHandler
(ConsoleCallback newHandler) Registers a new callback handler with the console.private void
Will eventually display an initial greeting to the user.static void
testConsole
(boolean exitOnClose)
-
Field Details
-
TXT_NORMAL
Color of the Jython text as it is being entered. -
TXT_GOOD
Color of text coming from "stdout". -
TXT_WARN
Not used just yet... -
TXT_ERROR
Color of text coming from "stderr". -
logger
Logger
object for Jython consoles. -
BAD_OFFSETS
Offset array used when actual offsets cannot be determined. -
PS1
Normal jython prompt.- See Also:
-
PS2
Prompt that indicates more input is needed.- See Also:
-
WHITESPACE
ActualString
of whitespace to insert for blocks and whatnot.- See Also:
-
BANNER
Not used yet. -
FONT
All text will appear in this font. -
jythonHistory
Jython statements entered by the user. -
jythonRunner
Thread that handles Jython command execution. -
callback
A hook that allows external classes to respond to events. -
textPane
Where the user interacts with the Jython interpreter. -
document
textPane
's internal representation. -
panel
Panel that holdstextPane
. -
windowTitle
Title of the console window. -
exitOnClose
-
-
Constructor Details
-
Console
public Console()Build a console with no initial commands. -
Console
-
Console
Builds a console and executes a list of Jython statements. It's been useful for dirty tricks needed during setup.- Parameters:
initialCommands
- Jython statements to execute.
-
-
Method Details
-
getPanel
Returns the panel containing the various UI components. -
getTextPane
Returns theJTextPane
used by the console. -
injectObject
Inserts the specified object into Jython's local namespace using the specified name.Example:
console.injectObject("test", new PyJavaInstance("a test"))
Allows the interpreter to refer to theString
"a test"
astest
.- Parameters:
name
- Object name as it will appear within the interpreter.object
- Object to place in the interpreter's local namespace.
-
ejectObjectByName
-
ejectObject
-
runFile
Runs the file specified bypath
in theInterpreter
.- Parameters:
name
-__name__
attribute to use for loadingpath
.path
- The path to the Jython file.
-
result
Displays non-error output.- Parameters:
text
- The message to display.
-
error
Displays an error.- Parameters:
text
- The error message.
-
prompt
Shows the normal Jython prompt. -
generatedOutput
Displays non-error output that was not the result of an "associated"Command
.- Parameters:
text
- The text to display.- See Also:
-
generatedError
Displays error output. Differs fromerror(String)
in that this is intended for output not "associated" with aCommand
.Example: say you fire off a background thread. If it generates an error somehow, this is the method you want.
- Parameters:
text
- The error message.
-
moreInput
Shows the prompt that indicates more input is needed. -
moreInput
-
showBanner
Will eventually display an initial greeting to the user.- Throws:
BadLocationException
- Upon attempting to clear out an invalid portion of the document.
-
endln
Inserts a newline character at the end of the input.- Parameters:
color
- Perhaps this should go!?
-
insert
Does the actual work of displaying color-coded messages intextPane
.- Parameters:
color
- The color of the message.text
- The actual message.
-
insertAtCaret
-
canInsertAt
Determines whether or notposition
is an acceptable place to insert text. Currently the criteria for "acceptable" means thatposition
is located within the last (or active) line, and not within eitherPS1
orPS2
.- Parameters:
position
- Position to test. Values less than zero are not allowed.- Returns:
- Whether or not text can be inserted at
position
.
-
getLineCount
- Returns:
- Number of lines in the document.
-
getLineOffsetStart
-
getLineOffsetEnd
-
getLineOffsets
-
getOffsetLine
Returns the line number that contains the specified offset.- Parameters:
offset
- Offset whose line number you want.- Returns:
- Line number.
-
locateLastLine
Returns the offsets of the beginning and end of the last line. -
onLastLine
Determines whether or not the caret is on the last line. -
getCaretLine
- Returns:
- The line number of the caret's offset within the text.
-
getLineText
Returns the line of text that occupies the specified line number.- Parameters:
lineNumber
- Line number whose text is to be returned.- Returns:
- Either the line of text or null if there was an error.
-
getLineJython
Returns the line of Jython that occupies a specified line number. This is different thangetLineText(int)
in that bothPS1
andPS2
are removed from the returned line.- Parameters:
lineNumber
- Line number whose text is to be returned.- Returns:
- Either the line of Jython or null if there was an error.
-
getPromptLength
- Parameters:
line
- The line in question. Cannot benull
.- Returns:
- Either the prompt length or zero if there was none.
- Throws:
NullPointerException
- ifline
isnull
.
-
getBlockDepth
Returns the "block depth" of a given line of Jython.Examples:
"print 'x'" -> 0 " print 'x'" -> 1 " die()" -> 3
- Parameters:
line
- Line to test. Can't benull
.whitespace
- The indentString
used withline
. Can't benull
.- Returns:
- Either the block depth (
>= 0
) or-1
if there was an error.
-
setCallbackHandler
Registers a new callback handler with the console. Note that to maximize utility, this method also registers the same handler withjythonRunner
.- Parameters:
newHandler
- The new callback handler.- Throws:
NullPointerException
- if the new handler is null.
-
getJythonReferencesTo
-
getJavaInstances
Returns a subset of Jython's local namespace containing only variables that are "pure" Java objects.- Returns:
- Jython variable names mapped to their Java instantiation.
-
getJythonObject
Retrieves the specified Jython variable from the interpreters local namespace.- Parameters:
var
- Variable name to retrieve.- Returns:
- Either the variable or null. Note that null will also be
returned if
Runner.copyLocals()
returned null.
-
getLocalNamespace
Returns a copy of Jython's local namespace.- Returns:
- Jython variable names mapped to
PyObjects
.
-
handlePaste
-
handleHome
-
handleEnd
Moves the caret to the end of the line it is currently on, rather than the end of the document. -
handleUp
-
handleDown
-
handleTab
Inserts the contents ofWHITESPACE
wherever the cursor is located. -
handleDelete
-
handleEnter
Handles the user pressing enter by basically grabbing the line of jython under the caret. If the caret is on the last line, the line is queued for execution. Otherwise the line is reinserted at the end of the document--this lets the user preview a previous command before they rerun it. -
getHistory
Returns the Jython statements as entered by the user, ordered from first to last.- Returns:
- User's history.
-
queueLine
Sends a line of Jython to the interpreter viajythonRunner
and saves it to the history.- Parameters:
line
- Jython to queue for execution.
-
queueBatch
Sends a batch of Jython commands to the interpreter. This is different than simply callingqueueLine(String)
for each command; the interpreter will attempt to execute each batched command before returning "control" to the console.This method is mostly useful for restoring Console sessions. Each command in
commands
will appear in the console as though the user typed it. The batch of commands will also be saved to the history.- Parameters:
name
- Identifier for the batch. Doesn't need to be unique, merely non-null.commands
- The commands to execute.
-
addPretendHistory
-
run
Puts together the GUI once EventQueue has processed all other pending events. -
keyPressed
Noop.- Specified by:
keyPressed
in interfaceKeyListener
-
keyReleased
Noop.- Specified by:
keyReleased
in interfaceKeyListener
-
keyTyped
- Specified by:
keyTyped
in interfaceKeyListener
-
hasAction
-
getUserPath
-
testConsole
-
main
-