ch.aplu.util
Class Console

java.lang.Object
  extended by ch.aplu.util.Console
All Implemented Interfaces:
java.awt.print.Printable, java.lang.Runnable

public class Console
extends java.lang.Object
implements java.lang.Runnable, java.awt.print.Printable

Console window for line oriented input and output.

Once the console window is instantiated all output to stdout and stderr is redirected to this window. Only one console instance is allowed.
To avoid creating an object, the static factory method init() may be used.
Only 7-bit-ASCII characters are supported.

All Swing GUI methods are invoked from the Event Dispatch Thread (EDT) if it's not done by the caller.

Default properties of the console window can be changed from the library defaults by using a Java properties file aplu_util.properties. For more details, consult aplu_util.properties found in the distribution.

When the close button of the console window title bar is hit, System.exit(0) is executed that terminates the JVM, but you can modifiy this behavior by registering your own implementation of the ExitListener interface or by using the key ConsoleClosingMode in aplu_util.properties.

Part of code from Rjhm van den Bergh (rvdb@comweb.nl) with thanks for the permission to use and distribute


Nested Class Summary
static class Console.ClosingMode
           
 
Field Summary
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Constructor Summary
Console()
          Construct a Console with default attributes ( see init() ).
Console(Position position, Size size, java.awt.Font font)
          Construct a Console with attributes.
 
Method Summary
static void addExitListener(ExitListener exitListener)
          Register an ExitListener to get a notification when the close button is clicked.
static void clear()
          Erase all text in console window
static void delay(int time)
          Delay execution for the given amount of time ( in ms ).
static void end()
          Hide the console window and releases resources.
static java.lang.Double getDouble()
          Same as readDouble() but returns a Double object.
static javax.swing.JFrame getFrame()
          Return a reference to the JFrame instance used by the console
static java.lang.Integer getInt()
          Same as readInt() but returns an Integer object.
static char getKey()
          Return the unicode character associated with last key pressed and flush the one-charactor buffer.
static int getKeyCode()
          Return the keycode associated with last key pressed and flush the one-character buffer.
static int getKeyCodeWait()
          Wait until a key is typed and return the keycode associated with last key pressed.
static char getKeyWait()
          Wait until a key is typed and return the unicode character associated with last key pressed.
static char getLastKey()
          Return the key character associated with last key pressed.
static int getLastKeyCode()
          Return the key code associated with last key pressed.
static int getLastModifiers()
          Return the modifiers associated with last key pressed.
static java.lang.String getLastModifiersText()
          Return the modifiers text associated with last key pressed.
static javax.swing.JTextArea getTextArea()
          Return a reference to the JTextArea of the console window
static void hide()
          Hide the console window.
static Console init()
          If no console was created before, create a new Console instance with default attributes and returns a reference to it; otherwise return the existing Console reference and show the exiting console window if it was hidden (content is erased).
static Console init(java.awt.Font font)
          If no console was created before, create a new Console instance with given font and returns a reference to it; otherwise return the existing Console reference and use the given font.
static Console init(Position position, Size size)
          If no console was created before, create a new Console instance with given position and size and returns a reference to it; otherwise return the existing Console reference and show the exiting console window with new position and size.
static Console init(Position position, Size size, java.awt.Font font)
          If no console was created before, create a new Console instance with given position, size, font and returns a reference to it; otherwise return the existing Console reference and use the position, size and font.
static Console init(java.lang.String filename)
          Redirect all output to stdout and stderr to a text file with the given filename.
static Console initw()
          Same as init() but enable autowrapping.
static Console initw(java.awt.Font font)
          Same as init(Font font) but enable autowrapping.
static Console initw(Position position, Size size)
          Same as init(Position position, Size size), but enable autowrapping.
static Console initw(Position position, Size size, java.awt.Font font)
          Same as init(Position position, Size size, Font font) but enable autowrapping.
 boolean isDisposed()
          Return true, if the Console windows was disposed or released.
static boolean kbhit()
          Return true if a key was hit since the last time the one-character buffer was read with getKey() oder getKeyWait().
static java.lang.String pad(java.lang.String num, int fieldWidth)
          Right justify the given number in a field with the given field width (pad the field with leading spaces).
static java.lang.String pad(java.lang.String num, int fieldWidth, int decimalWidth)
          Pad given number with trailing spaces to optain decimal width and right justify in a field with the given width (pad the the field with leading spaces).
