jdk/src/share/classes/javax/swing/AbstractListModel.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public abstract class AbstractListModel implements ListModel, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Adds a listener to the list that's notified each time a change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * to the data model occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param l the <code>ListDataListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void addListDataListener(ListDataListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        listenerList.add(ListDataListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Removes a listener from the list that's notified each time a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * change to the data model occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param l the <code>ListDataListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void removeListDataListener(ListDataListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        listenerList.remove(ListDataListener.class, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns an array of all the list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * registered on this <code>AbstractListModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return all of this model's <code>ListDataListener</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *         or an empty array if no list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *         are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @see #addListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @see #removeListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public ListDataListener[] getListDataListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return (ListDataListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                ListDataListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
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
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <b>after</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * one or more elements of the list change.  The changed elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * are specified by the closed interval index0, index1 -- the endpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * are included.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * index0 need not be less than or equal to index1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param index0 one end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param index1 the other end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected void fireContentsChanged(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                ((ListDataListener)listeners[i+1]).contentsChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
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
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <b>after</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * one or more elements are added to the model.  The new elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * are specified by a closed interval index0, index1 -- the enpoints
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * are included.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * index0 need not be less than or equal to index1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param index0 one end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param index1 the other end of the new interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected void fireIntervalAdded(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                ((ListDataListener)listeners[i+1]).intervalAdded(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
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
     * <code>AbstractListModel</code> subclasses must call this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <b>after</b> one or more elements are removed from the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>index0</code> and <code>index1</code> are the end points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * of the interval that's been removed.  Note that <code>index0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * need not be less than or equal to <code>index1</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param source the <code>ListModel</code> that changed, typically "this"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param index0 one end of the removed interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *               including <code>index0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param index1 the other end of the removed interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *               including <code>index1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    protected void fireIntervalRemoved(Object source, int index0, int index1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ListDataEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (listeners[i] == ListDataListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                ((ListDataListener)listeners[i+1]).intervalRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
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
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * upon this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * For example, you can query a list model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * for its list data listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *          on this model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see #getListDataListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}