1. /*
  2. * @(#)AbstractListModel.java 1.20 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import javax.swing.event.*;
  9. import java.io.Serializable;
  10. /**
  11. * The Abstract definition for the data model the provides
  12. * a List with its contents.
  13. * <p>
  14. * <strong>Warning:</strong>
  15. * Serialized objects of this class will not be compatible with
  16. * future Swing releases. The current serialization support is appropriate
  17. * for short term storage or RMI between applications running the same
  18. * version of Swing. A future release of Swing will provide support for
  19. * long term persistence.
  20. *
  21. * @version 1.20 11/29/01
  22. * @author Hans Muller
  23. */
  24. public abstract class AbstractListModel implements ListModel, Serializable
  25. {
  26. protected EventListenerList listenerList = new EventListenerList();
  27. /**
  28. * Add a listener to the list that's notified each time a change
  29. * to the data model occurs.
  30. * @param l the ListDataListener
  31. */
  32. public void addListDataListener(ListDataListener l) {
  33. listenerList.add(ListDataListener.class, l);
  34. }
  35. /**
  36. * Remove a listener from the list that's notified each time a
  37. * change to the data model occurs.
  38. * @param l the ListDataListener
  39. */
  40. public void removeListDataListener(ListDataListener l) {
  41. listenerList.remove(ListDataListener.class, l);
  42. }
  43. /**
  44. * AbstractListModel subclasses must call this method <b>after</b>
  45. * one or more elements of the list change. The changed elements
  46. * are specified by a closed interval index0, index1, i.e. the
  47. * range that includes both index0 and index1. Note that
  48. * index0 need not be less than or equal to index1.
  49. *
  50. * @param source The ListModel that changed, typically "this".
  51. * @param index0 One end of the new interval.
  52. * @param index1 The other end of the new interval.
  53. * @see EventListenerList
  54. * @see DefaultListModel
  55. */
  56. protected void fireContentsChanged(Object source, int index0, int index1)
  57. {
  58. Object[] listeners = listenerList.getListenerList();
  59. ListDataEvent e = null;
  60. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  61. if (listeners[i] == ListDataListener.class) {
  62. if (e == null) {
  63. e = new ListDataEvent(source, ListDataEvent.CONTENTS_CHANGED, index0, index1);
  64. }
  65. ((ListDataListener)listeners[i+1]).contentsChanged(e);
  66. }
  67. }
  68. }
  69. /**
  70. * AbstractListModel subclasses must call this method <b>after</b>
  71. * one or more elements are added to the model. The new elements
  72. * are specified by a closed interval index0, index1, i.e. the
  73. * range that includes both index0 and index1. Note that
  74. * index0 need not be less than or equal to index1.
  75. *
  76. * @param source The ListModel that changed, typically "this".
  77. * @param index0 One end of the new interval.
  78. * @param index1 The other end of the new interval.
  79. * @see EventListenerList
  80. * @see DefaultListModel
  81. */
  82. protected void fireIntervalAdded(Object source, int index0, int index1)
  83. {
  84. Object[] listeners = listenerList.getListenerList();
  85. ListDataEvent e = null;
  86. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  87. if (listeners[i] == ListDataListener.class) {
  88. if (e == null) {
  89. e = new ListDataEvent(source, ListDataEvent.INTERVAL_ADDED, index0, index1);
  90. }
  91. ((ListDataListener)listeners[i+1]).intervalAdded(e);
  92. }
  93. }
  94. }
  95. /**
  96. * AbstractListModel subclasses must call this method <b>after</b>
  97. * one or more elements are removed from the model. The new elements
  98. * are specified by a closed interval index0, index1, i.e. the
  99. * range that includes both index0 and index1. Note that
  100. * index0 need not be less than or equal to index1.
  101. *
  102. * @param source The ListModel 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 fireIntervalRemoved(Object source, int index0, int index1)
  109. {
  110. Object[] listeners = listenerList.getListenerList();
  111. ListDataEvent e = null;
  112. for (int i = listeners.length - 2; i >= 0; i -= 2) {
  113. if (listeners[i] == ListDataListener.class) {
  114. if (e == null) {
  115. e = new ListDataEvent(source, ListDataEvent.INTERVAL_REMOVED, index0, index1);
  116. }
  117. ((ListDataListener)listeners[i+1]).intervalRemoved(e);
  118. }
  119. }
  120. }
  121. }