#
# $Id: listener.icn,v 1.2 2004/06/27 16:19:05 rparlett Exp $
#
# This file is in the public domain.
#
# Author: Robert Parlett (parlett@dial.pipex.com)
#

package util

#
# A class extended by a listener which is added
# to a ListenerList.
#
class Listener(obj,
               meth,
               type)

   #
   # Return the object which is listening
   #
   method get_object()
      return obj
   end

   #
   # Return the method in the object which is invoked on an
   # event.
   #
   method get_method()
      return meth
   end

   #
   # The type for which we are listening, or &null implying
   # we are listening for all types.
   #
   method get_type()
      return type
   end

   #
   # Fire an event to the method, if it matches.
   #
   method maybe_fire(event)
      if /type | type === event.get_type() then
         meth(obj, event)
   end

   # Look for the method in the given object, stopping on error.
   # @p
   method find_method(obj, method_name)
      local t, m
      t := lang::get_class(obj) | stop("no class found for ", image(obj))
      every m := !t.get_methods() do {
          if m.get_method_name() == method_name then
             return m.get_as_procedure()
      }
      stop("no such method ", method_name)
   end

   initially(obj, method_name, type) 
      self.obj := obj
      self.meth := find_method(obj, method_name)
      self.type := type
end