Class java.awt.swing.text.DefaultStyledDocument
java.lang.Object
|
+----java.awt.swing.text.AbstractDocument
|
+----java.awt.swing.text.DefaultStyledDocument
- public class DefaultStyledDocument
- extends AbstractDocument
- implements StyledDocument
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 boundries since logical styles are
assigned to paragraph boundries.
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 Swing1.0 applications. It will
not be possible to load serialized Swing1.0 objects with future releases
of Swing. The JDK1.2 release of Swing will be the compatibility
baseline for the serialized form of Swing objects.
- See Also:
- Document, AbstractDocument
Method Summary
|
Style
|
addStyle(String nm,
Style parent)
Adds a new style into the logical style hierarchy.
|
AbstractDocument.AbstractElement
|
createDefaultRoot()
Creates the root element to be used to represent the
default document structure.
|
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 a paragraph element.
|
Style
|
getStyle(String nm)
Fetches a named style previously added.
|
void
|
insert(int offset,
DefaultStyledDocument.ElementSpec[] data)
Inserts new elements in bulk.
|
void
|
insertUpdate(AbstractDocument.DefaultDocumentEvent chng,
AttributeSet attr)
Updates document structure as a result of text insertion.
|
void
|
removeStyle(String nm)
Removes a named style previously added to the document.
|
void
|
removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Updates document structure as a result of text removal.
|
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.
|
Methods inherited from class java.awt.swing.text.AbstractDocument
|
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAttributeContext, getContent, getCurrentWriter, getDefaultRootElement, getDocumentProperties, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, insertUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, removeUpdate, render, setDocumentProperties, writeLock, writeUnlock |
Methods inherited from class java.lang.Object
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
BUFFER_SIZE_DEFAULT
public static final int BUFFER_SIZE_DEFAULT
- The default size of the initial content buffer.
DefaultStyledDocument
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
DefaultStyledDocument
public DefaultStyledDocument(StyleContext styles)
- Constructs a styled document with the default content
storage implementation and a shared set of styles.
- Parameters:
styles
- the styles
DefaultStyledDocument
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.
getDefaultRootElement
public Element getDefaultRootElement()
- Gets the default root element.
- Returns:
- the root
- Overrides:
- getDefaultRootElement in class AbstractDocument
- See Also:
- getDefaultRootElement
insert
protected void insert(int offset,
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
data
- the element data
- Throws:
- BadLocationException - for an invalid starting offset
addStyle
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.
- Implements:
- 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)
- Removes a named style previously added to the document.
- Implements:
- removeStyle in interface StyledDocument
- Parameters:
nm
- the name of the style to remove
getStyle
public Style getStyle(String nm)
- Fetches a named style previously added.
- Implements:
- getStyle in interface StyledDocument
- Parameters:
nm
- the name of the style
- Returns:
- the style
setLogicalStyle
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
Threads
and Swing for more information.
- Implements:
- setLogicalStyle in interface StyledDocument
- Parameters:
pos
- the offset from the start of the document
s
- the logical style to assign to the paragraph
getLogicalStyle
public Style getLogicalStyle(int p)
- Fetches the logical style assigned to the paragraph
represented by the given position.
- Implements:
- getLogicalStyle in interface StyledDocument
- Parameters:
p
- the location to translate to a paragraph
and determine the logical style assigned. This
is an offset from the start of the document.
- Returns:
- the style
setCharacterAttributes
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
Threads
and Swing for more information.
- Implements:
- setCharacterAttributes in interface StyledDocument
- Parameters:
offset
- the offset in the document
length
- the length
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)
- Sets attributes for a paragraph.
This method is thread safe, although most Swing methods
are not. Please see
Threads
and Swing for more information.
- Implements:
- setParagraphAttributes in interface StyledDocument
- Parameters:
offset
- the offset into the paragraph
length
- the number of characters affected
s
- the attributes
replace
- whether to replace existing attributes, or merge them
getParagraphElement
public Element getParagraphElement(int pos)
- Gets a paragraph element.
- Implements:
- getParagraphElement in interface StyledDocument
- Parameters:
pos
- the starting offset
- Returns:
- the element
getCharacterElement
public Element getCharacterElement(int pos)
- Gets a character element based on a position.
- Implements:
- getCharacterElement in interface StyledDocument
- Parameters:
pos
- the position in the document
- Returns:
- the element
insertUpdate
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
- Overrides:
- insertUpdate in class AbstractDocument
removeUpdate
protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
- Updates document structure as a result of text removal.
- Parameters:
chng
- a description of the document change
- Overrides:
- removeUpdate in class AbstractDocument
createDefaultRoot
protected 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)
- Gets the foreground color from an attribute set.
- Implements:
- getForeground in interface StyledDocument
- Parameters:
attr
- the attribute set
- Returns:
- the color
getBackground
public Color getBackground(AttributeSet attr)
- Gets the background color from an attribute set.
- Implements:
- getBackground in interface StyledDocument
- Parameters:
attr
- the attribute set
- Returns:
- the color
getFont
public Font getFont(AttributeSet attr)
- Gets the font from an attribute set.
- Implements:
- getFont in interface StyledDocument
- Parameters:
attr
- the attribute set
- Returns:
- the font
Submit a bug or feature
Submit comments/suggestions about new javadoc look.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.