Save This Page
Home » openjdk-7 » javax » swing » [javadoc | source]
    1   /*
    2    * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package javax.swing;
   27   
   28   import javax.swing.event;
   29   import java.io.Serializable;
   30   import java.util.EventListener;
   31   
   32   /**
   33    * The abstract definition for the data model that provides
   34    * a <code>List</code> with its contents.
   35    * <p>
   36    * <strong>Warning:</strong>
   37    * Serialized objects of this class will not be compatible with
   38    * future Swing releases. The current serialization support is
   39    * appropriate for short term storage or RMI between applications running
   40    * the same version of Swing.  As of 1.4, support for long term storage
   41    * of all JavaBeans<sup><font size="-2">TM</font></sup>
   42    * has been added to the <code>java.beans</code> package.
   43    * Please see {@link java.beans.XMLEncoder}.
   44    *
   45    * @author Hans Muller
   46    */
   47   public abstract class AbstractListModel implements ListModel, Serializable
   48   {
   49       protected EventListenerList listenerList = new EventListenerList();
   50   
   51   
   52       /**
   53        * Adds a listener to the list that's notified each time a change
   54        * to the data model occurs.
   55        *
   56        * @param l the <code>ListDataListener</code> to be added
   57        */
   58       public void addListDataListener(ListDataListener l) {
   59           listenerList.add(ListDataListener.class, l);
   60       }
   61   
   62   
   63       /**
   64        * Removes a listener from the list that's notified each time a
   65        * change to the data model occurs.
   66        *
   67        * @param l the <code>ListDataListener</code> to be removed
   68        */
   69       public void removeListDataListener(ListDataListener l) {
   70           listenerList.remove(ListDataListener.class, l);
   71       }
   72   
   73   
   74       /**
   75        * Returns an array of all the list data listeners
   76        * registered on this <code>AbstractListModel</code>.
   77        *
   78        * @return all of this model's <code>ListDataListener</code>s,
   79        *         or an empty array if no list data listeners
   80        *         are currently registered
   81        *
   82        * @see #addListDataListener
   83        * @see #removeListDataListener
   84        *
   85        * @since 1.4
   86        */
   87       public ListDataListener[] getListDataListeners() {
   88           return (ListDataListener[])listenerList.getListeners(
   89                   ListDataListener.class);
   90       }
   91   
   92   
   93       /**
   94        * <code>AbstractListModel</code> subclasses must call this method
   95        * <b>after</b>
   96        * one or more elements of the list change.  The changed elements
   97        * are specified by the closed interval index0, index1 -- the endpoints
   98        * are included.  Note that
   99        * index0 need not be less than or equal to index1.
  100        *
  101        * @param source the <code>ListModel</code> that changed, typically "this"
  102        * @param index0 one end of the new interval
  103        * @param index1 the other end of the new interval
  104        * @see EventListenerList
  105        * @see DefaultListModel
  106        */
  107       protected void fireContentsChanged(Object source, int index0, int index1)
  108       {
  109           Object[] listeners = listenerList.getListenerList();
  110           ListDataEvent e = null;
  111   
  112           for (int i = listeners.length - 2; i >= 0; i -= 2) {
  113               if (listeners[i] == ListDataListener.class) {
  114                   if (e == null) {
  115                       e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
  116                   }
  117                   ((ListDataListener)listeners[i+1]).contentsChanged(e);
  118               }
  119           }
  120       }
  121   
  122   
  123       /**
  124        * <code>AbstractListModel</code> subclasses must call this method
  125        * <b>after</b>
  126        * one or more elements are added to the model.  The new elements
  127        * are specified by a closed interval index0, index1 -- the enpoints
  128        * are included.  Note that
  129        * index0 need not be less than or equal to index1.
  130        *
  131        * @param source the <code>ListModel</code> that changed, typically "this"
  132        * @param index0 one end of the new interval
  133        * @param index1 the other end of the new interval
  134        * @see EventListenerList
  135        * @see DefaultListModel
  136        */
  137       protected void fireIntervalAdded(Object source, int index0, int index1)
  138       {
  139           Object[] listeners = listenerList.getListenerList();
  140           ListDataEvent e = null;
  141   
  142           for (int i = listeners.length - 2; i >= 0; i -= 2) {
  143               if (listeners[i] == ListDataListener.class) {
  144                   if (e == null) {
  145                       e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
  146                   }
  147                   ((ListDataListener)listeners[i+1]).intervalAdded(e);
  148               }
  149           }
  150       }
  151   
  152   
  153       /**
  154        * <code>AbstractListModel</code> subclasses must call this method
  155        * <b>after</b> one or more elements are removed from the model.
  156        * <code>index0</code> and <code>index1</code> are the end points
  157        * of the interval that's been removed.  Note that <code>index0</code>
  158        * need not be less than or equal to <code>index1</code>.
  159        *
  160        * @param source the <code>ListModel</code> that changed, typically "this"
  161        * @param index0 one end of the removed interval,
  162        *               including <code>index0</code>
  163        * @param index1 the other end of the removed interval,
  164        *               including <code>index1</code>
  165        * @see EventListenerList
  166        * @see DefaultListModel
  167        */
  168       protected void fireIntervalRemoved(Object source, int index0, int index1)
  169       {
  170           Object[] listeners = listenerList.getListenerList();
  171           ListDataEvent e = null;
  172   
  173           for (int i = listeners.length - 2; i >= 0; i -= 2) {
  174               if (listeners[i] == ListDataListener.class) {
  175                   if (e == null) {
  176                       e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
  177                   }
  178                   ((ListDataListener)listeners[i+1]).intervalRemoved(e);
  179               }
  180           }
  181       }
  182   
  183       /**
  184        * Returns an array of all the objects currently registered as
  185        * <code><em>Foo</em>Listener</code>s
  186        * upon this model.
  187        * <code><em>Foo</em>Listener</code>s
  188        * are registered using the <code>add<em>Foo</em>Listener</code> method.
  189        * <p>
  190        * You can specify the <code>listenerType</code> argument
  191        * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
  192        * For example, you can query a list model
  193        * <code>m</code>
  194        * for its list data listeners
  195        * with the following code:
  196        *
  197        * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
  198        *
  199        * If no such listeners exist,
  200        * this method returns an empty array.
  201        *
  202        * @param listenerType  the type of listeners requested;
  203        *          this parameter should specify an interface
  204        *          that descends from <code>java.util.EventListener</code>
  205        * @return an array of all objects registered as
  206        *          <code><em>Foo</em>Listener</code>s
  207        *          on this model,
  208        *          or an empty array if no such
  209        *          listeners have been added
  210        * @exception ClassCastException if <code>listenerType</code> doesn't
  211        *          specify a class or interface that implements
  212        *          <code>java.util.EventListener</code>
  213        *
  214        * @see #getListDataListeners
  215        *
  216        * @since 1.3
  217        */
  218       public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
  219           return listenerList.getListeners(listenerType);
  220       }
  221   }

Save This Page
Home » openjdk-7 » javax » swing » [javadoc | source]