javax.swing.text
public abstract class AbstractDocument extends Object implements Document, Serializable
Document
interface.
This class provides some common functionality for all Element
s,
most notably it implements a locking mechanism to make document modification
thread-safe.Modifier and Type | Class and Description |
---|---|
class |
AbstractDocument.AbstractElement
An abstract base implementation of the
Element interface. |
static interface |
AbstractDocument.AttributeContext
Defines a set of methods for managing text attributes for one or more
Document s. |
class |
AbstractDocument.BranchElement
|
static interface |
AbstractDocument.Content
A sequence of data that can be edited.
|
class |
AbstractDocument.DefaultDocumentEvent
Stores the changes when a
Document is beeing modified. |
static class |
AbstractDocument.ElementEdit
An implementation of
DocumentEvent.ElementChange to be added
to AbstractDocument.DefaultDocumentEvent s. |
class |
AbstractDocument.LeafElement
An implementation of
Element that represents a leaf in the
document structure. |
Modifier and Type | Field and Description |
---|---|
protected static String |
BAD_LOCATION
Standard error message to indicate a bad location.
|
static String |
BidiElementName
Standard name for unidirectional
Element s. |
static String |
ContentElementName
Standard name for content
Element s. |
static String |
ElementNameAttribute
Attribute key for storing the element name.
|
protected EventListenerList |
listenerList
Manages event listeners for this
Document . |
static String |
ParagraphElementName
Standard name for paragraph
Element s. |
static String |
SectionElementName
Standard name for section
Element s. |
StreamDescriptionProperty, TitleProperty
Modifier | Constructor and Description |
---|---|
protected |
AbstractDocument(AbstractDocument.Content doc)
Creates a new
AbstractDocument with the specified
AbstractDocument.Content model. |
protected |
AbstractDocument(AbstractDocument.Content doc,
AbstractDocument.AttributeContext ctx)
Creates a new
AbstractDocument with the specified
AbstractDocument.Content model and AbstractDocument.AttributeContext . |
Modifier and Type | Method and Description |
---|---|
void |
addDocumentListener(DocumentListener listener)
Adds a
DocumentListener object to this document. |
void |
addUndoableEditListener(UndoableEditListener listener)
Adds an
UndoableEditListener to this Document . |
protected Element |
createBranchElement(Element parent,
AttributeSet attributes)
Creates and returns a branch element with the specified
parent and attributes . |
protected Element |
createLeafElement(Element parent,
AttributeSet attributes,
int start,
int end)
Creates and returns a leaf element with the specified
parent and attributes . |
Position |
createPosition(int offset)
Creates a
Position that keeps track of the location at the
specified offset . |
void |
dump(PrintStream out)
Dumps diagnostic information to the specified
PrintStream . |
protected void |
fireChangedUpdate(DocumentEvent event)
Notifies all registered listeners when the document model changes.
|
protected void |
fireInsertUpdate(DocumentEvent event)
Notifies all registered listeners when content is inserted in the document
model.
|
protected void |
fireRemoveUpdate(DocumentEvent event)
Notifies all registered listeners when content is removed from the
document model.
|
protected void |
fireUndoableEditUpdate(UndoableEditEvent event)
Notifies all registered listeners when an
UndoableEdit has
been performed on this Document . |
int |
getAsynchronousLoadPriority()
Returns the asynchronous loading priority.
|
protected AbstractDocument.AttributeContext |
getAttributeContext()
Returns the
AbstractDocument.AttributeContext used in this Document . |
Element |
getBidiRootElement()
Returns the root element for bidirectional content.
|
protected AbstractDocument.Content |
getContent()
Returns the
AbstractDocument.Content model for this Document |
protected Thread |
getCurrentWriter()
Returns the thread that currently modifies this
Document
if there is one, otherwise null . |
abstract Element |
getDefaultRootElement()
Returns the default root
Element of this Document . |
DocumentFilter |
getDocumentFilter()
Returns the currently installed
DocumentFilter for this
Document . |
DocumentListener[] |
getDocumentListeners()
Returns all registered
DocumentListener s. |
Dictionary<Object,Object> |
getDocumentProperties()
Returns the properties of this
Document . |
Position |
getEndPosition()
Returns a
Position which will always mark the end of the
Document . |
int |
getLength()
Returns the length of this
Document 's content. |
<T extends EventListener> |
getListeners(Class<T> listenerType)
Returns all registered listeners of a given listener type.
|
abstract Element |
getParagraphElement(int pos)
Returns the paragraph
Element that holds the specified position. |
Object |
getProperty(Object key)
Returns a property from this
Document 's property list. |
Element[] |
getRootElements()
Returns all root elements of this
Document . |
Position |
getStartPosition()
Returns a
Position which will always mark the beginning of the
Document . |
String |
getText(int offset,
int length)
Returns a piece of this
Document 's content. |
void |
getText(int offset,
int length,
Segment segment)
Fetches a piece of this
Document 's content and stores
it in the given Segment . |
UndoableEditListener[] |
getUndoableEditListeners()
Returns all registered
UndoableEditListener s. |
void |
insertString(int offset,
String text,
AttributeSet attributes)
Inserts a String into this
Document at the specified
position and assigning the specified attributes to it. |
protected void |
insertUpdate(AbstractDocument.DefaultDocumentEvent chng,
AttributeSet attr)
Called to indicate that text has been inserted into this
Document . |
protected void |
postRemoveUpdate(AbstractDocument.DefaultDocumentEvent chng)
Called after some content has been removed from this
Document . |
void |
putProperty(Object key,
Object value)
Stores a property in this
Document 's property list. |
void |
readLock()
Blocks until a read lock can be obtained.
|
void |
readUnlock()
Releases the read lock.
|
void |
remove(int offset,
int length)
Removes a piece of content from this
Document . |
void |
removeDocumentListener(DocumentListener listener)
Removes a
DocumentListener object from this document. |
void |
removeUndoableEditListener(UndoableEditListener listener)
Removes an
UndoableEditListener from this Document . |
protected void |
removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Called before some content gets removed from this
Document . |
void |
render(Runnable runnable)
Called to render this
Document visually. |
void |
replace(int offset,
int length,
String text,
AttributeSet attributes)
Replaces a piece of content in this
Document with
another piece of content. |
void |
setAsynchronousLoadPriority(int p)
Sets the asynchronous loading priority for this
Document . |
void |
setDocumentFilter(DocumentFilter filter)
Sets the
DocumentFilter for this Document . |
void |
setDocumentProperties(Dictionary<Object,Object> p)
Sets the properties of this
Document . |
protected void |
writeLock()
Blocks until a write lock can be obtained.
|
protected void |
writeUnlock()
Releases the write lock.
|
protected static final String BAD_LOCATION
public static final String BidiElementName
Element
s.public static final String ContentElementName
Element
s. These are usually
AbstractDocument.LeafElement
s.public static final String ParagraphElementName
Element
s. These are usually
AbstractDocument.BranchElement
s.public static final String SectionElementName
Element
s. These are usually
DefaultStyledDocument.SectionElement
s.public static final String ElementNameAttribute
protected EventListenerList listenerList
Document
.protected AbstractDocument(AbstractDocument.Content doc)
AbstractDocument
with the specified
AbstractDocument.Content
model.doc
- the Content
model to be used in this
Document
GapContent
,
StringContent
protected AbstractDocument(AbstractDocument.Content doc, AbstractDocument.AttributeContext ctx)
AbstractDocument
with the specified
AbstractDocument.Content
model and AbstractDocument.AttributeContext
.doc
- the Content
model to be used in this
Document
ctx
- the AttributeContext
to useGapContent
,
StringContent
public abstract Element getParagraphElement(int pos)
Element
that holds the specified position.pos
- the position for which to get the paragraph elementElement
that holds the specified positionpublic abstract Element getDefaultRootElement()
Element
of this Document
.
Usual Document
s only have one root element and return this.
However, there may be Document
implementations that
support multiple root elements, they have to return a default root element
here.getDefaultRootElement
in interface Document
Element
of this Document
protected Element createBranchElement(Element parent, AttributeSet attributes)
parent
and attributes
. Note that the new
Element
is linked to the parent Element
through Element.getParentElement()
, but it is not yet added
to the parent Element
as child.parent
- the parent Element
for the new branch elementattributes
- the text attributes to be installed in the new elementElement
AbstractDocument.BranchElement
protected Element createLeafElement(Element parent, AttributeSet attributes, int start, int end)
parent
and attributes
. Note that the new
Element
is linked to the parent Element
through Element.getParentElement()
, but it is not yet added
to the parent Element
as child.parent
- the parent Element
for the new branch elementattributes
- the text attributes to be installed in the new elementElement
AbstractDocument.LeafElement
public Position createPosition(int offset) throws BadLocationException
Position
that keeps track of the location at the
specified offset
.createPosition
in interface Document
offset
- the location in the document to keep track by the new
Position
Position
BadLocationException
- if offset
is not a valid
location in the documents content modelprotected void fireChangedUpdate(DocumentEvent event)
event
- the DocumentEvent
to be firedprotected void fireInsertUpdate(DocumentEvent event)
event
- the DocumentEvent
to be firedprotected void fireRemoveUpdate(DocumentEvent event)
event
- the DocumentEvent
to be firedprotected void fireUndoableEditUpdate(UndoableEditEvent event)
UndoableEdit
has
been performed on this Document
.event
- the UndoableEditEvent
to be firedpublic int getAsynchronousLoadPriority()
-1
if this
document should not be loaded asynchronously.protected final AbstractDocument.AttributeContext getAttributeContext()
AbstractDocument.AttributeContext
used in this Document
.AbstractDocument.AttributeContext
used in this Document
public Element getBidiRootElement()
protected final AbstractDocument.Content getContent()
AbstractDocument.Content
model for this Document
AbstractDocument.Content
model for this Document
GapContent
,
StringContent
protected final Thread getCurrentWriter()
Document
if there is one, otherwise null
. This can be used to
distinguish between a method call that is part of an ongoing modification
or if it is a separate modification for which a new lock must be aquired.Document
if there is one, otherwise null
public Dictionary<Object,Object> getDocumentProperties()
Document
.Document
public final Position getEndPosition()
Position
which will always mark the end of the
Document
.getEndPosition
in interface Document
Position
which will always mark the end of the
Document
public int getLength()
Document
's content.public <T extends EventListener> T[] getListeners(Class<T> listenerType)
listenerType
- the type of the listeners to be queriedpublic final Object getProperty(Object key)
Document
's property list.getProperty
in interface Document
key
- the key of the property to be fetchedkey
or null
if there
is no such property storedpublic Element[] getRootElements()
Document
. By default
this just returns the single root element returned by
getDefaultRootElement()
. Document
implementations
that support multiple roots must override this method and return all roots
here.getRootElements
in interface Document
Document
public final Position getStartPosition()
Position
which will always mark the beginning of the
Document
.getStartPosition
in interface Document
Position
which will always mark the beginning of the
Document
public String getText(int offset, int length) throws BadLocationException
Document
's content.getText
in interface Document
offset
- the start offset of the contentlength
- the length of the contentoffset
and
length
BadLocationException
- if offset
or offset +
length
are invalid locations with this
Document
public void getText(int offset, int length, Segment segment) throws BadLocationException
Document
's content and stores
it in the given Segment
.getText
in interface Document
offset
- the start offset of the contentlength
- the length of the contentsegment
- the Segment
to store the content inBadLocationException
- if offset
or offset +
length
are invalid locations with this
Document
public void insertString(int offset, String text, AttributeSet attributes) throws BadLocationException
Document
at the specified
position and assigning the specified attributes to it.
If a DocumentFilter
is installed in this document, the
corresponding method of the filter object is called.
The method has no effect when text
is null
or has a length of zero.
insertString
in interface Document
offset
- the location at which the string should be insertedtext
- the content to be insertedattributes
- the text attributes to be assigned to that stringBadLocationException
- if offset
is not a valid
location in this Document
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
Document
. The default implementation does nothing.
This method is executed within a write lock.chng
- the DefaultDocumentEvent
describing the changeattr
- the attributes of the changed contentprotected void postRemoveUpdate(AbstractDocument.DefaultDocumentEvent chng)
Document
. The default implementation does nothing.
This method is executed within a write lock.chng
- the DefaultDocumentEvent
describing the changepublic final void putProperty(Object key, Object value)
Document
's property list.putProperty
in interface Document
key
- the key of the property to be storedvalue
- the value of the property to be storedpublic final void readLock()
Document
.public final void readUnlock()
Document
, writing may begin now.public void remove(int offset, int length) throws BadLocationException
Document
.
If a DocumentFilter
is installed in this document, the
corresponding method of the filter object is called. The
DocumentFilter
is called even if length
is zero. This is different from replace(int, int, java.lang.String, javax.swing.text.AttributeSet)
.
Note: When length
is zero or below the call is not
forwarded to the underlying AbstractDocument.Content
instance
of this document and no exception is thrown.
remove
in interface Document
offset
- the start offset of the fragment to be removedlength
- the length of the fragment to be removedBadLocationException
- if offset
or
offset + length
or invalid locations within this
documentpublic void replace(int offset, int length, String text, AttributeSet attributes) throws BadLocationException
Document
with
another piece of content.
If a DocumentFilter
is installed in this document, the
corresponding method of the filter object is called.
The method has no effect if length
is zero (and
only zero) and, at the same time, text
is
null
or has zero length.
offset
- the start offset of the fragment to be removedlength
- the length of the fragment to be removedtext
- the text to replace the content withattributes
- the text attributes to assign to the new contentBadLocationException
- if offset
or
offset + length
or invalid locations within this
documentpublic void addDocumentListener(DocumentListener listener)
DocumentListener
object to this document.addDocumentListener
in interface Document
listener
- the listener to addpublic void removeDocumentListener(DocumentListener listener)
DocumentListener
object from this document.removeDocumentListener
in interface Document
listener
- the listener to removepublic DocumentListener[] getDocumentListeners()
DocumentListener
s.DocumentListener
spublic void addUndoableEditListener(UndoableEditListener listener)
UndoableEditListener
to this Document
.addUndoableEditListener
in interface Document
listener
- the listener to addpublic void removeUndoableEditListener(UndoableEditListener listener)
UndoableEditListener
from this Document
.removeUndoableEditListener
in interface Document
listener
- the listener to removepublic UndoableEditListener[] getUndoableEditListeners()
UndoableEditListener
s.UndoableEditListener
sprotected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Document
.
The default implementation does nothing but may be overridden by
subclasses to modify the Document
structure in response
to a remove request. The method is executed within a write lock.chng
- the DefaultDocumentEvent
describing the changepublic void render(Runnable runnable)
Document
visually. It obtains a read
lock, ensuring that no changes will be made to the document
during the rendering process. It then calls the Runnable.run()
method on runnable
. This method must not attempt
to modifiy the Document
, since a deadlock will occur if it
tries to obtain a write lock. When the Runnable.run()
method
completes (either naturally or by throwing an exception), the read lock
is released. Note that there is nothing in this method related to
the actual rendering. It could be used to execute arbitrary code within
a read lock.public void setAsynchronousLoadPriority(int p)
Document
.
A value of -1
indicates that this Document
should be loaded synchronously.p
- the asynchronous loading priority to setpublic void setDocumentProperties(Dictionary<Object,Object> p)
Document
.p
- the document properties to setprotected final void writeLock()
protected final void writeUnlock()
public DocumentFilter getDocumentFilter()
DocumentFilter
for this
Document
.DocumentFilter
for this
Document
public void setDocumentFilter(DocumentFilter filter)
DocumentFilter
for this Document
.filter
- the DocumentFilter
to setpublic void dump(PrintStream out)
PrintStream
.out
- the stream to write the diagnostic information to