static Position position(int ulx, int uly)
          Return a Position reference with specified upperleft x and y coordinates.
static Console print(boolean b)
          Print a boolean value.
static Console print(char c)
          Print a character.
static Console print(char[] s)
          Print an array of characters.
static Console print(double d)
          Print a double-precision floating-point number.
static Console print(float f)
          Print a floating-point number.
 int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pageIndex)
          For internal use only.
static Console print(int i)
          Print an integer.
static Console print(long l)
          Print a long integer.
static Console print(java.lang.Object obj)
          Print an object.
static Console print(java.lang.String s)
          Print a string.
static Console printf(java.util.Locale l, java.lang.String format, java.lang.Object... args)
          Print a formatted string using the specified format string and varargs and applying given locale during formatting.
static Console printf(java.lang.String format, java.lang.Object... args)
          Print a formatted string using the specified format string and varargs.
static Console println()
          Terminate the current line by writing the line separator string.
static Console println(boolean b)
          Print a boolean and then terminate the line.
static Console println(char c)
          Print a character and then terminate the line.
static Console println(char[] s)
          Print an array of characters and then terminate the line.
static Console println(double d)
          Print a double and then terminate the line.
static Console println(float f)
          Print a float and then terminate the line.
static Console println(int i)
          Print an integer and then terminate the line.
static Console println(long l)
          Print a long and then terminate the line.
static Console println(java.lang.Object obj)
          Print an Object and then terminate the line.
static Console println(java.lang.String s)
          Print a String and then terminate the line.
 boolean printScreen()
          Same as printScreen(scale) with scale = 1
 boolean printScreen(double scale)
          Print the current text area to an attached printer with the given magnification scale factor.
static char readChar()
          Wait until a key is typed and return the unicode character associated it.
static double readDouble()
          Wait until a sequence of numbers with trailing newline is typed.
static int readInt()
          Wait until a sequence of numbers with trailing newline is typed.
static java.lang.String readLine()
          Wait until a sequence of characters with trailing newline is typed.
 void run()
          For internal use only.
static void setAutowrap(boolean enable)
          Enable/disable autowrapping.
 void setClosingMode(Console.ClosingMode mode)
          Determines what happens when the title bar close button is hit.
static void setTitle(java.lang.String title)
          Set another title in the console's title bar.
static void show()
          Show the console window.
static void showFonts()
          Show all available fonts.
static void showHorizontalScrollBar(boolean b)
          Insert/remove a horizonal scroll bar.
static void showVerticalScrollBar(boolean b)
          Insert/remove a vertical scroll bar.
static Size size(int width, int height)
          Return a Size reference with specified width and height.
static void terminate()
          Terminate application.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Console

public Console(Position position,
               Size size,
               java.awt.Font font)
Construct a Console with attributes. Runs in Event Dispatch Thread (EDT).

Parameters:
position - a reference to a Position object
size - a reference to a Size object
font - a reference to a Font object
See Also:
Position, Size, Font

Console

public Console()
Construct a Console with default attributes ( see init() ).

Method Detail

init

public static Console init()
If no console was created before, create a new Console instance with default attributes and returns a reference to it; otherwise return the existing Console reference and show the exiting console window if it was hidden (content is erased). Default size: half the screen dimensions
Default position: centered to screen
Default font: Font( "Courier New", Font.PLAIN, 16 )


initw

public static Console initw()
Same as init() but enable autowrapping.


init

public static Console init(Position position,
                           Size size)
If no console was created before, create a new Console instance with given position and size and returns a reference to it; otherwise return the existing Console reference and show the exiting console window with new position and size.

Parameters:
position - a reference to a Position object
size - a reference to a Size object
See Also:
Position, Size

initw

public static Console initw(Position position,
                            Size size)
Same as init(Position position, Size size), but enable autowrapping.


init

public static Console init(java.awt.Font font)
If no console was created before, create a new Console instance with given font and returns a reference to it; otherwise return the existing Console reference and use the given font.

Parameters:
font - a reference to a Font object
See Also:
Font

initw

public static Console initw(java.awt.Font font)
Same as init(Font font) but enable autowrapping.


init

public static Console init(Position position,
                           Size size,
                           java.awt.Font font)
If no console was created before, create a new Console instance with given position, size, font and returns a reference to it; otherwise return the existing Console reference and use the position, size and font.

