All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.text.AbstractDocument

java.lang.Object
   |
   +----com.sun.java.swing.text.AbstractDocument

public abstract class AbstractDocument
extends Object
implements Document, Serializable
An implementation of the document interface to serve as a basis for implementing various kinds of documents. At this level there is very little policy, so there is a corresponding increase in difficulty of use. This class implements a locking mechanism for the document. It allows multiple readers or one writer, and writers must wait until all observers of the document have been notified of a previous change before beginning another mutation to the document.


Variable Index

 o BAD_LOCATION
Error message to indicate a bad location.
 o ContentElementName
Name of elements used to represent content
 o ElementNameAttribute
Name of the attribute used to specify element names.
 o listenerList
 o ParagraphElementName
Name of elements used to represent paragraphs
 o SectionElementName
Name of elements used to hold sections (lines/paragraphs).

Constructor Index

 o AbstractDocument(AbstractDocument. Content)
Constructs a new AbstractDocument, wrapped around some specified content.

Method Index

 o addDocumentListener(DocumentListener)
Adds a document listener for notification of any changes.
 o createBranchElement(Element, AttributeSet)
Creates a document branch element, that can contain other elements.
 o createLeafElement(Element, AttributeSet, int, int)
Creates a document leaf element.
 o createPosition(int)
Returns a position that will track change as the document is altered.
 o dump(PrintStream)
Gives a diagnostic dump.
 o externalNotifyChange(DocumentEvent)
Notifies external listeners of a document attribute change.
 o externalNotifyInsert(DocumentEvent)
Notifies external listeners of a document insertion.
 o externalNotifyRemove(DocumentEvent)
Notifies external listeners of a document removal.
 o fireChangedUpdate(DocumentEvent)
 o fireInsertUpdate(DocumentEvent)
 o fireRemoveUpdate(DocumentEvent)
 o getContent()
Gets the content for the document.
 o getCurrentWriter()
Fetches the current writing thread if there is one.
 o getDefaultRootElement()
Returns the root element that views should be based upon unless some other mechanism for assigning views to element structures is provided.
 o getEndPosition()
Returns a position that represents the end of the document.
 o getLength()
Returns the length of the data.
 o getProperty(Object)
Gets a property for the document.
 o getRootElements()
Gets all root elements defined.
 o getStartPosition()
Returns a position that represents the start of the document.
 o getText(int, int)
Gets a subsequence of text from the document.
 o getText(int, int, Segment)
Gets some text from the document.
 o insertString(int, String, AttributeSet)
Inserts some content into the document.
 o insertUpdate(DefaultDocumentEvent, AttributeSet)
Updates document structure as a result of text insertion.
 o notifyUnlock()
Releases the notification lock.
 o putProperty(Object, Object)
Sets a property for the document.
 o readLock()
Acquires a lock to begin reading some state from the document.
 o readUnlock()
Does a read unlock.
 o remove(int, int)
Removes some content from the document.
 o removeDocumentListener(DocumentListener)
Removes a document listener.
 o removeUpdate(DefaultDocumentEvent)
Updates any document structure as a result of text removal.
 o writeAbandoned()
Releases the write/notify lock held because the write operation is being aborted.
 o writeLock()
Acquires a lock to begin mutating the document this lock protects.
 o writeUnlockNotifyLock()
Releases the write lock indicating that there will be no more mutations to the document, and acquire a lock for notifying observers of the document of the mutations to the document.

Variables

 o listenerList
 protected EventListenerList listenerList
 o BAD_LOCATION
 protected static final String BAD_LOCATION
Error message to indicate a bad location.

 o ParagraphElementName
 public static final String ParagraphElementName
Name of elements used to represent paragraphs

 o ContentElementName
 public static final String ContentElementName
Name of elements used to represent content

 o SectionElementName
 public static final String SectionElementName
Name of elements used to hold sections (lines/paragraphs).

 o ElementNameAttribute
 public static final String ElementNameAttribute
