javax.swing.text
Class DefaultStyledDocument

java.lang.Object
  |
  +--javax.swing.text.AbstractDocument
        |
        +--javax.swing.text.DefaultStyledDocument
All Implemented Interfaces:
Document, Serializable, StyledDocument

public class DefaultStyledDocument
extends AbstractDocument
implements StyledDocument

Safe: 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 java.beans.XMLEncoder.

Version:
1.120 12/03/01
Author:
Timothy Prinzing
See Also:
Document, AbstractDocument, Serialized Form

Field Summary
protected  javax.swing.text.DefaultStyledDocument.ElementBuffer buffer
           
static int BUFFER_SIZE_DEFAULT
          Enabled: The default size of the initial content buffer.
private  Vector listeningStyles
          Styles listening to.
private  ChangeListener styleChangeListener
          Listens to Styles.
private  ChangeListener styleContextChangeListener
          Listens to Styles.
private  javax.swing.text.DefaultStyledDocument.ChangeUpdateRunnable updateRunnable
          Run to create a change event for the document
 
Fields inherited from class javax.swing.text.AbstractDocument
AsyncLoadPriority, BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, I18NProperty, listenerList, MultiByteProperty, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
DefaultStyledDocument()
          Enabled: Constructs a default styled document.
DefaultStyledDocument(javax.swing.text.AbstractDocument.Content c, StyleContext styles)
          Suppressed: Constructs a styled document.
DefaultStyledDocument(StyleContext styles)
          Enabled: Constructs a styled document with the default content storage implementation and a shared set of styles.
 
Method Summary
 void addDocumentListener(DocumentListener listener)
          Enabled: Adds a document listener for notification of any changes.
 Style addStyle(String nm, Style parent)
          Enabled: Adds a new style into the logical style hierarchy.
protected  void create(javax.swing.text.DefaultStyledDocument.ElementSpec[] data)
          Initialize the document to reflect the given element structure (i.e.
protected  javax.swing.text.AbstractDocument.AbstractElement createDefaultRoot()
          Creates the root element to be used to represent the default document structure.
(package private)  short createSpecsForInsertAfterNewline(Element paragraph, Element pParagraph, AttributeSet pattr, Vector parseBuffer, int offset, int endOffset)
          This is called by insertUpdate when inserting after a new line.
(package private)  ChangeListener createStyleChangeListener()
          Returns a new instance of StyleChangeHandler.
(package private)  ChangeListener createStyleContextChangeListener()
          Returns a new instance of StyleContextChangeHandler.
 Color getBackground(AttributeSet attr)
          Enabled: Gets the background color from an attribute set.
 Element getCharacterElement(int pos)
          Enabled: Gets a character element based on a position.
 Element getDefaultRootElement()
          Enabled: Gets the default root element.
 Font getFont(AttributeSet attr)
          Enabled: Gets the font from an attribute set.
 Color getForeground(AttributeSet attr)
          Enabled: Gets the foreground color from an attribute set.
 Style getLogicalStyle(int p)
          Enabled: Fetches the logical style assigned to the paragraph represented by the given position.
 Element getParagraphElement(int pos)
          Enabled: Gets the paragraph element at the offset pos.
 Style getStyle(String nm)
          Enabled: Fetches a named style previously added.
 Enumeration getStyleNames()
          Enabled: Fetches the list of of style names.
protected  void insert(int offset, javax.swing.text.DefaultStyledDocument.ElementSpec[] data)
          Inserts new elements in bulk.
protected  void insertUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
          Updates document structure as a result of text insertion.
private  void readObject(ObjectInputStream s)
           
 void removeDocumentListener(DocumentListener listener)
          Enabled: Removes a document listener.
 void removeStyle(String nm)
          Enabled: Removes a named style previously added to the document.
protected  void removeUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng)
          Updates document structure as a result of text removal.
 void setCharacterAttributes(int offset, int length, AttributeSet s, boolean replace)
          Enabled: Sets attributes for some part of the document.
 void setLogicalStyle(int pos, Style s)
          Enabled: Sets the logical style to use for the paragraph at the given position.
 void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace)
          Enabled: Sets attributes for a paragraph.
protected  void styleChanged(Style style)
          Called when any of this document's styles have changed.
(package private)  void updateStylesListeningTo()
          Adds a ChangeListener to new styles, and removes ChangeListener from old styles.
 
Methods inherited from class javax.swing.text.AbstractDocument
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, handleInsertString, handleRemove, insertString, isLeftToRight, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, updateBidi, 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
addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeUndoableEditListener, render
 

Field Detail

BUFFER_SIZE_DEFAULT

public static final int BUFFER_SIZE_DEFAULT
Enabled: The default size of the initial content buffer.


buffer

protected javax.swing.text.DefaultStyledDocument.ElementBuffer buffer

listeningStyles

private transient Vector listeningStyles
Styles listening to.


styleChangeListener

private transient ChangeListener styleChangeListener
Listens to Styles.


styleContextChangeListener

private transient ChangeListener styleContextChangeListener
Listens to Styles.


updateRunnable

private transient javax.swing.text.DefaultStyledDocument.ChangeUpdateRunnable updateRunnable
Run to create a change event for the document

Constructor Detail

DefaultStyledDocument

public DefaultStyledDocument(javax.swing.text.AbstractDocument.Content c,
                             StyleContext styles)
Suppressed: Constructs a styled document.

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

DefaultStyledDocument

public DefaultStyledDocument(StyleContext styles)
Enabled: Constructs a styled document with the default content storage implementation and a shared set of styles.

Parameters:
styles - the styles

DefaultStyledDocument

public DefaultStyledDocument()
Enabled: 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.

Method Detail

getDefaultRootElement

