public final class DocumentUtilities extends Object
Modifier and Type | Method and Description |
---|---|
static void |
addDocumentListener(Document doc,
DocumentListener listener,
DocumentListenerPriority priority)
Add document listener to document with given priority
or default to using regular
Document.addDocumentListener(DocumentListener)
if the given document is not listener priority aware. |
static void |
addEventPropertyStorage(DocumentEvent evt)
Document provider should call this method to allow for document event
properties being stored in document events.
|
static boolean |
addPriorityDocumentListener(Document doc,
DocumentListener listener,
DocumentListenerPriority priority)
Suitable for document implementations - adds document listener
to document with given priority and does not do anything
if the given document is not listener priority aware.
|
static void |
addPropertyChangeListener(Document doc,
PropertyChangeListener l)
Adds
PropertyChangeListener to a document. |
static PropertyChangeListener |
addWeakPropertyChangeListener(Document doc,
PropertyChangeListener listenerImplementation)
Adds a weak
PropertyChangeListener to a document. |
static StringBuilder |
appendEvent(StringBuilder sb,
DocumentEvent evt)
Get string representation of an offset for debugging purposes
in form "offset[line:column]".
|
static StringBuilder |
appendOffset(StringBuilder sb,
Document doc,
int offset)
Get string representation of an offset for debugging purposes
in form "offset[line:column]".
|
static String |
debugOffset(Document doc,
int offset)
Get string representation of an offset for debugging purposes
in form "offset[line:column]".
|
static int |
fixOffset(int offset,
DocumentEvent evt)
Fix the given offset according to the performed modification.
|
static int |
getDocumentListenerCount(Document doc)
Get total count of document listeners attached to a particular document
(useful e.g.
|
static long |
getDocumentTimestamp(Document doc)
Attempts to get the timestamp of a
Document . |
static long |
getDocumentVersion(Document doc)
Attempts to get the version of a
Document . |
static Object |
getEventProperty(DocumentEvent evt,
Object key)
Get a property of a given document event.
|
static String |
getMimeType(Document doc)
Gets the mime type of a document.
|
static String |
getMimeType(JTextComponent component)
Gets the mime type of a document in
JTextComponent . |
static String |
getModificationText(DocumentEvent evt)
Get text of the given document modification.
|
static Element |
getParagraphElement(Document doc,
int offset)
Get the paragraph element for the given document.
|
static Element |
getParagraphRootElement(Document doc)
Get the root of the paragraph elements for the given document.
|
static CharSequence |
getText(Document doc)
Get text of the given document as char sequence.
|
static CharSequence |
getText(Document doc,
int offset,
int length)
Get a portion of text of the given document as char sequence.
|
static DocumentListener |
initPriorityListening(Document doc)
This method should be used by swing document implementations that
want to support document listeners prioritization.
|
static boolean |
isReadLocked(Document doc)
Check whether the given document is read-locked by at least one thread
or whether it was write-locked by the current thread (write-locking
grants the read-access automatically).
|
static boolean |
isTypingModification(Document doc)
This method should be used by document listeners to check whether
the just performed document modification was caused by user's typing.
|
static boolean |
isTypingModification(DocumentEvent evt)
Deprecated.
|
static boolean |
isWriteLocked(Document doc)
Check whether the given document is write-locked by the current thread.
|
static void |
putEventProperty(DocumentEvent evt,
Map.Entry mapEntry)
Set a property of a given document event by using the given map entry.
|
static void |
putEventProperty(DocumentEvent evt,
Object key,
Object value)
Set a property of a given document event.
|
static boolean |
putEventPropertyIfSupported(DocumentEvent evt,
Object key,
Object value)
Sets a property for the document event, if the event supports the feature.
|
static void |
removeDocumentListener(Document doc,
DocumentListener listener,
DocumentListenerPriority priority)
Remove document listener that was previously added to the document
with given priority or use default
Document.removeDocumentListener(DocumentListener)
if the given document is not listener priority aware. |
static boolean |
removePriorityDocumentListener(Document doc,
DocumentListener listener,
DocumentListenerPriority priority)
Suitable for document implementations - removes document listener
from document with given priority and does not do anything
if the given document is not listener priority aware.
|
static void |
removePropertyChangeListener(Document doc,
PropertyChangeListener l)
Removes
PropertyChangeListener from a document. |
static void |
setTypingModification(Document doc,
boolean typingModification)
Mark that the ongoing document modification(s) will be caused
by user's typing.
|
public static void addDocumentListener(Document doc, DocumentListener listener, DocumentListenerPriority priority)
Document.addDocumentListener(DocumentListener)
if the given document is not listener priority aware.doc
- document to which the listener should be added.listener
- document listener to add.priority
- priority with which the listener should be added.
If the document does not support document listeners ordering
then the listener is added in a regular way by using
Document.addDocumentListener(
javax.swing.event.DocumentListener)
method.public static boolean addPriorityDocumentListener(Document doc, DocumentListener listener, DocumentListenerPriority priority)
doc
- document to which the listener should be added.listener
- document listener to add.priority
- priority with which the listener should be added.public static void removeDocumentListener(Document doc, DocumentListener listener, DocumentListenerPriority priority)
Document.removeDocumentListener(DocumentListener)
if the given document is not listener priority aware.doc
- document from which the listener should be removed.listener
- document listener to remove.priority
- priority with which the listener should be removed.
It should correspond to the priority with which the listener
was added originally.public static boolean removePriorityDocumentListener(Document doc, DocumentListener listener, DocumentListenerPriority priority)
doc
- document from which the listener should be removed.listener
- document listener to remove.priority
- priority with which the listener should be removed.public static DocumentListener initPriorityListening(Document doc)
class MyDocument extends AbstractDocument { MyDocument() { super.addDocumentListener(DocumentUtilities.initPriorityListening(this)); } public void addDocumentListener(DocumentListener listener) { if (!DocumentUtilities.addDocumentListener(this, listener, DocumentListenerPriority.DEFAULT)) super.addDocumentListener(listener); } public void removeDocumentListener(DocumentListener listener) { if (!DocumentUtilities.removeDocumentListener(this, listener, DocumentListenerPriority.DEFAULT)) super.removeDocumentListener(listener); } }
doc
- document to be initialized.super.addDocumentListener()
in document's constructor.IllegalStateException
- when the document already has
the property initialized.public static int getDocumentListenerCount(Document doc)
AbstractDocument
the method
returns zero.doc
- non-null document.public static void setTypingModification(Document doc, boolean typingModification)
DocumentUtilities.isTypingModification(Document)
.
This method should always be used in the following pattern:
DocumentUtilities.setTypingModification(doc, true); try { doc.insertString(offset, typedText, null); } finally { DocumentUtilities.setTypingModification(doc, false); }
public static boolean isTypingModification(Document doc)
@Deprecated public static boolean isTypingModification(DocumentEvent evt)
public static CharSequence getText(Document doc)
doc
- document for which the charsequence is being obtained.doc.getLength() + 1
where the extra character is '\n'
(it corresponds to AbstractDocument-based document implementations).
public static CharSequence getText(Document doc, int offset, int length) throws BadLocationException
doc
- document for which the charsequence is being obtained.offset
- starting offset of the charsequence to obtain.length
- length of the charsequence to obtain. It must be >= 0
and <doc.getLength() + 1
.BadLocationException
- some portion of the given range
was not a valid part of the document. The location in the exception
is the first bad position encountered.
public static void addEventPropertyStorage(DocumentEvent evt)
evt
- document event to which the storage should be added.
It must be an undoable edit allowing to add an edit.public static Object getEventProperty(DocumentEvent evt, Object key)
evt
- non-null document event from which the property should be retrieved.key
- non-null key of the property.public static boolean putEventPropertyIfSupported(DocumentEvent evt, Object key, Object value)
evt
- the event to modifykey
- property keyvalue
- property valuepublic static void putEventProperty(DocumentEvent evt, Object key, Object value)
evt
- non-null document event to which the property should be stored.key
- non-null key of the property.value
- for the given property.public static void putEventProperty(DocumentEvent evt, Map.Entry mapEntry)
CompactMap.MapEntry
. Other map entry implementations
will be delegated to DocumentUtilities.putEventProperty(DocumentEvent, Object, Object)
.evt
- non-null document event to which the property should be stored.mapEntry
- non-null map entry which should be stored.
Generally after this method finishes the DocumentUtilities.getEventProperty(DocumentEvent, Object)
will return mapEntry.getValue()
for mapEntry.getKey()
key.public static int fixOffset(int offset, DocumentEvent evt)
offset
- >=0 offset in a document.evt
- document event describing change in the document.public static String getModificationText(DocumentEvent evt)
String.class
.evt
- document event describing either document insertion or removal
(change event type events will produce null result).public static boolean isReadLocked(Document doc)
AbstractDocument
based documents and it uses reflection.
doc
- non-null document instance.public static boolean isWriteLocked(Document doc)
AbstractDocument
based documents and it uses reflection.doc
- non-null document instance.public static Element getParagraphElement(Document doc, int offset)
doc
- non-null document instance.offset
- offset in the document >=0public static Element getParagraphRootElement(Document doc)
doc
- non-null document instance.public static String debugOffset(Document doc, int offset)
doc
- non-null document in which the offset is located.offset
- offset in the document.public static StringBuilder appendOffset(StringBuilder sb, Document doc, int offset)
sb
- valid string builder to which text will be appended or null in which case
the method itself will create a string builder and it will return it.doc
- non-null document in which the offset is located.offset
- offset in the document.public static StringBuilder appendEvent(StringBuilder sb, DocumentEvent evt)
sb
- valid string builder to which text will be appended or null in which case
the method itself will create a string builder and it will return it.evt
- non-null document event.public static String getMimeType(Document doc)
null
. This method should work reliably
for Netbeans documents that have their mime type stored in a special
property. For any other documents it will probably just return null
.doc
- The document to get the mime type for.null
.public static String getMimeType(JTextComponent component)
JTextComponent
. If
the mime type can't be determined this method will return null
.
It tries to determine the document's mime type first and if that does not
work it uses mime type from the EditorKit
attached to the
component.component
- The component to get the mime type for.null
.public static long getDocumentVersion(Document doc)
Document
. Netbeans editor
documents are versioned, which means that every time a document is modified
its version is incremented. This method can be used to read the latest version
of a netbeans document.doc
- The document to get a version for.0
if the document does not
support versioning (ie. is not a netbeans editor document).public static long getDocumentTimestamp(Document doc)
Document
. Netbeans editor
documents are versioned and timestamped whenever they are modified.
This method can be used to read the timestamp of the most recent modification.
The timestamp is a number of milliseconds returned from System.currentTimeMillis()
at the document modification.doc
- The document to get the timestamp for.0
if the document does not
support timestamps (ie. is not a netbeans editor document).public static void addPropertyChangeListener(Document doc, PropertyChangeListener l)
PropertyChangeListener
to a document.
In general, document properties are key-value pairs where both the key
and the value can be any Object
. Contrary to that PropertyChangeListener
s
can only handle named properties that can have an arbitrary value, but have String
names.
Therefore the listenera attached to a document will only ever recieve document
properties, which keys are of java.lang.String
type.
Additionally, the list of document properties that clients can listen on is not part of this contract.
Note that this method does not work with WeakListeners
.
Use DocumentUtilities.addWeakPropertyChangeListener(javax.swing.text.Document, java.beans.PropertyChangeListener)
for that purpose.
doc
- The document to add the listener to.l
- The listener to add to the document.public static void removePropertyChangeListener(Document doc, PropertyChangeListener l)
PropertyChangeListener
from a document.doc
- The document to remove the listener from.l
- The listener to remove from the document.public static PropertyChangeListener addWeakPropertyChangeListener(Document doc, PropertyChangeListener listenerImplementation)
PropertyChangeListener
to a document.
In general, document properties are key-value pairs where both the key
and the value can be any Object
. Contrary to that PropertyChangeListener
s
can only handle named properties that can have an arbitrary value, but have String
names.
Therefore the listenera attached to a document will only ever recieve document
properties, which keys are of java.lang.String
type.
Additionally, the list of document properties that clients can listen on is not part of this contract.
doc
- The document to add the listener to.listenerImplementation
- The listener to be added weakly to the document.DocumentUtilities.removePropertyChangeListener(javax.swing.text.Document, java.beans.PropertyChangeListener)
. If the document does not
support PropertyChangeLister
null
is returned.