java.lang.Object | |
↳ | javax.swing.text.AbstractDocument |
![]() |
![]() |
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. The
read lock is acquired and released using the render
method. A write lock is aquired by the methods that mutate the
document, and are held for the duration of the method call.
Notification is done on the thread that produced the mutation,
and the thread has full read access to the document for the
duration of the notification, but other readers are kept out
until the notification has finished. The notification is a
beans event notification which does not allow any further
mutations until all listeners have been notified.
Any models subclassed from this class and used in conjunction
with a text component that has a look and feel implementation
that is derived from BasicTextUI may be safely updated
asynchronously, because all access to the View hierarchy
is serialized by BasicTextUI if the document is of type
AbstractDocument
. The locking assumes that an
independent thread will access the View hierarchy only from
the DocumentListener methods, and that there will be only
one event thread active at a time.
If concurrency support is desired, there are the following
additional implications. The code path for any DocumentListener
implementation and any UndoListener implementation must be threadsafe,
and not access the component lock if trying to be safe from deadlocks.
The repaint
and revalidate
methods
on JComponent are safe.
AbstractDocument models an implied break at the end of the document.
Among other things this allows you to position the caret after the last
character. As a result of this, getLength
returns one less
than the length of the Content. If you create your own Content, be
sure and initialize it to have an additional character. Refer to
StringContent and GapContent for examples of this. Another implication
of this is that Elements that model the implied end character will have
an endOffset == (getLength() + 1). For example, in DefaultStyledDocument
getParagraphElement(getLength()).getEndOffset() == getLength() + 1
.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans
package.
Please see XMLEncoder
.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
AbstractDocument.AbstractElement | Implements the abstract part of an element. | ||||||||||
AbstractDocument.AttributeContext | An interface that can be used to allow MutableAttributeSet implementations to use pluggable attribute compression techniques. | ||||||||||
AbstractDocument.BranchElement | Implements a composite element that contains other elements. | ||||||||||
AbstractDocument.Content | Interface to describe a sequence of character content that can be edited. | ||||||||||
AbstractDocument.DefaultDocumentEvent | Stores document changes as the document is being modified. | ||||||||||
AbstractDocument.ElementEdit | An implementation of ElementChange that can be added to the document event. | ||||||||||
AbstractDocument.LeafElement | Implements an element that directly represents content of some kind. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | BAD_LOCATION | Error message to indicate a bad location. | |||||||||
String | BidiElementName | Name of elements used to hold a unidirectional run | |||||||||
String | ContentElementName | Name of elements used to represent content | |||||||||
String | ElementNameAttribute | Name of the attribute used to specify element names. | |||||||||
String | ParagraphElementName | Name of elements used to represent paragraphs | |||||||||
String | SectionElementName | Name of elements used to hold sections (lines/paragraphs). |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
listenerList | The event listener list for the document. |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
AbstractDocument , wrapped around some
specified content storage mechanism. | |||||||||||
Constructs a new
AbstractDocument , wrapped around some
specified content storage mechanism. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a document listener for notification of any changes.
| |||||||||||
Adds an undo listener for notification of any changes.
| |||||||||||
Returns a position that will track change as the document
is altered.
| |||||||||||
Gives a diagnostic dump.
| |||||||||||
Gets the asynchronous loading priority.
| |||||||||||
Returns the root element of the bidirectional structure for this
document.
| |||||||||||
Returns the root element that views should be based upon
unless some other mechanism for assigning views to element
structures is provided.
| |||||||||||
Returns the
DocumentFilter that is responsible for
filtering of insertion/removal. | |||||||||||
Returns an array of all the document listeners
registered on this document.
| |||||||||||
Supports managing a set of properties.
| |||||||||||
Returns a position that represents the end of the document.
| |||||||||||
Returns the length of the data.
| |||||||||||
Returns an array of all the objects currently registered
as
FooListener s
upon this document. | |||||||||||
Get the paragraph element containing the given position.
| |||||||||||
A convenience method for looking up a property value.
| |||||||||||
Gets all root elements defined.
| |||||||||||
Returns a position that represents the start of the document.
| |||||||||||
Fetches the text contained within the given portion
of the document.
| |||||||||||
Gets a sequence of text from the document.
| |||||||||||
Returns an array of all the undoable edit listeners
registered on this document.
| |||||||||||
Inserts some content into the document.
| |||||||||||
A convenience method for storing up a property value.
| |||||||||||
Acquires a lock to begin reading some state from the
document.
| |||||||||||
Does a read unlock.
| |||||||||||
Removes some content from the document.
| |||||||||||
Removes a document listener.
| |||||||||||
Removes an undo listener.
| |||||||||||
This allows the model to be safely rendered in the presence
of currency, if the model supports being updated asynchronously.
| |||||||||||
Deletes the region of text from
offset to
offset + length , and replaces it with text . | |||||||||||
Sets the asynchronous loading priority.
| |||||||||||
Sets the
DocumentFilter . | |||||||||||
Replaces the document properties dictionary for this document.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a document branch element, that can contain other elements.
| |||||||||||
Creates a document leaf element.
| |||||||||||
Notifies all listeners that have registered interest for
notification on this event type.
| |||||||||||
Notifies all listeners that have registered interest for
notification on this event type.
| |||||||||||
Notifies all listeners that have registered interest for
notification on this event type.
| |||||||||||
Notifies all listeners that have registered interest for
notification on this event type.
| |||||||||||
Fetches the context for managing attributes.
| |||||||||||
Gets the content for the document.
| |||||||||||
Fetches the current writing thread if there is one.
| |||||||||||
Updates document structure as a result of text insertion.
| |||||||||||
Updates any document structure as a result of text removal.
| |||||||||||
Updates any document structure as a result of text removal.
| |||||||||||
Acquires a lock to begin mutating the document this lock
protects.
| |||||||||||
Releases a write lock previously obtained via
writeLock . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
Error message to indicate a bad location.
Name of elements used to hold a unidirectional run
Name of elements used to represent content
Name of the attribute used to specify element names.
Name of elements used to represent paragraphs
Name of elements used to hold sections (lines/paragraphs).
Constructs a new AbstractDocument
, wrapped around some
specified content storage mechanism.
data | the content |
---|
Constructs a new AbstractDocument
, wrapped around some
specified content storage mechanism.
data | the content |
---|---|
context | the attribute context |
Adds a document listener for notification of any changes.
listener | the DocumentListener to add |
---|
Adds an undo listener for notification of any changes.
Undo/Redo operations performed on the UndoableEdit
will cause the appropriate DocumentEvent to be fired to keep
the view(s) in sync with the model.
listener | the UndoableEditListener to add |
---|
Returns a position that will track change as the document is altered.
This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.
offs | the position in the model >= 0 |
---|
BadLocationException | if the given position does not represent a valid location in the associated document |
---|
Gets the asynchronous loading priority. If less than zero, the document should not be loaded asynchronously.
-1
if the document should not be loaded asynchronously
Returns the root element of the bidirectional structure for this document. Its children represent character runs with a given Unicode bidi level.
Returns the root element that views should be based upon unless some other mechanism for assigning views to element structures is provided.
Returns the DocumentFilter
that is responsible for
filtering of insertion/removal. A null
return value
implies no filtering is to occur.
Returns an array of all the document listeners registered on this document.
DocumentListener
s
or an empty array if no document listeners are
currently registeredSupports managing a set of properties. Callers
can use the documentProperties
dictionary
to annotate the document with document-wide properties.
null
Dictionary
Returns a position that represents the end of the document. The position returned can be counted on to track change and stay located at the end of the document.
Returns the length of the data. This is the number of characters of content that represents the users data.
Returns an array of all the objects currently registered
as FooListener
s
upon this document.
FooListener
s are registered using the
addFooListener
method.
You can specify the listenerType
argument
with a class literal, such as
FooListener.class
.
For example, you can query a
document d
for its document listeners with the following code:
DocumentListener[] mls = (DocumentListener[])(d.getListeners(DocumentListener.class));If no such listeners exist, this method returns an empty array.
listenerType | the type of listeners requested; this parameter
should specify an interface that descends from
java.util.EventListener |
---|
FooListener
s on this component,
or an empty array if no such
listeners have been addedClassCastException | if listenerType
doesn't specify a class or interface that implements
java.util.EventListener |
---|
Get the paragraph element containing the given position. Sub-classes must define for themselves what exactly constitutes a paragraph. They should keep in mind however that a paragraph should at least be the unit of text over which to run the Unicode bidirectional algorithm.
pos | the starting offset >= 0 |
---|
A convenience method for looking up a property value. It is equivalent to:
getDocumentProperties().get(key);
key | the non-null property key |
---|
null
Gets all root elements defined. Typically, there will only be one so the default implementation is to return the default root element.
Returns a position that represents the start of the document. The position returned can be counted on to track change and stay located at the beginning of the document.
Fetches the text contained within the given portion of the document.
If the partialReturn property on the txt parameter is false, the data returned in the Segment will be the entire length requested and may or may not be a copy depending upon how the data was stored. If the partialReturn property is true, only the amount of text that can be returned without creating a copy is returned. Using partial returns will give better performance for situations where large parts of the document are being scanned. The following is an example of using the partial return to access the entire document:
int nleft = doc.getDocumentLength(); Segment text = new Segment(); int offs = 0; text.setPartialReturn(true); while (nleft > 0) { doc.getText(offs, nleft, text); // do something with text nleft -= text.count; offs += text.count; }
offset | the starting offset >= 0 |
---|---|
length | the number of characters to retrieve >= 0 |
txt | the Segment object to retrieve the text into |
BadLocationException | the range given includes a position that is not a valid position within the document |
---|
Gets a sequence of text from the document.
offset | the starting offset >= 0 |
---|---|
length | the number of characters to retrieve >= 0 |
BadLocationException | the range given includes a position that is not a valid position within the document |
---|
Returns an array of all the undoable edit listeners registered on this document.
UndoableEditListener
s
or an empty array if no undoable edit listeners are
currently registeredInserts some content into the document. Inserting content causes a write lock to be held while the actual changes are taking place, followed by notification to the observers on the thread that grabbed the write lock.
This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.
offs | the starting offset >= 0 |
---|---|
str | the string to insert; does nothing with null/empty strings |
a | the attributes for the inserted content |
BadLocationException | the given insert position is not a valid position within the document |
---|
A convenience method for storing up a property value. It is equivalent to:
getDocumentProperties().put(key, value);If
value
is null
this method will
remove the property.key | the non-null key |
---|---|
value | the property value |
Acquires a lock to begin reading some state from the
document. There can be multiple readers at the same time.
Writing blocks the readers until notification of the change
to the listeners has been completed. This method should
be used very carefully to avoid unintended compromise
of the document. It should always be balanced with a
readUnlock
.
Does a read unlock. This signals that one of the readers is done. If there are no more readers then writing can begin again. This should be balanced with a readLock, and should occur in a finally statement so that the balance is guaranteed. The following is an example.
readLock();
try {
// do something
} finally {
readUnlock();
}
Removes some content from the document. Removing content causes a write lock to be held while the actual changes are taking place. Observers are notified of the change on the thread that called this method.
This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.
offs | the starting offset >= 0 |
---|---|
len | the number of characters to remove >= 0 |
BadLocationException | the given remove position is not a valid position within the document |
---|
Removes a document listener.
listener | the DocumentListener to remove |
---|
Removes an undo listener.
listener | the UndoableEditListener to remove |
---|
This allows the model to be safely rendered in the presence of currency, if the model supports being updated asynchronously. The given runnable will be executed in a way that allows it to safely read the model with no changes while the runnable is being executed. The runnable itself may not make any mutations.
This is implemented to aquire a read lock for the duration of the runnables execution. There may be multiple runnables executing at the same time, and all writers will be blocked while there are active rendering runnables. If the runnable throws an exception, its lock will be safely released. There is no protection against a runnable that never exits, which will effectively leave the document locked for it's lifetime.
If the given runnable attempts to make any mutations in this implementation, a deadlock will occur. There is no tracking of individual rendering threads to enable detecting this situation, but a subclass could incur the overhead of tracking them and throwing an error.
This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.
r | the renderer to execute |
---|
Deletes the region of text from offset
to
offset + length
, and replaces it with text
.
It is up to the implementation as to how this is implemented, some
implementations may treat this as two distinct operations: a remove
followed by an insert, others may treat the replace as one atomic
operation.
offset | index of child element |
---|---|
length | length of text to delete, may be 0 indicating don't delete anything |
text | text to insert, null indicates no text to insert |
attrs | AttributeSet indicating attributes of inserted text,
null
is legal, and typically treated as an empty attributeset,
but exact interpretation is left to the subclass |
BadLocationException | the given position is not a valid position within the document |
---|
Sets the asynchronous loading priority.
p | the new asynchronous loading priority; a value less than zero indicates that the document should not be loaded asynchronously |
---|
Sets the DocumentFilter
. The DocumentFilter
is passed insert
and remove
to conditionally
allow inserting/deleting of the text. A null
value
indicates that no filtering will occur.
filter | the DocumentFilter used to constrain text |
---|
Replaces the document properties dictionary for this document.
x | the new dictionary |
---|
Creates a document branch element, that can contain other elements.
parent | the parent element |
---|---|
a | the attributes |
Creates a document leaf element. Hook through which elements are created to represent the document structure. Because this implementation keeps structure and content separate, 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.
parent | the parent element |
---|---|
a | the attributes for the element |
p0 | the beginning of the range >= 0 |
p1 | the end of the range >= p0 |
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
e | the event |
---|
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
e | the event |
---|
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
e | the event |
---|
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.
e | the event |
---|
Fetches the context for managing attributes. This method effectively establishes the strategy used for compressing AttributeSet information.
Gets the content for the document.
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.
null
if there are no modifications in progress
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.
chng | a description of the change |
---|---|
attr | the attributes for the change |
Updates any document structure as a result of text removal. This method is called after the text has been removed from the Content. This will happen within a write lock. If a subclass of this class reimplements this method, it should delegate to the superclass as well.
chng | a description of the change |
---|
Updates any document structure as a result of text removal. This method is called before the text is actually removed from the Content. This will happen within a write lock. If a subclass of this class reimplements this method, it should delegate to the superclass as well.
chng | a description of the change |
---|
Acquires a lock to begin mutating the document this lock
protects. There can be no writing, notification of changes, or
reading going on in order to gain the lock. Additionally a thread is
allowed to gain more than one writeLock
,
as long as it doesn't attempt to gain additional writeLock
s
from within document notification. Attempting to gain a
writeLock
from within a DocumentListener notification will
result in an IllegalStateException
. The ability
to obtain more than one writeLock
per thread allows
subclasses to gain a writeLock, perform a number of operations, then
release the lock.
Calls to writeLock
must be balanced with calls to writeUnlock
, else the
Document
will be left in a locked state so that no
reading or writing can be done.
IllegalStateException | thrown on illegal lock attempt. If the document is implemented properly, this can only happen if a document listener attempts to mutate the document. This situation violates the bean event model where order of delivery is not guaranteed and all listeners should be notified before further mutations are allowed. |
---|
Releases a write lock previously obtained via writeLock
.
After decrementing the lock count if there are no oustanding locks
this will allow a new writer, or readers.