edu.rice.cs.util.text
Class ConsoleDocument

java.lang.Object
  extended by edu.rice.cs.util.text.ConsoleDocument
All Implemented Interfaces:
ConsoleDocumentInterface, EditDocumentInterface
Direct Known Subclasses:
InteractionsDocument

public class ConsoleDocument
extends Object
implements ConsoleDocumentInterface

A GUI-toolkit agnostic interface to a console document. This class assumes that the embedded document supports readers/writers locking and uses that locking protocol to ensure the integrity of the data added in this class WHY is prompt considered part of a console document rather than an interactions document?

Version:
$Id: ConsoleDocument.java 4691 2008-12-02 23:33:27Z dlsmith $

Field Summary
protected  Runnable _beep
          A runnable command to use for a notification beep.
protected  DrJavaBook _book
          The book object used for printing that represents several pages
protected  ConsoleDocumentInterface _document
          The embedded document storing the text and _hasPrompt property for this console model.
protected  String _prompt
          String to use for the prompt.
static String DEFAULT_CONSOLE_PROMPT
          The default prompt to use in the console.
static String DEFAULT_STYLE
          Default text style.
static String SYSTEM_ERR_STYLE
          Style for System.err
static String SYSTEM_IN_STYLE
          Style for System.in
static String SYSTEM_OUT_STYLE
          Style for System.out
 
Constructor Summary
ConsoleDocument(ConsoleDocumentInterface doc)
          Creates a new ConsoleDocument with the given embedded ConsoleDocumentInterface (a SwingDocument in native DrJava).
 
Method Summary
protected  void _clearCurrentInputText()
          Removes the text from the current prompt to the end of the document.
 void append(String str, String style)
          Appends a string to this in the given named style, if the edit condition allows it.
 void cleanUpPrintJob()
          Clears the pageable object used to hold the print job.
 void clearCurrentInput()
          Clears the current input text.
 void disablePrompt()
          Disables the prompt in this document.
 void forceInsertText(int offs, String str, String style)
          Inserts a string into the document at the given offset and style, regardless of the edit condition.
 void forceRemoveText(int offs, int len)
          Removes a portion of the document, regardless of the edit condition.
 String getCurrentInput()
          Returns the string that the user has entered at the current prompt.
 String getDefaultStyle()
          Gets the String identifying the default style for this document if one exists; null otherwise.
 String getDocText(int offs, int len)
          Returns a portion of the document.
 DocumentEditCondition getEditCondition()
          Gets the object which determines whether an insert/remove edit should be applied based on the inputs.
 int getLength()
          Returns the length of the document.
 Pageable getPageable()
          Returns the Pageable object for printing.
 String getPrompt()
          Accessor for the string used for the prompt.
 int getPromptLength()
          Returns the length of the prompt string.
 int getPromptPos()
          Returns the first location in the document where editing is allowed.
 String getText()
          Returns the entire text of the document.
 boolean hasPrompt()
           
 void insertBeforeLastPrompt(String text, String style)
          Inserts the given string with the given attributes just before the most recent prompt.
 void insertNewline(int pos)
          Inserts a new line at the given position.
 void insertPrompt()
          Prints a prompt for a new input.
 void insertText(int offs, String str, String style)
          Inserts a string into the document at the given offset and named style, if the edit condition allows it.
 void preparePrintJob()
          This method tells the document to prepare all the DrJavaBook and PagePrinter objects.
 void print()
          Prints the given document by bringing up a "Print" window.
 void removeText(int offs, int len)
          Removes a portion of the document, if the edit condition (including promptPos) allows it.
 void reset(String banner)
          Resets the document to a clean state.
 void setBeep(Runnable beep)
          Sets a runnable action to use as a beep.
 void setEditCondition(DocumentEditCondition condition)
          Provides an object which can determine whether an insert or remove edit should be applied, based on the inputs.
 void setHasPrompt(boolean val)
          Setter for the _hasPrompt property.
 void setPrompt(String prompt)
          Sets the string to use for the prompt.
 void setPromptPos(int newPos)
          Sets the prompt position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONSOLE_PROMPT

public static final String DEFAULT_CONSOLE_PROMPT
The default prompt to use in the console.

See Also:
Constant Field Values

DEFAULT_STYLE

public static final String DEFAULT_STYLE
Default text style.

See Also:
Constant Field Values

SYSTEM_OUT_STYLE

public static final String SYSTEM_OUT_STYLE
Style for System.out

See Also:
Constant Field Values

SYSTEM_ERR_STYLE

public static final String SYSTEM_ERR_STYLE
Style for System.err

See Also:
Constant Field Values

SYSTEM_IN_STYLE

public static final String SYSTEM_IN_STYLE
Style for System.in

See Also:
Constant Field Values

_document

protected final ConsoleDocumentInterface _document
The embedded document storing the text and _hasPrompt property for this console model.


_beep

protected volatile Runnable _beep
A runnable command to use for a notification beep.


_prompt

protected volatile String _prompt
String to use for the prompt.


_book

protected volatile DrJavaBook _book
The book object used for printing that represents several pages

Constructor Detail

ConsoleDocument

public ConsoleDocument(ConsoleDocumentInterface doc)
Creates a new ConsoleDocument with the given embedded ConsoleDocumentInterface (a SwingDocument in native DrJava).

Parameters:
doc - the embedded ConsoleDocumentInterface object
Method Detail

hasPrompt

public boolean hasPrompt()
Specified by:
hasPrompt in interface ConsoleDocumentInterface
Returns:
true iff this document has a prompt and is ready to accept input.

