KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > AbstractListModel


1 /*
2  * @(#)AbstractListModel.java 1.34 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import javax.swing.event.*;
11 import java.io.Serializable JavaDoc;
12 import java.util.EventListener JavaDoc;
13
14 /**
15  * The abstract definition for the data model that provides
16  * a <code>List</code> with its contents.
17  * <p>
18  * <strong>Warning:</strong>
19  * Serialized objects of this class will not be compatible with
20  * future Swing releases. The current serialization support is
21  * appropriate for short term storage or RMI between applications running
22  * the same version of Swing. As of 1.4, support for long term storage
23  * of all JavaBeans<sup><font size="-2">TM</font></sup>
24  * has been added to the <code>java.beans</code> package.
25  * Please see {@link java.beans.XMLEncoder}.
26  *
27  * @version 1.34 05/05/04
28  * @author Hans Muller
29  */

30 public abstract class AbstractListModel implements ListModel JavaDoc, Serializable JavaDoc
31 {
32     protected EventListenerList listenerList = new EventListenerList();
33
34
35     /**
36      * Adds a listener to the list that's notified each time a change
37      * to the data model occurs.
38      *
39      * @param l the <code>ListDataListener</code> to be added
40      */

41     public void addListDataListener(ListDataListener l) {
42     listenerList.add(ListDataListener.class, l);
43     }
44
45
46     /**
47      * Removes a listener from the list that's notified each time a
48      * change to the data model occurs.
49      *
50      * @param l the <code>ListDataListener</code> to be removed
51      */

52     public void removeListDataListener(ListDataListener l) {
53     listenerList.remove(ListDataListener.class, l);
54     }
55
56
57     /**
58      * Returns an array of all the list data listeners
59      * registered on this <code>AbstractListModel</code>.
60      *
61      * @return all of this model's <code>ListDataListener</code>s,
62      * or an empty array if no list data listeners
63      * are currently registered
64      *
65      * @see #addListDataListener
66      * @see #removeListDataListener
67      *
68      * @since 1.4
69      */

70     public ListDataListener[] getListDataListeners() {
71         return (ListDataListener[])listenerList.getListeners(
72                 ListDataListener.class);
73     }
74
75
76     /**
77      * <code>AbstractListModel</code> subclasses must call this method
78      * <b>after</b>
79      * one or more elements of the list change. The changed elements
80      * are specified by the closed interval index0, index1 -- the endpoints
81      * are included. Note that
82      * index0 need not be less than or equal to index1.
83      *
84      * @param source the <code>ListModel</code> that changed, typically "this"
85      * @param index0 one end of the new interval
86      * @param index1 the other end of the new interval
87      * @see EventListenerList
88      * @see DefaultListModel
89      */

90     protected void fireContentsChanged(Object JavaDoc source, int index0, int index1)
91     {
92     Object JavaDoc[] listeners = listenerList.getListenerList();
93     ListDataEvent e = null;
94
95     for (int i = listeners.length - 2; i >= 0; i -= 2) {
96         if (listeners[i] == ListDataListener.class) {
97         if (e == null) {
98             e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
99         }
100         ((ListDataListener)listeners[i+1]).contentsChanged(e);
101         }
102     }
103     }
104
105
106     /**
107      * <code>AbstractListModel</code> subclasses must call this method
108      * <b>after</b>
109      * one or more elements are added to the model. The new elements
110      * are specified by a closed interval index0, index1 -- the enpoints
111      * are included. Note that
112      * index0 need not be less than or equal to index1.
113      *
114      * @param source the <code>ListModel</code> that changed, typically "this"
115      * @param index0 one end of the new interval
116      * @param index1 the other end of the new interval
117      * @see EventListenerList
118      * @see DefaultListModel
119      */

120     protected void fireIntervalAdded(Object JavaDoc source, int index0, int index1)
121     {
122     Object JavaDoc[] listeners = listenerList.getListenerList();
123     ListDataEvent e = null;
124
125     for (int i = listeners.length - 2; i >= 0; i -= 2) {
126         if (listeners[i] == ListDataListener.class) {
127         if (e == null) {
128             e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
129         }
130         ((ListDataListener)listeners[i+1]).intervalAdded(e);
131         }
132     }
133     }
134
135
136     /**
137      * <code>AbstractListModel</code> subclasses must call this method
138      * <b>after</b> one or more elements are removed from the model.
139      * <code>index0</code> and <code>index1</code> are the end points
140      * of the interval that's been removed. Note that <code>index0</code>
141      * need not be less than or equal to <code>index1</code>.
142      *
143      * @param source the <code>ListModel</code> that changed, typically "this"
144      * @param index0 one end of the removed interval,
145      * including <code>index0</code>
146      * @param index1 the other end of the removed interval,
147      * including <code>index1</code>
148      * @see EventListenerList
149      * @see DefaultListModel
150      */

151     protected void fireIntervalRemoved(Object JavaDoc source, int index0, int index1)
152     {
153     Object JavaDoc[] listeners = listenerList.getListenerList();
154     ListDataEvent e = null;
155
156     for (int i = listeners.length - 2; i >= 0; i -= 2) {
157         if (listeners[i] == ListDataListener.class) {
158         if (e == null) {
159             e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
160         }
161         ((ListDataListener)listeners[i+1]).intervalRemoved(e);
162         }
163     }
164     }
165
166     /**
167      * Returns an array of all the objects currently registered as
168      * <code><em>Foo</em>Listener</code>s
169      * upon this model.
170      * <code><em>Foo</em>Listener</code>s
171      * are registered using the <code>add<em>Foo</em>Listener</code> method.
172      * <p>
173      * You can specify the <code>listenerType</code> argument
174      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
175      * For example, you can query a list model
176      * <code>m</code>
177      * for its list data listeners
178      * with the following code:
179      *
180      * <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
181      *
182      * If no such listeners exist,
183      * this method returns an empty array.
184      *
185      * @param listenerType the type of listeners requested;
186      * this parameter should specify an interface
187      * that descends from <code>java.util.EventListener</code>
188      * @return an array of all objects registered as
189      * <code><em>Foo</em>Listener</code>s
190      * on this model,
191      * or an empty array if no such
192      * listeners have been added
193      * @exception ClassCastException if <code>listenerType</code> doesn't
194      * specify a class or interface that implements
195      * <code>java.util.EventListener</code>
196      *
197      * @see #getListDataListeners
198      *
199      * @since 1.3
200      */

201     public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
202     return listenerList.getListeners(listenerType);
203     }
204 }
205
Popular Tags