1. /*
  2. * @(#)DefaultSingleSelectionModel.java 1.25 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. * A generic implementation of SingleSelectionModel.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is appropriate
  20. * for short term storage or RMI between applications running the same
  21. * version of Swing. A future release of Swing will provide support for
  22. * long term persistence.
  23. *
  24. * @version 1.25 02/02/00
  25. * @author Dave Moore
  26. */
  27. public class DefaultSingleSelectionModel implements SingleSelectionModel,
  28. Serializable {
  29. /* Only one ModelChangeEvent is needed per model instance since the
  30. * event's only (read-only) state is the source property. The source
  31. * of events generated here is always "this".
  32. */
  33. protected transient ChangeEvent changeEvent = null;
  34. /** The collection of registered listeners */
  35. protected EventListenerList listenerList = new EventListenerList();
  36. private int index = -1;
  37. // implements javax.swing.SingleSelectionModel
  38. public int getSelectedIndex() {
  39. return index;
  40. }
  41. // implements javax.swing.SingleSelectionModel
  42. public void setSelectedIndex(int index) {
  43. if (this.index != index) {
  44. this.index = index;
  45. fireStateChanged();
  46. }
  47. }
  48. // implements javax.swing.SingleSelectionModel
  49. public void clearSelection() {
  50. setSelectedIndex(-1);
  51. }
  52. // implements javax.swing.SingleSelectionModel
  53. public boolean isSelected() {
  54. boolean ret = false;
  55. if (getSelectedIndex() != -1) {
  56. ret = true;
  57. }
  58. return ret;
  59. }
  60. /**
  61. * Adds a ChangeListener to the button.
  62. */
  63. public void addChangeListener(ChangeListener l) {
  64. listenerList.add(ChangeListener.class, l);
  65. }
  66. /**
  67. * Removes a ChangeListener from the button.
  68. */
  69. public void removeChangeListener(ChangeListener l) {
  70. listenerList.remove(ChangeListener.class, l);
  71. }
  72. /*
  73. * Notify all listeners that have registered interest for
  74. * notification on this event type. The event instance
  75. * is lazily created using the parameters passed into
  76. * the fire method.
  77. * @see EventListenerList
  78. */
  79. protected void fireStateChanged() {
  80. // Guaranteed to return a non-null array
  81. Object[] listeners = listenerList.getListenerList();
  82. // Process the listeners last to first, notifying
  83. // those that are interested in this event
  84. for (int i = listeners.length-2; i>=0; i-=2) {
  85. if (listeners[i]==ChangeListener.class) {
  86. // Lazily create the event:
  87. if (changeEvent == null)
  88. changeEvent = new ChangeEvent(this);
  89. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  90. }
  91. }
  92. }
  93. /**
  94. * Return an array of all the listeners of the given type that
  95. * were added to this model.
  96. *
  97. * @returns all of the objects recieving <em>listenerType</em> notifications
  98. * from this model
  99. *
  100. * @since 1.3
  101. */
  102. public EventListener[] getListeners(Class listenerType) {
  103. return listenerList.getListeners(listenerType);
  104. }
  105. }