javax.swing.text
Class DefaultStyledDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.DefaultStyledDocument
All Implemented Interfaces:
Serializable, Document, StyledDocument
Direct Known Subclasses:
HTMLDocument

public class DefaultStyledDocument
extends AbstractDocument
implements StyledDocument

The default implementation of StyledDocument. The document is modeled as an Element tree, which has a DefaultStyledDocument.SectionElement as single root, which has one or more AbstractDocument.BranchElements as paragraph nodes and each paragraph node having one or more AbstractDocument.LeafElements as content nodes.

See Also:
Serialized Form

Nested Class Summary
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 Elements that should be created in DefaultStyledDocument.ElementBuffer.
protected  class DefaultStyledDocument.SectionElement
          An element type for sections.
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
 
Field Summary
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.
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
DefaultStyledDocument()
          Creates a new DefaultStyledDocument.
DefaultStyledDocument(AbstractDocument.Content content, StyleContext context)
          Creates a new DefaultStyledDocument that uses the specified StyleContext and Content buffer.
DefaultStyledDocument(StyleContext context)
          Creates a new DefaultStyledDocument that uses the specified StyleContext.
 
Method Summary
 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.
 
Methods inherited from class javax.swing.text.AbstractDocument
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.text.Document
addDocumentListener, addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
 

Field Detail

BUFFER_SIZE_DEFAULT

public static final int BUFFER_SIZE_DEFAULT
The default size to use for new content buffers.

See Also:
Constant Field Values

buffer

protected DefaultStyledDocument.ElementBuffer buffer
The EditorBuffer that is used to manage to Element hierarchy.

Constructor Detail

DefaultStyledDocument

public DefaultStyledDocument()
Creates a new DefaultStyledDocument.


DefaultStyledDocument

public DefaultStyledDocument(StyleContext context)
Creates a new DefaultStyledDocument that uses the specified StyleContext.

Parameters:
context - the StyleContext to use

DefaultStyledDocument

public DefaultStyledDocument(AbstractDocument.Content content,
                             StyleContext context)
Creates a new DefaultStyledDocument that uses the specified StyleContext and Content buffer.

Parameters:
content - the Content buffer to use
context - the StyleContext to use
Method Detail

addStyle

public Style addStyle(String nm,
                      Style parent)