Parameters:
position - a reference to a Position object, null to keep default
size - a reference to a Size object
font - a reference to a Font object
See Also:
Position, Size, Font

initw

public static Console initw(Position position,
                            Size size,
                            java.awt.Font font)
Same as init(Position position, Size size, Font font) but enable autowrapping.


init

public static Console init(java.lang.String filename)
Redirect all output to stdout and stderr to a text file with the given filename. If the file exists the text is appended, otherwise the file is created. (No console window is shown, so no input is possible.) If null is given, all output to stdout and stderr is ignored. This may be used to hide output from library functions.


end

public static void end()
Hide the console window and releases resources. Redirect System.out to stdout. (The Console reference should not be used any more.)


clear

public static void clear()
Erase all text in console window


position

public static Position position(int ulx,
                                int uly)
Return a Position reference with specified upperleft x and y coordinates. May be used in init() to avoid the keyword new


size

public static Size size(int width,
                        int height)
Return a Size reference with specified width and height. May be used in init() to avoid the keyword new


addExitListener

public static void addExitListener(ExitListener exitListener)
Register an ExitListener to get a notification when the close button is clicked. After registering the automatic termination of the application is disabled. To close the Console window without termination, Console.end() should be called. To terminate the application, System.exit() should be called.


run

public void run()
For internal use only.

Specified by:
run in interface java.lang.Runnable

show

public static void show()
Show the console window.


hide

public static void hide()
Hide the console window.


kbhit

public static boolean kbhit()
Return true if a key was hit since the last time the one-character buffer was read with getKey() oder getKeyWait(). The one-character buffer is not modified. (Put the current thread to sleep for 1 ms to reduce CPU time when used in an iteration loop.)


getKey

public static char getKey()
Return the unicode character associated with last key pressed and flush the one-charactor buffer. Return KeyEvent.CHAR_UNDEFINED if the one-character buffer is empty. (No echo in the console window.)


getKeyCode

public static int getKeyCode()
Return the keycode associated with last key pressed and flush the one-character buffer. Return KeyEvent.CHAR_UNDEFINED if the one-character buffer is empty. (No echo in the console window.)


getKeyWait

public static char getKeyWait()
Wait until a key is typed and return the unicode character associated with last key pressed. Flush the one-character buffer. (No echo in the console window.)


getKeyCodeWait

public static int getKeyCodeWait()
Wait until a key is typed and return the keycode associated with last key pressed. Flush the one-character buffer. (No echo in the console window.)


readChar

public static char readChar()
Wait until a key is typed and return the unicode character associated it. (Echo the character in the console window.)


readLine

public static java.lang.String readLine()
Wait until a sequence of characters with trailing newline is typed. Return the string (without the trailing newline) (Echo the characters in the console wiindow.)


getLastKey

public static char getLastKey()
Return the key character associated with last key pressed. Do not flush the one-charater buffer. Return KeyEvent.CHAR_UNDEFINED, if no key was pressed.


getLastKeyCode

public static int getLastKeyCode()
Return the key code associated with last key pressed. Do not flush the one-character buffer. Return 0, if no key was pressed.


getLastModifiers

public static int getLastModifiers()
Return the modifiers associated with last key pressed. Do not flush the one-character buffer. Return 0, if no key was pressed.


getLastModifiersText

public static java.lang.String getLastModifiersText()
Return the modifiers text associated with last key pressed. Do not flush the one-character buffer. Return 0, if no key was pressed. Return empty string, if no key was pressed.


readInt

public static int readInt()
Wait until a sequence of numbers with trailing newline is typed. Return the converted integer (if possible) (Echo the character in the console window.)


getInt

public static java.lang.Integer getInt()
Same as readInt() but returns an Integer object. Return null, if entered character sequence cannot converted to an integer.


readDouble

public static double readDouble()
Wait until a sequence of numbers with trailing newline is typed. Return the converted double (if possible) (Echo the character in the console window.)


getDouble

public static java.lang.Double getDouble()
Same as readDouble() but returns a Double object. Return null, if entered character sequence cannot converted to a double.


terminate

public static void terminate()
Terminate application.


showFonts

public static void showFonts()
Show all available fonts.


delay

public static void delay(int time)
Delay execution for the given amount of time ( in ms ).


pad

public static java.lang.String pad(java.lang.String num,
                                   int fieldWidth)
