java.lang.Object
org.bzdev.swing.text.SafeDocument
org.bzdev.swing.text.SafeStyledDocument
- All Implemented Interfaces:
Document
,StyledDocument
A StyledDocument class that is thread safe.
Operations are performed on the Swing event-dispatching thread.
An existing document is encapsulated, with access to it controlled
so as to be thread safe.
This class makes use of the static method
SwingUtilities.invokeLater(Runnable)
. One should avoid the
use of synchronized methods that call methods in this class when
those synchronized methods might be called from tasks waiting on
the AWT event dispatch queue, as there is a possibility of
deadlock: If for some class methods m1 and m2 are synchronized and
call one of the methods in this class, and m1 is called, a call to
SwingUtilities.invokeLater(Runnable)
may process other
entries on its event queue first, causing m2 to be called, but m2
will wait until m1 returns, which cannot occur until m2 returns.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.swing.text.SafeDocument
SafeDocument.RIException, SafeDocument.RTException, SafeDocument.RunnableWithBadLocException
-
Field Summary
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a new style into the logical style hierarchy.protected Style
doitStyle
(org.bzdev.swing.text.SafeStyledDocument.RunnableWithStyle r) Execute code on the event dispatch thread and return a Style.getBackground
(AttributeSet attr) Takes a set of attributes and turn it into a background color specification.getCharacterElement
(int pos) Gets the element that represents the character that is at the given offset within the document.getFont
(AttributeSet attr) Takes a set of attributes and turn it into a font specification.getForeground
(AttributeSet attr) Takes a set of attributes and turn it into a foreground color specification.getLogicalStyle
(int p) Gets a logical style for a given position in a paragraph.getParagraphElement
(int pos) Gets the element that represents the paragraph that encloses the given offset within the document.Fetches a named style previously added.void
removeStyle
(String nm) Removes a named style previously added to the document.void
setCharacterAttributes
(int offset, int length, AttributeSet attr, boolean replace) Changes the content element attributes used for the given range of existing content in 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 attr, boolean replace) Sets paragraph attributes.Methods inherited from class org.bzdev.swing.text.SafeDocument
addDocumentListener, addUndoableEditListener, createPosition, doitElement, doitElements, doitInt, doitObject, doitPosition, doitPositionBLE, doitStringBLE, doitVoid, doitVoidBLE, getDefaultRootElement, getEncapsulatedDocument, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.swing.text.Document
addDocumentListener, addUndoableEditListener, createPosition, getDefaultRootElement, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
-
Constructor Details
-
SafeStyledDocument
Constructor.- Parameters:
document
- the document to encapsulate
-
-
Method Details
-
doitStyle
Execute code on the event dispatch thread and return a Style.- Parameters:
r
- the runable to execute- Returns:
- the style produced by running
r
-
addStyle
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
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
Removes a named style previously added to the document.- Specified by:
removeStyle
in interfaceStyledDocument
- Parameters:
nm
- the name of the style to remove
-
getStyle
Fetches a named style previously added.- Specified by:
getStyle
in interfaceStyledDocument
- Parameters:
nm
- the name of the style- Returns:
- the style
-
setCharacterAttributes
Changes the content element attributes used for the given range of existing content in the document. All of the attributes defined in the given Attributes argument are applied to the given range. This method can be used to completely remove all content level attributes for the given range by giving an Attributes argument that has no attributes defined and setting replace to true.- Specified by:
setCharacterAttributes
in interfaceStyledDocument
- Parameters:
offset
- the start of the change >= 0length
- the length of the change >= 0attr
- the non-null attributes to change to. Any attributes defined will be applied to the text for the given range.replace
- indicates whether or not the previous attributes should be cleared before the new attributes as set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.
-
setParagraphAttributes
Sets paragraph attributes.- Specified by:
setParagraphAttributes
in interfaceStyledDocument
- Parameters:
offset
- the start of the change >= 0length
- the length of the change >= 0attr
- the non-null attributes to change to. Any attributes defined will be applied to the text for the given range.replace
- indicates whether or not the previous attributes should be cleared before the new attributes are set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.
-
setLogicalStyle
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.- Specified by:
setLogicalStyle
in interfaceStyledDocument
- Parameters:
pos
- the starting position >= 0s
- the style to set
-
getLogicalStyle
Gets a logical style for a given position in a paragraph.- Specified by:
getLogicalStyle
in interfaceStyledDocument
- Parameters:
p
- the position >= 0- Returns:
- the style
-
getParagraphElement
Gets the element that represents the paragraph that encloses the given offset within the document.- Specified by:
getParagraphElement
in interfaceStyledDocument
- Parameters:
pos
- the offset >= 0- Returns:
- the element
-
getCharacterElement
Gets the element that represents the character that is at the given offset within the document.- Specified by:
getCharacterElement
in interfaceStyledDocument
- Parameters:
pos
- the offset >= 0- Returns:
- the element
-
getForeground
Takes a set of attributes and turn it into a foreground color specification. This might be used to specify things like brighter, more hue, etc.- Specified by:
getForeground
in interfaceStyledDocument
- Parameters:
attr
- the set of attributes- Returns:
- the color
-
getBackground
Takes a set of attributes and turn it into a background color specification. This might be used to specify things like brighter, more hue, etc.- Specified by:
getBackground
in interfaceStyledDocument
- Parameters:
attr
- the set of attributes- Returns:
- the color
-
getFont
Takes a set of attributes and turn it into a font specification. This can be used to turn things like family, style, size, etc into a font that is available on the system the document is currently being used on.- Specified by:
getFont
in interfaceStyledDocument
- Parameters:
attr
- the set of attributes- Returns:
- the font
-