public class

DefaultStyledDocument

extends AbstractDocument
implements StyledDocument
java.lang.Object
   ↳ javax.swing.text.AbstractDocument
     ↳ javax.swing.text.DefaultStyledDocument
Known Direct Subclasses

Class Overview

A document that can be marked up with character and paragraph styles in a manner similar to the Rich Text Format. The element structure for this document represents style crossings for style runs. These style runs are mapped into a paragraph element structure (which may reside in some other structure). The style runs break at paragraph boundaries since logical styles are assigned to paragraph boundaries.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

Summary

Nested Classes
class DefaultStyledDocument.AttributeUndoableEdit An UndoableEdit used to remember AttributeSet changes to an Element. 
class DefaultStyledDocument.ElementBuffer Class to manage changes to the element hierarchy. 
class DefaultStyledDocument.ElementSpec Specification for building elements. 
class DefaultStyledDocument.SectionElement Default root element for a document... 
Constants
int BUFFER_SIZE_DEFAULT The default size of the initial content buffer.
[Expand]
Inherited Constants
From class javax.swing.text.AbstractDocument
From interface javax.swing.text.Document
Fields
protected DefaultStyledDocument.ElementBuffer buffer
[Expand]
Inherited Fields
From class javax.swing.text.AbstractDocument
Public Constructors
DefaultStyledDocument(AbstractDocument.Content c, StyleContext styles)
Constructs a styled document.
DefaultStyledDocument(StyleContext styles)
Constructs a styled document with the default content storage implementation and a shared set of styles.
DefaultStyledDocument()
Constructs a default styled document.
Public Methods
void addDocumentListener(DocumentListener listener)
Adds a document listener for notification of any changes.
Style addStyle(String nm, Style parent)
Adds a new style into the logical style hierarchy.
Color getBackground(AttributeSet attr)
Gets the background color from an attribute set.
Element getCharacterElement(int pos)
Gets a character element based on a position.
Element getDefaultRootElement()
Gets the default root element.
Font getFont(AttributeSet attr)
Gets the font from an attribute set.
Color getForeground(AttributeSet attr)
Gets the foreground color from an attribute set.
Style getLogicalStyle(int p)
Fetches the logical style assigned to the paragraph represented by the given position.
Element getParagraphElement(int pos)
Gets the paragraph element at the offset pos.
Style getStyle(String nm)
Fetches a named style previously added.
Enumeration<?> getStyleNames()
Fetches the list of of style names.
void removeDocumentListener(DocumentListener listener)
Removes a document listener.
void removeStyle(String nm)
Removes a named style previously added to the document.
void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace)
Sets attributes for some part of the document.
void setLogicalStyle(int pos, Style s)
Sets the logical style to use for the paragraph at the given position.
void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)
Sets attributes for a paragraph.
Protected Methods
void create(ElementSpec[] data)
Initialize the document to reflect the given element structure (i.e.
AbstractDocument.AbstractElement createDefaultRoot()
Creates the root element to be used to represent the default document structure.
void insert(int offset, ElementSpec[] data)
Inserts new elements in bulk.
void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
Updates document structure as a result of text insertion.
void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Updates document structure as a result of text removal.
void styleChanged(Style style)
Called when any of this document's styles have changed.
[Expand]
Inherited Methods
From class javax.swing.text.AbstractDocument
From class java.lang.Object
From interface javax.swing.text.Document
From interface javax.swing.text.StyledDocument

Constants

public static final int BUFFER_SIZE_DEFAULT

The default size of the initial content buffer.

Constant Value: 4096 (0x00001000)

Fields

Public Constructors

public DefaultStyledDocument (AbstractDocument.Content c, StyleContext styles)

Constructs a styled document.

Parameters
c the container for the content
styles resources and style definitions which may be shared across documents

public DefaultStyledDocument (StyleContext styles)

Constructs a styled document with the default content storage implementation and a shared set of styles.

Parameters
styles the styles

public DefaultStyledDocument ()

Constructs a default styled document. This buffers input content by a size of BUFFER_SIZE_DEFAULT and has a style context that is scoped by the lifetime of the document and is not shared with other documents.

Public Methods

public void addDocumentListener (DocumentListener listener)

Adds a document listener for notification of any changes.

Parameters
listener the listener

public Style addStyle (String nm, Style parent)

Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.

Parameters
nm the name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
parent the parent style. This may be null if unspecified attributes need not be resolved in some other style.
Returns
  • the style

public Color getBackground (AttributeSet attr)

Gets the background color from an attribute set.

Parameters
attr the attribute set
Returns
  • the color

public Element getCharacterElement (int pos)

Gets a character element based on a position.

Parameters
pos the position in the document >= 0
Returns
  • the element

public Element getDefaultRootElement ()

Gets the default root element.

Returns
  • the root

public Font getFont (AttributeSet attr)

Gets the font from an attribute set.

Parameters
attr the attribute set
Returns
  • the font

public Color getForeground (AttributeSet attr)

Gets the foreground color from an attribute set.

Parameters
attr the attribute set
Returns
  • the color

public Style getLogicalStyle (int p)

Fetches the logical style assigned to the paragraph represented by the given position.

Parameters
p the location to translate to a paragraph and determine the logical style assigned >= 0. This is an offset from the start of the document.
Returns
  • the style, null if none

public Element getParagraphElement (int pos)

Gets the paragraph element at the offset pos. A paragraph consists of at least one child Element, which is usually a leaf.

Parameters
pos the starting offset >= 0
Returns
  • the element

public Style getStyle (String nm)

Fetches a named style previously added.

Parameters
nm the name of the style
Returns
  • the style

public Enumeration<?> getStyleNames ()

Fetches the list of of style names.

Returns
  • all the style names

public void removeDocumentListener (DocumentListener listener)

Removes a document listener.

Parameters
listener the listener

public void removeStyle (String nm)

Removes a named style previously added to the document.

Parameters
nm the name of the style to remove

public void setCharacterAttributes (int offset, int length, AttributeSet s, boolean replace)

Sets attributes for some part of the document. A write lock is held by this operation while changes are being made, and a DocumentEvent is sent to the listeners after the change has been successfully completed.

This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

Parameters
offset the offset in the document >= 0
length the length >= 0
s the attributes
replace true if the previous attributes should be replaced before setting the new attributes

public void setLogicalStyle (int pos, Style s)

Sets the logical style to use for the paragraph at the given position. If attributes aren't explicitly set for character and paragraph attributes they will resolve through the logical style assigned to the paragraph, which in turn may resolve through some hierarchy completely independent of the element hierarchy in the document.

This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

Parameters
pos the offset from the start of the document >= 0
s the logical style to assign to the paragraph, null if none

public void setParagraphAttributes (int offset, int length, AttributeSet s, boolean replace)

Sets attributes for a paragraph.

This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

Parameters
offset the offset into the paragraph >= 0
length the number of characters affected >= 0
s the attributes
replace whether to replace existing attributes, or merge them

Protected Methods

protected void create (ElementSpec[] data)

Initialize the document to reflect the given element structure (i.e. the structure reported by the getDefaultRootElement method. If the document contained any data it will first be removed.

protected AbstractDocument.AbstractElement createDefaultRoot ()

Creates the root element to be used to represent the default document structure.

Returns
  • the element base

protected void insert (int offset, ElementSpec[] data)

Inserts new elements in bulk. This is useful to allow parsing with the document in an unlocked state and prepare an element structure modification. This method takes an array of tokens that describe how to update an element structure so the time within a write lock can be greatly reduced in an asynchronous update situation.

This method is thread safe, although most Swing methods are not. Please see How to Use Threads for more information.

Parameters
offset the starting offset >= 0
data the element data
Throws
BadLocationException for an invalid starting offset

protected void insertUpdate (AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)

Updates document structure as a result of text insertion. This will happen within a write lock. This implementation simply parses the inserted content for line breaks and builds up a set of instructions for the element buffer.

Parameters
chng a description of the document change
attr the attributes

protected void removeUpdate (AbstractDocument.DefaultDocumentEvent chng)

Updates document structure as a result of text removal.

Parameters
chng a description of the document change

protected void styleChanged (Style style)

Called when any of this document's styles have changed. Subclasses may wish to be intelligent about what gets damaged.

Parameters
style The Style that has changed.