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: ChangeSupport.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:46  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>ChangeEvent</code>s.
043  *
044  * @see javax.swing.event.ChangeEvent
045  * @see javax.swing.event.ChangeListener
046  *
047  * @author Mark Lindner
048  */
049
050public class ChangeSupport
051  {
052  private EventListenerList listeners;
053  private Object source;
054
055  /** Construct a new <code>ChangeSupport</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 ChangeSupport(Object source)
062    {
063    this.source = source;
064    listeners = new EventListenerList();
065    }
066
067  /** Add a <code>ChangeListener</code> to this object's list of listeners.
068    *
069    * @param listener The listener to add.
070    */  
071  
072  public void addChangeListener(ChangeListener listener)
073    {
074    listeners.add(ChangeListener.class, listener);
075    }
076
077  /** Remove a <code>ChangeListener</code> from this object's list of
078    * listeners.
079    *
080    * @param listener The listener to remove.
081    */
082
083  public void removeChangeListener(ChangeListener listener)
084    {
085    listeners.remove(ChangeListener.class, listener);
086    }
087
088  /** Fire a change event.
089    */
090  
091  public void fireChangeEvent()
092    {
093    ChangeEvent evt = null;
094    
095    Object[] list = listeners.getListenerList();
096
097    for(int i = list.length - 2; i >= 0; i -= 2)
098      {
099      if(list[i]==ChangeListener.class)
100        {
101        // Lazily create the event:
102        if(evt == null)
103          evt = new ChangeEvent(source);
104        ((ChangeListener)list[i + 1]).stateChanged(evt);
105        }
106      }
107    }
108  
109  }
110
111/* end of source file */