ur_rna.Utilities.swing

## Class TextDocListener

- java.lang.Object
- - ur_rna.Utilities.swing.TextDocListener

- All Implemented Interfaces:
    java.util.EventListener, javax.swing.event.DocumentListener

      public class TextDocListener
      extends java.lang.Object
      implements javax.swing.event.DocumentListener

  Simplifies management of JTextComponent text-change event handling. Usually to be notified of text-change events, one must register a DocumentListener, which has 3 methods. To complicate matters, a Document may fire events on threads other than the UI thread. This class simplifies the situation by combining all three methods into a single one that fires an event that is guaranteed to be on the UI thread. It also has a boolean `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()) { ... } }`

- - ### Field Summary

    Fields
    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 Summary

    Constructors
    Constructor and Description
    `TextDocListener()`
    `TextDocListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener)`
    `TextDocListener(java.lang.Runnable listener)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    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)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### changed

          boolean changed

    - #### count

          int count

    - #### listeners

          java.lang.Object[] listeners

    - #### suspendCount

          int suspendCount

  - ### Constructor Detail

    - #### TextDocListener

          public TextDocListener()

    - #### TextDocListener

          public TextDocListener(java.util.function.Consumer<javax.swing.event.DocumentEvent> listener)

    - #### TextDocListener

          public TextDocListener(java.lang.Runnable listener)

  - ### Method Detail

    - #### add

          private void add(java.lang.Object o)

    - #### addListener

          public 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.
      Parameters:
        `listener` - a DocumentEvent Consumer that is called when DocumentEvents are generated.

    - #### addListener

          public void addListener(java.lang.Runnable listener)

      Add a method that will be called whenever the the Documents this class is listening to generate DocumentEvents.
      Parameters:
        `listener` - a Runnable that is called when DocumentEvents are generated.

    - #### changedUpdate

          public void changedUpdate(javax.swing.event.DocumentEvent e)

      Specified by:
        `changedUpdate` in interface `javax.swing.event.DocumentListener`

    - #### hasChanged

          public boolean hasChanged()

    - #### insertUpdate

          public void insertUpdate(javax.swing.event.DocumentEvent e)

      Specified by:
        `insertUpdate` in interface `javax.swing.event.DocumentListener`

    - #### listen

          public void listen(javax.swing.text.JTextComponent txt)

      Add this as a DocumentListener to a JTextComponent's Document.

    - #### markChanged

          public void markChanged()

    - #### notifyListeners

          private void notifyListeners(javax.swing.event.DocumentEvent e)

    - #### removeListener

          private boolean removeListener(java.lang.Object listener)

    - #### removeUpdate

          public void removeUpdate(javax.swing.event.DocumentEvent e)

      Specified by:
        `removeUpdate` in interface `javax.swing.event.DocumentListener`

    - #### reset

          public void reset()

    - #### resetIfChanged

          public boolean resetIfChanged()

      Sets the `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()`.

    - #### resumeUpdates

          public void resumeUpdates()

    - #### suspendUpdates

          public void suspendUpdates()

    - #### update

          private void update(javax.swing.event.DocumentEvent e)