Right justify the given number in a field with the given field width (pad the field with leading spaces). Return the padded string (if possible)


pad

public static java.lang.String pad(java.lang.String num,
                                   int fieldWidth,
                                   int decimalWidth)
Pad given number with trailing spaces to optain decimal width and right justify in a field with the given width (pad the the field with leading spaces). Return the padded string (if possible)


getTextArea

public static javax.swing.JTextArea getTextArea()
Return a reference to the JTextArea of the console window


print

public static Console print(boolean b)
Print a boolean value. Return a reference to Console to concatenate print methods.


print

public static Console print(char c)
Print a character. Return a reference to Console to concatenate print methods.


print

public static Console print(char[] s)
Print an array of characters. Return a reference to Console to concatenate print methods.


print

public static Console print(double d)
Print a double-precision floating-point number. Return a reference to Console to concatenate print methods.


print

public static Console print(float f)
Print a floating-point number. Return a reference to Console to concatenate print methods.


print

public static Console print(int i)
Print an integer. Return a reference to Console to concatenate print methods.


print

public static Console print(long l)
Print a long integer. Return a reference to Console to concatenate print methods.


print

public static Console print(java.lang.Object obj)
Print an object. Return a reference to Console to concatenate print methods.


print

public static Console print(java.lang.String s)
Print a string. Return a reference to Console to concatenate print methods.


println

public static Console println()
Terminate the current line by writing the line separator string. Return a reference to Console to concatenate print methods.


println

public static Console println(boolean b)
Print a boolean and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(char c)
Print a character and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(char[] s)
Print an array of characters and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(double d)
Print a double and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(float f)
Print a float and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(int i)
Print an integer and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(long l)
Print a long and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(java.lang.Object obj)
Print an Object and then terminate the line. Return a reference to Console to concatenate print methods.


println

public static Console println(java.lang.String s)
Print a String and then terminate the line. Return a reference to Console to concatenate print methods.


printf

public static Console printf(java.lang.String format,
                             java.lang.Object... args)
Print a formatted string using the specified format string and varargs. (See PrintStream.printf() for more information) Return a reference to Console to concatenate print methods.


printf

public static Console printf(java.util.Locale l,
                             java.lang.String format,
                             java.lang.Object... args)
Print a formatted string using the specified format string and varargs and applying given locale during formatting. (See PrintStream.printf() for more information) Return a reference to Console to concatenate print methods.


print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat pf,
                 int pageIndex)
For internal use only. Implements Printable.print().

Specified by:
print in interface java.awt.print.Printable

printScreen

public boolean printScreen(double scale)
Print the current text area to an attached printer with the given magnification scale factor. A standard printer dialog is shown before printing is started.
Return false, if printing is canceled in this dialog, return true, when print data is sent to the printer spooler.
Only the text thats fits on one page is printed.


printScreen

public boolean printScreen()
Same as printScreen(scale) with scale = 1


getFrame

public static javax.swing.JFrame getFrame()
Return a reference to the JFrame instance used by the console


showVerticalScrollBar

public static void showVerticalScrollBar(boolean b)
Insert/remove a vertical scroll bar. Default scroll bar policy: scroll bar added as needed


showHorizontalScrollBar

public static void showHorizontalScrollBar(boolean b)
Insert/remove a horizonal scroll bar. Default scroll bar policy: scroll bar added as needed


setTitle

public static void setTitle(java.lang.String title)
Set another title in the console's title bar.


setAutowrap

public static void setAutowrap(boolean enable)
Enable/disable autowrapping. If enabled the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the current line width.


isDisposed

public boolean isDisposed()
Return true, if the Console windows was disposed or released.


setClosingMode

public void setClosingMode(Console.ClosingMode mode)
Determines what happens when the title bar close button is hit. Values: On of the enums in ClosingMode:
TerminateOnClose -> Terminating and shutting down JRE by System.exit(0)
HideOnClose -> Hides the console window, use show() to make it visible
ClearOnClose -> Clears the graphics content
AskOnClose -> Shows confirmation dialog asking for termination
DisposeOnClose -> Closes the consle window, but does not shutdown JRE
ReleaseOnClose -> Like DisposeOnClose, but throws runtime exception when graphics methods are called
NothingOnClose -> Does nothing
Default value: TerminateOnClose or value read from aplu_utils.properties