1 /* 2 * Copyright (c) 1997, 2014, 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 protected EventListenerList listenerList = new EventListenerList(); 54 55 56 /** 57 * Adds a listener to the list that's notified each time a change 58 * to the data model occurs. 59 * 60 * @param l the <code>ListDataListener</code> to be added 61 */ 62 public void addListDataListener(ListDataListener l) { 63 listenerList.add(ListDataListener.class, l); 64 } 65 66 67 /** 68 * Removes a listener from the list that's notified each time a 69 * change to the data model occurs. 70 * 71 * @param l the <code>ListDataListener</code> to be removed 72 */ 73 public void removeListDataListener(ListDataListener l) { 74 listenerList.remove(ListDataListener.class, l); 75 } 76 77 78 /** 79 * Returns an array of all the list data listeners 80 * registered on this <code>AbstractListModel</code>. 81 * 82 * @return all of this model's <code>ListDataListener</code>s, 83 * or an empty array if no list data listeners 84 * are currently registered 85 * 86 * @see #addListDataListener 87 * @see #removeListDataListener 88 * 89 * @since 1.4 90 */ 91 public ListDataListener[] getListDataListeners() { 92 return listenerList.getListeners(ListDataListener.class); 93 } 94 95 96 /** 97 * <code>AbstractListModel</code> subclasses must call this method 98 * <b>after</b> 99 * one or more elements of the list change. The changed elements 100 * are specified by the closed interval index0, index1 -- the endpoints 101 * are included. Note that 102 * index0 need not be less than or equal to index1. 103 * 104 * @param source the <code>ListModel</code> that changed, typically "this" 105 * @param index0 one end of the new interval 106 * @param index1 the other end of the new interval 107 * @see EventListenerList 108 * @see DefaultListModel 109 */ 110 protected void fireContentsChanged(Object source, int index0, int index1) 111 { 112 Object[] listeners = listenerList.getListenerList(); 113 ListDataEvent e = null; 114 115 for (int i = listeners.length - 2; i >= 0; i -= 2) { 116 if (listeners[i] == ListDataListener.class) { 117 if (e == null) { 118 e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1); 119 } 120 ((ListDataListener)listeners[i+1]).contentsChanged(e); 121 } 122 } 123 } 124 125 126 /** 127 * <code>AbstractListModel</code> subclasses must call this method 128 * <b>after</b> 129 * one or more elements are added to the model. The new elements 130 * are specified by a closed interval index0, index1 -- the enpoints 131 * are included. Note that 132 * index0 need not be less than or equal to index1. 133 * 134 * @param source the <code>ListModel</code> that changed, typically "this" 135 * @param index0 one end of the new interval 136 * @param index1 the other end of the new interval 137 * @see EventListenerList 138 * @see DefaultListModel 139 */ 140 protected void fireIntervalAdded(Object source, int index0, int index1) 141 { 142 Object[] listeners = listenerList.getListenerList(); 143 ListDataEvent e = null; 144 145 for (int i = listeners.length - 2; i >= 0; i -= 2) { 146 if (listeners[i] == ListDataListener.class) { 147 if (e == null) { 148 e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1); 149 } 150 ((ListDataListener)listeners[i+1]).intervalAdded(e); 151 } 152 } 153 } 154 155 156 /** 157 * <code>AbstractListModel</code> subclasses must call this method 158 * <b>after</b> one or more elements are removed from the model. 159 * <code>index0</code> and <code>index1</code> are the end points 160 * of the interval that's been removed. Note that <code>index0</code> 161 * need not be less than or equal to <code>index1</code>. 162 * 163 * @param source the <code>ListModel</code> that changed, typically "this" 164 * @param index0 one end of the removed interval, 165 * including <code>index0</code> 166 * @param index1 the other end of the removed interval, 167 * including <code>index1</code> 168 * @see EventListenerList 169 * @see DefaultListModel 170 */ 171 protected void fireIntervalRemoved(Object source, int index0, int index1) 172 { 173 Object[] listeners = listenerList.getListenerList(); 174 ListDataEvent e = null; 175 176 for (int i = listeners.length - 2; i >= 0; i -= 2) { 177 if (listeners[i] == ListDataListener.class) { 178 if (e == null) { 179 e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1); 180 } 181 ((ListDataListener)listeners[i+1]).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 <T> the type of {@code EventListener} class being requested 206 * @param listenerType the type of listeners requested; 207 * this parameter should specify an interface 208 * that descends from <code>java.util.EventListener</code> 209 * @return an array of all objects registered as 210 * <code><em>Foo</em>Listener</code>s 211 * on this model, 212 * or an empty array if no such 213 * listeners have been added 214 * @exception ClassCastException if <code>listenerType</code> doesn't 215 * specify a class or interface that implements 216 * <code>java.util.EventListener</code> 217 * 218 * @see #getListDataListeners 219 * 220 * @since 1.3 221 */ 222 public <T extends EventListener> T[] getListeners(Class<T> listenerType) { 223 return listenerList.getListeners(listenerType); 224 } 225 }