001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import javax.swing.event.*;
029 import java.io.Serializable;
030 import java.util.EventListener;
031
032 /**
033 * The abstract definition for the data model that provides
034 * a <code>List</code> with its contents.
035 * <p>
036 * <strong>Warning:</strong>
037 * Serialized objects of this class will not be compatible with
038 * future Swing releases. The current serialization support is
039 * appropriate for short term storage or RMI between applications running
040 * the same version of Swing. As of 1.4, support for long term storage
041 * of all JavaBeans<sup><font size="-2">TM</font></sup>
042 * has been added to the <code>java.beans</code> package.
043 * Please see {@link java.beans.XMLEncoder}.
044 *
045 * @version 1.41 05/05/07
046 * @author Hans Muller
047 */
048 public abstract class AbstractListModel implements ListModel,
049 Serializable {
050 protected EventListenerList listenerList = new EventListenerList();
051
052 /**
053 * Adds a listener to the list that's notified each time a change
054 * to the data model occurs.
055 *
056 * @param l the <code>ListDataListener</code> to be added
057 */
058 public void addListDataListener(ListDataListener l) {
059 listenerList.add(ListDataListener.class, l);
060 }
061
062 /**
063 * Removes a listener from the list that's notified each time a
064 * change to the data model occurs.
065 *
066 * @param l the <code>ListDataListener</code> to be removed
067 */
068 public void removeListDataListener(ListDataListener l) {
069 listenerList.remove(ListDataListener.class, l);
070 }
071
072 /**
073 * Returns an array of all the list data listeners
074 * registered on this <code>AbstractListModel</code>.
075 *
076 * @return all of this model's <code>ListDataListener</code>s,
077 * or an empty array if no list data listeners
078 * are currently registered
079 *
080 * @see #addListDataListener
081 * @see #removeListDataListener
082 *
083 * @since 1.4
084 */
085 public ListDataListener[] getListDataListeners() {
086 return (ListDataListener[]) listenerList
087 .getListeners(ListDataListener.class);
088 }
089
090 /**
091 * <code>AbstractListModel</code> subclasses must call this method
092 * <b>after</b>
093 * one or more elements of the list change. The changed elements
094 * are specified by the closed interval index0, index1 -- the endpoints
095 * are included. Note that
096 * index0 need not be less than or equal to index1.
097 *
098 * @param source the <code>ListModel</code> that changed, typically "this"
099 * @param index0 one end of the new interval
100 * @param index1 the other end of the new interval
101 * @see EventListenerList
102 * @see DefaultListModel
103 */
104 protected void fireContentsChanged(Object source, int index0,
105 int index1) {
106 Object[] listeners = listenerList.getListenerList();
107 ListDataEvent e = null;
108
109 for (int i = listeners.length - 2; i >= 0; i -= 2) {
110 if (listeners[i] == ListDataListener.class) {
111 if (e == null) {
112 e = new ListDataEvent(source,
113 ListDataEvent.CONTENTS_CHANGED, index0,
114 index1);
115 }
116 ((ListDataListener) listeners[i + 1])
117 .contentsChanged(e);
118 }
119 }
120 }
121
122 /**
123 * <code>AbstractListModel</code> subclasses must call this method
124 * <b>after</b>
125 * one or more elements are added to the model. The new elements
126 * are specified by a closed interval index0, index1 -- the enpoints
127 * are included. Note that
128 * index0 need not be less than or equal to index1.
129 *
130 * @param source the <code>ListModel</code> that changed, typically "this"
131 * @param index0 one end of the new interval
132 * @param index1 the other end of the new interval
133 * @see EventListenerList
134 * @see DefaultListModel
135 */
136 protected void fireIntervalAdded(Object source, int index0,
137 int index1) {
138 Object[] listeners = listenerList.getListenerList();
139 ListDataEvent e = null;
140
141 for (int i = listeners.length - 2; i >= 0; i -= 2) {
142 if (listeners[i] == ListDataListener.class) {
143 if (e == null) {
144 e = new ListDataEvent(source,
145 ListDataEvent.INTERVAL_ADDED, index0,
146 index1);
147 }
148 ((ListDataListener) listeners[i + 1]).intervalAdded(e);
149 }
150 }
151 }
152
153 /**
154 * <code>AbstractListModel</code> subclasses must call this method
155 * <b>after</b> one or more elements are removed from the model.
156 * <code>index0</code> and <code>index1</code> are the end points
157 * 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 EventListenerList
166 * @see DefaultListModel
167 */
168 protected void fireIntervalRemoved(Object source, int index0,
169 int index1) {
170 Object[] listeners = listenerList.getListenerList();
171 ListDataEvent e = null;
172
173 for (int i = listeners.length - 2; i >= 0; i -= 2) {
174 if (listeners[i] == ListDataListener.class) {
175 if (e == null) {
176 e = new ListDataEvent(source,
177 ListDataEvent.INTERVAL_REMOVED, index0,
178 index1);
179 }
180 ((ListDataListener) listeners[i + 1])
181 .intervalRemoved(e);
182 }
183 }
184 }
185
186 /**
187 * Returns an array of all the objects currently registered as
188 * <code><em>Foo</em>Listener</code>s
189 * upon this model.
190 * <code><em>Foo</em>Listener</code>s
191 * are registered using the <code>add<em>Foo</em>Listener</code> method.
192 * <p>
193 * You can specify the <code>listenerType</code> argument
194 * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
195 * For example, you can query a list model
196 * <code>m</code>
197 * for its list data listeners
198 * with the following code:
199 *
200 * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
201 *
202 * If no such listeners exist,
203 * this method returns an empty array.
204 *
205 * @param listenerType the type of listeners requested;
206 * this parameter should specify an interface
207 * that descends from <code>java.util.EventListener</code>
208 * @return an array of all objects registered as
209 * <code><em>Foo</em>Listener</code>s
210 * on this model,
211 * or an empty array if no such
212 * listeners have been added
213 * @exception ClassCastException if <code>listenerType</code> doesn't
214 * specify a class or interface that implements
215 * <code>java.util.EventListener</code>
216 *
217 * @see #getListDataListeners
218 *
219 * @since 1.3
220 */
221 public <T extends EventListener> T[] getListeners(
222 Class<T> listenerType) {
223 return listenerList.getListeners(listenerType);
224 }
225 }
|