| 1 |  |  /* | 
 
   | 2 |  |   * Angkor Web Framework | 
 
   | 3 |  |   * | 
 
   | 4 |  |   * Distributable under LGPL license. | 
 
   | 5 |  |   * See terms of license at gnu.org. | 
 
   | 6 |  |   */ | 
 
   | 7 |  |   | 
 
   | 8 |  |  package com.tirsen.angkor.event; | 
 
   | 9 |  |   | 
 
   | 10 |  |  import com.tirsen.angkor.Debug; | 
 
   | 11 |  |  import com.tirsen.angkor.Multicaster; | 
 
   | 12 |  |  import com.tirsen.angkor.RenderContext; | 
 
   | 13 |  |  import org.apache.log4j.Category; | 
 
   | 14 |  |   | 
 
   | 15 |  |  import java.io.Serializable; | 
 
   | 16 |  |   | 
 
   | 17 |  |  /** | 
 
   | 18 |  |   * Utility class for sending action events. | 
 
   | 19 |  |   * | 
 
   | 20 |  |   * <!-- $Id: ActionSourceHelper.java,v 1.3 2002/10/09 21:37:37 tirsen Exp $ --> | 
 
   | 21 |  |   * | 
 
   | 22 |  |   * @author $Author: tirsen $ | 
 
   | 23 |  |   * @version $Revision: 1.3 $ | 
 
   | 24 |  |   */ | 
 
   | 25 |  |  public class ActionSourceHelper implements ActionSource, Serializable | 
 
   | 26 |  |  { | 
 
   | 27 |  |      private static final Category logger = Category.getInstance(Debug.LOGGER_NAME); | 
 
   | 28 |  |      private Action action; | 
 
   | 29 |  |      private Object source; | 
 
   | 30 |  |      private ActionListener listeners; | 
 
   | 31 |  |   | 
 
   | 32 | 2 |      public ActionSourceHelper(Object source) | 
 
   | 33 |  |      { | 
 
   | 34 | 2 |          this.source = source; | 
 
   | 35 |  |      } | 
 
   | 36 |  |   | 
 
   | 37 | 14 |      public Action getAction() | 
 
   | 38 |  |      { | 
 
   | 39 | 14 |          return action; | 
 
   | 40 |  |      } | 
 
   | 41 |  |   | 
 
   | 42 | 2 |      public void setAction(Action action) | 
 
   | 43 |  |      { | 
 
   | 44 | 2 |          this.action = action; | 
 
   | 45 |  |      } | 
 
   | 46 |  |   | 
 
   | 47 | 0 |      public void addActionListener(ActionListener listener) | 
 
   | 48 |  |      { | 
 
   | 49 | 0 |          if (listeners == null) listeners = (ActionListener) Multicaster.create(ActionListener.class, true); | 
 
   | 50 | 0 |          Multicaster.add(listeners, listener); | 
 
   | 51 |  |      } | 
 
   | 52 |  |   | 
 
   | 53 | 0 |      public void removeActionListener(ActionListener listener) | 
 
   | 54 |  |      { | 
 
   | 55 | 0 |          Multicaster.remove(listeners, listener); | 
 
   | 56 | 0 |          if (Multicaster.isEmpty(listeners)) listeners = null; | 
 
   | 57 |  |      } | 
 
   | 58 |  |   | 
 
   | 59 | 1 |      public void signalActionEvent(RenderContext context) | 
 
   | 60 |  |      { | 
 
   | 61 | 1 |          if (hasListeners()) | 
 
   | 62 |  |          { | 
 
   | 63 | 1 |              ActionEvent evt = new ActionEvent(source); | 
 
   | 64 | 1 |              if (action != null) | 
 
   | 65 |  |              { | 
 
   | 66 | 1 |                  logger.info("invoking action " + action); | 
 
   | 67 | 1 |                  context.getEventQueue().postEvent(ActionListener.class, action, evt, "actionPerformed"); | 
 
   | 68 |  |              } | 
 
   | 69 | 0 |              if (!Multicaster.isEmpty(listeners)) context.getEventQueue().postEvent(ActionListener.class, listeners, evt, "actionPerformed"); | 
 
   | 70 |  |          } | 
 
   | 71 |  |      } | 
 
   | 72 |  |   | 
 
   | 73 | 0 |      public void signalActionEvent() | 
 
   | 74 |  |      { | 
 
   | 75 | 0 |          if (hasListeners()) | 
 
   | 76 |  |          { | 
 
   | 77 | 0 |              signalActionEvent(RenderContext.getRenderContext()); | 
 
   | 78 |  |          } | 
 
   | 79 |  |      } | 
 
   | 80 |  |   | 
 
   | 81 | 1 |      public boolean hasListeners() | 
 
   | 82 |  |      { | 
 
   | 83 | 1 |          return !Multicaster.isEmpty(listeners) || action != null; | 
 
   | 84 |  |      } | 
 
   | 85 |  |  } | 
 
   | 86 |  |   |