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