Adds a style into the style hierarchy. Unspecified style attributes can be resolved in the parent style, if one is specified. While it is legal to add nameless styles (nm == null
Specified by:
addStyle in interface StyledDocument
Parameters:
nm - the name of the style or null if the style should be unnamed
parent - the parent in which unspecified style attributes are resolved, or null if that is not necessary
Returns:
the newly created Style

createDefaultRoot

protected AbstractDocument.AbstractElement createDefaultRoot()
Create the default root element for this kind of Document.

Returns:
the default root element for this kind of Document

getCharacterElement

public Element getCharacterElement(int position)
Returns the Element that corresponds to the character at the specified position.

Specified by:
getCharacterElement in interface StyledDocument
Parameters:
position - the position of which we query the corresponding Element
Returns:
the Element that corresponds to the character at the specified position

getBackground

public Color getBackground(AttributeSet attributes)
Extracts a background color from a set of attributes.

Specified by:
getBackground in interface StyledDocument
Parameters:
attributes - the attributes from which to get a background color
Returns:
the background color that correspond to the attributes

getDefaultRootElement

public Element getDefaultRootElement()
Returns the default root element.

Specified by:
getDefaultRootElement in interface Document
Specified by:
getDefaultRootElement in class AbstractDocument
Returns:
the default root element

getFont

public Font getFont(AttributeSet attributes)
Extracts a font from a set of attributes.

Specified by:
getFont in interface StyledDocument
Parameters:
attributes - the attributes from which to get a font
Returns:
the font that correspond to the attributes

getForeground

public Color getForeground(AttributeSet attributes)
Extracts a foreground color from a set of attributes.

Specified by:
getForeground in interface StyledDocument
Parameters:
attributes - the attributes from which to get a foreground color
Returns:
the foreground color that correspond to the attributes

getLogicalStyle

public Style getLogicalStyle(int position)
Returns the logical Style for the specified position.

Specified by:
getLogicalStyle in interface StyledDocument
Parameters:
position - the position from which to query to logical style
Returns:
the logical Style for the specified position

getParagraphElement

public Element getParagraphElement(int position)
Returns the paragraph element for the specified position. If the position is outside the bounds of the document's root element, then the closest element is returned. That is the last paragraph if position >= endIndex or the first paragraph if position < startIndex.

Specified by:
getParagraphElement in interface StyledDocument
Specified by:
getParagraphElement in class AbstractDocument
Parameters:
position - the position for which to query the paragraph element
Returns:
the paragraph element for the specified position

getStyle

public Style getStyle(String nm)
Looks up and returns a named Style.

Specified by:
getStyle in interface StyledDocument
Parameters:
nm - the name of the Style
Returns:
the found Style of null if no such Style exists

removeStyle

public void removeStyle(String nm)
Removes a named Style from the style hierarchy.

Specified by:
removeStyle in interface StyledDocument
Parameters:
nm - the name of the Style to be removed

setCharacterAttributes

public void setCharacterAttributes(int offset,
                                   int length,
                                   AttributeSet attributes,
                                   boolean replace)
Sets text attributes for the fragment specified by offset and length.

Specified by:
setCharacterAttributes in interface StyledDocument
Parameters:
offset - the start offset of the fragment
length - the length of the fragment
attributes - the text attributes to set
replace - if true, the attributes of the current selection are overridden, otherwise they are merged

setLogicalStyle

public void setLogicalStyle(int position,
                            Style style)
Sets the logical style for the paragraph at the specified position.

Specified by:
setLogicalStyle in interface StyledDocument
Parameters:
position - the position at which the logical style is added
style - the style to set for the current paragraph

setParagraphAttributes

public void setParagraphAttributes(int offset,
                                   int length,
                                   AttributeSet attributes,
                                   boolean replace)
Sets text attributes for the paragraph at the specified fragment.

Specified by:
setParagraphAttributes in interface StyledDocument
Parameters:
offset - the beginning of the fragment
length - the length of the fragment
attributes - the text attributes to set
replace - if true, the attributes of the current selection are overridden, otherwise they are merged

insertUpdate

protected void insertUpdate(AbstractDocument.DefaultDocumentEvent ev,
                            AttributeSet attr)
Called in response to content insert actions. This is used to update the element structure.

Overrides:
insertUpdate in class AbstractDocument
Parameters:
ev - the DocumentEvent describing the change
attr - the attributes for the change

removeUpdate

protected void removeUpdate(AbstractDocument.DefaultDocumentEvent ev)
Updates the document structure in response to text removal. This is forwarded to the DefaultStyledDocument.ElementBuffer of this document. Any changes to the document structure are added to the specified document event and sent to registered listeners.

Overrides:
removeUpdate in class AbstractDocument
Parameters:
ev - the document event that records the changes to the document

getStyleNames

public Enumeration<?> getStyleNames()
Returns an enumeration of all style names.

Returns:
an enumeration of all style names

styleChanged

protected void styleChanged(Style style)
Called when any of this document's styles changes.

Parameters:
style - the style that changed

insert

protected void insert(int offset,
                      DefaultStyledDocument.ElementSpec[] data)
               throws BadLocationException
Inserts a bulk of structured content at once.

Parameters:
offset - the offset at which the content should be inserted
data - the actual content spec to be inserted
Throws:
BadLocationException

create

protected void create(DefaultStyledDocument.ElementSpec[] data)
Initializes the DefaultStyledDocument with the specified data.

Parameters:
data - the specification of the content with which the document is initialized