ti.swing.console
Class ColorInputHandler

java.lang.Object
  extended byti.swing.console.InputAdapter
      extended byti.swing.console.ColorInputHandler
All Implemented Interfaces:
InputHandler

public class ColorInputHandler
extends InputAdapter

An input handler which provides support for color, by use of escaped sequences of characters. The escape character, and the format of the escaped character sequences, is not well defined, so the static methods fgColor(java.awt.Color, java.lang.String), bgColor(java.awt.Color, java.lang.String), and hyperlink(java.lang.Runnable, java.lang.String) should be used, for example:

   // to make text with blue background, green foreground:
   out.println(
     ColorInputHandler.fgColor( Color.green,
       ColorInputHandler.bgColor( Color.blue, "Some Text" ) ) );
   
   // to make a hyperlink:
   out.println(
     ColorInputHandler.hyperlink( new Runnable() {
       public void run() {
         // do something here!
       }
     }, "Some Text" ) );
 
Attributes can be combined and nested as needed. Attributes created by fgColor(java.awt.Color, java.lang.String) and bgColor(java.awt.Color, java.lang.String) are constant, and so the resulting string can be reused, written to disk, read from disk, etc. Attributes created by hyperlink(java.lang.Runnable, java.lang.String) are dynamic, and can only be used once for each time they are created by calling hyperlink(java.lang.Runnable, java.lang.String).

Version:
0.1
Author:
Rob Clark

Constructor Summary
ColorInputHandler(Console console)
          Class Constructor.
 
Method Summary
 void append(char[] cbuf, int off, int len)
          Append characters to the end of the character stream.
static java.lang.String bgColor(java.awt.Color c, java.lang.String str)
          Create a string with a background color attribute applied.
 void close()
          Close method for doing any cleanup.
static java.lang.String fgColor(java.awt.Color c, java.lang.String str)
          Create a string with a foreground color attribute applied.
static java.lang.String hyperlink(java.awt.Color c, java.lang.Runnable r, java.lang.String str)
          Create a string with a hyperlink.
static java.lang.String hyperlink(java.lang.Runnable r, java.lang.String str)
          Create a string with a hyperlink.
 
Methods inherited from class ti.swing.console.InputAdapter
addRegion, getBufferLock, getData, getOffset, getRegions, lock, removeRegion, unlock, zap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorInputHandler

public ColorInputHandler(Console console)
Class Constructor.

Parameters:
console - the console we are adding this input handler to
Method Detail

fgColor

public static java.lang.String fgColor(java.awt.Color c,
                                       java.lang.String str)
Create a string with a foreground color attribute applied.

Parameters:
c - the color to apply
str - the string to apply it to
Returns:
a string with embedded control characters

bgColor

public static java.lang.String bgColor(java.awt.Color c,
                                       java.lang.String str)
Create a string with a background color attribute applied.

Parameters:
c - the color to apply
str - the string to apply it to
Returns:
a string with embedded control characters

hyperlink

public static java.lang.String hyperlink(java.lang.Runnable r,
                                         java.lang.String str)
Create a string with a hyperlink. Each time the user clicks the hyperlink, the runnable is invoked.

Parameters:
r - the runnable to invoke when user clicks link
str - the string to apply it to
Returns:
a string with embedded control characters

hyperlink

public static java.lang.String hyperlink(java.awt.Color c,
                                         java.lang.Runnable r,
                                         java.lang.String str)
Create a string with a hyperlink. Each time the user clicks the hyperlink, the runnable is invoked.

Parameters:
c - the color to apply
r - the runnable to invoke when user clicks link
str - the string to apply it to
Returns:
a string with embedded control characters

append

public void append(char[] cbuf,
                   int off,
                   int len)
Append characters to the end of the character stream. Note that this method is the entry point to a bunch of internal processing that is not thread safe, so it is synchronized on the buffer lock.

Specified by:
append in interface InputHandler
Overrides:
append in class InputAdapter
Parameters:
cbuf - the character buffer
off - the offset into cbuf to first character to append
len - the number of characters to append

close

public void close()
Description copied from class: InputAdapter
Close method for doing any cleanup.

Specified by:
close in interface InputHandler
Overrides:
close in class InputAdapter