AbstractDocument (Java SE 15 & JDK 15) (original) (raw)
Method Details
getDocumentProperties
Supports managing a set of properties. Callers can use the documentProperties
dictionary to annotate the document with document-wide properties.
Returns:
a non-null
Dictionary
See Also:
setDocumentProperties(java.util.Dictionary<java.lang.Object, java.lang.Object>)
setDocumentProperties
Replaces the document properties dictionary for this document.
Parameters:x
- the new dictionary
See Also:
getDocumentProperties()
fireInsertUpdate
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.
Parameters:e
- the event
See Also:
EventListenerList
fireChangedUpdate
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.
Parameters:e
- the event
See Also:
EventListenerList
fireRemoveUpdate
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.
Parameters:e
- the event
See Also:
EventListenerList
fireUndoableEditUpdate
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.
Parameters:e
- the event
See Also:
EventListenerList
getListeners
public <T extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as _Foo_Listener
s upon this document._Foo_Listener
s are registered using theadd_Foo_Listener
method.
You can specify the listenerType
argument with a class literal, such as_Foo_Listener.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.
Type Parameters:T
- the listener type
Parameters:listenerType
- the type of listeners requested
Returns:
an array of all objects registered as_Foo_Listener
s on this component, or an empty array if no such listeners have been added
Throws:[ClassCastException](../../../../java.base/java/lang/ClassCastException.html "class in java.lang")
- if listenerType
doesn't specify a class or interface that implementsjava.util.EventListener
Since:
1.3
See Also:
getDocumentListeners(), getUndoableEditListeners()
getAsynchronousLoadPriority
public int getAsynchronousLoadPriority()
Gets the asynchronous loading priority. If less than zero, the document should not be loaded asynchronously.
Returns:
the asynchronous loading priority, or -1
if the document should not be loaded asynchronously
setAsynchronousLoadPriority
public void setAsynchronousLoadPriority(int p)
Sets the asynchronous loading priority.
Parameters:p
- the new asynchronous loading priority; a value less than zero indicates that the document should not be loaded asynchronously
setDocumentFilter
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.
Parameters:filter
- the DocumentFilter
used to constrain text
Since:
1.4
See Also:
getDocumentFilter()
getDocumentFilter
Returns the DocumentFilter
that is responsible for filtering of insertion/removal. A null
return value implies no filtering is to occur.
Returns:
the DocumentFilter
Since:
1.4
See Also:
setDocumentFilter(javax.swing.text.DocumentFilter)
render
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 acquire 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 seeConcurrency in Swing for more information.
Specified by:[render](Document.html#render%28java.lang.Runnable%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:r
- the renderer to execute
getLength
public int getLength()
Returns the length of the data. This is the number of characters of content that represents the users data.
Specified by:[getLength](Document.html#getLength%28%29)
in interface [Document](Document.html "interface in javax.swing.text")
Returns:
the length >= 0
See Also:
Document.getLength()
addDocumentListener
Adds a document listener for notification of any changes.
Specified by:[addDocumentListener](Document.html#addDocumentListener%28javax.swing.event.DocumentListener%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:listener
- the DocumentListener
to add
See Also:
Document.addDocumentListener(javax.swing.event.DocumentListener)
removeDocumentListener
Removes a document listener.
Specified by:[removeDocumentListener](Document.html#removeDocumentListener%28javax.swing.event.DocumentListener%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:listener
- the DocumentListener
to remove
See Also:
Document.removeDocumentListener(javax.swing.event.DocumentListener)
getDocumentListeners
Returns an array of all the document listeners registered on this document.
Returns:
all of this document's DocumentListener
s or an empty array if no document listeners are currently registered
Since:
1.4
See Also:
addDocumentListener(javax.swing.event.DocumentListener), removeDocumentListener(javax.swing.event.DocumentListener)
addUndoableEditListener
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.
Specified by:[addUndoableEditListener](Document.html#addUndoableEditListener%28javax.swing.event.UndoableEditListener%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:listener
- the UndoableEditListener
to add
See Also:
Document.addUndoableEditListener(javax.swing.event.UndoableEditListener)
removeUndoableEditListener
Removes an undo listener.
Specified by:[removeUndoableEditListener](Document.html#removeUndoableEditListener%28javax.swing.event.UndoableEditListener%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:listener
- the UndoableEditListener
to remove
See Also:
Document.removeDocumentListener(javax.swing.event.DocumentListener)
getUndoableEditListeners
Returns an array of all the undoable edit listeners registered on this document.
Returns:
all of this document's UndoableEditListener
s or an empty array if no undoable edit listeners are currently registered
Since:
1.4
See Also:
addUndoableEditListener(javax.swing.event.UndoableEditListener), removeUndoableEditListener(javax.swing.event.UndoableEditListener)
getProperty
A convenience method for looking up a property value. It is equivalent to:
getDocumentProperties().get(key);
Specified by:[getProperty](Document.html#getProperty%28java.lang.Object%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:key
- the non-null
property key
Returns:
the value of this property or null
See Also:
getDocumentProperties()
putProperty
public final void putProperty(Object key,Object value)
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.
Specified by:[putProperty](Document.html#putProperty%28java.lang.Object,java.lang.Object%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:key
- the non-null
keyvalue
- the property value
See Also:
getDocumentProperties()
remove
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 seeConcurrency in Swing for more information.
Specified by:[remove](Document.html#remove%28int,int%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:offs
- the starting offset >= 0len
- the number of characters to remove >= 0
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- the given remove position is not a valid position within the document
See Also:
Document.remove(int, int)
replace
Deletes the region of text from offset
tooffset + 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.
Parameters:offset
- index of child elementlength
- length of text to delete, may be 0 indicating don't delete anythingtext
- text to insert, null
indicates no text to insertattrs
- AttributeSet indicating attributes of inserted text,null
is legal, and typically treated as an empty attributeset, but exact interpretation is left to the subclass
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- the given position is not a valid position within the document
Since:
1.4
insertString
Inserts 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 seeConcurrency in Swing for more information.
Specified by:[insertString](Document.html#insertString%28int,java.lang.String,javax.swing.text.AttributeSet%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:offs
- the starting offset >= 0str
- the string to insert; does nothing with null/empty stringsa
- the attributes for the inserted content
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- the given insert position is not a valid position within the document
See Also:
Document.insertString(int, java.lang.String, javax.swing.text.AttributeSet)
getText
Gets a sequence of text from the document.
Specified by:[getText](Document.html#getText%28int,int%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:offset
- the starting offset >= 0length
- the number of characters to retrieve >= 0
Returns:
the text
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- the range given includes a position that is not a valid position within the document
See Also:
Document.getText(int, int)
getText
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;
}
Specified by:[getText](Document.html#getText%28int,int,javax.swing.text.Segment%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:offset
- the starting offset >= 0length
- the number of characters to retrieve >= 0txt
- the Segment object to retrieve the text into
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- the range given includes a position that is not a valid position within the document
createPosition
Returns a position that will track change as the document is altered.
This method is thread safe, although most Swing methods are not. Please seeConcurrency in Swing for more information.
Specified by:[createPosition](Document.html#createPosition%28int%29)
in interface [Document](Document.html "interface in javax.swing.text")
Parameters:offs
- the position in the model >= 0
Returns:
the position
Throws:[BadLocationException](BadLocationException.html "class in javax.swing.text")
- if the given position does not represent a valid location in the associated document
See Also:
Document.createPosition(int)
getStartPosition
public final Position getStartPosition()
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.
Specified by:[getStartPosition](Document.html#getStartPosition%28%29)
in interface [Document](Document.html "interface in javax.swing.text")
Returns:
the position
getEndPosition
public final Position getEndPosition()
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.
Specified by:[getEndPosition](Document.html#getEndPosition%28%29)
in interface [Document](Document.html "interface in javax.swing.text")
Returns:
the position
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.
Specified by:[getRootElements](Document.html#getRootElements%28%29)
in interface [Document](Document.html "interface in javax.swing.text")
Returns:
the root element
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.
Specified by:[getDefaultRootElement](Document.html#getDefaultRootElement%28%29)
in interface [Document](Document.html "interface in javax.swing.text")
Returns:
the root element
See Also:
Document.getDefaultRootElement()
getBidiRootElement
public Element getBidiRootElement()
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 of the bidirectional structure for this document
getParagraphElement
public abstract Element getParagraphElement(int pos)
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.
Parameters:pos
- the starting offset >= 0
Returns:
the element
getAttributeContext
Fetches the context for managing attributes. This method effectively establishes the strategy used for compressing AttributeSet information.
Returns:
the context
insertUpdate
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 changeattr
- the attributes for the change
removeUpdate
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.
Parameters:chng
- a description of the change
postRemoveUpdate
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.
Parameters:chng
- a description of the change
dump
Gives a diagnostic dump.
Parameters:out
- the output stream
getContent
Gets the content for the document.
Returns:
the content
createLeafElement
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.
Parameters:parent
- the parent elementa
- the attributes for the elementp0
- the beginning of the range >= 0p1
- the end of the range >= p0
Returns:
the new element
createBranchElement
Creates a document branch element, that can contain other elements.
Parameters:parent
- the parent elementa
- the attributes
Returns:
the element
getCurrentWriter
protected final 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:
the thread actively modifying the document or null
if there are no modifications in progress
writeLock
protected final void writeLock()
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 awriteLock
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 theDocument
will be left in a locked state so that no reading or writing can be done.
Throws:[IllegalStateException](../../../../java.base/java/lang/IllegalStateException.html "class in java.lang")
- 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.
writeUnlock
protected final void writeUnlock()
Releases a write lock previously obtained via writeLock
. After decrementing the lock count if there are no outstanding locks this will allow a new writer, or readers.
See Also:
writeLock()
readLock
public final void readLock()
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 areadUnlock
.
See Also:
readUnlock()
readUnlock
public final void 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();
}
See Also:
readLock()