Source for javax.swing.AbstractListModel

   1: /* AbstractListModel.java --
   2:    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.io.Serializable;
  42: import java.util.EventListener;
  43: 
  44: import javax.swing.event.EventListenerList;
  45: import javax.swing.event.ListDataEvent;
  46: import javax.swing.event.ListDataListener;
  47: 
  48: /**
  49:  * Provides standard implementations of some methods in {@link ListModel}.
  50:  *
  51:  * @author Ronald Veldema
  52:  * @author Andrew Selkirk
  53:  */
  54: public abstract class AbstractListModel implements ListModel, Serializable
  55: {
  56:   private static final long serialVersionUID = -3285184064379168730L;
  57: 
  58:   /** List of ListDataListeners called for each change to the list. */
  59:   protected EventListenerList listenerList;
  60: 
  61:   public AbstractListModel()
  62:   {
  63:     listenerList = new EventListenerList();
  64:   }
  65: 
  66:   /**
  67:    * Add a listener object to this model. The listener will be called
  68:    * any time the set of elements in the model is changed.
  69:    *
  70:    * @param listener The listener to add
  71:    */
  72:   public void addListDataListener(ListDataListener listener)
  73:   {
  74:     listenerList.add(ListDataListener.class, listener);
  75:   }
  76: 
  77:   /**
  78:    * Add a listener object to this model. The listener will no longer be
  79:    * called when the set of elements in the model is changed.
  80:    *
  81:    * @param listener The listener to remove
  82:    */
  83:   public void removeListDataListener(ListDataListener listener)
  84:   {
  85:     listenerList.remove(ListDataListener.class, listener);
  86:   }
  87: 
  88:   /**
  89:    * Call {@link ListDataListener#contentsChanged} on each element of the
  90:    * {@link #listenerList} which is a {@link ListDataListener}. The event
  91:    * fired has type {@ListDataEvent.CONTENTS_CHANGED} and represents a
  92:    * change to the data elements in the range [startIndex, endIndex]
  93:    * inclusive.
  94:    *
  95:    * @param source The source of the change, typically <code>this</code>
  96:    * @param startIndex The index of the first element which changed
  97:    * @param endIndex The index of the last element which changed
  98:    */
  99:   protected void fireContentsChanged(Object source, int startIndex,
 100:                                      int endIndex)
 101:   {
 102:     ListDataEvent event = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED,
 103:                                             startIndex, endIndex);
 104:     ListDataListener[] listeners = getListDataListeners();
 105: 
 106:     for (int index = 0; index < listeners.length; index++)
 107:       listeners[index].contentsChanged(event);
 108:   }
 109: 
 110:   /**
 111:    * Call {@link ListDataListener#intervalAdded} on each element of the
 112:    * {@link #listenerList} which is a {@link ListDataListener}. The event
 113:    * fired has type {@ListDataEvent.INTERVAL_ADDED} and represents an
 114:    * addition of the data elements in the range [startIndex, endIndex]
 115:    * inclusive.
 116:    *
 117:    * @param source The source of the change, typically <code>this</code>
 118:    * @param startIndex The index of the first new element
 119:    * @param endIndex The index of the last new element
 120:    */
 121:   protected void fireIntervalAdded(Object source, int startIndex, int endIndex)
 122:   {
 123:     ListDataEvent event =
 124:       new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED,
 125:             startIndex, endIndex);
 126:     ListDataListener[] listeners = getListDataListeners();
 127: 
 128:     for (int index = 0; index < listeners.length; index++)
 129:       listeners[index].intervalAdded(event);
 130:   }
 131: 
 132:   /**
 133:    * Call {@link ListDataListener#intervalRemoved} on each element of the
 134:    * {@link #listenerList} which is a {@link ListDataListener}. The event
 135:    * fired has type {@ListDataEvent.INTERVAL_REMOVED} and represents a
 136:    * removal of the data elements in the range [startIndex, endIndex]
 137:    * inclusive.
 138:    *
 139:    * @param source The source of the change, typically <code>this</code>
 140:    * @param startIndex The index of the first element removed
 141:    * @param endIndex The index of the last element removed
 142:    */
 143:   protected void fireIntervalRemoved(Object source, int startIndex,
 144:                                      int endIndex)
 145:   {
 146:     ListDataEvent event =
 147:       new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED,
 148:             startIndex, endIndex);
 149:     ListDataListener[] listeners = getListDataListeners();
 150: 
 151:     for (int index = 0; index < listeners.length; index++)
 152:       listeners[index].intervalRemoved(event);
 153:   }
 154: 
 155:   /**
 156:    * Return the subset of {@link EventListener} objects found in this
 157:    * object's {@link #listenerList} which are elements of the specified
 158:    * type.
 159:    *
 160:    * @param listenerType The type of listeners to select
 161:    *
 162:    * @return The set of listeners of the specified type
 163:    */
 164:   public EventListener[] getListeners(Class listenerType)
 165:   {
 166:     return listenerList.getListeners(listenerType);
 167:   }
 168: 
 169:   /**
 170:    * A synonym for <code>getListeners(ListDataListener.class)</code>.
 171:    *
 172:    * @return The set of ListDataListeners found in the {@link #listenerList}
 173:    */
 174:   public ListDataListener[] getListDataListeners()
 175:   {
 176:     return (ListDataListener[]) getListeners(ListDataListener.class);
 177:   }
 178: }