OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / AbstractListModel.java
1 /* AbstractListModel.java --
2    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 package javax.swing;
40
41 import java.io.Serializable;
42 import java.util.EventListener;
43
44 import javax.swing.event.EventListenerList;
45 import javax.swing.event.ListDataEvent;
46 import javax.swing.event.ListDataListener;
47
48 /**
49  * AbstractListModel
50  *
51  * @author Ronald Veldema
52  * @author Andrew Selkirk
53  * @version 1.0
54  */
55 public abstract class AbstractListModel implements ListModel, Serializable
56 {
57   private static final long serialVersionUID = -3285184064379168730L;
58
59   /** List of ListDataListeners called for each change to the list. */
60   protected EventListenerList listenerList;
61
62   public AbstractListModel()
63   {
64     listenerList = new EventListenerList();
65   }
66
67   /**
68    * Add a listener object to this model. The listener will be called
69    * any time the set of elements in the model is changed.
70    *
71    * @param listener The listener to add
72    */
73   public void addListDataListener(ListDataListener listener)
74   {
75     listenerList.add(ListDataListener.class, listener);
76   }
77
78   /**
79    * Add a listener object to this model. The listener will no longer be
80    * called when the set of elements in the model is changed.
81    *
82    * @param listener The listener to remove
83    */
84   public void removeListDataListener(ListDataListener listener)
85   {
86     listenerList.remove(ListDataListener.class, listener);
87   }
88
89   /**
90    * Call {@link ListDataListener#contentsChanged} on each element of the
91    * {@link listenerList} which is a {@link ListDataListener}. The event
92    * fired has type {@ListDataEvent.CONTENTS_CHANGED} and represents a
93    * change to the data elements in the range [startIndex, endIndex]
94    * inclusive.
95    *
96    * @param source The source of the change, typically <code>this</code>
97    * @param startIndex The index of the first element which changed
98    * @param endIndex The index of the last element which changed
99    */
100   protected void fireContentsChanged(Object source, int startIndex,
101                                      int endIndex)
102   {
103     ListDataEvent event = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED,
104                                             startIndex, endIndex);
105     ListDataListener[] listeners = getListDataListeners();
106
107     for (int index = 0; index < listeners.length; index++)
108       listeners[index].contentsChanged(event);
109   }
110
111   /**
112    * Call {@link ListDataListener#intervalAdded} on each element of the
113    * {@link listenerList} which is a {@link ListDataListener}. The event
114    * fired has type {@ListDataEvent.INTERVAL_ADDED} and represents an
115    * addition of the data elements in the range [startIndex, endIndex]
116    * inclusive.
117    *
118    * @param source The source of the change, typically <code>this</code>
119    * @param startIndex The index of the first new element
120    * @param endIndex The index of the last new element
121    */
122   protected void fireIntervalAdded(Object source, int startIndex, int endIndex)
123   {
124     ListDataEvent event =
125       new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED,
126                         startIndex, endIndex);
127     ListDataListener[] listeners = getListDataListeners();
128
129     for (int index = 0; index < listeners.length; index++)
130       listeners[index].intervalAdded(event);
131   }
132
133   /**
134    * Call {@link ListDataListener#intervalRemoved} on each element of the
135    * {@link listenerList} which is a {@link ListDataListener}. The event
136    * fired has type {@ListDataEvent.INTERVAL_REMOVED} and represents a
137    * removal of the data elements in the range [startIndex, endIndex]
138    * inclusive.
139    *
140    * @param source The source of the change, typically <code>this</code>
141    * @param startIndex The index of the first element removed
142    * @param endIndex The index of the last element removed
143    */
144   protected void fireIntervalRemoved(Object source, int startIndex,
145                                      int endIndex)
146   {
147     ListDataEvent event =
148       new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED,
149                         startIndex, endIndex);
150     ListDataListener[] listeners = getListDataListeners();
151
152     for (int index = 0; index < listeners.length; index++)
153       listeners[index].intervalRemoved(event);
154   }
155
156   /**
157    * Return the subset of {@link EventListener} objects found in this
158    * object's {@link listenerList} which are elements of the specified
159    * type.
160    *
161    * @param listenerType The type of listeners to select
162    *
163    * @return The set of listeners of the specified type
164    */
165   public EventListener[] getListeners(Class listenerType)
166   {
167     return listenerList.getListeners(listenerType);
168   }
169
170   /**
171    * A synonym for <code>getListeners(ListDataListener.class)</code>.
172    *
173    * @return The set of ListDataListeners found in the {@link listenerList}
174    */
175   public ListDataListener[] getListDataListeners()
176   {
177     return (ListDataListener[]) getListeners(ListDataListener.class);
178   }
179 }