Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/AbstractListModel.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import javax.swing.event.*;28import java.io.Serializable;29import java.util.EventListener;3031/**32* The abstract definition for the data model that provides33* a <code>List</code> with its contents.34* <p>35* <strong>Warning:</strong>36* Serialized objects of this class will not be compatible with37* future Swing releases. The current serialization support is38* appropriate for short term storage or RMI between applications running39* the same version of Swing. As of 1.4, support for long term storage40* of all JavaBeans™41* has been added to the <code>java.beans</code> package.42* Please see {@link java.beans.XMLEncoder}.43*44* @param <E> the type of the elements of this model45*46* @author Hans Muller47*/48public abstract class AbstractListModel<E> implements ListModel<E>, Serializable49{50protected EventListenerList listenerList = new EventListenerList();515253/**54* Adds a listener to the list that's notified each time a change55* to the data model occurs.56*57* @param l the <code>ListDataListener</code> to be added58*/59public void addListDataListener(ListDataListener l) {60listenerList.add(ListDataListener.class, l);61}626364/**65* Removes a listener from the list that's notified each time a66* change to the data model occurs.67*68* @param l the <code>ListDataListener</code> to be removed69*/70public void removeListDataListener(ListDataListener l) {71listenerList.remove(ListDataListener.class, l);72}737475/**76* Returns an array of all the list data listeners77* registered on this <code>AbstractListModel</code>.78*79* @return all of this model's <code>ListDataListener</code>s,80* or an empty array if no list data listeners81* are currently registered82*83* @see #addListDataListener84* @see #removeListDataListener85*86* @since 1.487*/88public ListDataListener[] getListDataListeners() {89return listenerList.getListeners(ListDataListener.class);90}919293/**94* <code>AbstractListModel</code> subclasses must call this method95* <b>after</b>96* one or more elements of the list change. The changed elements97* are specified by the closed interval index0, index1 -- the endpoints98* are included. Note that99* 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 interval103* @param index1 the other end of the new interval104* @see EventListenerList105* @see DefaultListModel106*/107protected void fireContentsChanged(Object source, int index0, int index1)108{109Object[] listeners = listenerList.getListenerList();110ListDataEvent e = null;111112for (int i = listeners.length - 2; i >= 0; i -= 2) {113if (listeners[i] == ListDataListener.class) {114if (e == null) {115e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);116}117((ListDataListener)listeners[i+1]).contentsChanged(e);118}119}120}121122123/**124* <code>AbstractListModel</code> subclasses must call this method125* <b>after</b>126* one or more elements are added to the model. The new elements127* are specified by a closed interval index0, index1 -- the enpoints128* are included. Note that129* 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 interval133* @param index1 the other end of the new interval134* @see EventListenerList135* @see DefaultListModel136*/137protected void fireIntervalAdded(Object source, int index0, int index1)138{139Object[] listeners = listenerList.getListenerList();140ListDataEvent e = null;141142for (int i = listeners.length - 2; i >= 0; i -= 2) {143if (listeners[i] == ListDataListener.class) {144if (e == null) {145e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);146}147((ListDataListener)listeners[i+1]).intervalAdded(e);148}149}150}151152153/**154* <code>AbstractListModel</code> subclasses must call this method155* <b>after</b> one or more elements are removed from the model.156* <code>index0</code> and <code>index1</code> are the end points157* 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 EventListenerList166* @see DefaultListModel167*/168protected void fireIntervalRemoved(Object source, int index0, int index1)169{170Object[] listeners = listenerList.getListenerList();171ListDataEvent e = null;172173for (int i = listeners.length - 2; i >= 0; i -= 2) {174if (listeners[i] == ListDataListener.class) {175if (e == null) {176e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);177}178((ListDataListener)listeners[i+1]).intervalRemoved(e);179}180}181}182183/**184* Returns an array of all the objects currently registered as185* <code><em>Foo</em>Listener</code>s186* upon this model.187* <code><em>Foo</em>Listener</code>s188* are registered using the <code>add<em>Foo</em>Listener</code> method.189* <p>190* You can specify the <code>listenerType</code> argument191* with a class literal, such as <code><em>Foo</em>Listener.class</code>.192* For example, you can query a list model193* <code>m</code>194* for its list data listeners195* 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 interface204* that descends from <code>java.util.EventListener</code>205* @return an array of all objects registered as206* <code><em>Foo</em>Listener</code>s207* on this model,208* or an empty array if no such209* listeners have been added210* @exception ClassCastException if <code>listenerType</code> doesn't211* specify a class or interface that implements212* <code>java.util.EventListener</code>213*214* @see #getListDataListeners215*216* @since 1.3217*/218public <T extends EventListener> T[] getListeners(Class<T> listenerType) {219return listenerList.getListeners(listenerType);220}221}222223224