Class DefaultStyledDocument
- All Implemented Interfaces:
-
Serializable
,Document
,StyledDocument
- Direct Known Subclasses:
HTMLDocument
public class DefaultStyledDocument extends AbstractDocument implements 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
.
- See Also:
Nested Class Summary
Modifier and Type | Class | 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... maps out the paragraphs/lines contained. |
Nested classes/interfaces declared in class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
Field Summary
Modifier and Type | Field | Description |
---|---|---|
protected DefaultStyledDocument.ElementBuffer |
buffer |
The element buffer. |
static final int |
BUFFER_SIZE_DEFAULT |
The default size of the initial content buffer. |
Fields declared in class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
Fields declared in interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
Constructor Summary
Constructor | Description |
---|---|
DefaultStyledDocument() |
Constructs a default styled document. |
DefaultStyledDocument |
Constructs a styled document. |
DefaultStyledDocument |
Constructs a styled document with the default content storage implementation and a shared set of styles. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void |
addDocumentListener |
Adds a document listener for notification of any changes. |
Style |
addStyle |
Adds a new style into the logical style hierarchy. |
protected void |
create |
Initialize the document to reflect the given element structure (i.e. the structure reported by the getDefaultRootElement method. |
protected AbstractDocument.AbstractElement |
createDefaultRoot() |
Creates the root element to be used to represent the default document structure. |
Color |
getBackground |
Gets the background color from an attribute set. |
Element |
getCharacterElement |
Gets a character element based on a position. |
Element |
getDefaultRootElement() |
Gets the default root element. |
Font |
getFont |
Gets the font from an attribute set. |
Color |
getForeground |
Gets the foreground color from an attribute set. |
Style |
getLogicalStyle |
Fetches the logical style assigned to the paragraph represented by the given position. |
Element |
getParagraphElement |
Gets the paragraph element at the offset pos . |
Style |
getStyle |
Fetches a named style previously added. |
Enumeration<?> |
getStyleNames() |
Fetches the list of style names. |
protected void |
insert |
Inserts new elements in bulk. |
protected void |
insertUpdate |
Updates document structure as a result of text insertion. |
void |
removeDocumentListener |
Removes a document listener. |
void |
removeElement |
Removes an element from this document. |
void |
removeStyle |
Removes a named style previously added to the document. |
protected void |
removeUpdate |
Updates document structure as a result of text removal. |
void |
setCharacterAttributes |
Sets attributes for some part of the document. |
void |
setLogicalStyle |
Sets the logical style to use for the paragraph at the given position. |
void |
setParagraphAttributes |
Sets attributes for a paragraph. |
protected void |
styleChanged |
Called when any of this document's styles have changed. |
Methods declared in 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, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface javax.swing.text.Document
addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeUndoableEditListener, render
Field Details
BUFFER_SIZE_DEFAULT
public static final int BUFFER_SIZE_DEFAULT
- See Also:
buffer
protected DefaultStyledDocument.ElementBuffer buffer
Constructor Details
DefaultStyledDocument
public DefaultStyledDocument(AbstractDocument.Content c, StyleContext styles)
- Parameters:
-
c
- the container for the content -
styles
- resources and style definitions which may be shared across documents
DefaultStyledDocument
public DefaultStyledDocument(StyleContext styles)
- Parameters:
-
styles
- the styles
DefaultStyledDocument
public DefaultStyledDocument()
Method Details
getDefaultRootElement
public Element getDefaultRootElement()
- Specified by:
-
getDefaultRootElement
in interfaceDocument
- Specified by:
-
getDefaultRootElement
in classAbstractDocument
- Returns:
- the root
- See Also:
create
protected void create(DefaultStyledDocument.ElementSpec[] data)
getDefaultRootElement
method. If the document contained any data it will first be removed.- Parameters:
-
data
- the element data
insert
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.
- Parameters:
-
offset
- the starting offset >= 0 -
data
- the element data - Throws:
-
BadLocationException
- for an invalid starting offset
removeElement
public void removeElement(Element elem)
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.
- Parameters:
-
elem
- the element to remove - Throws:
-
NullPointerException
- if the element isnull
-
IllegalArgumentException
- if the element could not be removed -
IllegalStateException
- if the element structure is invalid - Since:
- 1.7
addStyle
public Style addStyle(String nm, Style parent)
- Specified by:
-
addStyle
in interfaceStyledDocument
- 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)
- Specified by:
-
removeStyle
in interfaceStyledDocument
- Parameters:
-
nm
- the name of the style to remove
getStyle
public Style getStyle(String nm)
- Specified by:
-
getStyle
in interfaceStyledDocument
- Parameters:
-
nm
- the name of the style - Returns:
- the style
getStyleNames
public Enumeration<?> getStyleNames()
- Returns:
- all the style names
setLogicalStyle
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.
- Specified by:
-
setLogicalStyle
in interfaceStyledDocument
- 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)
- Specified by:
-
getLogicalStyle
in interfaceStyledDocument
- 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)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
setCharacterAttributes
in interfaceStyledDocument
- 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)
This method is thread safe, although most Swing methods are not. Please see Concurrency in Swing for more information.
- Specified by:
-
setParagraphAttributes
in interfaceStyledDocument
- 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)
pos
. A paragraph consists of at least one child Element, which is usually a leaf.- Specified by:
-
getParagraphElement
in interfaceStyledDocument
- Specified by:
-
getParagraphElement
in classAbstractDocument
- Parameters:
-
pos
- the starting offset >= 0 - Returns:
- the element
getCharacterElement
public Element getCharacterElement(int pos)
- Specified by:
-
getCharacterElement
in interfaceStyledDocument
- Parameters:
-
pos
- the position in the document >= 0 - Returns:
- the element
insertUpdate
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng, AttributeSet attr)
- Overrides:
-
insertUpdate
in classAbstractDocument
- Parameters:
-
chng
- a description of the document change -
attr
- the attributes
removeUpdate
protected void removeUpdate(AbstractDocument.DefaultDocumentEvent chng)
- Overrides:
-
removeUpdate
in classAbstractDocument
- Parameters:
-
chng
- a description of the document change
createDefaultRoot
protected AbstractDocument.AbstractElement createDefaultRoot()
- Returns:
- the element base
getForeground
public Color getForeground(AttributeSet attr)
- Specified by:
-
getForeground
in interfaceStyledDocument
- Parameters:
-
attr
- the attribute set - Returns:
- the color
getBackground
public Color getBackground(AttributeSet attr)
- Specified by:
-
getBackground
in interfaceStyledDocument
- Parameters:
-
attr
- the attribute set - Returns:
- the color
getFont
public Font getFont(AttributeSet attr)
- Specified by:
-
getFont
in interfaceStyledDocument
- Parameters:
-
attr
- the attribute set - Returns:
- the font
styleChanged
protected void styleChanged(Style style)
- Parameters:
-
style
- The Style that has changed.
addDocumentListener
public void addDocumentListener(DocumentListener listener)
- Specified by:
-
addDocumentListener
in interfaceDocument
- Overrides:
-
addDocumentListener
in classAbstractDocument
- Parameters:
-
listener
- the listener - See Also:
removeDocumentListener
public void removeDocumentListener(DocumentListener listener)
- Specified by:
-
removeDocumentListener
in interfaceDocument
- Overrides:
-
removeDocumentListener
in classAbstractDocument
- Parameters:
-
listener
- the listener - See Also:
© 1993, 2021, 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.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/DefaultStyledDocument.html