DefaultStyledDocument (Java SE 15 & JDK 15) (original) (raw)
All Implemented Interfaces:
[Serializable](../../../../java.base/java/io/Serializable.html "interface in java.io")
, [Document](Document.html "interface in javax.swing.text")
, [StyledDocument](StyledDocument.html "interface in javax.swing.text")
Direct Known Subclasses:
[HTMLDocument](html/HTMLDocument.html "class in javax.swing.text.html")
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.
See Also:
Nested Class Summary
Field Summary
Constructor Summary
Constructors
Constructor | Description |
---|---|
DefaultStyledDocument() | Constructs a default styled document. |
DefaultStyledDocument(AbstractDocument.Content c,StyleContext styles) | Constructs a styled document. |
DefaultStyledDocument(StyleContext styles) | 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(DocumentListener listener) | Adds a document listener for notification of any changes. |
Style | addStyle(String nm,Style parent) | Adds a new style into the logical style hierarchy. |
protected void | create(DefaultStyledDocument.ElementSpec[] data) | Initialize the document to reflect the given element structure (i.e. the structure reported by thegetDefaultRootElement method. |
protected 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 the paragraph element at the offset pos. |
Style | getStyle(String nm) | Fetches a named style previously added. |
Enumeration<?> | getStyleNames() | Fetches the list of style names. |
protected void | insert(int offset,DefaultStyledDocument.ElementSpec[] data) | Inserts new elements in bulk. |
protected void | insertUpdate(AbstractDocument.DefaultDocumentEvent chng,AttributeSet attr) | Updates document structure as a result of text insertion. |
void | removeDocumentListener(DocumentListener listener) | Removes a document listener. |
void | removeElement(Element elem) | Removes an element from this document. |
void | removeStyle(String nm) | Removes a named style previously added to the document. |
protected 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. |
protected void | styleChanged(Style style) | Called when any of this document's styles have changed. |
Methods declared in class javax.swing.text.AbstractDocument
[addUndoableEditListener](AbstractDocument.html#addUndoableEditListener%28javax.swing.event.UndoableEditListener%29), [createBranchElement](AbstractDocument.html#createBranchElement%28javax.swing.text.Element,javax.swing.text.AttributeSet%29), [createLeafElement](AbstractDocument.html#createLeafElement%28javax.swing.text.Element,javax.swing.text.AttributeSet,int,int%29), [createPosition](AbstractDocument.html#createPosition%28int%29), [dump](AbstractDocument.html#dump%28java.io.PrintStream%29), [fireChangedUpdate](AbstractDocument.html#fireChangedUpdate%28javax.swing.event.DocumentEvent%29), [fireInsertUpdate](AbstractDocument.html#fireInsertUpdate%28javax.swing.event.DocumentEvent%29), [fireRemoveUpdate](AbstractDocument.html#fireRemoveUpdate%28javax.swing.event.DocumentEvent%29), [fireUndoableEditUpdate](AbstractDocument.html#fireUndoableEditUpdate%28javax.swing.event.UndoableEditEvent%29), [getAsynchronousLoadPriority](AbstractDocument.html#getAsynchronousLoadPriority%28%29), [getAttributeContext](AbstractDocument.html#getAttributeContext%28%29), [getBidiRootElement](AbstractDocument.html#getBidiRootElement%28%29), [getContent](AbstractDocument.html#getContent%28%29), [getCurrentWriter](AbstractDocument.html#getCurrentWriter%28%29), [getDocumentFilter](AbstractDocument.html#getDocumentFilter%28%29), [getDocumentListeners](AbstractDocument.html#getDocumentListeners%28%29), [getDocumentProperties](AbstractDocument.html#getDocumentProperties%28%29), [getEndPosition](AbstractDocument.html#getEndPosition%28%29), [getLength](AbstractDocument.html#getLength%28%29), [getListeners](AbstractDocument.html#getListeners%28java.lang.Class%29), [getProperty](AbstractDocument.html#getProperty%28java.lang.Object%29), [getRootElements](AbstractDocument.html#getRootElements%28%29), [getStartPosition](AbstractDocument.html#getStartPosition%28%29), [getText](AbstractDocument.html#getText%28int,int%29), [getText](AbstractDocument.html#getText%28int,int,javax.swing.text.Segment%29), [getUndoableEditListeners](AbstractDocument.html#getUndoableEditListeners%28%29), [insertString](AbstractDocument.html#insertString%28int,java.lang.String,javax.swing.text.AttributeSet%29), [postRemoveUpdate](AbstractDocument.html#postRemoveUpdate%28javax.swing.text.AbstractDocument.DefaultDocumentEvent%29), [putProperty](AbstractDocument.html#putProperty%28java.lang.Object,java.lang.Object%29), [readLock](AbstractDocument.html#readLock%28%29), [readUnlock](AbstractDocument.html#readUnlock%28%29), [remove](AbstractDocument.html#remove%28int,int%29), [removeUndoableEditListener](AbstractDocument.html#removeUndoableEditListener%28javax.swing.event.UndoableEditListener%29), [render](AbstractDocument.html#render%28java.lang.Runnable%29), [replace](AbstractDocument.html#replace%28int,int,java.lang.String,javax.swing.text.AttributeSet%29), [setAsynchronousLoadPriority](AbstractDocument.html#setAsynchronousLoadPriority%28int%29), [setDocumentFilter](AbstractDocument.html#setDocumentFilter%28javax.swing.text.DocumentFilter%29), [setDocumentProperties](AbstractDocument.html#setDocumentProperties%28java.util.Dictionary%29), [writeLock](AbstractDocument.html#writeLock%28%29), [writeUnlock](AbstractDocument.html#writeUnlock%28%29)
Methods declared in class java.lang.Object
[clone](../../../../java.base/java/lang/Object.html#clone%28%29), [equals](../../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../../java.base/java/lang/Object.html#finalize%28%29), [getClass](../../../../java.base/java/lang/Object.html#getClass%28%29), [hashCode](../../../../java.base/java/lang/Object.html#hashCode%28%29), [notify](../../../../java.base/java/lang/Object.html#notify%28%29), [notifyAll](../../../../java.base/java/lang/Object.html#notifyAll%28%29), [toString](../../../../java.base/java/lang/Object.html#toString%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long,int%29)
Methods declared in interface javax.swing.text.Document
[addUndoableEditListener](Document.html#addUndoableEditListener%28javax.swing.event.UndoableEditListener%29), [createPosition](Document.html#createPosition%28int%29), [getEndPosition](Document.html#getEndPosition%28%29), [getLength](Document.html#getLength%28%29), [getProperty](Document.html#getProperty%28java.lang.Object%29), [getRootElements](Document.html#getRootElements%28%29), [getStartPosition](Document.html#getStartPosition%28%29), [getText](Document.html#getText%28int,int%29), [getText](Document.html#getText%28int,int,javax.swing.text.Segment%29), [insertString](Document.html#insertString%28int,java.lang.String,javax.swing.text.AttributeSet%29), [putProperty](Document.html#putProperty%28java.lang.Object,java.lang.Object%29), [remove](Document.html#remove%28int,int%29), [removeUndoableEditListener](Document.html#removeUndoableEditListener%28javax.swing.event.UndoableEditListener%29), [render](Document.html#render%28java.lang.Runnable%29)
Field Details
BUFFER_SIZE_DEFAULT
public static final int BUFFER_SIZE_DEFAULT
The default size of the initial content buffer.
See Also:
Constant Field Valuesbuffer
The element buffer.
Constructor Details
DefaultStyledDocument
Constructs a styled document.
Parameters:
c
- the container for the content
styles
- resources and style definitions which may be shared across documentsDefaultStyledDocument
public DefaultStyledDocument(StyleContext styles)
Constructs a styled document with the default content storage implementation and a shared set of styles.
Parameters:
styles
- the stylesDefaultStyledDocument
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.Method Details
getDefaultRootElement
public Element getDefaultRootElement()
Gets the default root element.
Specified by:
[getDefaultRootElement](Document.html#getDefaultRootElement%28%29)
in interface[Document](Document.html "interface in javax.swing.text")
Specified by:
[getDefaultRootElement](AbstractDocument.html#getDefaultRootElement%28%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Returns:
the root
See Also:
Document.getDefaultRootElement()create
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.
Parameters:
data
- the element datainsert
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 seeConcurrency in Swing for more information.
Parameters:
offset
- the starting offset >= 0
data
- the element data
Throws:
[BadLocationException](BadLocationException.html "class in javax.swing.text")
- for an invalid starting offsetremoveElement
public 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, theDocumentEvent
for changes in element structure andUndoableEditEvent
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 isnull,
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](../../../../java.base/java/lang/NullPointerException.html "class in java.lang")
- if the element isnull
[IllegalArgumentException](../../../../java.base/java/lang/IllegalArgumentException.html "class in java.lang")
- if the element could not be removed
[IllegalStateException](../../../../java.base/java/lang/IllegalStateException.html "class in java.lang")
- if the element structure is invalid
Since:
1.7addStyle
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](StyledDocument.html#addStyle%28java.lang.String,javax.swing.text.Style%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
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 styleremoveStyle
public void removeStyle(String nm)
Removes a named style previously added to the document.
Specified by:
[removeStyle](StyledDocument.html#removeStyle%28java.lang.String%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
nm
- the name of the style to removegetStyle
Fetches a named style previously added.
Specified by:
[getStyle](StyledDocument.html#getStyle%28java.lang.String%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
nm
- the name of the style
Returns:
the stylegetStyleNames
Fetches the list of style names.
Returns:
all the style namessetLogicalStyle
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 seeConcurrency in Swing for more information.
Specified by:
[setLogicalStyle](StyledDocument.html#setLogicalStyle%28int,javax.swing.text.Style%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
pos
- the offset from the start of the document >= 0
s
- the logical style to assign to the paragraph, null if nonegetLogicalStyle
public Style getLogicalStyle(int p)
Fetches the logical style assigned to the paragraph represented by the given position.
Specified by:
[getLogicalStyle](StyledDocument.html#getLogicalStyle%28int%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
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 nonesetCharacterAttributes
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 seeConcurrency in Swing for more information.
Specified by:
[setCharacterAttributes](StyledDocument.html#setCharacterAttributes%28int,int,javax.swing.text.AttributeSet,boolean%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
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 attributessetParagraphAttributes
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 seeConcurrency in Swing for more information.
Specified by:
[setParagraphAttributes](StyledDocument.html#setParagraphAttributes%28int,int,javax.swing.text.AttributeSet,boolean%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
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 themgetParagraphElement
public Element getParagraphElement(int pos)
Gets the paragraph element at the offsetpos
. A paragraph consists of at least one child Element, which is usually a leaf.
Specified by:
[getParagraphElement](StyledDocument.html#getParagraphElement%28int%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Specified by:
[getParagraphElement](AbstractDocument.html#getParagraphElement%28int%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Parameters:
pos
- the starting offset >= 0
Returns:
the elementgetCharacterElement
public Element getCharacterElement(int pos)
Gets a character element based on a position.
Specified by:
[getCharacterElement](StyledDocument.html#getCharacterElement%28int%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
pos
- the position in the document >= 0
Returns:
the elementinsertUpdate
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](AbstractDocument.html#insertUpdate%28javax.swing.text.AbstractDocument.DefaultDocumentEvent,javax.swing.text.AttributeSet%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Parameters:
chng
- a description of the document change
attr
- the attributesremoveUpdate
Updates document structure as a result of text removal.
Overrides:
[removeUpdate](AbstractDocument.html#removeUpdate%28javax.swing.text.AbstractDocument.DefaultDocumentEvent%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Parameters:
chng
- a description of the document changecreateDefaultRoot
Creates the root element to be used to represent the default document structure.
Returns:
the element basegetForeground
Gets the foreground color from an attribute set.
Specified by:
[getForeground](StyledDocument.html#getForeground%28javax.swing.text.AttributeSet%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
attr
- the attribute set
Returns:
the colorgetBackground
Gets the background color from an attribute set.
Specified by:
[getBackground](StyledDocument.html#getBackground%28javax.swing.text.AttributeSet%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
attr
- the attribute set
Returns:
the colorgetFont
Gets the font from an attribute set.
Specified by:
[getFont](StyledDocument.html#getFont%28javax.swing.text.AttributeSet%29)
in interface[StyledDocument](StyledDocument.html "interface in javax.swing.text")
Parameters:
attr
- the attribute set
Returns:
the fontstyleChanged
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
Adds a document listener for notification of any changes.
Specified by:
[addDocumentListener](Document.html#addDocumentListener%28javax.swing.event.DocumentListener%29)
in interface[Document](Document.html "interface in javax.swing.text")
Overrides:
[addDocumentListener](AbstractDocument.html#addDocumentListener%28javax.swing.event.DocumentListener%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Parameters:
listener
- the listener
See Also:
Document.addDocumentListener(javax.swing.event.DocumentListener)removeDocumentListener
Removes a document listener.
Specified by:
[removeDocumentListener](Document.html#removeDocumentListener%28javax.swing.event.DocumentListener%29)
in interface[Document](Document.html "interface in javax.swing.text")
Overrides:
[removeDocumentListener](AbstractDocument.html#removeDocumentListener%28javax.swing.event.DocumentListener%29)
in class[AbstractDocument](AbstractDocument.html "class in javax.swing.text")
Parameters:
listener
- the listener
See Also:
Document.removeDocumentListener(javax.swing.event.DocumentListener)