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 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 JavaBeans™ has been added to the java.beans
package. Please see XMLEncoder
.
Document
, AbstractDocument
Modifier and Type | Class and Description |
---|---|
static class |
DefaultStyledDocument.AttributeUndoableEdit An UndoableEdit used to remember AttributeSet changes to an Element. |
class |
DefaultStyledDocument.ElementBuffer Class to manage changes to the element hierarchy. |
static class |
DefaultStyledDocument.ElementSpec Specification for building elements. |
protected class |
DefaultStyledDocument.SectionElement Default root element for a document... |
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
public static final int BUFFER_SIZE_DEFAULT
The default size of the initial content buffer.
protected DefaultStyledDocument.ElementBuffer buffer
public DefaultStyledDocument(AbstractDocument.Content c, StyleContext styles)
Constructs a styled document.
c
- the container for the contentstyles
- resources and style definitions which may be shared across documentspublic DefaultStyledDocument(StyleContext styles)
Constructs a styled document with the default content storage implementation and a shared set of styles.
styles
- the stylespublic 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 Element getDefaultRootElement()
Gets the default root element.
getDefaultRootElement
in interface Document
getDefaultRootElement
in class AbstractDocument
Document.getDefaultRootElement()
protected void create(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.
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 Concurrency in Swing for more information.
offset
- the starting offset >= 0data
- the element dataBadLocationException
- for an invalid starting offsetpublic void removeElement(Element elem)
Removes an element from this document.
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 removeNullPointerException
- if the element is null
IllegalArgumentException
- if the element could not be removedIllegalStateException
- if the element structure is invalidpublic 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.
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(String nm)
Removes a named style previously added to the document.
removeStyle
in interface StyledDocument
nm
- the name of the style to removepublic Style getStyle(String nm)
Fetches a named style previously added.
getStyle
in interface StyledDocument
nm
- the name of the stylepublic Enumeration<?> getStyleNames()
Fetches the list of of style names.
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 Concurrency in Swing for more information.
setLogicalStyle
in interface StyledDocument
pos
- the offset from the start of the document >= 0s
- the logical style to assign to the paragraph, null if nonepublic Style getLogicalStyle(int p)
Fetches the logical style assigned to the paragraph represented by the given position.
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)
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 Concurrency in Swing for more information.
setCharacterAttributes
in interface StyledDocument
offset
- the offset in the document >= 0length
- the length >= 0s
- the attributesreplace
- true if the previous attributes should be replaced before setting the new attributespublic 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 Concurrency in Swing for more information.
setParagraphAttributes
in interface StyledDocument
offset
- the offset into the paragraph >= 0length
- the number of characters affected >= 0s
- the attributesreplace
- whether to replace existing attributes, or merge thempublic 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.
getParagraphElement
in interface StyledDocument
getParagraphElement
in class AbstractDocument
pos
- the starting offset >= 0public Element getCharacterElement(int pos)
Gets a character element based on a position.
getCharacterElement
in interface StyledDocument
pos
- the position in the document >= 0protected 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.
insertUpdate
in class AbstractDocument
chng
- a description of the document changeattr
- the attributesprotected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
Updates document structure as a result of text removal.
removeUpdate
in class AbstractDocument
chng
- a description of the document changeprotected AbstractDocument.AbstractElement createDefaultRoot()
Creates the root element to be used to represent the default document structure.
public Color getForeground(AttributeSet attr)
Gets the foreground color from an attribute set.
getForeground
in interface StyledDocument
attr
- the attribute setpublic Color getBackground(AttributeSet attr)
Gets the background color from an attribute set.
getBackground
in interface StyledDocument
attr
- the attribute setpublic Font getFont(AttributeSet attr)
Gets the font from an attribute set.
getFont
in interface StyledDocument
attr
- the attribute setprotected void styleChanged(Style style)
Called when any of this document's styles have changed. Subclasses may wish to be intelligent about what gets damaged.
style
- The Style that has changed.public void addDocumentListener(DocumentListener listener)
Adds a document listener for notification of any changes.
addDocumentListener
in interface Document
addDocumentListener
in class AbstractDocument
listener
- the listenerDocument.addDocumentListener(javax.swing.event.DocumentListener)
public void removeDocumentListener(DocumentListener listener)
Removes a document listener.
removeDocumentListener
in interface Document
removeDocumentListener
in class AbstractDocument
listener
- the listenerDocument.removeDocumentListener(javax.swing.event.DocumentListener)
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.