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