1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing;
  27 
  28 import javax.swing.event.*;
  29 import java.io.Serializable;
  30 import java.util.EventListener;
  31 
  32 /**
  33  * A generic implementation of SingleSelectionModel.
  34  * <p>
  35  * <strong>Warning:</strong>
  36  * Serialized objects of this class will not be compatible with
  37  * future Swing releases. The current serialization support is
  38  * appropriate for short term storage or RMI between applications running
  39  * the same version of Swing.  As of 1.4, support for long term storage
  40  * of all JavaBeans&trade;
  41  * has been added to the <code>java.beans</code> package.
  42  * Please see {@link java.beans.XMLEncoder}.
  43  *
  44  * @author Dave Moore
  45  * @since 1.2
  46  */
  47 @SuppressWarnings("serial") // Same-version serialization only
  48 public class DefaultSingleSelectionModel implements SingleSelectionModel,
  49 Serializable {
  50     /**
  51      * Only one ModelChangeEvent is needed per model instance since the
  52      * event's only (read-only) state is the source property.  The source
  53      * of events generated here is always "this".
  54      */
  55     protected transient ChangeEvent changeEvent = null;
  56     /** The collection of registered listeners */
  57     protected EventListenerList listenerList = new EventListenerList();
  58 
  59     private int index = -1;
  60 
  61     /**
  62      * {@inheritDoc}
  63      */
  64     public int getSelectedIndex() {
  65         return index;
  66     }
  67 
  68     /**
  69      * {@inheritDoc}
  70      */
  71     public void setSelectedIndex(int index) {
  72         if (this.index != index) {
  73             this.index = index;
  74             fireStateChanged();
  75         }
  76     }
  77 
  78     /**
  79      * {@inheritDoc}
  80      */
  81     public void clearSelection() {
  82         setSelectedIndex(-1);
  83     }
  84 
  85     /**
  86      * {@inheritDoc}
  87      */
  88     public boolean isSelected() {
  89         boolean ret = false;
  90         if (getSelectedIndex() != -1) {
  91             ret = true;
  92         }
  93         return ret;
  94     }
  95 
  96     /**
  97      * Adds a <code>ChangeListener</code> to the button.
  98      */
  99     public void addChangeListener(ChangeListener l) {
 100         listenerList.add(ChangeListener.class, l);
 101     }
 102 
 103     /**
 104      * Removes a <code>ChangeListener</code> from the button.
 105      */
 106     public void removeChangeListener(ChangeListener l) {
 107         listenerList.remove(ChangeListener.class, l);
 108     }
 109 
 110     /**
 111      * Returns an array of all the change listeners
 112      * registered on this <code>DefaultSingleSelectionModel</code>.
 113      *
 114      * @return all of this model's <code>ChangeListener</code>s
 115      *         or an empty
 116      *         array if no change listeners are currently registered
 117      *
 118      * @see #addChangeListener
 119      * @see #removeChangeListener
 120      *
 121      * @since 1.4
 122      */
 123     public ChangeListener[] getChangeListeners() {
 124         return listenerList.getListeners(ChangeListener.class);
 125     }
 126 
 127     /**
 128      * Notifies all listeners that have registered interest for
 129      * notification on this event type.  The event instance
 130      * is created lazily.
 131      * @see EventListenerList
 132      */
 133     protected void fireStateChanged() {
 134         // Guaranteed to return a non-null array
 135         Object[] listeners = listenerList.getListenerList();
 136         // Process the listeners last to first, notifying
 137         // those that are interested in this event
 138         for (int i = listeners.length-2; i>=0; i-=2) {
 139             if (listeners[i]==ChangeListener.class) {
 140                 // Lazily create the event:
 141                 if (changeEvent == null)
 142                     changeEvent = new ChangeEvent(this);
 143                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
 144             }
 145         }
 146     }
 147 
 148     /**
 149      * Returns an array of all the objects currently registered as
 150      * <code><em>Foo</em>Listener</code>s
 151      * upon this model.
 152      * <code><em>Foo</em>Listener</code>s
 153      * are registered using the <code>add<em>Foo</em>Listener</code> method.
 154      * <p>
 155      * You can specify the <code>listenerType</code> argument
 156      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
 157      * For example, you can query a <code>DefaultSingleSelectionModel</code>
 158      * instance <code>m</code>
 159      * for its change listeners
 160      * with the following code:
 161      *
 162      * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
 163      *
 164      * If no such listeners exist,
 165      * this method returns an empty array.
 166      *
 167      * @param <T>  the type of {@code EventListener} class being requested
 168      * @param listenerType  the type of listeners requested;
 169      *          this parameter should specify an interface
 170      *          that descends from <code>java.util.EventListener</code>
 171      * @return an array of all objects registered as
 172      *          <code><em>Foo</em>Listener</code>s
 173      *          on this model,
 174      *          or an empty array if no such
 175      *          listeners have been added
 176      * @exception ClassCastException if <code>listenerType</code> doesn't
 177      *          specify a class or interface that implements
 178      *          <code>java.util.EventListener</code>
 179      *
 180      * @see #getChangeListeners
 181      *
 182      * @since 1.3
 183      */
 184     public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
 185         return listenerList.getListeners(listenerType);
 186     }
 187 }