001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ActionSupport.java,v $
023   Revision 1.5  2004/05/05 22:16:57  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:34:08  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 01:38:45  markl
030   Source code cleanup.
031
032   Revision 1.2  1999/01/10 03:26:20  markl
033   added GPL header & RCS tag
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.event;
038
039import java.awt.event.*;
040import javax.swing.event.*;
041
042/** A support object for generating <code>ActionEvent</code>s.
043  *
044  * @see java.awt.event.ActionEvent
045  * @see java.awt.event.ActionListener
046  *
047  * @author Mark Lindner
048  */
049
050public class ActionSupport
051  {
052  private EventListenerList listeners;
053  private Object source;
054
055  /** Construct a new <code>ActionSupport</code> object.
056    *
057    * @param source The owner of this object (and the source of the events that
058    * will be generated by it).
059    */  
060  
061  public ActionSupport(Object source)
062    {
063    this.source = source;
064    listeners = new EventListenerList();
065    }
066
067  /** Add an <code>ActionListener</code> to this object's list of listeners.
068    *
069    * @param listener The listener to add.
070    */  
071  
072  public void addActionListener(ActionListener listener)
073    {
074    listeners.add(ActionListener.class, listener);
075    }
076
077  /** Remove an <code>ActionListener</code> from this object's list of
078    * listeners.
079    *
080    * @param listener The listener to remove.
081    */
082
083  public void removeActionListener(ActionListener listener)
084    {
085    listeners.remove(ActionListener.class, listener);
086    }
087
088  /** Fire an <code>ActionEvent</code> with an ID of
089    * <code>ACTION_PERFORMED</code>, a null command string, and an empty
090    * modifier mask.
091    */
092  
093  public void fireActionEvent()
094    {
095    fireActionEvent(ActionEvent.ACTION_PERFORMED, null, 0);
096    }
097
098  /** Fire an <code>ActionEvent</code> with an ID of
099    * <code>ACTION_PERFORMED</code>, the given command string, and an empty
100    * modifier mask.
101    *
102    * @param command The command string for the event.
103    */  
104  
105  public void fireActionEvent(String command)
106    {
107    fireActionEvent(ActionEvent.ACTION_PERFORMED, command, 0);
108    }
109
110  /** Fire an <code>ActionEvent</code> with the given ID and command string,
111    * and an empty modifier mask.
112    *
113    * @param id The ID for the event.
114    * @param command The command string for the event.
115    */
116  
117  public void fireActionEvent(int id, String command)
118    {
119    fireActionEvent(id, command, 0);
120    }
121
122  /** Fire an <code>ActionEvent</code> with the given ID, command string, and
123    * modifier mask.
124    *
125    * @param id The ID for the event.
126    * @param command The command string for the event.
127    * @param modifiers The modifier mask for the event.
128    */
129  
130  public void fireActionEvent(int id, String command, int modifiers)
131    {
132    ActionEvent evt = null;
133    
134    Object[] list = listeners.getListenerList();
135
136    for(int i = list.length - 2; i >= 0; i -= 2)
137      {
138      if(list[i]==ActionListener.class)
139        {
140        // Lazily create the event:
141        if(evt == null)
142          evt = new ActionEvent(source, id, command, modifiers);
143        ((ActionListener)list[i + 1]).actionPerformed(evt);
144        }
145      }
146    }
147  
148  }
149
150/* end of source file */