edu.rice.cs.util.text
Class SwingDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.DefaultStyledDocument
          extended by edu.rice.cs.util.text.SwingDocument
All Implemented Interfaces:
AbstractDocumentInterface, EditDocumentInterface, Serializable, Document, StyledDocument
Direct Known Subclasses:
AbstractDJDocument

public class SwingDocument
extends DefaultStyledDocument
implements EditDocumentInterface, AbstractDocumentInterface

A swing implementation of the toolkit-independent EditDocumentInterface. This document must use the readers/writers locking protocol established in its superclasses. TODO: create a separate DummySwingDocument class for testing and make SwingDocument abstract.

Version:
$Id: SwingDocument.java 4691 2008-12-02 23:33:27Z dlsmith $
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.text.DefaultStyledDocument
DefaultStyledDocument.AttributeUndoableEdit, DefaultStyledDocument.ElementBuffer, DefaultStyledDocument.ElementSpec, DefaultStyledDocument.SectionElement
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
 
Field Summary
protected  DocumentEditCondition _condition
          Determines which edits are legal on this document.
protected  boolean _isModifiedSinceSave
          The modified state.
protected  HashMap<String,AttributeSet> _styles
          Maps names to attribute sets
protected static Object _wrappedPosListLock
          Lock that protects _wrappedPosListLock in DefinitionsDocument.
 
Fields inherited from class javax.swing.text.DefaultStyledDocument
buffer, BUFFER_SIZE_DEFAULT
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
SwingDocument()
          Creates a new document adapter for a Swing StyledDocument.
 
Method Summary
 String _getText(int pos, int len)
          Sanitized version of getText(int, int) that converts BadLocationException to UnexpectedException.
 void addColoring(int start, int end, String style)
          Adds the given coloring style to the styles list.
 void append(String str)
          Appends given string with default style to end of this document.
 void append(String str, AttributeSet set)
          Appends given string with specified attributes to end of this document.
 void append(String str, String style)
          Appends given string with specified named style to end of this document.
 void clear()
           
 Position createUnwrappedPosition(int offs)
          Performs the default behavior for createPosition in DefaultStyledDocument.
 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 getDefaultStyle()
          A SwingDocument instance does not have a default style
 AttributeSet getDocStyle(String name)
          Returns the style with the given name, or null if no such named style exists.
 String getDocText(int offs, int len)
          Returns a portion of the document.
 DocumentEditCondition getEditCondition()
          Gets the object which can determine whether an insert or remove edit should be applied, based on the inputs.
 Pageable getPageable()
          Returns the Pageable object for printing.
 String getText()
          Gets the document text; this method is threadsafe.
 void insertString(int offs, String str, AttributeSet set)
          Overrides superclass's insertString to impose the edit condition.
 void insertText(int offs, String str, String style)
          Inserts a string into the document at the given offset and style, if the edit condition allows it.
 void print()
          Prints the given console document
 void remove(int offs, int len)
          Overrides superclass's remove to impose the edit condition.
 void removeText(int offs, int len)
          Removes a portion of the document, if the edit condition allows it.
 void setDocStyle(String name, AttributeSet s)
          Adds the given AttributeSet as a style with the given name.
 void setEditCondition(DocumentEditCondition condition)
          Provides an object which can determine whether an insert or remove edit should be applied, based on the inputs.
 
Methods inherited from class javax.swing.text.DefaultStyledDocument
addDocumentListener, addStyle, create, createDefaultRoot, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, insert, insertUpdate, removeDocumentListener, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, setParagraphAttributes, styleChanged
 
Methods inherited from class javax.swing.text.AbstractDocument
addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.rice.cs.util.text.EditDocumentInterface
getLength
 
Methods inherited from interface edu.rice.cs.util.text.AbstractDocumentInterface
createPosition, getLength, getText
 
Methods inherited from interface javax.swing.text.Document
addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, putProperty, removeUndoableEditListener, render
 

Field Detail

_isModifiedSinceSave

protected volatile boolean _isModifiedSinceSave
The modified state.


_styles

protected final HashMap<String,AttributeSet> _styles
Maps names to attribute sets


_condition

protected DocumentEditCondition _condition
Determines which edits are legal on this document.


_wrappedPosListLock

protected static final Object _wrappedPosListLock
Lock that protects _wrappedPosListLock in DefinitionsDocument. Placed here to ensure that it initialized before use!

Constructor Detail

SwingDocument

public SwingDocument()
Creates a new document adapter for a Swing StyledDocument. TODO: convert _styles and _condition to lazily initialized volatiles as soon as support for Java 1.4 is dropped and the double-check idiom is safe.

Method Detail

setDocStyle

public void setDocStyle(String name,
                        AttributeSet s)
Adds the given AttributeSet as a style with the given name. It can then be used in insertString.

Parameters:
name - Name of the style, to be passed to insertString
s - AttributeSet to use for the style

getDocStyle

public AttributeSet getDocStyle(String name)
Returns the style with the given name, or null if no such named style exists.


addColoring

public void addColoring(int start,
                        int end,
                        String style)
Adds the given coloring style to the styles list. Not supported in SwingDocument. ONly runs in event thread.


getEditCondition

public DocumentEditCondition getEditCondition()
Gets the object which can determine whether an insert or remove edit should be applied, based on the inputs.

Specified by:
getEditCondition in interface EditDocumentInterface
Returns:
an Object 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

clear

public void clear()

insertText

public void insertText(int offs,
                       String str,
                       String style)
Inserts a string into the document at the given offset and 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

forceInsertText

public 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.

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

insertString

public void insertString(int offs,
                         String str,
                         AttributeSet set)
                  throws BadLocationException
Overrides superclass's insertString to impose the edit condition. The AttributeSet is ignored in the condition, which sees a null style name.

Specified by:
insertString in interface AbstractDocumentInterface
Specified by:
insertString in interface Document
Overrides:
insertString in class AbstractDocument
Throws:
BadLocationException

removeText

public void removeText(int offs,
                       int len)
Removes a portion of the document, if the edit condition 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)
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

remove

public void remove(int offs,
                   int len)
            throws BadLocationException
Overrides superclass's remove to impose the edit condition.

Specified by:
remove in interface AbstractDocumentInterface
Specified by:
remove in interface Document
Overrides:
remove in class AbstractDocument
Throws:
BadLocationException

getDocText

public String getDocText(int offs,
                         int len)
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()
Gets the document text; this method is threadsafe.

Specified by:
getText in interface AbstractDocumentInterface

_getText

public String _getText(int pos,
                       int len)
Sanitized version of getText(int, int) that converts BadLocationException to UnexpectedException.


append

public void append(String str,
                   AttributeSet set)
Appends given string with specified attributes to end of this document.

Specified by:
append in interface AbstractDocumentInterface

append

public void append(String str,
                   String style)
Appends given string with specified named style to end of this document.

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.

append

public void append(String str)
Appends given string with default style to end of this document.


getDefaultStyle

public String getDefaultStyle()
A SwingDocument instance does not have a default style

Specified by:
getDefaultStyle in interface EditDocumentInterface

print

public void print()
Description copied from interface: EditDocumentInterface
Prints the given console document

Specified by:
print in interface EditDocumentInterface

getPageable

public Pageable getPageable()
Description copied from interface: EditDocumentInterface
Returns the Pageable object for printing.

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

createUnwrappedPosition

public Position createUnwrappedPosition(int offs)
                                 throws BadLocationException
Performs the default behavior for createPosition in DefaultStyledDocument.

Throws:
BadLocationException