1. /*
  2. * @(#)DefaultSingleSelectionModel.java 1.33 03/01/23
  3. *
  4. * Copyright 2003 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. import java.util.EventListener;
  11. /**
  12. * A generic implementation of SingleSelectionModel.
  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
  17. * appropriate for short term storage or RMI between applications running
  18. * the same version of Swing. As of 1.4, support for long term storage
  19. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  20. * has been added to the <code>java.beans</code> package.
  21. * Please see {@link java.beans.XMLEncoder}.
  22. *
  23. * @version 1.33 01/23/03
  24. * @author Dave Moore
  25. */
  26. public class DefaultSingleSelectionModel implements SingleSelectionModel,
  27. Serializable {
  28. /* Only one ModelChangeEvent is needed per model instance since the
  29. * event's only (read-only) state is the source property. The source
  30. * of events generated here is always "this".
  31. */
  32. protected transient ChangeEvent changeEvent = null;
  33. /** The collection of registered listeners */
  34. protected EventListenerList listenerList = new EventListenerList();
  35. private int index = -1;
  36. // implements javax.swing.SingleSelectionModel
  37. public int getSelectedIndex() {
  38. return index;
  39. }
  40. // implements javax.swing.SingleSelectionModel
  41. public void setSelectedIndex(int index) {
  42. if (this.index != index) {
  43. this.index = index;
  44. fireStateChanged();
  45. }
  46. }
  47. // implements javax.swing.SingleSelectionModel
  48. public void clearSelection() {
  49. setSelectedIndex(-1);
  50. }
  51. // implements javax.swing.SingleSelectionModel
  52. public boolean isSelected() {
  53. boolean ret = false;
  54. if (getSelectedIndex() != -1) {
  55. ret = true;
  56. }
  57. return ret;
  58. }
  59. /**
  60. * Adds a <code>ChangeListener</code> to the button.
  61. */
  62. public void addChangeListener(ChangeListener l) {
  63. listenerList.add(ChangeListener.class, l);
  64. }
  65. /**
  66. * Removes a <code>ChangeListener</code> from the button.
  67. */
  68. public void removeChangeListener(ChangeListener l) {
  69. listenerList.remove(ChangeListener.class, l);
  70. }
  71. /**
  72. * Returns an array of all the change listeners
  73. * registered on this <code>DefaultSingleSelectionModel</code>.
  74. *
  75. * @return all of this model's <code>ChangeListener</code>s
  76. * or an empty
  77. * array if no change listeners are currently registered
  78. *
  79. * @see #addChangeListener
  80. * @see #removeChangeListener
  81. *
  82. * @since 1.4
  83. */
  84. public ChangeListener[] getChangeListeners() {
  85. return (ChangeListener[])listenerList.getListeners(
  86. ChangeListener.class);
  87. }
  88. /**
  89. * Notifies all listeners that have registered interest for
  90. * notification on this event type. The event instance
  91. * is created lazily.
  92. * @see EventListenerList
  93. */
  94. protected void fireStateChanged() {
  95. // Guaranteed to return a non-null array
  96. Object[] listeners = listenerList.getListenerList();
  97. // Process the listeners last to first, notifying
  98. // those that are interested in this event
  99. for (int i = listeners.length-2; i>=0; i-=2) {
  100. if (listeners[i]==ChangeListener.class) {
  101. // Lazily create the event:
  102. if (changeEvent == null)
  103. changeEvent = new ChangeEvent(this);
  104. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  105. }
  106. }
  107. }
  108. /**
  109. * Returns an array of all the objects currently registered as
  110. * <code><em>Foo</em>Listener</code>s
  111. * upon this model.
  112. * <code><em>Foo</em>Listener</code>s
  113. * are registered using the <code>add<em>Foo</em>Listener</code> method.
  114. * <p>
  115. * You can specify the <code>listenerType</code> argument
  116. * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
  117. * For example, you can query a <code>DefaultSingleSelectionModel</code>
  118. * instance <code>m</code>
  119. * for its change listeners
  120. * with the following code:
  121. *
  122. * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
  123. *
  124. * If no such listeners exist,
  125. * this method returns an empty array.
  126. *
  127. * @param listenerType the type of listeners requested;
  128. * this parameter should specify an interface
  129. * that descends from <code>java.util.EventListener</code>
  130. * @return an array of all objects registered as
  131. * <code><em>Foo</em>Listener</code>s
  132. * on this model,
  133. * or an empty array if no such
  134. * listeners have been added
  135. * @exception ClassCastException if <code>listenerType</code> doesn't
  136. * specify a class or interface that implements
  137. * <code>java.util.EventListener</code>
  138. *
  139. * @see #getChangeListeners
  140. *
  141. * @since 1.3
  142. */
  143. public EventListener[] getListeners(Class listenerType) {
  144. return listenerList.getListeners(listenerType);
  145. }
  146. }