public Element getDefaultRootElement()
Enabled: Gets the default root element.

Specified by:
getDefaultRootElement in interface Document
Specified by:
getDefaultRootElement in class AbstractDocument
Returns:
the root
See Also:
Document.getDefaultRootElement()

create

protected void create(javax.swing.text.DefaultStyledDocument.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.


insert

protected void insert(int offset,
                      javax.swing.text.DefaultStyledDocument.ElementSpec[] data)
               throws BadLocationException
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 Threads and Swing for more information.

Parameters:
offset - the starting offset >= 0
data - the element data
BadLocationException

addStyle

public Style addStyle(String nm,
                      Style parent)
Enabled: 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.

Specified by:
addStyle in interface StyledDocument
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

removeStyle

public void removeStyle(String nm)
Enabled: Removes a named style previously added to the document.

Specified by:
removeStyle in interface StyledDocument
Parameters:
nm - the name of the style to remove

getStyle

public Style getStyle(String nm)
Enabled: Fetches a named style previously added.

Specified by:
getStyle in interface StyledDocument
Parameters:
nm - the name of the style
Returns:
the style

getStyleNames

public Enumeration getStyleNames()
Enabled: Fetches the list of of style names.

Returns:
all the style names

setLogicalStyle

public void setLogicalStyle(int pos,
                            Style s)
Enabled: 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 Threads and Swing for more information.

Specified by:
setLogicalStyle in interface StyledDocument
Parameters:
pos - the offset from the start of the document >= 0
s - the logical style to assign to the paragraph, null if none

getLogicalStyle

public Style getLogicalStyle(int p)
Enabled: Fetches the logical style assigned to the paragraph represented by the given position.

Specified by:
getLogicalStyle in interface StyledDocument
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

setCharacterAttributes

public void setCharacterAttributes(int offset,
                                   int length,
                                   AttributeSet s,
                                   boolean replace)
Enabled: 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 Threads and Swing for more information.

Specified by:
setCharacterAttributes in interface StyledDocument
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

setParagraphAttributes

public void setParagraphAttributes(int offset,
                                   int length,
                                   AttributeSet s,
                                   boolean replace)
Enabled: Sets attributes for a paragraph.

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

Specified by:
setParagraphAttributes in interface StyledDocument
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

getParagraphElement

public Element getParagraphElement(int pos)
Enabled: Gets the paragraph element at the offset pos. A paragraph consists of at least one child Element, which is usually a leaf.

Specified by:
getParagraphElement in interface StyledDocument
Specified by:
getParagraphElement in class AbstractDocument
Parameters:
pos - the starting offset >= 0
Returns:
the element

getCharacterElement

public Element getCharacterElement(int pos)
Enabled: Gets a character element based on a position.

Specified by:
getCharacterElement in interface StyledDocument
Parameters:
pos - the position in the document >= 0
Returns:
the element

insertUpdate

protected void insertUpdate(javax.swing.text.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.

Overrides:
insertUpdate in class AbstractDocument
Parameters:
chng - a description of the document change
attr - the attributes

createSpecsForInsertAfterNewline

short createSpecsForInsertAfterNewline(Element paragraph,
                                       Element pParagraph,
                                       AttributeSet pattr,
                                       Vector parseBuffer,
                                       int offset,
                                       int endOffset)
This is called by insertUpdate when inserting after a new line. It generates, in parseBuffer, ElementSpecs that will position the stack in paragraph.

It returns the direction the last StartSpec should have (this don't necessarily create the last start spec).


removeUpdate

protected void removeUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng)
Updates document structure as a result of text removal.

Overrides:
removeUpdate in class AbstractDocument
Parameters:
chng - a description of the document change

createDefaultRoot

protected javax.swing.text.AbstractDocument.AbstractElement createDefaultRoot()
Creates the root element to be used to represent the default document structure.

Returns:
the element base

getForeground

public Color getForeground(AttributeSet attr)
Enabled: Gets the foreground color from an attribute set.

Specified by:
getForeground in interface StyledDocument
Parameters:
attr - the attribute set
Returns:
the color

getBackground

public Color getBackground(AttributeSet attr)
Enabled: Gets the background color from an attribute set.

Specified by:
getBackground in interface StyledDocument
Parameters:
attr - the attribute set
Returns:
the color

getFont

public Font getFont(AttributeSet attr)
Enabled: Gets the font from an attribute set.

Specified by:
getFont in interface StyledDocument
Parameters:
attr - the attribute set
Returns:
the font

styleChanged

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.

addDocumentListener

public void addDocumentListener(DocumentListener listener)
Enabled: Adds a document listener for notification of any changes.

Specified by:
addDocumentListener in interface Document
Overrides:
addDocumentListener in class AbstractDocument
Parameters:
listener - the listener
See Also:
Document.addDocumentListener(javax.swing.event.DocumentListener)

removeDocumentListener

public void removeDocumentListener(DocumentListener listener)
Enabled: Removes a document listener.

Specified by:
removeDocumentListener in interface Document
Overrides:
removeDocumentListener in class AbstractDocument
Parameters:
listener - the listener
See Also:
Document.removeDocumentListener(javax.swing.event.DocumentListener)

createStyleChangeListener

ChangeListener createStyleChangeListener()
Returns a new instance of StyleChangeHandler.


createStyleContextChangeListener

ChangeListener createStyleContextChangeListener()
Returns a new instance of StyleContextChangeHandler.


updateStylesListeningTo

void updateStylesListeningTo()
Adds a ChangeListener to new styles, and removes ChangeListener from old styles.


readObject

private void readObject(ObjectInputStream s)
                 throws ClassNotFoundException,
                        IOException
ClassNotFoundException
IOException


comments?