1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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
  42  * has been added to the <code>java.beans</code> package.
  43  * Please see {@link java.beans.XMLEncoder}.
  44  *
  45  * @param <E> the type of the elements of this model
  46  *
  47  * @author Hans Muller
  48  * @since 1.2
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public abstract class AbstractListModel<E> implements ListModel<E>, Serializable
  52 {
  53     /**
  54      * The listener list.
  55      */
  56     protected EventListenerList listenerList = new EventListenerList();
  57 
  58 
  59     /**
  60      * Adds a listener to the list that's notified each time a change
  61      * to the data model occurs.
  62      *
  63      * @param l the <code>ListDataListener</code> to be added
  64      */
  65     public void addListDataListener(ListDataListener l) {
  66         listenerList.add(ListDataListener.class, l);
  67     }
  68 
  69 
  70     /**
  71      * Removes a listener from the list that's notified each time a
  72      * change to the data model occurs.
  73      *
  74      * @param l the <code>ListDataListener</code> to be removed
  75      */
  76     public void removeListDataListener(ListDataListener l) {
  77         listenerList.remove(ListDataListener.class, l);
  78     }
  79 
  80 
  81     /**
  82      * Returns an array of all the list data listeners
  83      * registered on this <code>AbstractListModel</code>.
  84      *
  85      * @return all of this model's <code>ListDataListener</code>s,
  86      *         or an empty array if no list data listeners
  87      *         are currently registered
  88      *
  89      * @see #addListDataListener
  90      * @see #removeListDataListener
  91      *
  92      * @since 1.4
  93      */
  94     public ListDataListener[] getListDataListeners() {
  95         return listenerList.getListeners(ListDataListener.class);
  96     }
  97 
  98 
  99     /**
 100      * <code>AbstractListModel</code> subclasses must call this method
 101      * <b>after</b>
 102      * one or more elements of the list change.  The changed elements
 103      * are specified by the closed interval index0, index1 -- the endpoints
 104      * are included.  Note that
 105      * index0 need not be less than or equal to index1.
 106      *
 107      * @param source the <code>ListModel</code> that changed, typically "this"
 108      * @param index0 one end of the new interval
 109      * @param index1 the other end of the new interval
 110      * @see EventListenerList
 111      * @see DefaultListModel
 112      */
 113     protected void fireContentsChanged(Object source, int index0, int index1)
 114     {
 115         Object[] listeners = listenerList.getListenerList();
 116         ListDataEvent e = null;
 117 
 118         for (int i = listeners.length - 2; i >= 0; i -= 2) {
 119             if (listeners[i] == ListDataListener.class) {
 120                 if (e == null) {
 121                     e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
 122                 }
 123                 ((ListDataListener)listeners[i+1]).contentsChanged(e);
 124             }
 125         }
 126     }
 127 
 128 
 129     /**
 130      * <code>AbstractListModel</code> subclasses must call this method
 131      * <b>after</b>
 132      * one or more elements are added to the model.  The new elements
 133      * are specified by a closed interval index0, index1 -- the enpoints
 134      * are included.  Note that
 135      * index0 need not be less than or equal to index1.
 136      *
 137      * @param source the <code>ListModel</code> that changed, typically "this"
 138      * @param index0 one end of the new interval
 139      * @param index1 the other end of the new interval
 140      * @see EventListenerList
 141      * @see DefaultListModel
 142      */
 143     protected void fireIntervalAdded(Object source, int index0, int index1)
 144     {
 145         Object[] listeners = listenerList.getListenerList();
 146         ListDataEvent e = null;
 147 
 148         for (int i = listeners.length - 2; i >= 0; i -= 2) {
 149             if (listeners[i] == ListDataListener.class) {
 150                 if (e == null) {
 151                     e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
 152                 }
 153                 ((ListDataListener)listeners[i+1]).intervalAdded(e);
 154             }
 155         }
 156     }
 157 
 158 
 159     /**
 160      * <code>AbstractListModel</code> subclasses must call this method
 161      * <b>after</b> one or more elements are removed from the model.
 162      * <code>index0</code> and <code>index1</code> are the end points
 163      * of the interval that's been removed.  Note that <code>index0</code>
 164      * need not be less than or equal to <code>index1</code>.
 165      *
 166      * @param source the <code>ListModel</code> that changed, typically "this"
 167      * @param index0 one end of the removed interval,
 168      *               including <code>index0</code>
 169      * @param index1 the other end of the removed interval,
 170      *               including <code>index1</code>
 171      * @see EventListenerList
 172      * @see DefaultListModel
 173      */
 174     protected void fireIntervalRemoved(Object source, int index0, int index1)
 175     {
 176         Object[] listeners = listenerList.getListenerList();
 177         ListDataEvent e = null;
 178 
 179         for (int i = listeners.length - 2; i >= 0; i -= 2) {
 180             if (listeners[i] == ListDataListener.class) {
 181                 if (e == null) {
 182                     e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
 183                 }
 184                 ((ListDataListener)listeners[i+1]).intervalRemoved(e);
 185             }
 186         }
 187     }
 188 
 189     /**
 190      * Returns an array of all the objects currently registered as
 191      * <code><em>Foo</em>Listener</code>s
 192      * upon this model.
 193      * <code><em>Foo</em>Listener</code>s
 194      * are registered using the <code>add<em>Foo</em>Listener</code> method.
 195      * <p>
 196      * You can specify the <code>listenerType</code> argument
 197      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
 198      * For example, you can query a list model
 199      * <code>m</code>
 200      * for its list data listeners
 201      * with the following code:
 202      *
 203      * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
 204      *
 205      * If no such listeners exist,
 206      * this method returns an empty array.
 207      *
 208      * @param <T> the type of {@code EventListener} class being requested
 209      * @param listenerType  the type of listeners requested;
 210      *          this parameter should specify an interface
 211      *          that descends from <code>java.util.EventListener</code>
 212      * @return an array of all objects registered as
 213      *          <code><em>Foo</em>Listener</code>s
 214      *          on this model,
 215      *          or an empty array if no such
 216      *          listeners have been added
 217      * @exception ClassCastException if <code>listenerType</code> doesn't
 218      *          specify a class or interface that implements
 219      *          <code>java.util.EventListener</code>
 220      *
 221      * @see #getListDataListeners
 222      *
 223      * @since 1.3
 224      */
 225     public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
 226         return listenerList.getListeners(listenerType);
 227     }
 228 }