public class Console extends Object implements Runnable, 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 String |
BANNER
Not used yet.
|
private ConsoleCallback |
callback
A hook that allows external classes to respond to events.
|
private Document |
document
textPane 's internal representation. |
private boolean |
exitOnClose |
private static Font |
FONT
All text will appear in this font.
|
private List<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 JPanel |
panel
Panel that holds
textPane . |
private static String |
PS1
Normal jython prompt.
|
private static String |
PS2
Prompt that indicates more input is needed.
|
private JTextPane |
textPane
Where the user interacts with the Jython interpreter.
|
protected static Color |
TXT_ERROR
Color of text coming from "stderr".
|
protected static Color |
TXT_GOOD
Color of text coming from "stdout".
|
protected static Color |
TXT_NORMAL
Color of the Jython text as it is being entered.
|
protected static Color |
TXT_WARN
Not used just yet...
|
private static String |
WHITESPACE
Actual
String of whitespace to insert for blocks and whatnot. |
private String |
windowTitle
Title of the console window.
|
Constructor and Description |
---|
Console()
Build a console with no initial commands.
|
Console(boolean exitOnClose) |
Console(boolean exitOnClose,
List<String> initialCommands)
Builds a console and executes a list of Jython statements.
|
Modifier and Type | Method and Description |
---|---|
void |
addPretendHistory(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(String name) |
protected void |
endln(Color color)
Inserts a newline character at the end of the input.
|
void |
error(String text)
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 |
getCaretLine() |
List<String> |
getHistory()
Returns the Jython statements as entered by the user, ordered from first
to last.
|
Map<String,Object> |
getJavaInstances()
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.
|
Set<String> |
getJythonReferencesTo(Object obj) |
int |
getLineCount() |
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) |
String |
getLineText(int lineNumber)
Returns the line of text that occupies the specified line number.
|
Map<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.
|
JPanel |
getPanel()
Returns the panel containing the various UI components.
|
static int |
getPromptLength(String line)
|
protected JTextPane |
getTextPane()
Returns the
JTextPane used by the console. |
static String |
getUserPath(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(JTextPane jtp,
KeyEvent e) |
void |
injectObject(String name,
Object object)
Inserts the specified object into Jython's local namespace using the
specified name.
|
protected void |
insert(Color color,
String text)
Does the actual work of displaying color-coded messages in
textPane . |
protected void |
insertAtCaret(Color color,
String text) |
void |
keyPressed(KeyEvent e)
Noop.
|
void |
keyReleased(KeyEvent e)
Noop.
|
void |
keyTyped(KeyEvent e) |
private int[] |
locateLastLine()
Returns the offsets of the beginning and end of the last line.
|
static void |
main(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(String name,
List<String> commands)
Sends a batch of Jython commands to the interpreter.
|
void |
queueLine(String line)
Sends a line of Jython to the interpreter via
jythonRunner and
saves it to the history. |
void |
result(String text)
Displays non-error output.
|
void |
run()
Puts together the GUI once EventQueue has processed all other pending
events.
|
void |
runFile(String name,
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 Color TXT_NORMAL
private static final org.slf4j.Logger logger
Logger
object for Jython consoles.private static final int[] BAD_OFFSETS
private static final String PS1
private static final String PS2
private static final String WHITESPACE
String
of whitespace to insert for blocks and whatnot.private final List<String> jythonHistory
private Runner jythonRunner
private ConsoleCallback callback
private String windowTitle
private MenuWrangler menuWrangler
private final boolean exitOnClose
public Console()
public Console(boolean exitOnClose)
protected JTextPane getTextPane()
JTextPane
used by the console.public void injectObject(String name, 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(String name)
public void ejectObject(org.python.core.PyObject pyObject)
public void runFile(String name, String path)
path
in the Interpreter
.name
- __name__
attribute to use for loading path
.path
- The path to the Jython file.public void result(String text)
text
- The message to display.public void prompt()
public void generatedOutput(String text)
Command
.text
- The text to display.generatedError(String)
public void generatedError(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 BadLocationException
BadLocationException
- Upon attempting to clear out an invalid
portion of the document.protected void endln(Color color)
color
- Perhaps this should go!?protected void insert(Color color, String text)
textPane
.color
- The color of the message.text
- The actual message.protected void insertAtCaret(Color color, 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 String getLineText(int lineNumber)
lineNumber
- Line number whose text is to be returned.public 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(String line)
line
- The line in question. Cannot be null
.NullPointerException
- if line
is null
.public static int getBlockDepth(String line, 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.NullPointerException
- if the new handler is null.public Set<String> getJythonReferencesTo(Object obj)
public Map<String,Object> getJavaInstances()
public org.python.core.PyObject getJythonObject(String var)
var
- Variable name to retrieve.Runner.copyLocals()
returned null.public Map<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 List<String> getHistory()
public void queueLine(String line)
jythonRunner
and
saves it to the history.line
- Jython to queue for execution.public void queueBatch(String name, List<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(String line)
public void run()
public void keyPressed(KeyEvent e)
keyPressed
in interface KeyListener
public void keyReleased(KeyEvent e)
keyReleased
in interface KeyListener
public void keyTyped(KeyEvent e)
keyTyped
in interface KeyListener
public static String getUserPath(String[] args)
public static void testConsole(boolean exitOnClose)