javax.swing.text
public class DefaultStyledDocument extends AbstractDocument implements StyledDocument
StyledDocument
. The document is
modeled as an Element
tree, which has a DefaultStyledDocument.SectionElement
as
single root, which has one or more AbstractDocument.BranchElement
s
as paragraph nodes and each paragraph node having one or more
AbstractDocument.LeafElement
s as content nodes.Modifier and Type | Class and Description |
---|---|
static class |
DefaultStyledDocument.AttributeUndoableEdit
An
UndoableEdit that can undo attribute changes to an element. |
class |
DefaultStyledDocument.ElementBuffer
Performs all structural changes to the
Element
hierarchy. |
static class |
DefaultStyledDocument.ElementSpec
Carries specification information for new
Element s that should be
created in DefaultStyledDocument.ElementBuffer . |
protected class |
DefaultStyledDocument.SectionElement
An element type for sections.
|
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
Modifier and Type | Field and Description |
---|---|
protected DefaultStyledDocument.ElementBuffer |
buffer
The
EditorBuffer that is used to manage to
Element hierarchy. |
static int |
BUFFER_SIZE_DEFAULT
The default size to use for new content buffers.
|
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
StreamDescriptionProperty, TitleProperty
Constructor and Description |
---|
DefaultStyledDocument()
Creates a new
DefaultStyledDocument . |
DefaultStyledDocument(AbstractDocument.Content content,
StyleContext context)
|
DefaultStyledDocument(StyleContext context)
Creates a new
DefaultStyledDocument that uses the specified
StyleContext . |
Modifier and Type | Method and Description |
---|---|
Style |
addStyle(String nm,
Style parent)
Adds a style into the style hierarchy.
|
protected void |
create(DefaultStyledDocument.ElementSpec[] data)
Initializes the
DefaultStyledDocument with the specified
data. |
protected AbstractDocument.AbstractElement |
createDefaultRoot()
Create the default root element for this kind of
Document . |
Color |
getBackground(AttributeSet attributes)
Extracts a background color from a set of attributes.
|
Element |
getCharacterElement(int position)
Returns the
Element that corresponds to the character at the
specified position. |
Element |
getDefaultRootElement()
Returns the default root element.
|
Font |
getFont(AttributeSet attributes)
Extracts a font from a set of attributes.
|
Color |
getForeground(AttributeSet attributes)
Extracts a foreground color from a set of attributes.
|
Style |
getLogicalStyle(int position)
Returns the logical
Style for the specified position. |
Element |
getParagraphElement(int position)
Returns the paragraph element for the specified position.
|
Style |
getStyle(String nm)
Looks up and returns a named
Style . |
Enumeration<?> |
getStyleNames()
Returns an enumeration of all style names.
|
protected void |
insert(int offset,
DefaultStyledDocument.ElementSpec[] data)
Inserts a bulk of structured content at once.
|
protected void |
insertUpdate(AbstractDocument.DefaultDocumentEvent ev,
AttributeSet attr)
Called in response to content insert actions.
|
void |
removeStyle(String nm)
Removes a named
Style from the style hierarchy. |
protected void |
removeUpdate(AbstractDocument.DefaultDocumentEvent ev)
Updates the document structure in response to text removal.
|
void |
setCharacterAttributes(int offset,
int length,
AttributeSet attributes,
boolean replace)
Sets text attributes for the fragment specified by
offset
and length . |
void |
setLogicalStyle(int position,
Style style)
Sets the logical style for the paragraph at the specified position.
|
void |
setParagraphAttributes(int offset,
int length,
AttributeSet attributes,
boolean replace)
Sets text attributes for the paragraph at the specified fragment.
|
protected void |
styleChanged(Style style)
Called when any of this document's styles changes.
|
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addDocumentListener, addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
public static final int BUFFER_SIZE_DEFAULT
protected DefaultStyledDocument.ElementBuffer buffer
EditorBuffer
that is used to manage to
Element
hierarchy.public DefaultStyledDocument()
DefaultStyledDocument
.public DefaultStyledDocument(StyleContext context)
DefaultStyledDocument
that uses the specified
StyleContext
.context
- the StyleContext
to usepublic DefaultStyledDocument(AbstractDocument.Content content, StyleContext context)
content
- the Content
buffer to usecontext
- the StyleContext
to usepublic Style addStyle(String nm, Style parent)
parent
style, if one is specified. While it
is legal to add nameless styles (nm == null
addStyle
in interface StyledDocument
nm
- the name of the style or null
if the style should
be unnamedparent
- the parent in which unspecified style attributes are
resolved, or null
if that is not necessaryStyle
protected AbstractDocument.AbstractElement createDefaultRoot()
Document
.Document
public Element getCharacterElement(int position)
Element
that corresponds to the character at the
specified position.getCharacterElement
in interface StyledDocument
position
- the position of which we query the corresponding
Element
Element
that corresponds to the character at the
specified positionpublic Color getBackground(AttributeSet attributes)
getBackground
in interface StyledDocument
attributes
- the attributes from which to get a background colorpublic Element getDefaultRootElement()
getDefaultRootElement
in interface Document
getDefaultRootElement
in class AbstractDocument
public Font getFont(AttributeSet attributes)
getFont
in interface StyledDocument
attributes
- the attributes from which to get a fontpublic Color getForeground(AttributeSet attributes)
getForeground
in interface StyledDocument
attributes
- the attributes from which to get a foreground colorpublic Style getLogicalStyle(int position)
Style
for the specified position.getLogicalStyle
in interface StyledDocument
position
- the position from which to query to logical styleStyle
for the specified positionpublic Element getParagraphElement(int position)
position >= endIndex
or the first paragraph if
position < startIndex
.getParagraphElement
in interface StyledDocument
getParagraphElement
in class AbstractDocument
position
- the position for which to query the paragraph elementpublic Style getStyle(String nm)
Style
.getStyle
in interface StyledDocument
nm
- the name of the Style
Style
of null
if no such
Style
existspublic void removeStyle(String nm)
Style
from the style hierarchy.removeStyle
in interface StyledDocument
nm
- the name of the Style
to be removedpublic void setCharacterAttributes(int offset, int length, AttributeSet attributes, boolean replace)
offset
and length
.setCharacterAttributes
in interface StyledDocument
offset
- the start offset of the fragmentlength
- the length of the fragmentattributes
- the text attributes to setreplace
- if true
, the attributes of the current selection
are overridden, otherwise they are mergedpublic void setLogicalStyle(int position, Style style)
setLogicalStyle
in interface StyledDocument
position
- the position at which the logical style is addedstyle
- the style to set for the current paragraphpublic void setParagraphAttributes(int offset, int length, AttributeSet attributes, boolean replace)
setParagraphAttributes
in interface StyledDocument
offset
- the beginning of the fragmentlength
- the length of the fragmentattributes
- the text attributes to setreplace
- if true
, the attributes of the current selection
are overridden, otherwise they are mergedprotected void insertUpdate(AbstractDocument.DefaultDocumentEvent ev, AttributeSet attr)
insertUpdate
in class AbstractDocument
ev
- the DocumentEvent
describing the changeattr
- the attributes for the changeprotected void removeUpdate(AbstractDocument.DefaultDocumentEvent ev)
DefaultStyledDocument.ElementBuffer
of this document. Any changes to the
document structure are added to the specified document event and sent to
registered listeners.removeUpdate
in class AbstractDocument
ev
- the document event that records the changes to the documentpublic Enumeration<?> getStyleNames()
protected void styleChanged(Style style)
style
- the style that changedprotected void insert(int offset, DefaultStyledDocument.ElementSpec[] data) throws BadLocationException
offset
- the offset at which the content should be inserteddata
- the actual content spec to be insertedBadLocationException
protected void create(DefaultStyledDocument.ElementSpec[] data)
DefaultStyledDocument
with the specified
data.data
- the specification of the content with which the document is
initialized