Name of the attribute used to specify element names.

Constructors

 o AbstractDocument
 protected AbstractDocument(AbstractDocument. Content data)
Constructs a new AbstractDocument, wrapped around some specified content.

Parameters:
data - the content

Methods

 o getLength
 public int getLength()
Returns the length of the data. This method grabs a read lock. If there is currently a writer, then this call is blocked until the writer is finished making changes.

Returns:
the length
See Also:
getLength
 o addDocumentListener
 public synchronized void addDocumentListener(DocumentListener listener)
Adds a document listener for notification of any changes.

Parameters:
listener - the listener
See Also:
addDocumentListener
 o removeDocumentListener
 public synchronized void removeDocumentListener(DocumentListener listener)
Removes a document listener.

Parameters:
listener - the listener
See Also:
removeDocumentListener
 o fireInsertUpdate
 protected void fireInsertUpdate(DocumentEvent e)
 o fireChangedUpdate
 protected void fireChangedUpdate(DocumentEvent e)
 o fireRemoveUpdate
 protected void fireRemoveUpdate(DocumentEvent e)
 o getProperty
 public Object getProperty(Object key)
Gets a property for the document.

Parameters:
key - the property key
Returns:
the value
 o putProperty
 public void putProperty(Object key,
                         Object value)
Sets a property for the document.

Parameters:
key - the property key
value - the property value
 o remove
 public void remove(int offs,
                    int len) throws BadLocationException
Removes some content from the document. Removing content causes a write lock to be held while the actual changes are taking place, followed by a read lock which is held while observers are notified of the change.

Parameters:
offs - the starting offset
len - the number of characters to remove
Throws: BadLocationException
the given remove position is not a valid position within the document
See Also:
remove
 o insertString
 public void insertString(int offs,
                          String str,
                          AttributeSet a) throws BadLocationException
Inserts some content into the document. Inserting content causes a write lock to be held while the actual changes are taking place, followed by a notification lock (that allows reads but not writes) while observers are notified of the change.

Parameters:
offs - the starting offset
str - the string to insert
a - the attributes for the inserted content
Throws: BadLocationException
the given insert position is not a valid position within the document
See Also:
insertString
 o getText
 public String getText(int offset,
                       int length) throws BadLocationException
Gets a subsequence of text from the document. This method grabs a read lock. If there is currently a writer, then this call is blocked until the writer is finished making changes.

Parameters:
offset - the starting offset
length - the number of characters to retrieve
Throws: BadLocationException
the range given includes a position that is not a valid position within the document
See Also:
getText
 o getText
 public void getText(int offset,
                     int length,
                     Segment txt) throws BadLocationException
Gets some text from the document.

Parameters:
offset - the starting offset
length - the number of characters to retrieve
txt - the Segment object to retrieve the text into
Throws: BadLocationException
the range given includes a position that is not a valid position within the document
 o getStartPosition
 public Position getStartPosition()
Returns a position that represents the start of the document.

Returns:
the position
See Also:
getStartPosition
 o getEndPosition
 public Position getEndPosition()
Returns a position that represents the end of the document. This method grabs a read lock. If there is currently a writer, then this call is blocked until the writer is finished making changes.

Returns:
the position
See Also:
getEndPosition
 o createPosition
 public synchronized Position createPosition(int offs) throws BadLocationException
Returns a position that will track change as the document is altered.

Parameters:
offs - the position
Throws: BadLocationException
if the given position does not represent a valid location in the associated document
See Also:
createPosition
 o getRootElements
 public Element[] getRootElements()
Gets all root elements defined. Typically, there will only be one so the default implementation is to return the default root element.

Returns:
the root element
 o getDefaultRootElement
 public abstract Element getDefaultRootElement()
Returns the root element that views should be based upon unless some other mechanism for assigning views to element structures is provided.

