001 /* AbstractListModel.java --
002 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing;
040
041 import java.io.Serializable;
042 import java.util.EventListener;
043
044 import javax.swing.event.EventListenerList;
045 import javax.swing.event.ListDataEvent;
046 import javax.swing.event.ListDataListener;
047
048 /**
049 * Provides standard implementations of some methods in {@link ListModel}.
050 *
051 * @author Ronald Veldema
052 * @author Andrew Selkirk
053 */
054 public abstract class AbstractListModel implements ListModel, Serializable
055 {
056 private static final long serialVersionUID = -3285184064379168730L;
057
058 /** List of ListDataListeners called for each change to the list. */
059 protected EventListenerList listenerList;
060
061 /**
062 * Creates a new model instance - initialises the event listener list.
063 */
064 public AbstractListModel()
065 {
066 listenerList = new EventListenerList();
067 }
068
069 /**
070 * Add a listener object to this model. The listener will be called
071 * any time the set of elements in the model is changed.
072 *
073 * @param listener The listener to add
074 */
075 public void addListDataListener(ListDataListener listener)
076 {
077 listenerList.add(ListDataListener.class, listener);
078 }
079
080 /**
081 * Add a listener object to this model. The listener will no longer be
082 * called when the set of elements in the model is changed.
083 *
084 * @param listener The listener to remove
085 */
086 public void removeListDataListener(ListDataListener listener)
087 {
088 listenerList.remove(ListDataListener.class, listener);
089 }
090
091 /**
092 * Call {@link ListDataListener#contentsChanged} on each element of the
093 * {@link #listenerList} which is a {@link ListDataListener}. The event
094 * fired has type {@link ListDataEvent#CONTENTS_CHANGED} and represents a
095 * change to the data elements in the range [startIndex, endIndex]
096 * inclusive.
097 *
098 * @param source The source of the change, typically <code>this</code>
099 * @param startIndex The index of the first element which changed
100 * @param endIndex The index of the last element which changed
101 */
102 protected void fireContentsChanged(Object source, int startIndex,
103 int endIndex)
104 {
105 ListDataEvent event = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED,
106 startIndex, endIndex);
107 ListDataListener[] listeners = getListDataListeners();
108
109 for (int index = 0; index < listeners.length; index++)
110 listeners[index].contentsChanged(event);
111 }
112
113 /**
114 * Call {@link ListDataListener#intervalAdded} on each element of the
115 * {@link #listenerList} which is a {@link ListDataListener}. The event
116 * fired has type {@link ListDataEvent#INTERVAL_ADDED} and represents an
117 * addition of the data elements in the range [startIndex, endIndex]
118 * inclusive.
119 *
120 * @param source The source of the change, typically <code>this</code>
121 * @param startIndex The index of the first new element
122 * @param endIndex The index of the last new element
123 */
124 protected void fireIntervalAdded(Object source, int startIndex, int endIndex)
125 {
126 ListDataEvent event =
127 new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED,
128 startIndex, endIndex);
129 ListDataListener[] listeners = getListDataListeners();
130
131 for (int index = 0; index < listeners.length; index++)
132 listeners[index].intervalAdded(event);
133 }
134
135 /**
136 * Call {@link ListDataListener#intervalRemoved} on each element of the
137 * {@link #listenerList} which is a {@link ListDataListener}. The event
138 * fired has type {@link ListDataEvent#INTERVAL_REMOVED} and represents a
139 * removal of the data elements in the range [startIndex, endIndex]
140 * inclusive.
141 *
142 * @param source The source of the change, typically <code>this</code>
143 * @param startIndex The index of the first element removed
144 * @param endIndex The index of the last element removed
145 */
146 protected void fireIntervalRemoved(Object source, int startIndex,
147 int endIndex)
148 {
149 ListDataEvent event =
150 new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED,
151 startIndex, endIndex);
152 ListDataListener[] listeners = getListDataListeners();
153
154 for (int index = 0; index < listeners.length; index++)
155 listeners[index].intervalRemoved(event);
156 }
157
158 /**
159 * Return the subset of {@link EventListener} objects found in this
160 * object's {@link #listenerList} which are elements of the specified
161 * type.
162 *
163 * @param listenerType The type of listeners to select
164 *
165 * @return The set of listeners of the specified type
166 */
167 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
168 {
169 return listenerList.getListeners(listenerType);
170 }
171
172 /**
173 * A synonym for <code>getListeners(ListDataListener.class)</code>.
174 *
175 * @return The set of ListDataListeners found in the {@link #listenerList}
176 */
177 public ListDataListener[] getListDataListeners()
178 {
179 return (ListDataListener[]) getListeners(ListDataListener.class);
180 }
181 }