setHasPrompt

public void setHasPrompt(boolean val)
Description copied from interface: ConsoleDocumentInterface
Setter for the _hasPrompt property.

Specified by:
setHasPrompt in interface ConsoleDocumentInterface

getPrompt

public String getPrompt()
Accessor for the string used for the prompt.


setPrompt

public void setPrompt(String prompt)
Sets the string to use for the prompt.

Parameters:
prompt - String to use for the prompt.

getPromptLength

public int getPromptLength()
Returns the length of the prompt string.


getEditCondition

public DocumentEditCondition getEditCondition()
Gets the object which determines whether an insert/remove edit should be applied based on the inputs.

Specified by:
getEditCondition in interface EditDocumentInterface
Returns:
the DocumentEditCondition to determine legality of inputs

setEditCondition

public void setEditCondition(DocumentEditCondition condition)
Provides an object which can determine whether an insert or remove edit should be applied, based on the inputs.

Specified by:
setEditCondition in interface EditDocumentInterface
Parameters:
condition - Object to determine legality of inputs

getPromptPos

public int getPromptPos()
Returns the first location in the document where editing is allowed.


setPromptPos

public void setPromptPos(int newPos)
Sets the prompt position. Only used in tests.

Parameters:
newPos - the new position.

setBeep

public void setBeep(Runnable beep)
Sets a runnable action to use as a beep.

Parameters:
beep - Runnable beep command

reset

public void reset(String banner)
Resets the document to a clean state. Only runs in the event thread.


insertPrompt

public void insertPrompt()
Prints a prompt for a new input.


disablePrompt

public void disablePrompt()
Disables the prompt in this document.


insertNewline

public void insertNewline(int pos)
Inserts a new line at the given position.

Parameters:
pos - Position to insert the new line

insertBeforeLastPrompt

public void insertBeforeLastPrompt(String text,
                                   String style)
Inserts the given string with the given attributes just before the most recent prompt.

Parameters:
text - String to insert
style - name of style to format the string

insertText

public void insertText(int offs,
                       String str,
                       String style)
                throws EditDocumentException
Inserts a string into the document at the given offset and named style, if the edit condition allows it.

Specified by:
insertText in interface EditDocumentInterface
Parameters:
offs - Offset into the document
str - String to be inserted
style - Name of the style to use. Must have been added using addStyle.
Throws:
EditDocumentException - if the offset is illegal

append

public void append(String str,
                   String style)
            throws EditDocumentException
Appends a string to this in the given named style, if the edit condition allows it.

Specified by:
append in interface EditDocumentInterface
Parameters:
str - String to be inserted
style - Name of the style to use. Must have been added using addStyle.
Throws:
EditDocumentException - if the offset is illegal

forceInsertText

public void forceInsertText(int offs,
                            String str,
                            String style)
                     throws EditDocumentException
Inserts a string into the document at the given offset and style, regardless of the edit condition.

Specified by:
forceInsertText in interface EditDocumentInterface
Parameters:
offs - Offset into the document
str - String to be inserted
style - Name of the style to use. Must have been added using addStyle.
Throws:
EditDocumentException - if the offset is illegal

removeText

public void removeText(int offs,
                       int len)
                throws EditDocumentException
Removes a portion of the document, if the edit condition (including promptPos) allows it.

Specified by:
removeText in interface EditDocumentInterface
Parameters:
offs - Offset to start deleting from
len - Number of characters to remove
Throws:
EditDocumentException - if the offset or length are illegal

forceRemoveText

public void forceRemoveText(int offs,
                            int len)
                     throws EditDocumentException
Removes a portion of the document, regardless of the edit condition.

Specified by:
forceRemoveText in interface EditDocumentInterface
Parameters:
offs - Offset to start deleting from
len - Number of characters to remove
Throws:
EditDocumentException - if the offset or length are illegal

getLength

public int getLength()
Returns the length of the document.

Specified by:
getLength in interface EditDocumentInterface

getDocText

public String getDocText(int offs,
                         int len)
                  throws EditDocumentException
Returns a portion of the document.

Specified by:
getDocText in interface EditDocumentInterface
Parameters:
offs - First offset of the desired text
len - Number of characters to return
Throws:
EditDocumentException - if the offset or length are illegal

getText

public String getText()
Returns the entire text of the document. Identical to getText() in AbstractDocumentInterface.

Throws:
EditDocumentException - if the offset or length are illegal

getCurrentInput

public String getCurrentInput()
Returns the string that the user has entered at the current prompt. May contain newline characters.


clearCurrentInput

public void clearCurrentInput()
Clears the current input text.


_clearCurrentInputText

protected void _clearCurrentInputText()
Removes the text from the current prompt to the end of the document.


getDefaultStyle

public String getDefaultStyle()
Description copied from interface: EditDocumentInterface
Gets the String identifying the default style for this document if one exists; null otherwise.

Specified by:
getDefaultStyle in interface EditDocumentInterface

getPageable

public Pageable getPageable()
                     throws IllegalStateException
Returns the Pageable object for printing.

Specified by:
getPageable in interface EditDocumentInterface
Returns:
A Pageable representing this document.
Throws:
IllegalStateException

preparePrintJob

public void preparePrintJob()
This method tells the document to prepare all the DrJavaBook and PagePrinter objects.


print

public void print()
           throws PrinterException
Prints the given document by bringing up a "Print" window.

Specified by:
print in interface EditDocumentInterface
Throws:
PrinterException

cleanUpPrintJob

public void cleanUpPrintJob()
Clears the pageable object used to hold the print job.