public class BasicDocument extends java.lang.Object implements javax.swing.text.Document, TextBufferListener, java.beans.PropertyChangeListener
BasicDocument
is an implementation of the
Document
interface, similar to "PlainDocument" without
the extra overhead of the Element structures for maintaining line
number information, and with a different text buffer
implementation. This document does not maintain any attribute
information currently.
Each BasicDocument
instance has an associated
LanguageSupport
that adds extra document support
specific to a given document language, such as Java or HTML. The
LanguageSupport
for a document is set up using the
provided filename through suffix mapping, or by explicitly
setting the LanguageSupport
by calling
setLanguageSupport()
with a LanguageSupport
instance.
Like the Swing text document implementations, this document
implementation provides support for UndoableEdit's. The document
relies on the underlying TextBuffer
for generating
the UndoableEdit's. Note that UndoableEdit's are fired by this
document only for edits initiated through this document. Edits
performed on the TextBuffer directly apart from this document
will not result in an UndoableEdit through this document.
Note that the compound edit support here in BasicDocument
has been deprecated. Clients should use the edit support in
BasicEditorPane
instead, which also includes support
for specifying an edit name (for display) and for saving caret
position information with the UndoableEdit
. Please
refer for information regarding undo support.
Lastly, please note that this document implementation currently does not support composed or bidi text input due to the model changes to optimize memory use and performance. This is an open issue that will be addressed soon.
LanguageSupport
Modifier and Type | Class and Description |
---|---|
static interface |
BasicDocument.PrePostDocumentListener
The
PrePostDocumentListener is a listener interface that
allows secondary models (such as a DocumentRenderer or CodeFoldingModel)
to be notified before and/or after regular document notification. |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
eolTypePropertyName
public constant for the eol type property name.
|
static java.lang.String |
readOnlyPropertyName
Public constant for the read only property name.
|
EDIT_BEGIN_ATTRIBUTE, EDIT_END_ATTRIBUTE, EOL_TYPE_ATTRIBUTE, MODIFIED_ATTRIBUTE, READ_ONLY_ATTRIBUTE, RELOAD_END_ATTRIBUTE, RELOAD_START_ATTRIBUTE
Constructor and Description |
---|
BasicDocument()
Create a new default
BasicDocument . |
BasicDocument(java.lang.String fileName)
Creates a plain text document - this uses the the default
TextBuffer implementation as the underlying data model.
|
BasicDocument(java.lang.String fileName,
TextBuffer buffer)
Creates a plain text document using the buffer provider
specified.
|
BasicDocument(TextBuffer buffer)
Create a new
BasicDocument using the provided buffer. |
Modifier and Type | Method and Description |
---|---|
void |
addDocumentListener(javax.swing.event.DocumentListener listener)
Registers the given listener to begin receiving notifications
when changes are made to the document.
|
void |
addPrePostDocumentListener(BasicDocument.PrePostDocumentListener listener)
Registers the given listener to begin receiving pre/post notifications.
|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Add a PropertyChangeListener to the listener list.
|
void |
addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
Registers the given listener to begin receiving notifications
when undoable edits are made to the document.
|
void |
attributeUpdate(TextBuffer buffer,
int attribute)
Provides notification regarding a change to one of the buffer
attributes.
|
void |
beginEdit()
Deprecated.
all edits (compound or otherwise) made to the
document related to an editor pane should be initiated
through
BasicEditorPane.beginEdit() |
javax.swing.text.Position |
createPosition(int offset)
This method allows an application to mark a place in a sequence
of character content.
|
void |
endEdit()
Deprecated.
all edits (compound or otherwise) made to the
document related to an editor pane should be ended
through
BasicEditorPane.endEdit() |
void |
firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue)
Report a bound property update to any registered listeners.
|
protected void |
fireUndoableEditEvent(javax.swing.undo.UndoableEdit edit)
An internal utility routine to fire UndoableEdit events that
are generated by this document to the undoable edit listeners.
|
protected NumberRange |
getComposedTextRange()
Fetch the current range of composed text input - this is
generally uncommitted text.
|
javax.swing.text.Element |
getDefaultRootElement()
Fetches the default root element for this document.
|
DocumentRenderer |
getDocumentRenderer()
Fetches the document renderer from the language support for
this document.
|
javax.swing.text.Position |
getEndPosition()
Returns a position that represents the end of the document.
|
LanguageSupport |
getLanguageSupport()
Fetches the LanguageSupport associated with this document.
|
int |
getLength()
Returns number of characters of data currently
in the document.
|
int |
getLineCount()
Fetches the number of lines in this
TextBuffer . |
int |
getLineEndOffset(int line)
Given a specified line, returns the offset from the beginning
of a
TextBuffer that the line ends at. |
int |
getLineFromOffset(int offset)
Given an offset from the beginning of the underlying
TextBuffer , fetches the line number containing that
offset using a Forward bias. |
LineMap |
getLineMap()
Fetches a line map for this document for calculating line
positions.
|
int |
getLineStartOffset(int line)
Given a specified line, returns the offset from the beginning
of the
TextBuffer for the start of the line. |
javax.swing.text.Element |
getParagraphElement(int pos)
Get the paragraph element containing the given position.
|
java.lang.Object |
getProperty(java.lang.Object key)
Gets properties associated with the document.
|
javax.swing.text.Element[] |
getRootElements()
Returns all of the root elements that are defined.
|
javax.swing.text.Position |
getStartPosition()
Returns a position that represents the start of the document.
|
java.lang.String |
getText(int offset,
int length)
Fetches the text contained within the given portion
of the document.
|
void |
getText(int offset,
int length,
javax.swing.text.Segment txt)
Fetches the text contained within the given portion
of the document.
|
TextBuffer |
getTextBuffer()
Fetches the underlying Content of this document.
|
void |
insertString(int offset,
java.lang.String str,
javax.swing.text.AttributeSet set)
Inserts a string into the document.
|
void |
insertUpdate(TextBuffer buffer,
int offset,
int count,
char[] insertedData)
Provides notification about an insertion into the text buffer.
|
boolean |
isCompoundEditInProgress()
Gets whether a compound edit is in progress.
|
void |
propertyChange(java.beans.PropertyChangeEvent event)
This method gets called when a bound property is changed.
|
void |
putProperty(java.lang.Object key,
java.lang.Object value)
Puts a new property on the list.
|
void |
readLock()
Attempts to acquire a read lock on this document and the
underlying text buffer - this is a blocking call.
|
void |
readUnlock()
Releases a held read lock on this Document.
|
void |
remove(int offset,
int length)
Removes a portion of the content of the document.
|
void |
removeDocumentListener(javax.swing.event.DocumentListener listener)
Unregisters the given listener from the notification list
so it will no longer receive change updates.
|
void |
removePrePostDocumentListener(BasicDocument.PrePostDocumentListener listener)
Unregisters the given listener from receiving pre/post notifications.
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Remove a PropertyChangeListener from the listener list.
|
void |
removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
Unregisters the given listener from the notification list
so it will no longer receive updates.
|
void |
removeUpdate(TextBuffer buffer,
int offset,
int count,
char[] removedData)
Provides notification about a removal from the text buffer.
|
void |
render(java.lang.Runnable r)
This allows the model to be safely rendered in the presence
of currency, if the model supports being updated asynchronously.
|
void |
setLanguageSupport(LanguageSupport support)
Set the LanguageSupport used by this document to the specified
one.
|
void |
setLanguageSupport(java.lang.String fileName)
Sets the LanguageSupport for editing the type of content indicated
by the specified filename.
|
void |
writeLock()
Attempts to acquire a write lock on this document and the
underlying text buffer - this is a blocking call.
|
void |
writeLock(boolean checkIfReadOnly)
Attempts to acquire a write lock on this document and the
underlying text buffer - this is a blocking call.
|
void |
writeUnlock()
Releases a held write lock on this Document.
|
public static final java.lang.String readOnlyPropertyName
public static final java.lang.String eolTypePropertyName
public BasicDocument()
BasicDocument
.public BasicDocument(java.lang.String fileName)
fileName
- the file name to use when creating a language supportpublic BasicDocument(java.lang.String fileName, TextBuffer buffer)
fileName
- the file name to use when creating a language supportbuffer
- the text bufer to use as the document modelpublic BasicDocument(TextBuffer buffer)
BasicDocument
using the provided buffer.buffer
- the text buffer to use as the document modelprotected NumberRange getComposedTextRange()
public TextBuffer getTextBuffer()
public void setLanguageSupport(java.lang.String fileName)
fileName
- the name of the filepublic void setLanguageSupport(LanguageSupport support)
support
- the LanguageSupport to usepublic LanguageSupport getLanguageSupport()
public DocumentRenderer getDocumentRenderer()
public void beginEdit()
BasicEditorPane.beginEdit()
BasicEditorPane
instance, use
BasicEditorPane.beginEdit()
instead so that caret
information can also be preserved.TextBuffer.beginEdit()
,
TextBuffer.endEdit()
public void endEdit()
BasicEditorPane.endEdit()
TextBuffer.beginEdit()
,
TextBuffer.endEdit()
public boolean isCompoundEditInProgress()
public void readLock()
public void readUnlock()
public void writeLock()
ReadOnlyException
- if the buffer is in read only mode.ReadTextBuffer.readLock()
,
TextBuffer.writeLock()
public void writeLock(boolean checkIfReadOnly)
ReadOnlyException
- if the buffer is in read only mode and
checkIfReadOnly is trueReadTextBuffer.readLock()
,
TextBuffer.writeLock()
public void writeUnlock()
public int getLineCount()
TextBuffer
. For
example, a 5-line TextBuffer
has lines 0 through 4.TextBuffer
public int getLineFromOffset(int offset)
TextBuffer
, fetches the line number containing that
offset using a Forward bias. This means if line A starts with
offset B, and previous line A-1 ends with offset B,
getLineFromOffset(B)
will return line A.public int getLineStartOffset(int line)
TextBuffer
for the start of the line.line
- a line in the TextBuffer
public int getLineEndOffset(int line)
TextBuffer
that the line ends at.line
- a line in the TextBuffer
public javax.swing.text.Element getParagraphElement(int pos)
public javax.swing.text.Element getDefaultRootElement()
getDefaultRootElement
in interface javax.swing.text.Document
public LineMap getLineMap()
public int getLength()
getLength
in interface javax.swing.text.Document
public void remove(int offset, int length) throws javax.swing.text.BadLocationException
remove
in interface javax.swing.text.Document
offset
- the offset from the begining >= 0length
- the number of characters to remove >= 0javax.swing.text.BadLocationException
- some portion of the removal range
was not a valid part of the document. The location in the exception
is the first bad position encountered.public void insertString(int offset, java.lang.String str, javax.swing.text.AttributeSet set) throws javax.swing.text.BadLocationException
insertString
in interface javax.swing.text.Document
offset
- the offset into the document to insert the content >= 0.
All positions that track change at or after the given location
will move.str
- the string to insertset
- the attributes to associate with the inserted
content. This may be null if there are no attributes.javax.swing.text.BadLocationException
- the given insert position is not a valid
position within the documentpublic java.lang.String getText(int offset, int length) throws javax.swing.text.BadLocationException
getText
in interface javax.swing.text.Document
offset
- the offset into the document representing the desired
start of the text >= 0length
- the length of the desired string >= 0javax.swing.text.BadLocationException
- some portion of the given range
was not a valid part of the document. The location in the exception
is the first bad position encountered.public void getText(int offset, int length, javax.swing.text.Segment txt) throws javax.swing.text.BadLocationException
getText
in interface javax.swing.text.Document
offset
- the offset into the document representing the desired
start of the text >= 0length
- the length of the desired string >= 0txt
- the Segment object to return the text injavax.swing.text.BadLocationException
- Some portion of the given range
was not a valid part of the document. The location in the exception
is the first bad position encountered.public javax.swing.text.Position getStartPosition()
getStartPosition
in interface javax.swing.text.Document
public javax.swing.text.Position getEndPosition()
getEndPosition
in interface javax.swing.text.Document
public javax.swing.text.Position createPosition(int offset) throws javax.swing.text.BadLocationException
createPosition
in interface javax.swing.text.Document
offset
- the offset from the start of the document >= 0javax.swing.text.BadLocationException
- if the given position does not
represent a valid location in the associated documentpublic javax.swing.text.Element[] getRootElements()
getRootElements
in interface javax.swing.text.Document
public void render(java.lang.Runnable r)
render
in interface javax.swing.text.Document
r
- a Runnable used to render the modelpublic void addDocumentListener(javax.swing.event.DocumentListener listener)
addDocumentListener
in interface javax.swing.text.Document
listener
- the listener to registerDocument.removeDocumentListener(javax.swing.event.DocumentListener)
public void removeDocumentListener(javax.swing.event.DocumentListener listener)
removeDocumentListener
in interface javax.swing.text.Document
listener
- the listener to registerDocument.addDocumentListener(javax.swing.event.DocumentListener)
public void addUndoableEditListener(javax.swing.event.UndoableEditListener listener)
addUndoableEditListener
in interface javax.swing.text.Document
listener
- the listener to registerUndoableEditEvent
public void removeUndoableEditListener(javax.swing.event.UndoableEditListener listener)
removeUndoableEditListener
in interface javax.swing.text.Document
listener
- the listener to registerUndoableEditEvent
public void propertyChange(java.beans.PropertyChangeEvent event)
propertyChange
in interface java.beans.PropertyChangeListener
event
- A PropertyChangeEvent object describing the event source
and the property that has changed.public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Note that this listener is stored using a WeakReference. This is intentional as part of the editor framework design, to avoid pinning certain UI objects in memory permanently.
listener
- The PropertyChangeListener to be addedpublic void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- The PropertyChangeListener to be removedpublic void firePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
propertyName
- The programmatic name of the property
that was changed.oldValue
- The old value of the property.newValue
- The new value of the property.public java.lang.Object getProperty(java.lang.Object key)
getProperty
in interface javax.swing.text.Document
key
- a non-null propertypublic void putProperty(java.lang.Object key, java.lang.Object value)
putProperty
in interface javax.swing.text.Document
key
- the non-null property keyvalue
- the property publicprotected void fireUndoableEditEvent(javax.swing.undo.UndoableEdit edit)
edit
- the UndoableEdit generatedpublic void insertUpdate(TextBuffer buffer, int offset, int count, char[] insertedData)
insertUpdate
in interface TextBufferListener
buffer
- offset
- the offset where the insert took placecount
- the number of characters inserted (insertedData.length)insertedData
- the contents of the insertionpublic void removeUpdate(TextBuffer buffer, int offset, int count, char[] removedData)
removeUpdate
in interface TextBufferListener
buffer
- offset
- the offset where the removal took placecount
- the number of characters removed (removedData.length)removedData
- the contents of the removalpublic void attributeUpdate(TextBuffer buffer, int attribute)
attributeUpdate
in interface TextBufferListener
buffer
- attribute
- the attribute that changedpublic void addPrePostDocumentListener(BasicDocument.PrePostDocumentListener listener)
listener
- the listener to registerpublic void removePrePostDocumentListener(BasicDocument.PrePostDocumentListener listener)
listener
- the listener to register