public class TextDocListener
extends java.lang.Object
implements javax.swing.event.DocumentListener
hasChanged()
that returns true if a change event occurred any time after
a call to reset()
. These calls can be combined in a synchronous way using resetIfChanged()
.
Clients can synchronize on this to prevent prevent its state from changing in-between calls to hasChanged() and reset():
private final TextDocListener docListener = new TextDocListener();
private function doActionIfDocChanged() {
boolean changed;
synchronized(docListener) {
changed = docListener.hasChanged();
if (changed)
docListener.reset();
}
if (changed) { ... }
}
Alternatively, a client can use the resetIfChanged()
function to both get the state and reset it in a synchronized way:
private function doActionIfDocChanged() {
if (docListener.resetIfChanged()) { ... }
}
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
changed |
(package private) int |
count |
(package private) java.lang.Object[] |
listeners |
(package private) int |
suspendCount |
Constructor and Description |
---|
TextDocListener() |
TextDocListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener) |
TextDocListener(java.lang.Runnable listener) |
Modifier and Type | Method and Description |
---|---|
private void |
add(java.lang.Object o) |
void |
addListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener)
Add a method that will receive DocumentEvents generated by the Documents this class is listening to.
|
void |
addListener(java.lang.Runnable listener)
Add a method that will be called whenever the the Documents this class is listening to generate DocumentEvents.
|
void |
changedUpdate(javax.swing.event.DocumentEvent e) |
boolean |
hasChanged() |
void |
insertUpdate(javax.swing.event.DocumentEvent e) |
void |
listen(javax.swing.text.JTextComponent txt)
Add this as a DocumentListener to a JTextComponent's Document.
|
void |
markChanged() |
private void |
notifyListeners(javax.swing.event.DocumentEvent e) |
private boolean |
removeListener(java.lang.Object listener) |
void |
removeUpdate(javax.swing.event.DocumentEvent e) |
void |
reset() |
boolean |
resetIfChanged()
Sets the
hasChanged() state to false and returns the previous value before the reset. |
void |
resumeUpdates() |
void |
suspendUpdates() |
private void |
update(javax.swing.event.DocumentEvent e) |
boolean changed
int count
java.lang.Object[] listeners
int suspendCount
public TextDocListener()
public TextDocListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener)
public TextDocListener(java.lang.Runnable listener)
private void add(java.lang.Object o)
public void addListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener)
listener
- a DocumentEvent Consumer that is called when DocumentEvents are generated.public void addListener(java.lang.Runnable listener)
listener
- a Runnable that is called when DocumentEvents are generated.public void changedUpdate(javax.swing.event.DocumentEvent e)
changedUpdate
in interface javax.swing.event.DocumentListener
public boolean hasChanged()
public void insertUpdate(javax.swing.event.DocumentEvent e)
insertUpdate
in interface javax.swing.event.DocumentListener
public void listen(javax.swing.text.JTextComponent txt)
public void markChanged()
private void notifyListeners(javax.swing.event.DocumentEvent e)
private boolean removeListener(java.lang.Object listener)
public void removeUpdate(javax.swing.event.DocumentEvent e)
removeUpdate
in interface javax.swing.event.DocumentListener
public void reset()
public boolean resetIfChanged()
hasChanged()
state to false and returns the previous value before the reset.
The change is performed atomically (synchronized) and allows a client to both get the changed state
and reset it without the possiblity of state being changed by another thread in between calls to
hasChanged()
and reset()
.public void resumeUpdates()
public void suspendUpdates()
private void update(javax.swing.event.DocumentEvent e)