jdk/src/share/classes/javax/swing/AbstractListModel.java
author rriggs
Tue, 03 Jun 2014 11:18:03 -0400
changeset 25122 1ecc464c69d2
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8044460: Cleanup new Boolean and single character strings Reviewed-by: pchelko, serb, rriggs Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    23
 * questions.
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    41
 * of all JavaBeans&trade;
2
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
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    49
@SuppressWarnings("serial") // Same-version serialization only
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 1639
diff changeset
    50
public abstract class AbstractListModel<E> implements ListModel<E>, Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Adds a listener to the list that's notified each time a change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * to the data model occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param l the <code>ListDataListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public void addListDataListener(ListDataListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        listenerList.add(ListDataListener.class, l);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Removes a listener from the list that's notified each time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * change to the data model occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param l the <code>ListDataListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void removeListDataListener(ListDataListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        listenerList.remove(ListDataListener.class, l);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns an array of all the list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * registered on this <code>AbstractListModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return all of this model's <code>ListDataListener</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         or an empty array if no list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *         are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #addListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see #removeListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public ListDataListener[] getListDataListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    91
        return listenerList.getListeners(ListDataListener.class);
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <b>after</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * one or more elements of the list change.  The changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * are specified by the closed interval index0, index1 -- the endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * are included.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * index0 need not be less than or equal to index1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param index0 one end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param index1 the other end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void fireContentsChanged(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                ((ListDataListener)listeners[i+1]).contentsChanged(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <b>after</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * one or more elements are added to the model.  The new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * are specified by a closed interval index0, index1 -- the enpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * are included.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * index0 need not be less than or equal to index1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param index0 one end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param index1 the other end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected void fireIntervalAdded(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                ((ListDataListener)listeners[i+1]).intervalAdded(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <b>after</b> one or more elements are removed from the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>index0</code> and <code>index1</code> are the end points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * of the interval that's been removed.  Note that <code>index0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * need not be less than or equal to <code>index1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param index0 one end of the removed interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *               including <code>index0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param index1 the other end of the removed interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *               including <code>index1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected void fireIntervalRemoved(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                ((ListDataListener)listeners[i+1]).intervalRemoved(e);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * For example, you can query a list model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * for its list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          on this model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see #getListDataListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}