author | rupashka |
Mon, 23 Nov 2009 20:57:17 +0300 | |
changeset 4378 | ef21a120cb18 |
parent 1639 | a97859015238 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.swing; |
|
27 |
||
28 |
import javax.swing.event.*; |
|
29 |
import java.io.Serializable; |
|
30 |
import java.util.EventListener; |
|
31 |
||
32 |
/** |
|
33 |
* The abstract definition for the data model that provides |
|
34 |
* a <code>List</code> with its contents. |
|
35 |
* <p> |
|
36 |
* <strong>Warning:</strong> |
|
37 |
* Serialized objects of this class will not be compatible with |
|
38 |
* future Swing releases. The current serialization support is |
|
39 |
* appropriate for short term storage or RMI between applications running |
|
40 |
* the same version of Swing. As of 1.4, support for long term storage |
|
41 |
* of all JavaBeans<sup><font size="-2">TM</font></sup> |
|
42 |
* has been added to the <code>java.beans</code> package. |
|
43 |
* Please see {@link java.beans.XMLEncoder}. |
|
44 |
* |
|
4378 | 45 |
* @param <E> the type of the elements of this model |
46 |
* |
|
2 | 47 |
* @author Hans Muller |
48 |
*/ |
|
4378 | 49 |
public abstract class AbstractListModel<E> implements ListModel<E>, Serializable |
2 | 50 |
{ |
51 |
protected EventListenerList listenerList = new EventListenerList(); |
|
52 |
||
53 |
||
54 |
/** |
|
55 |
* Adds a listener to the list that's notified each time a change |
|
56 |
* to the data model occurs. |
|
57 |
* |
|
58 |
* @param l the <code>ListDataListener</code> to be added |
|
59 |
*/ |
|
60 |
public void addListDataListener(ListDataListener l) { |
|
61 |
listenerList.add(ListDataListener.class, l); |
|
62 |
} |
|
63 |
||
64 |
||
65 |
/** |
|
66 |
* Removes a listener from the list that's notified each time a |
|
67 |
* change to the data model occurs. |
|
68 |
* |
|
69 |
* @param l the <code>ListDataListener</code> to be removed |
|
70 |
*/ |
|
71 |
public void removeListDataListener(ListDataListener l) { |
|
72 |
listenerList.remove(ListDataListener.class, l); |
|
73 |
} |
|
74 |
||
75 |
||
76 |
/** |
|
77 |
* Returns an array of all the list data listeners |
|
78 |
* registered on this <code>AbstractListModel</code>. |
|
79 |
* |
|
80 |
* @return all of this model's <code>ListDataListener</code>s, |
|
81 |
* or an empty array if no list data listeners |
|
82 |
* are currently registered |
|
83 |
* |
|
84 |
* @see #addListDataListener |
|
85 |
* @see #removeListDataListener |
|
86 |
* |
|
87 |
* @since 1.4 |
|
88 |
*/ |
|
89 |
public ListDataListener[] getListDataListeners() { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
90 |
return listenerList.getListeners(ListDataListener.class); |
2 | 91 |
} |
92 |
||
93 |
||
94 |
/** |
|
95 |
* <code>AbstractListModel</code> subclasses must call this method |
|
96 |
* <b>after</b> |
|
97 |
* one or more elements of the list change. The changed elements |
|
98 |
* are specified by the closed interval index0, index1 -- the endpoints |
|
99 |
* are included. Note that |
|
100 |
* index0 need not be less than or equal to index1. |
|
101 |
* |
|
102 |
* @param source the <code>ListModel</code> that changed, typically "this" |
|
103 |
* @param index0 one end of the new interval |
|
104 |
* @param index1 the other end of the new interval |
|
105 |
* @see EventListenerList |
|
106 |
* @see DefaultListModel |
|
107 |
*/ |
|
108 |
protected void fireContentsChanged(Object source, int index0, int index1) |
|
109 |
{ |
|
110 |
Object[] listeners = listenerList.getListenerList(); |
|
111 |
ListDataEvent e = null; |
|
112 |
||
113 |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
114 |
if (listeners[i] == ListDataListener.class) { |
|
115 |
if (e == null) { |
|
116 |
e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1); |
|
117 |
} |
|
118 |
((ListDataListener)listeners[i+1]).contentsChanged(e); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/** |
|
125 |
* <code>AbstractListModel</code> subclasses must call this method |
|
126 |
* <b>after</b> |
|
127 |
* one or more elements are added to the model. The new elements |
|
128 |
* are specified by a closed interval index0, index1 -- the enpoints |
|
129 |
* are included. Note that |
|
130 |
* index0 need not be less than or equal to index1. |
|
131 |
* |
|
132 |
* @param source the <code>ListModel</code> that changed, typically "this" |
|
133 |
* @param index0 one end of the new interval |
|
134 |
* @param index1 the other end of the new interval |
|
135 |
* @see EventListenerList |
|
136 |
* @see DefaultListModel |
|
137 |
*/ |
|
138 |
protected void fireIntervalAdded(Object source, int index0, int index1) |
|
139 |
{ |
|
140 |
Object[] listeners = listenerList.getListenerList(); |
|
141 |
ListDataEvent e = null; |
|
142 |
||
143 |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
144 |
if (listeners[i] == ListDataListener.class) { |
|
145 |
if (e == null) { |
|
146 |
e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1); |
|
147 |
} |
|
148 |
((ListDataListener)listeners[i+1]).intervalAdded(e); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
||
154 |
/** |
|
155 |
* <code>AbstractListModel</code> subclasses must call this method |
|
156 |
* <b>after</b> one or more elements are removed from the model. |
|
157 |
* <code>index0</code> and <code>index1</code> are the end points |
|
158 |
* of the interval that's been removed. Note that <code>index0</code> |
|
159 |
* need not be less than or equal to <code>index1</code>. |
|
160 |
* |
|
161 |
* @param source the <code>ListModel</code> that changed, typically "this" |
|
162 |
* @param index0 one end of the removed interval, |
|
163 |
* including <code>index0</code> |
|
164 |
* @param index1 the other end of the removed interval, |
|
165 |
* including <code>index1</code> |
|
166 |
* @see EventListenerList |
|
167 |
* @see DefaultListModel |
|
168 |
*/ |
|
169 |
protected void fireIntervalRemoved(Object source, int index0, int index1) |
|
170 |
{ |
|
171 |
Object[] listeners = listenerList.getListenerList(); |
|
172 |
ListDataEvent e = null; |
|
173 |
||
174 |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
|
175 |
if (listeners[i] == ListDataListener.class) { |
|
176 |
if (e == null) { |
|
177 |
e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1); |
|
178 |
} |
|
179 |
((ListDataListener)listeners[i+1]).intervalRemoved(e); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Returns an array of all the objects currently registered as |
|
186 |
* <code><em>Foo</em>Listener</code>s |
|
187 |
* upon this model. |
|
188 |
* <code><em>Foo</em>Listener</code>s |
|
189 |
* are registered using the <code>add<em>Foo</em>Listener</code> method. |
|
190 |
* <p> |
|
191 |
* You can specify the <code>listenerType</code> argument |
|
192 |
* with a class literal, such as <code><em>Foo</em>Listener.class</code>. |
|
193 |
* For example, you can query a list model |
|
194 |
* <code>m</code> |
|
195 |
* for its list data listeners |
|
196 |
* with the following code: |
|
197 |
* |
|
198 |
* <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre> |
|
199 |
* |
|
200 |
* If no such listeners exist, |
|
201 |
* this method returns an empty array. |
|
202 |
* |
|
203 |
* @param listenerType the type of listeners requested; |
|
204 |
* this parameter should specify an interface |
|
205 |
* that descends from <code>java.util.EventListener</code> |
|
206 |
* @return an array of all objects registered as |
|
207 |
* <code><em>Foo</em>Listener</code>s |
|
208 |
* on this model, |
|
209 |
* or an empty array if no such |
|
210 |
* listeners have been added |
|
211 |
* @exception ClassCastException if <code>listenerType</code> doesn't |
|
212 |
* specify a class or interface that implements |
|
213 |
* <code>java.util.EventListener</code> |
|
214 |
* |
|
215 |
* @see #getListDataListeners |
|
216 |
* |
|
217 |
* @since 1.3 |
|
218 |
*/ |
|
219 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
|
220 |
return listenerList.getListeners(listenerType); |
|
221 |
} |
|
222 |
} |