Returns:
the root element
See Also:
getDefaultRootElement
 o insertUpdate
 protected void insertUpdate(DefaultDocumentEvent chng,
                             AttributeSet attr)
Updates document structure as a result of text insertion. This will happen within a write lock. If a subclass of this class reimplements this method, it should delegate to the superclass as well.

Parameters:
chng - a description of the change
attr - the attributes for the change
 o removeUpdate
 protected void removeUpdate(DefaultDocumentEvent chng)
Updates any document structure as a result of text removal. This will happen within a write lock. If a subclass of this class reimplements this method, it should delegate to the superclass as well.

Parameters:
chng - a description of the change
 o dump
 public void dump(PrintStream out)
Gives a diagnostic dump.

Parameters:
out - the output stream
 o getContent
 protected final AbstractDocument. Content getContent()
Gets the content for the document.

Returns:
the content
 o externalNotifyInsert
 protected final void externalNotifyInsert(DocumentEvent e)
Notifies external listeners of a document insertion. This removes the write lock and grabs a notification lock that prevents further writes, but allows reads. At the end of notification the notification lock is removed and writes can resume if there are no active readers.

Parameters:
e - the document event
 o externalNotifyChange
 protected final void externalNotifyChange(DocumentEvent e)
Notifies external listeners of a document attribute change. This removes the write lock and grabs a notification lock that prevents further writes, but allows reads. At the end of notification the notification lock is removed and writes can resume if there are no active readers.

Parameters:
e - the document event
 o externalNotifyRemove
 protected final void externalNotifyRemove(DocumentEvent e)
Notifies external listeners of a document removal. This removes the write lock and grabs a notification lock that prevents further writes, but allows reads. At the end of notification the notification lock is removed and writes can resume if there are no active readers.

Parameters:
e - the document event
 o createLeafElement
 protected Element createLeafElement(Element parent,
                                     AttributeSet a,
                                     int p0,
                                     int p1)
Creates a document leaf element. Hook through which elements are created to represent the document structure. Because this implementation keeps structure and content seperate, elements grow automatically when content is extended so splits of existing elements follow. The document itself gets to decide how to generate elements to give flexibility in the type of elements used.

Parameters:
parent - the parent element
a - the attributes for the element
p0 - the beginning of the range
p1 - the end of the range
Returns:
the new element
 o createBranchElement
 protected Element createBranchElement(Element parent,
                                       AttributeSet a)
Creates a document branch element, that can contain other elements.

Parameters:
parent - the parent element
a - the attributes
 o getCurrentWriter
 protected final synchronized Thread getCurrentWriter()
Fetches the current writing thread if there is one. This can be used to distinguish whether a method is being called as part of an existing modification or if a lock needs to be acquired and a new transaction started.

Returns:
s the thread actively modifying the document or null if there are no modifications in progress
 o writeLock
 protected final synchronized void writeLock()
Acquires a lock to begin mutating the document this lock protects. There can be no notification of changes or reading going on in order to gain the lock.

 o writeUnlockNotifyLock
 protected final synchronized void writeUnlockNotifyLock()
Releases the write lock indicating that there will be no more mutations to the document, and acquire a lock for notifying observers of the document of the mutations to the document. Readers are allowed in this state.

 o writeAbandoned
 protected final synchronized void writeAbandoned()
Releases the write/notify lock held because the write operation is being aborted.

 o notifyUnlock
 protected final synchronized void notifyUnlock()
Releases the notification lock. This enables writing to occur again.

 o readLock
 protected final synchronized void readLock()
Acquires a lock to begin reading some state from the document. There can be multiple readers at the same time and reading can occur while notification to the listeners is going on, but writing blocks the readers. If the calling thread is currently writing, it simply returns.

 o readUnlock
 protected final synchronized void readUnlock()
Does a read unlock. One of the readers is done. If there are no more readers then writing can begin again. If the calling thread is currently writing, is simply returns.


All Packages  Class Hierarchy  This Package  Previous  Next  Index