Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-580

Add Event Support Utilities

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 3.0
    • 3.0
    • General
    • None
    • Java SE 5.0+

    Description

      I would like to propose some support be added to Lang for basic event handling. This would be based on the way that PropertyChangeSupport can be used to add and remove listeners and post events.

      Add interface EventSupport<L extends EventListener>

      addListener(L listener)
      The signature for the method that can add a listener of some subtype of EventListener

      removeListener(L listener)
      The signature for the method that can remove a listener of some subtype of EventListener

      Add class AbstractEventSupport implements EventSupport<L>, Iterable<L>

      AbstractEventSupport(Object eventSource)
      Constructs a new AbstractEventSupport object and associates it with the object that will be used as the source of all events (much like PropertyChangeSupport).

      addListener(L)
      An implementation that adds a listener to an internal collection.

      removeListener(L)
      An implementation that removes a listener from an internal collection.

      iterator()
      Returns an iterator over the attached listeners.

      getSource()
      Returns a reference to the source object of all events.

      The best way to describe this would be to demonstrate an example of how it can be used.

      public class ButtonPressedEventSupport extends AbstractEventSupport<ButtonPressedListener> {

      public ButtonPressedEventSupport(Object source)

      { super(source); }

      public void fireButtonPressed(Button button) {
      ButtonPressedEvent bpe = new ButtonPressedEvent(getSource(), button);
      for (ButtonPressedListener listener : this)

      { listener.buttonPressed(bpe); }

      }
      }

      public class MyWindow implements EventSupport<ButtonPressedListener> {
      private final ButtonPressedEventSupport buttonPressedEventSupport;

      public MyWindow

      { buttonPressedEventSupport = new ButtonPressedEventSupport(this); }

      public void addListener(ButtonPressedListener listener)

      { buttonPressedEventSupport.addListener(listener); }

      public void removeListener(ButtonPressedListener listener)

      { buttonPressedEventSupport.removeListener(listener); }

      ...

      private void onDetectButtonPressed(Button button)

      { buttonPressedEventSupport.fireButtonPressed(button); }

      }

      I haven't compiled the above code. It's just an example of how these classes could be used so that you're not constantly rewriting the code and interfaces for adding and removing listeners, and it provides a fairly easy method of creating methods to fire events.

      Attachments

        1. commons-lang-event-support-docs.patch
          8 kB
          Michael Wooten
        2. commons-lang-events-merged.patch
          23 kB
          Michael Wooten
        3. commons-lang-events-package-html.txt
          2 kB
          Michael Wooten
        4. commons-lang-event-support.patch
          32 kB
          Michael Wooten

        Activity

          People

            jwcarman James Carman
            mwooten.dev Michael Wooten
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 96h
                96h
                Remaining:
                Remaining Estimate - 96h
                96h
                Logged:
                Time Spent - Not Specified
                Not Specified