Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
javax.swing.text.AbstractDocument
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.
Nested Class Summary | |
abstract class |
|
static interface |
|
class |
|
static interface |
|
class |
|
static class |
|
class |
|
Field Summary | |
protected static String |
|
static String |
|
static String |
|
static String |
|
static String |
|
static String |
|
protected EventListenerList |
|
Fields inherited from interface javax.swing.text.Document | |
StreamDescriptionProperty , TitleProperty |
Constructor Summary | |
| |
|
Method Summary | |
Dictionary |
|
void |
|
void |
|
protected Element |
|
protected Element |
|
Position |
|
void |
|
|
|
protected void |
|
protected void |
|
protected void |
|
protected void |
|
int |
|
protected AbstractDocument.AttributeContext |
|
Element |
|
protected AbstractDocument.Content |
|
protected Thread |
|
abstract Element |
|
DocumentFilter |
|
DocumentListener[] |
|
Position |
|
int |
|
abstract Element |
|
Object |
|
Element[] |
|
Position |
|
String |
|
void | |
UndoableEditListener[] |
|
void |
|
protected void |
|
protected void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
protected void |
|
void | |
void |
|
void |
|
void |
|
void |
|
protected void |
|
protected void |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
protected static final String BAD_LOCATION
Standard error message to indicate a bad location.
- Field Value:
- "document location failure"
public static final String BidiElementName
Standard name for unidirectionalElement
s.
- Field Value:
- "bidi level"
public static final String ContentElementName
Standard name for contentElement
s. These are usuallyAbstractDocument.LeafElement
s.
- Field Value:
- "content"
public static final String ElementNameAttribute
Attribute key for storing the element name.
- Field Value:
- "$ename"
public static final String ParagraphElementName
Standard name for paragraphElement
s. These are usuallyAbstractDocument.BranchElement
s.
- Field Value:
- "paragraph"
public static final String SectionElementName
Standard name for sectionElement
s. These are usuallyDefaultStyledDocument.SectionElement
s.
- Field Value:
- "section"
protected AbstractDocument(AbstractDocument.Content doc)
Creates a newAbstractDocument
with the specifiedAbstractDocument.Content
model.
- Parameters:
doc
- theContent
model to be used in thisDocument
- See Also:
GapContent
,StringContent
protected AbstractDocument(AbstractDocument.Content doc, AbstractDocument.AttributeContext ctx)
Creates a newAbstractDocument
with the specifiedAbstractDocument.Content
model andAbstractDocument.AttributeContext
.
- Parameters:
doc
- theContent
model to be used in thisDocument
ctx
- theAttributeContext
to use
- See Also:
GapContent
,StringContent
public Dictionary()
Returns the properties of thisDocument
.
- Returns:
- the properties of this
Document
public void addDocumentListener(DocumentListener listener)
Adds aDocumentListener
object to this document.
- Specified by:
- addDocumentListener in interface Document
- Parameters:
listener
- the listener to add
public void addUndoableEditListener(UndoableEditListener listener)
Adds anUndoableEditListener
to thisDocument
.
- Specified by:
- addUndoableEditListener in interface Document
- Parameters:
listener
- the listener to add
protected Element createBranchElement(Element parent, AttributeSet attributes)
Creates and returns a branch element with the specifiedparent
andattributes
. Note that the newElement
is linked to the parentElement
throughElement.getParentElement()
, but it is not yet added to the parentElement
as child.
- Parameters:
parent
- the parentElement
for the new branch elementattributes
- the text attributes to be installed in the new element
- Returns:
- the new branch
Element
- See Also:
AbstractDocument.BranchElement
protected Element createLeafElement(Element parent, AttributeSet attributes, int start, int end)
Creates and returns a leaf element with the specifiedparent
andattributes
. Note that the newElement
is linked to the parentElement
throughElement.getParentElement()
, but it is not yet added to the parentElement
as child.
- Parameters:
parent
- the parentElement
for the new branch elementattributes
- the text attributes to be installed in the new element
- Returns:
- the new branch
Element
- See Also:
AbstractDocument.LeafElement
public Position createPosition(int offset) throws BadLocationException
Creates aPosition
that keeps track of the location at the specifiedoffset
.
- Specified by:
- createPosition in interface Document
- Parameters:
offset
- the location in the document to keep track by the newPosition
- Returns:
- the newly created
Position
- Throws:
BadLocationException
- ifoffset
is not a valid location in the documents content model
public void dump(PrintStream out)
Dumps diagnostic information to the specifiedPrintStream
.
- Parameters:
out
- the stream to write the diagnostic information to
publicextends EventListener> T[] getListeners (ClasslistenerType)
Returns all registered listeners of a given listener type.
- Parameters:
listenerType
- the type of the listeners to be queried
- Returns:
- all registered listeners of the specified type
protected void fireChangedUpdate(DocumentEvent event)
Notifies all registered listeners when the document model changes.
- Parameters:
event
- theDocumentEvent
to be fired
protected void fireInsertUpdate(DocumentEvent event)
Notifies all registered listeners when content is inserted in the document model.
- Parameters:
event
- theDocumentEvent
to be fired
protected void fireRemoveUpdate(DocumentEvent event)
Notifies all registered listeners when content is removed from the document model.
- Parameters:
event
- theDocumentEvent
to be fired
protected void fireUndoableEditUpdate(UndoableEditEvent event)
Notifies all registered listeners when anUndoableEdit
has been performed on thisDocument
.
- Parameters:
event
- theUndoableEditEvent
to be fired
public int getAsynchronousLoadPriority()
Returns the asynchronous loading priority. Returns-1
if this document should not be loaded asynchronously.
- Returns:
- the asynchronous loading priority
protected final AbstractDocument.AttributeContext getAttributeContext()
Returns theAbstractDocument.AttributeContext
used in thisDocument
.
- Returns:
- the
AbstractDocument.AttributeContext
used in thisDocument
public Element getBidiRootElement()
Returns the root element for bidirectional content.
- Returns:
- the root element for bidirectional content
protected final AbstractDocument.Content getContent()
Returns theAbstractDocument.Content
model for thisDocument
- Returns:
- the
AbstractDocument.Content
model for thisDocument
- See Also:
GapContent
,StringContent
protected final Thread getCurrentWriter()
Returns the thread that currently modifies thisDocument
if there is one, otherwisenull
. 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.
- Returns:
- the thread that currently modifies this
Document
if there is one, otherwisenull
public abstract Element getDefaultRootElement()
Returns the default rootElement
of thisDocument
. UsualDocument
s only have one root element and return this. However, there may beDocument
implementations that support multiple root elements, they have to return a default root element here.
- Specified by:
- getDefaultRootElement in interface Document
- Returns:
- the default root
Element
of thisDocument
public DocumentFilter getDocumentFilter()
Returns the currently installedDocumentFilter
for thisDocument
.
- Returns:
- the currently installed
DocumentFilter
for thisDocument
- Since:
- 1.4
public DocumentListener[] getDocumentListeners()
Returns all registeredDocumentListener
s.
- Returns:
- all registered
DocumentListener
s
public final Position getEndPosition()
Returns aPosition
which will always mark the end of theDocument
.
- Specified by:
- getEndPosition in interface Document
- Returns:
- a
Position
which will always mark the end of theDocument
public int getLength()
Returns the length of thisDocument
's content.
- Returns:
- the length of this
Document
's content
public abstract Element getParagraphElement(int pos)
Returns the paragraphElement
that holds the specified position.
- Parameters:
pos
- the position for which to get the paragraph element
- Returns:
- the paragraph
Element
that holds the specified position
public final Object getProperty(Object key)
Returns a property from thisDocument
's property list.
- Specified by:
- getProperty in interface Document
- Parameters:
key
- the key of the property to be fetched
- Returns:
- the property for
key
ornull
if there is no such property stored
public Element[] getRootElements()
Returns all root elements of thisDocument
. By default this just returns the single root element returned bygetDefaultRootElement()
.Document
implementations that support multiple roots must override this method and return all roots here.
- Specified by:
- getRootElements in interface Document
- Returns:
- all root elements of this
Document
public final Position getStartPosition()
Returns aPosition
which will always mark the beginning of theDocument
.
- Specified by:
- getStartPosition in interface Document
- Returns:
- a
Position
which will always mark the beginning of theDocument
public String getText(int offset, int length) throws BadLocationException
Returns a piece of thisDocument
's content.
- Parameters:
offset
- the start offset of the contentlength
- the length of the content
- Returns:
- the piece of content specified by
offset
andlength
- Throws:
BadLocationException
- ifoffset
oroffset + length
are invalid locations with thisDocument
public void getText(int offset, int length, Segment segment) throws BadLocationException
Fetches a piece of thisDocument
's content and stores it in the givenSegment
.
- Parameters:
offset
- the start offset of the contentlength
- the length of the contentsegment
- theSegment
to store the content in
- Throws:
BadLocationException
- ifoffset
oroffset + length
are invalid locations with thisDocument
public UndoableEditListener[] getUndoableEditListeners()
Returns all registeredUndoableEditListener
s.
- Returns:
- all registered
UndoableEditListener
s
public void insertString(int offset, String text, AttributeSet attributes) throws BadLocationException
Inserts a String into thisDocument
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
isnull
or has a length of zero.
- Specified by:
- insertString in interface Document
- Parameters:
offset
- the location at which the string should be insertedtext
- the content to be insertedattributes
- the text attributes to be assigned to that string
- Throws:
BadLocationException
- ifoffset
is not a valid location in thisDocument
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
Called to indicate that text has been inserted into thisDocument
. The default implementation does nothing. This method is executed within a write lock.
- Parameters:
chng
- theDefaultDocumentEvent
describing the changeattr
- the attributes of the changed content
protected void postRemoveUpdate(AbstractDocument.DefaultDocumentEvent chng)
Called after some content has been removed from thisDocument
. The default implementation does nothing. This method is executed within a write lock.
- Parameters:
chng
- theDefaultDocumentEvent
describing the change
public final void putProperty(Object key, Object value)
Stores a property in thisDocument
's property list.
- Specified by:
- putProperty in interface Document
- Parameters:
key
- the key of the property to be storedvalue
- the value of the property to be stored
public final void readLock()
Blocks until a read lock can be obtained. Must block if there is currently a writer modifying theDocument
.
public final void readUnlock()
Releases the read lock. If this was the only reader on thisDocument
, writing may begin now.
public void remove(int offset, int length) throws BadLocationException
Removes a piece of content from thisDocument
.If a
DocumentFilter
is installed in this document, the corresponding method of the filter object is called. TheDocumentFilter
is called even iflength
is zero. This is different fromreplace(int,int,String,AttributeSet)
.Note: When
length
is zero or below the call is not forwarded to the underlyingAbstractDocument.Content
instance of this document and no exception is thrown.
- Parameters:
offset
- the start offset of the fragment to be removedlength
- the length of the fragment to be removed
- Throws:
BadLocationException
- ifoffset
oroffset + length
or invalid locations within this document
public void removeDocumentListener(DocumentListener listener)
Removes aDocumentListener
object from this document.
- Specified by:
- removeDocumentListener in interface Document
- Parameters:
listener
- the listener to remove
public void removeUndoableEditListener(UndoableEditListener listener)
Removes anUndoableEditListener
from thisDocument
.
- Specified by:
- removeUndoableEditListener in interface Document
- Parameters:
listener
- the listener to remove
protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Called before some content gets removed from thisDocument
. The default implementation does nothing but may be overridden by subclasses to modify theDocument
structure in response to a remove request. The method is executed within a write lock.
- Parameters:
chng
- theDefaultDocumentEvent
describing the change
public void render(Runnable runnable)
Called to render thisDocument
visually. It obtains a read lock, ensuring that no changes will be made to thedocument
during the rendering process. It then calls theRunnable.run()
method onrunnable
. This method must not attempt to modifiy theDocument
, since a deadlock will occur if it tries to obtain a write lock. When theRunnable.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.
- Parameters:
runnable
- theRunnable
to execute
public void replace(int offset, int length, String text, AttributeSet attributes) throws BadLocationException
Replaces a piece of content in thisDocument
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
isnull
or has zero length.
- Parameters:
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 content
- Throws:
BadLocationException
- ifoffset
oroffset + length
or invalid locations within this document
- Since:
- 1.4
public void setAsynchronousLoadPriority(int p)
Sets the asynchronous loading priority for thisDocument
. A value of-1
indicates that thisDocument
should be loaded synchronously.
- Parameters:
p
- the asynchronous loading priority to set
public void setDocumentFilter(DocumentFilter filter)
Sets theDocumentFilter
for thisDocument
.
- Parameters:
filter
- theDocumentFilter
to set
- Since:
- 1.4