Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/AbstractListModel.java
41153 views
1
/*
2
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* 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
42
* has been added to the <code>java.beans</code> package.
43
* Please see {@link java.beans.XMLEncoder}.
44
*
45
* @param <E> the type of the elements of this model
46
*
47
* @author Hans Muller
48
* @since 1.2
49
*/
50
@SuppressWarnings("serial") // Same-version serialization only
51
public abstract class AbstractListModel<E> implements ListModel<E>, Serializable
52
{
53
/**
54
* The listener list.
55
*/
56
protected EventListenerList listenerList = new EventListenerList();
57
58
/**
59
* Constructor for subclasses to call.
60
*/
61
protected AbstractListModel() {}
62
63
/**
64
* Adds a listener to the list that's notified each time a change
65
* to the data model occurs.
66
*
67
* @param l the <code>ListDataListener</code> to be added
68
*/
69
public void addListDataListener(ListDataListener l) {
70
listenerList.add(ListDataListener.class, l);
71
}
72
73
74
/**
75
* Removes a listener from the list that's notified each time a
76
* change to the data model occurs.
77
*
78
* @param l the <code>ListDataListener</code> to be removed
79
*/
80
public void removeListDataListener(ListDataListener l) {
81
listenerList.remove(ListDataListener.class, l);
82
}
83
84
85
/**
86
* Returns an array of all the list data listeners
87
* registered on this <code>AbstractListModel</code>.
88
*
89
* @return all of this model's <code>ListDataListener</code>s,
90
* or an empty array if no list data listeners
91
* are currently registered
92
*
93
* @see #addListDataListener
94
* @see #removeListDataListener
95
*
96
* @since 1.4
97
*/
98
public ListDataListener[] getListDataListeners() {
99
return listenerList.getListeners(ListDataListener.class);
100
}
101
102
103
/**
104
* <code>AbstractListModel</code> subclasses must call this method
105
* <b>after</b>
106
* one or more elements of the list change. The changed elements
107
* are specified by the closed interval index0, index1 -- the endpoints
108
* are included. Note that
109
* index0 need not be less than or equal to index1.
110
*
111
* @param source the <code>ListModel</code> that changed, typically "this"
112
* @param index0 one end of the new interval
113
* @param index1 the other end of the new interval
114
* @see EventListenerList
115
* @see DefaultListModel
116
*/
117
protected void fireContentsChanged(Object source, int index0, int index1)
118
{
119
Object[] listeners = listenerList.getListenerList();
120
ListDataEvent e = null;
121
122
for (int i = listeners.length - 2; i >= 0; i -= 2) {
123
if (listeners[i] == ListDataListener.class) {
124
if (e == null) {
125
e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
126
}
127
((ListDataListener)listeners[i+1]).contentsChanged(e);
128
}
129
}
130
}
131
132
133
/**
134
* <code>AbstractListModel</code> subclasses must call this method
135
* <b>after</b>
136
* one or more elements are added to the model. The new elements
137
* are specified by a closed interval index0, index1 -- the enpoints
138
* are included. Note that
139
* index0 need not be less than or equal to index1.
140
*
141
* @param source the <code>ListModel</code> that changed, typically "this"
142
* @param index0 one end of the new interval
143
* @param index1 the other end of the new interval
144
* @see EventListenerList
145
* @see DefaultListModel
146
*/
147
protected void fireIntervalAdded(Object source, int index0, int index1)
148
{
149
Object[] listeners = listenerList.getListenerList();
150
ListDataEvent e = null;
151
152
for (int i = listeners.length - 2; i >= 0; i -= 2) {
153
if (listeners[i] == ListDataListener.class) {
154
if (e == null) {
155
e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
156
}
157
((ListDataListener)listeners[i+1]).intervalAdded(e);
158
}
159
}
160
}
161
162
163
/**
164
* <code>AbstractListModel</code> subclasses must call this method
165
* <b>after</b> one or more elements are removed from the model.
166
* <code>index0</code> and <code>index1</code> are the end points
167
* of the interval that's been removed. Note that <code>index0</code>
168
* need not be less than or equal to <code>index1</code>.
169
*
170
* @param source the <code>ListModel</code> that changed, typically "this"
171
* @param index0 one end of the removed interval,
172
* including <code>index0</code>
173
* @param index1 the other end of the removed interval,
174
* including <code>index1</code>
175
* @see EventListenerList
176
* @see DefaultListModel
177
*/
178
protected void fireIntervalRemoved(Object source, int index0, int index1)
179
{
180
Object[] listeners = listenerList.getListenerList();
181
ListDataEvent e = null;
182
183
for (int i = listeners.length - 2; i >= 0; i -= 2) {
184
if (listeners[i] == ListDataListener.class) {
185
if (e == null) {
186
e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
187
}
188
((ListDataListener)listeners[i+1]).intervalRemoved(e);
189
}
190
}
191
}
192
193
/**
194
* Returns an array of all the objects currently registered as
195
* <code><em>Foo</em>Listener</code>s
196
* upon this model.
197
* <code><em>Foo</em>Listener</code>s
198
* are registered using the <code>add<em>Foo</em>Listener</code> method.
199
* <p>
200
* You can specify the <code>listenerType</code> argument
201
* with a class literal, such as <code><em>Foo</em>Listener.class</code>.
202
* For example, you can query a list model
203
* <code>m</code>
204
* for its list data listeners
205
* with the following code:
206
*
207
* <pre>ListDataListener[] ldls = (ListDataListener[])(m.getListeners(ListDataListener.class));</pre>
208
*
209
* If no such listeners exist,
210
* this method returns an empty array.
211
*
212
* @param <T> the type of {@code EventListener} class being requested
213
* @param listenerType the type of listeners requested;
214
* this parameter should specify an interface
215
* that descends from <code>java.util.EventListener</code>
216
* @return an array of all objects registered as
217
* <code><em>Foo</em>Listener</code>s
218
* on this model,
219
* or an empty array if no such
220
* listeners have been added
221
* @exception ClassCastException if <code>listenerType</code> doesn't
222
* specify a class or interface that implements
223
* <code>java.util.EventListener</code>
224
*
225
* @see #getListDataListeners
226
*
227
* @since 1.3
228
*/
229
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
230
return listenerList.getListeners(listenerType);
231
}
232
}
233
234