public class Console extends java.lang.Object implements java.lang.Runnable, java.awt.event.KeyListener
Modifier and Type | Class and Description |
---|---|
static class |
Console.Actions
Maps a "jython action" to a keystroke.
|
static class |
Console.HistoryEntry |
static class |
Console.HistoryType |
private class |
Console.PopupListener |
Modifier and Type | Field and Description |
---|---|
private static int[] |
BAD_OFFSETS
Offset array used when actual offsets cannot be determined.
|
private static java.lang.String |
BANNER
Not used yet.
|
private ConsoleCallback |
callback
A hook that allows external classes to respond to events.
|
private javax.swing.text.Document |
document
textPane 's internal representation. |
private boolean |
exitOnClose |
private static java.awt.Font |
FONT
All text will appear in this font.
|
private java.util.List<java.lang.String> |
jythonHistory
Jython statements entered by the user.
|
private Runner |
jythonRunner
Thread that handles Jython command execution.
|
private static org.slf4j.Logger |
logger
Logger object for Jython consoles. |
private MenuWrangler |
menuWrangler |
private javax.swing.JPanel |
panel
Panel that holds
textPane . |
private static java.lang.String |
PS1
Normal jython prompt.
|
private static java.lang.String |
PS2
Prompt that indicates more input is needed.
|
private javax.swing.JTextPane |
textPane
Where the user interacts with the Jython interpreter.
|
protected static java.awt.Color |
TXT_ERROR
Color of text coming from "stderr".
|
protected static java.awt.Color |
TXT_GOOD
Color of text coming from "stdout".
|
protected static java.awt.Color |
TXT_NORMAL
Color of the Jython text as it is being entered.
|
protected static java.awt.Color |
TXT_WARN
Not used just yet...
|
private static java.lang.String |
WHITESPACE
Actual
String of whitespace to insert for blocks and whatnot. |
private java.lang.String |
windowTitle
Title of the console window.
|
Constructor and Description |
---|
Console()
Build a console with no initial commands.
|
Console(boolean exitOnClose) |
Console(boolean exitOnClose,
java.util.List<java.lang.String> initialCommands)
Builds a console and executes a list of Jython statements.
|
Modifier and Type | Method and Description |
---|---|
void |
addPretendHistory(java.lang.String line) |
private boolean |
canInsertAt(int position)
Determines whether or not
position is an acceptable place to
insert text. |
void |
ejectObject(org.python.core.PyObject pyObject) |
void |
ejectObjectByName(java.lang.String name) |
protected void |
endln(java.awt.Color color)
Inserts a newline character at the end of the input.
|
void |
error(java.lang.String text)
Displays an error.
|
void |
generatedError(java.lang.String text)
Displays error output.
|
void |
generatedOutput(java.lang.String text)
Displays non-error output that was not the result of an
"associated"
Command . |
static int |
getBlockDepth(java.lang.String line,
java.lang.String whitespace)
Returns the "block depth" of a given line of Jython.
|
int |
getCaretLine() |
java.util.List<java.lang.String> |
getHistory()
Returns the Jython statements as entered by the user, ordered from first
to last.
|
java.util.Map<java.lang.String,java.lang.Object> |
getJavaInstances()
Returns a subset of Jython's local namespace containing only variables
that are "pure" Java objects.
|
org.python.core.PyObject |
getJythonObject(java.lang.String var)
Retrieves the specified Jython variable from the interpreters local
namespace.
|
java.util.Set<java.lang.String> |
getJythonReferencesTo(java.lang.Object obj) |
int |
getLineCount() |
java.lang.String |
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) |
java.lang.String |
getLineText(int lineNumber)
Returns the line of text that occupies the specified line number.
|
java.util.Map<java.lang.String,org.python.core.PyObject> |
getLocalNamespace()
Returns a copy of Jython's local namespace.
|
int |
getOffsetLine(int offset)
Returns the line number that contains the specified offset.
|
javax.swing.JPanel |
getPanel()
Returns the panel containing the various UI components.
|
static int |
getPromptLength(java.lang.String line)
|
protected javax.swing.JTextPane |
getTextPane()
Returns the
JTextPane used by the console. |
static java.lang.String |
getUserPath(java.lang.String[] args) |
void |
handleDelete() |
void |
handleDown() |
void |
handleEnd()
Moves the caret to the end of the line it is currently on, rather than
the end of the document.
|
void |
handleEnter()
Handles the user pressing enter by basically grabbing the line of jython
under the caret.
|
void |
handleHome()
Handles the user hitting the
Home key. |
void |
handlePaste() |
void |
handleTab()
Inserts the contents of
WHITESPACE wherever the cursor is
located. |
void |
handleUp() |
private static boolean |
hasAction(javax.swing.JTextPane jtp,
java.awt.event.KeyEvent e) |
void |
injectObject(java.lang.String name,
java.lang.Object object)
Inserts the specified object into Jython's local namespace using the
specified name.
|
protected void |
insert(java.awt.Color color,
java.lang.String text)
Does the actual work of displaying color-coded messages in
textPane . |
protected void |
insertAtCaret(java.awt.Color color,
java.lang.String text) |
void |
keyPressed(java.awt.event.KeyEvent e)
Noop.
|
void |
keyReleased(java.awt.event.KeyEvent e)
Noop.
|
void |
keyTyped(java.awt.event.KeyEvent e) |
private int[] |
locateLastLine()
Returns the offsets of the beginning and end of the last line.
|
static void |
main(java.lang.String[] args) |
void |
moreInput()
Shows the prompt that indicates more input is needed.
|
void |
moreInput(int blockLevel) |
private boolean |
onLastLine()
Determines whether or not the caret is on the last line.
|
void |
prompt()
Shows the normal Jython prompt.
|
void |
queueBatch(java.lang.String name,
java.util.List<java.lang.String> commands)
Sends a batch of Jython commands to the interpreter.
|
void |
queueLine(java.lang.String line)
Sends a line of Jython to the interpreter via
jythonRunner and
saves it to the history. |
void |
result(java.lang.String text)
Displays non-error output.
|
void |
run()
Puts together the GUI once EventQueue has processed all other pending
events.
|
void |
runFile(java.lang.String name,
java.lang.String path)
Runs the file specified by
path in the Interpreter . |
void |
setCallbackHandler(ConsoleCallback newHandler)
Registers a new callback handler with the console.
|
private void |
showBanner()
Will eventually display an initial greeting to the user.
|
static void |
testConsole(boolean exitOnClose) |
protected static final java.awt.Color TXT_NORMAL
protected static final java.awt.Color TXT_GOOD
protected static final java.awt.Color TXT_WARN
protected static final java.awt.Color TXT_ERROR
private static final org.slf4j.Logger logger
Logger
object for Jython consoles.private static final int[] BAD_OFFSETS
private static final java.lang.String PS1
private static final java.lang.String PS2
private static final java.lang.String WHITESPACE
String
of whitespace to insert for blocks and whatnot.private static final java.lang.String BANNER
private static final java.awt.Font FONT
private final java.util.List<java.lang.String> jythonHistory
private Runner jythonRunner
private ConsoleCallback callback
private javax.swing.JTextPane textPane
private java.lang.String windowTitle
private MenuWrangler menuWrangler
private final boolean exitOnClose
public Console()
public Console(boolean exitOnClose)
public Console(boolean exitOnClose, java.util.List<java.lang.String> initialCommands)
initialCommands
- Jython statements to execute.public javax.swing.JPanel getPanel()
protected javax.swing.JTextPane getTextPane()
JTextPane
used by the console.public void injectObject(java.lang.String name, java.lang.Object object)
Example:
console.injectObject("test", new PyJavaInstance("a test"))
Allows the interpreter to refer to the String
"a test"
as test
.
name
- Object name as it will appear within the interpreter.object
- Object to place in the interpreter's local namespace.public void ejectObjectByName(java.lang.String name)
public void ejectObject(org.python.core.PyObject pyObject)
public void runFile(java.lang.String name, java.lang.String path)
path
in the Interpreter
.name
- __name__
attribute to use for loading path
.path
- The path to the Jython file.public void result(java.lang.String text)
text
- The message to display.public void error(java.lang.String text)
text
- The error message.public void prompt()
public void generatedOutput(java.lang.String text)
Command
.text
- The text to display.generatedError(String)
public void generatedError(java.lang.String text)
error(String)
in that this
is intended for output not "associated" with a Command
.
Example: say you fire off a background thread. If it generates an error somehow, this is the method you want.
text
- The error message.public void moreInput()
public void moreInput(int blockLevel)
private void showBanner() throws javax.swing.text.BadLocationException
javax.swing.text.BadLocationException
- Upon attempting to clear out an invalid
portion of the document.protected void endln(java.awt.Color color)
color
- Perhaps this should go!?protected void insert(java.awt.Color color, java.lang.String text)
textPane
.color
- The color of the message.text
- The actual message.protected void insertAtCaret(java.awt.Color color, java.lang.String text)
private boolean canInsertAt(int position)
position
is an acceptable place to
insert text. Currently the criteria for "acceptable" means
that position
is located within the last (or active) line, and
not within either PS1
or PS2
.position
- Position to test. Values less than zero are not allowed.position
.public int getLineCount()
public int getLineOffsetStart(int lineNumber)
public int getLineOffsetEnd(int lineNumber)
public int[] getLineOffsets(int lineNumber)
public int getOffsetLine(int offset)
offset
- Offset whose line number you want.private int[] locateLastLine()
private boolean onLastLine()
public int getCaretLine()
public java.lang.String getLineText(int lineNumber)
lineNumber
- Line number whose text is to be returned.public java.lang.String getLineJython(int lineNumber)
getLineText(int)
in that both
PS1
and PS2
are removed from the returned line.lineNumber
- Line number whose text is to be returned.public static int getPromptLength(java.lang.String line)
line
- The line in question. Cannot be null
.java.lang.NullPointerException
- if line
is null
.public static int getBlockDepth(java.lang.String line, java.lang.String whitespace)
Examples:
"print 'x'" -> 0 " print 'x'" -> 1 " die()" -> 3
line
- Line to test. Can't be null
.whitespace
- The indent String
used with line
.
Can't be null
.>= 0
) or -1
if there
was an error.public void setCallbackHandler(ConsoleCallback newHandler)
jythonRunner
.newHandler
- The new callback handler.java.lang.NullPointerException
- if the new handler is null.public java.util.Set<java.lang.String> getJythonReferencesTo(java.lang.Object obj)
public java.util.Map<java.lang.String,java.lang.Object> getJavaInstances()
public org.python.core.PyObject getJythonObject(java.lang.String var)
var
- Variable name to retrieve.Runner.copyLocals()
returned null.public java.util.Map<java.lang.String,org.python.core.PyObject> getLocalNamespace()
PyObjects
.public void handlePaste()
public void handleHome()
public void handleEnd()
public void handleUp()
public void handleDown()
public void handleTab()
WHITESPACE
wherever the cursor is
located.public void handleDelete()
public void handleEnter()
public java.util.List<java.lang.String> getHistory()
public void queueLine(java.lang.String line)
jythonRunner
and
saves it to the history.line
- Jython to queue for execution.public void queueBatch(java.lang.String name, java.util.List<java.lang.String> commands)
queueLine(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.
name
- Identifier for the batch. Doesn't need to be unique, merely
non-null.commands
- The commands to execute.public void addPretendHistory(java.lang.String line)
public void run()
run
in interface java.lang.Runnable
public void keyPressed(java.awt.event.KeyEvent e)
keyPressed
in interface java.awt.event.KeyListener
public void keyReleased(java.awt.event.KeyEvent e)
keyReleased
in interface java.awt.event.KeyListener
public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
private static boolean hasAction(javax.swing.JTextPane jtp, java.awt.event.KeyEvent e)
public static java.lang.String getUserPath(java.lang.String[] args)
public static void testConsole(boolean exitOnClose)
public static void main(java.lang.String[] args)