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: KTreeModelSupport.java,v $
023   Revision 1.2  2004/05/31 07:30:59  markl
024   javadoc updates
025
026   Revision 1.1  2004/05/13 22:25:42  markl
027   new classes
028   ----------------------------------------------------------------------------
029*/
030
031package kiwi.event;
032
033import java.util.*;
034
035/** A support object for generating <code>KTreeModelEvent</code>s.
036  *
037  * @see kiwi.event.KTreeModelEvent
038  * @author Mark Lindner
039  * @since Kiwi 2.0
040  */
041
042public class KTreeModelSupport
043  {
044  private Vector listeners;
045  private Object source;
046
047  /** Construct a new <code>KTreeModelSupport</code> object.
048    *
049    * @param source The owner of this object (and the source of the events that
050    * will be generated by it).
051    */
052
053  public KTreeModelSupport(Object source)
054    {
055    listeners = new Vector();
056    this.source = source;
057    }
058
059  /** Add a <code>KTreeModelListener</code> to this object's list
060   * of listeners.
061   *
062   * @param listener The listener to add.
063   */
064
065  public void addTreeModelListener(KTreeModelListener listener)
066    {
067    listeners.addElement(listener);
068    }
069
070  /** Remove a <code>KTreeModelListener</code> from this object's list of
071   * listeners.
072   *
073   * @param listener The listener to remove.
074   */
075
076  public void removeTreeModelListener(KTreeModelListener listener)
077    {
078    listeners.removeElement(listener);
079    }
080
081  /** Fire a <i>nodes added</i> event for a range of nodes. Notifies
082    * listeners that a range of children have been added to a node.
083    *
084    * @param parentNode The parent node.
085    * @param startIndex The start index of the range of child nodes.
086    * @param endIndex The end index of the range of child nodes.
087    */
088
089  public void fireNodesAdded(Object parentNode, int startIndex, int endIndex)
090    {
091    KTreeModelEvent evt = null;
092    Enumeration e = listeners.elements();
093    while(e.hasMoreElements())
094      {
095      KTreeModelListener l = (KTreeModelListener)e.nextElement();
096      if(evt == null)
097        evt = new KTreeModelEvent(source, parentNode, startIndex, endIndex);
098      l.nodesAdded(evt);
099      }
100    }
101
102  /** Fire a <i>nodes added</i> event for a single node. Notifies
103    * listeners that a child has been added to a node.
104    *
105    * @param parentNode The parent node.
106    * @param index The offset of the new child within the parent's list of
107    * children.
108    */
109  
110  public void fireNodeAdded(Object parentNode, int index)
111    {
112    fireNodesAdded(parentNode, index, index);
113    }
114
115  /** Fire a <i>nodes removed</i> event for a range of nodes. Notifies
116    * listeners that a range of children have been removed from a node.
117    *
118    * @param parentNode The parent node.
119    * @param startIndex The start index of the range of child nodes.
120    * @param endIndex The end index of the range of child nodes.
121    */
122
123  public void fireNodesRemoved(Object parentNode, int startIndex, int endIndex)
124    {
125    KTreeModelEvent evt = null;
126    Enumeration e = listeners.elements();
127    while(e.hasMoreElements())
128      {
129      KTreeModelListener l = (KTreeModelListener)e.nextElement();
130      if(evt == null)
131        evt = new KTreeModelEvent(source, parentNode, startIndex, endIndex);
132      l.nodesRemoved(evt);
133      }
134    }
135
136  /** Fire a <i>nodes removed</i> event for a single node. Notifies
137    * listeners that a child has been removed from a node.
138    *
139    * @param parentNode The parent node.
140    * @param index The offset of the removed child within the parent's list of
141    * children.
142    */
143  
144  public void fireNodeRemoved(Object parentNode, int index)
145    {
146    fireNodesRemoved(parentNode, index, index);
147    }
148  
149  /** Fire a <i>nodes changed</i> event for a range of nodes. Notifies
150    * listeners that a range of children of a given node have changed in some
151    * perceptible way.
152    *
153    * @param parentNode The parent node.
154    * @param startIndex The start index of the range of child nodes.
155    * @param endIndex The end index of the range of child nodes.
156    */
157
158  public void fireNodesChanged(Object parentNode, int startIndex, int endIndex)
159    {
160    KTreeModelEvent evt = null;
161    Enumeration e = listeners.elements();
162    while(e.hasMoreElements())
163      {
164      KTreeModelListener l = (KTreeModelListener)e.nextElement();
165      if(evt == null)
166        evt = new KTreeModelEvent(source, parentNode, startIndex, endIndex);
167      l.nodesChanged(evt);
168      }
169    }
170
171  /** Fire a <i>node changed</i> event for a single node. Notifies
172    * listeners that a child of a given node has changed in some
173    * perceptible way.
174    *
175    * @param parentNode The parent node.
176    * @param index The offset of the changed child within the parent's
177    * list of children.
178    */
179  
180  public void fireNodeChanged(Object parentNode, int index)
181    {
182    fireNodesChanged(parentNode, index, index);
183    }
184
185  /** Fire a <i>node structure changed</i> event. Notifies listeners that the
186    * subtree rooted at the given node has undergone a substantial structural
187    * change.
188    *
189    * @param node The root node of the subtree that has changed.
190    */
191
192  public void fireNodeStructureChanged(Object node)
193    {
194    KTreeModelEvent evt = null;
195    Enumeration e = listeners.elements();
196    while(e.hasMoreElements())
197      {
198      KTreeModelListener l = (KTreeModelListener)e.nextElement();
199      if(evt == null)
200        evt = new KTreeModelEvent(source, node);
201      l.structureChanged(evt);
202      }
203    }
204
205  /** Fire a <i>data changed</i> event. Notifies listeners that the entire
206   * tree structure has changed.
207   */
208  
209  public void fireDataChanged()
210    {
211    KTreeModelEvent evt = null;
212    Enumeration e = listeners.elements();
213    while(e.hasMoreElements())
214      {
215      KTreeModelListener l = (KTreeModelListener)e.nextElement();
216      if(evt == null)
217        evt = new KTreeModelEvent(source);
218      l.dataChanged(evt);
219      }
220    }
221  
222  }
223
224/* end of source file */