1. /*
  2. * @(#)AbstractListModel.java 1.22 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import javax.swing.event.*;
  12. import java.io.Serializable;
  13. import java.util.EventListener;
  14. /**
  15. * The Abstract definition for the data model the provides
  16. * a List 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 appropriate
  21. * for short term storage or RMI between applications running the same
  22. * version of Swing. A future release of Swing will provide support for
  23. * long term persistence.
  24. *
  25. * @version 1.22 02/02/00
  26. * @author Hans Muller
  27. */
  28. public abstract class AbstractListModel implements ListModel, Serializable
  29. {
  30. protected EventListenerList listenerList = new EventListenerList();
  31. /**
  32. * Add a listener to the list that's notified each time a change
  33. * to the data model occurs.
  34. * @param l the ListDataListener
  35. */
  36. public void addListDataListener(ListDataListener l) {
  37. listenerList.add(ListDataListener.class, l);
  38. }
  39. /**
  40. * Remove a listener from the list that's notified each time a
  41. * change to the data model occurs.
  42. * @param l the ListDataListener
  43. */
  44. public void removeListDataListener(ListDataListener l) {
  45. listenerList.remove(ListDataListener.class, l);
  46. }
  47. /**
  48. * AbstractListModel subclasses must call this method <b>after</b>
  49. * one or more elements of the list change. The changed elements
  50. * are specified by a closed interval index0, index1, i.e. the
  51. * range that includes both index0 and index1. Note that
  52. * index0 need not be less than or equal to index1.
  53. *
  54. * @param source The ListModel that changed, typically "this".
  55. * @param index0 One end of the new interval.
  56. * @param index1 The other end of the new interval.
  57. * @see EventListenerList
  58. * @see DefaultListModel
  59. */
  60. protected void fireContentsChanged(Object source, int index0, int index1)
  61. {
  62. Object[] listeners = listenerList.getListenerList();
  63. ListDataEvent e = null;
  64. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  65. if (listeners[i] == ListDataListener.class) {
  66. if (e == null) {
  67. e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
  68. }
  69. ((ListDataListener)listeners[i+1]).contentsChanged(e);
  70. }
  71. }
  72. }
  73. /**
  74. * AbstractListModel subclasses must call this method <b>after</b>
  75. * one or more elements are added to the model. The new elements
  76. * are specified by a closed interval index0, index1, i.e. the
  77. * range that includes both index0 and index1. Note that
  78. * index0 need not be less than or equal to index1.
  79. *
  80. * @param source The ListModel that changed, typically "this".
  81. * @param index0 One end of the new interval.
  82. * @param index1 The other end of the new interval.
  83. * @see EventListenerList
  84. * @see DefaultListModel
  85. */
  86. protected void fireIntervalAdded(Object source, int index0, int index1)
  87. {
  88. Object[] listeners = listenerList.getListenerList();
  89. ListDataEvent e = null;
  90. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  91. if (listeners[i] == ListDataListener.class) {
  92. if (e == null) {
  93. e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
  94. }
  95. ((ListDataListener)listeners[i+1]).intervalAdded(e);
  96. }
  97. }
  98. }
  99. /**
  100. * AbstractListModel subclasses must call this method <b>after</b>
  101. * one or more elements are removed from the model. The new elements
  102. * are specified by a closed interval index0, index1, i.e. the
  103. * range that includes both index0 and index1. Note that
  104. * index0 need not be less than or equal to index1.
  105. *
  106. * @param source The ListModel that changed, typically "this".
  107. * @param index0 One end of the new interval.
  108. * @param index1 The other end of the new interval.
  109. * @see EventListenerList
  110. * @see DefaultListModel
  111. */
  112. protected void fireIntervalRemoved(Object source, int index0, int index1)
  113. {
  114. Object[] listeners = listenerList.getListenerList();
  115. ListDataEvent e = null;
  116. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  117. if (listeners[i] == ListDataListener.class) {
  118. if (e == null) {
  119. e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
  120. }
  121. ((ListDataListener)listeners[i+1]).intervalRemoved(e);
  122. }
  123. }
  124. }
  125. /**
  126. * Return an array of all the listeners of the given type that
  127. * were added to this model.
  128. *
  129. * @returns all of the objects recieving <em>listenerType</em> notifications
  130. * from this model
  131. *
  132. * @since 1.3
  133. */
  134. public EventListener[] getListeners(Class listenerType) {
  135. return listenerList.getListeners(listenerType);
  136. }
  137. }