public class DefaultStyledDocument extends AbstractDocumentimplements StyledDocument
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 JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.
Document,
AbstractDocument
| Modifier and Type | Class and Description |
|---|---|
static class |
DefaultStyledDocument
An UndoableEdit used to remember AttributeSet changes to an Element.
|
class |
DefaultStyledDocument
Class to manage changes to the element hierarchy.
|
static class |
DefaultStyledDocument
Specification for building elements.
|
protected class |
DefaultStyledDocument
Default root element for a document...
|
AbstractDocument.AbstractElement , AbstractDocument.AttributeContext , AbstractDocument.BranchElement , AbstractDocument.Content , AbstractDocument.DefaultDocumentEvent , AbstractDocument.ElementEdit , AbstractDocument.LeafElement | Modifier and Type | Field and Description |
|---|---|
protected DefaultStyledDocument |
buffer
|
static int |
BUFFER_SIZE_DEFAULT
The default size of the initial content buffer.
|
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementNameStreamDescriptionProperty, TitleProperty| Constructor and Description |
|---|
DefaultStyledDocument()
Constructs a default styled document.
|
DefaultStyledDocument(AbstractDocument
Constructs a styled document.
|
DefaultStyledDocument(StyleContext
Constructs a styled document with the default content storage implementation and a shared set of styles.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addDocumentListener(DocumentListener
Adds a document listener for notification of any changes.
|
Style |
addStyle(String
Adds a new style into the logical style hierarchy.
|
protected void |
create(DefaultStyledDocument
Initialize the document to reflect the given element structure (i.e.
|
protected AbstractDocument |
createDefaultRoot()
Creates the root element to be used to represent the default document structure.
|
Color |
getBackground(AttributeSet
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
Gets the font from an attribute set.
|
Color |
getForeground(AttributeSet
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
Fetches a named style previously added.
|
Enumeration |
getStyleNames()
Fetches the list of of style names.
|
protected void |
insert(int offset, DefaultStyledDocument
Inserts new elements in bulk.
|
protected void |
insertUpdate(AbstractDocument
Updates document structure as a result of text insertion.
|
void |
removeDocumentListener(DocumentListener
Removes a document listener.
|
void |
removeElement(Element
Removes an element from this document.
|
void |
removeStyle(String
Removes a named style previously added to the document.
|
protected void |
removeUpdate(AbstractDocument
Updates document structure as a result of text removal.
|
void |
setCharacterAttributes(int offset, int length, AttributeSet
Sets attributes for some part of the document.
|
void |
setLogicalStyle(int pos, Style
Sets the logical style to use for the paragraph at the given position.
|
void |
setParagraphAttributes(int offset, int length, AttributeSet
Sets attributes for a paragraph.
|
protected void |
styleChanged(Style
Called when any of this document's styles have changed.
|
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, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlockclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeUndoableEditListener, renderpublic static final int BUFFER_SIZE_DEFAULT
protected DefaultStyledDocument.ElementBuffer buffer
public DefaultStyledDocument(AbstractDocument.Content c, StyleContext styles)
c - the container for the content
styles - resources and style definitions which may be shared across documents
public DefaultStyledDocument(StyleContextstyles)
styles - the styles
public DefaultStyledDocument()
public ElementgetDefaultRootElement()
getDefaultRootElement in interface
Document
getDefaultRootElement in class
AbstractDocument
Document.getDefaultRootElement()
protected void create(DefaultStyledDocument.ElementSpec [] data)
getDefaultRootElement method. If the document contained any data it will first be removed.
protected void insert(int offset,
DefaultStyledDocument.ElementSpec [] data)
throws BadLocationException
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
offset - the starting offset >= 0
data - the element data
BadLocationException - for an invalid starting offset
public void removeElement(Elementelem)
The element is removed from its parent element, as well as the text in the range identified by the element. If the element isn't associated with the document, IllegalArgumentException is thrown.
As empty branch elements are not allowed in the document, if the element is the sole child, its parent element is removed as well, recursively. This means that when replacing all the children of a particular element, new children should be added before removing old children.
Element removal results in two events being fired, the DocumentEvent for changes in element structure and UndoableEditEvent for changes in document content.
If the element contains end-of-content mark (the last "\n" character in document), this character is not removed; instead, preceding leaf element is extended to cover the character. If the last leaf already ends with "\n", it is included in content removal.
If the element is null, NullPointerException is thrown. If the element structure would become invalid after the removal, for example if the element is the document root element, IllegalArgumentException is thrown. If the current element structure is invalid, IllegalStateException is thrown.
elem - the element to remove
NullPointerException - if the element is
null
IllegalArgumentException - if the element could not be removed
IllegalStateException - if the element structure is invalid
public StyleaddStyle(String nm, Style parent)
addStyle in interface
StyledDocument
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.
public void removeStyle(Stringnm)
removeStyle in interface
StyledDocument
nm - the name of the style to remove
public StylegetStyle(String nm)
getStyle in interface
StyledDocument
nm - the name of the style
public Enumeration<?> getStyleNames()
public void setLogicalStyle(int pos,
Style s)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
setLogicalStyle in interface
StyledDocument
pos - the offset from the start of the document >= 0
s - the logical style to assign to the paragraph, null if none
public StylegetLogicalStyle(int p)
getLogicalStyle in interface
StyledDocument
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.
public void setCharacterAttributes(int offset,
int length,
AttributeSet s,
boolean replace)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
setCharacterAttributes in interface
StyledDocument
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 setParagraphAttributes(int offset,
int length,
AttributeSet s,
boolean replace)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
setParagraphAttributes in interface
StyledDocument
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
public ElementgetParagraphElement(int pos)
pos. A paragraph consists of at least one child Element, which is usually a leaf.
getParagraphElement in interface
StyledDocument
getParagraphElement in class
AbstractDocument
pos - the starting offset >= 0
public ElementgetCharacterElement(int pos)
getCharacterElement in interface
StyledDocument
pos - the position in the document >= 0
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
insertUpdate in class
AbstractDocument
chng - a description of the document change
attr - the attributes
protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
removeUpdate in class
AbstractDocument
chng - a description of the document change
protected AbstractDocument.AbstractElement createDefaultRoot()
public ColorgetForeground(AttributeSet attr)
getForeground in interface
StyledDocument
attr - the attribute set
public ColorgetBackground(AttributeSet attr)
getBackground in interface
StyledDocument
attr - the attribute set
public FontgetFont(AttributeSet attr)
getFont in interface
StyledDocument
attr - the attribute set
protected void styleChanged(Stylestyle)
style - The Style that has changed.
public void addDocumentListener(DocumentListenerlistener)
addDocumentListener in interface
Document
addDocumentListener in class
AbstractDocument
listener - the listener
Document.addDocumentListener(javax.swing.event.DocumentListener)
public void removeDocumentListener(DocumentListenerlistener)
removeDocumentListener in interface
Document
removeDocumentListener in class
AbstractDocument
listener - the listener
Document.removeDocumentListener(javax.swing.event.DocumentListener)