Path: blob/master/src/java.desktop/share/classes/javax/swing/AbstractListModel.java
41153 views
/*1* Copyright (c) 1997, 2015, 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 JavaBeans41* 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* @since 1.248*/49@SuppressWarnings("serial") // Same-version serialization only50public abstract class AbstractListModel<E> implements ListModel<E>, Serializable51{52/**53* The listener list.54*/55protected EventListenerList listenerList = new EventListenerList();5657/**58* Constructor for subclasses to call.59*/60protected AbstractListModel() {}6162/**63* Adds a listener to the list that's notified each time a change64* to the data model occurs.65*66* @param l the <code>ListDataListener</code> to be added67*/68public void addListDataListener(ListDataListener l) {69listenerList.add(ListDataListener.class, l);70}717273/**74* Removes a listener from the list that's notified each time a75* change to the data model occurs.76*77* @param l the <code>ListDataListener</code> to be removed78*/79public void removeListDataListener(ListDataListener l) {80listenerList.remove(ListDataListener.class, l);81}828384/**85* Returns an array of all the list data listeners86* registered on this <code>AbstractListModel</code>.87*88* @return all of this model's <code>ListDataListener</code>s,89* or an empty array if no list data listeners90* are currently registered91*92* @see #addListDataListener93* @see #removeListDataListener94*95* @since 1.496*/97public ListDataListener[] getListDataListeners() {98return listenerList.getListeners(ListDataListener.class);99}100101102/**103* <code>AbstractListModel</code> subclasses must call this method104* <b>after</b>105* one or more elements of the list change. The changed elements106* are specified by the closed interval index0, index1 -- the endpoints107* are included. Note that108* index0 need not be less than or equal to index1.109*110* @param source the <code>ListModel</code> that changed, typically "this"111* @param index0 one end of the new interval112* @param index1 the other end of the new interval113* @see EventListenerList114* @see DefaultListModel115*/116protected void fireContentsChanged(Object source, int index0, int index1)117{118Object[] listeners = listenerList.getListenerList();119ListDataEvent e = null;120121for (int i = listeners.length - 2; i >= 0; i -= 2) {122if (listeners[i] == ListDataListener.class) {123if (e == null) {124e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);125}126((ListDataListener)listeners[i+1]).contentsChanged(e);127}128}129}130131132/**133* <code>AbstractListModel</code> subclasses must call this method134* <b>after</b>135* one or more elements are added to the model. The new elements136* are specified by a closed interval index0, index1 -- the enpoints137* are included. Note that138* index0 need not be less than or equal to index1.139*140* @param source the <code>ListModel</code> that changed, typically "this"141* @param index0 one end of the new interval142* @param index1 the other end of the new interval143* @see EventListenerList144* @see DefaultListModel145*/146protected void fireIntervalAdded(Object source, int index0, int index1)147{148Object[] listeners = listenerList.getListenerList();149ListDataEvent e = null;150151for (int i = listeners.length - 2; i >= 0; i -= 2) {152if (listeners[i] == ListDataListener.class) {153if (e == null) {154e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);155}156((ListDataListener)listeners[i+1]).intervalAdded(e);157}158}159}160161162/**163* <code>AbstractListModel</code> subclasses must call this method164* <b>after</b> one or more elements are removed from the model.165* <code>index0</code> and <code>index1</code> are the end points166* of the interval that's been removed. Note that <code>index0</code>167* need not be less than or equal to <code>index1</code>.168*169* @param source the <code>ListModel</code> that changed, typically "this"170* @param index0 one end of the removed interval,171* including <code>index0</code>172* @param index1 the other end of the removed interval,173* including <code>index1</code>174* @see EventListenerList175* @see DefaultListModel176*/177protected void fireIntervalRemoved(Object source, int index0, int index1)178{179Object[] listeners = listenerList.getListenerList();180ListDataEvent e = null;181182for (int i = listeners.length - 2; i >= 0; i -= 2) {183if (listeners[i] == ListDataListener.class) {184if (e == null) {185e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);186}187((ListDataListener)listeners[i+1]).intervalRemoved(e);188}189}190}191192/**193* Returns an array of all the objects currently registered as194* <code><em>Foo</em>Listener</code>s195* upon this model.196* <code><em>Foo</em>Listener</code>s197* are registered using the <code>add<em>Foo</em>Listener</code> method.198* <p>199* You can specify the <code>listenerType</code> argument200* with a class literal, such as <code><em>Foo</em>Listener.class</code>.201* For example, you can query a list model202* <code>m</code>203* for its list data listeners204* with the following code:205*206* <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>207*208* If no such listeners exist,209* this method returns an empty array.210*211* @param <T> the type of {@code EventListener} class being requested212* @param listenerType the type of listeners requested;213* this parameter should specify an interface214* that descends from <code>java.util.EventListener</code>215* @return an array of all objects registered as216* <code><em>Foo</em>Listener</code>s217* on this model,218* or an empty array if no such219* listeners have been added220* @exception ClassCastException if <code>listenerType</code> doesn't221* specify a class or interface that implements222* <code>java.util.EventListener</code>223*224* @see #getListDataListeners225*226* @since 1.3227*/228public <T extends EventListener> T[] getListeners(Class<T> listenerType) {229return listenerList.getListeners(listenerType);230}231}232233234