Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultSingleSelectionModel.java
41153 views
/*1* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import javax.swing.event.*;28import java.io.Serializable;29import java.util.EventListener;3031/**32* A generic implementation of SingleSelectionModel.33* <p>34* <strong>Warning:</strong>35* Serialized objects of this class will not be compatible with36* future Swing releases. The current serialization support is37* appropriate for short term storage or RMI between applications running38* the same version of Swing. As of 1.4, support for long term storage39* of all JavaBeans40* has been added to the <code>java.beans</code> package.41* Please see {@link java.beans.XMLEncoder}.42*43* @author Dave Moore44* @since 1.245*/46@SuppressWarnings("serial") // Same-version serialization only47public class DefaultSingleSelectionModel implements SingleSelectionModel,48Serializable {49/**50* Only one ModelChangeEvent is needed per model instance since the51* event's only (read-only) state is the source property. The source52* of events generated here is always "this".53*/54protected transient ChangeEvent changeEvent = null;55/** The collection of registered listeners */56protected EventListenerList listenerList = new EventListenerList();5758private int index = -1;5960/**61* Constructs a {@code DefaultSingleSelectionModel}.62*/63public DefaultSingleSelectionModel() {}6465/**66* {@inheritDoc}67*/68public int getSelectedIndex() {69return index;70}7172/**73* {@inheritDoc}74*/75public void setSelectedIndex(int index) {76if (this.index != index) {77this.index = index;78fireStateChanged();79}80}8182/**83* {@inheritDoc}84*/85public void clearSelection() {86setSelectedIndex(-1);87}8889/**90* {@inheritDoc}91*/92public boolean isSelected() {93boolean ret = false;94if (getSelectedIndex() != -1) {95ret = true;96}97return ret;98}99100/**101* Adds a <code>ChangeListener</code> to the button.102*/103public void addChangeListener(ChangeListener l) {104listenerList.add(ChangeListener.class, l);105}106107/**108* Removes a <code>ChangeListener</code> from the button.109*/110public void removeChangeListener(ChangeListener l) {111listenerList.remove(ChangeListener.class, l);112}113114/**115* Returns an array of all the change listeners116* registered on this <code>DefaultSingleSelectionModel</code>.117*118* @return all of this model's <code>ChangeListener</code>s119* or an empty120* array if no change listeners are currently registered121*122* @see #addChangeListener123* @see #removeChangeListener124*125* @since 1.4126*/127public ChangeListener[] getChangeListeners() {128return listenerList.getListeners(ChangeListener.class);129}130131/**132* Notifies all listeners that have registered interest for133* notification on this event type. The event instance134* is created lazily.135* @see EventListenerList136*/137protected void fireStateChanged() {138// Guaranteed to return a non-null array139Object[] listeners = listenerList.getListenerList();140// Process the listeners last to first, notifying141// those that are interested in this event142for (int i = listeners.length-2; i>=0; i-=2) {143if (listeners[i]==ChangeListener.class) {144// Lazily create the event:145if (changeEvent == null)146changeEvent = new ChangeEvent(this);147((ChangeListener)listeners[i+1]).stateChanged(changeEvent);148}149}150}151152/**153* Returns an array of all the objects currently registered as154* <code><em>Foo</em>Listener</code>s155* upon this model.156* <code><em>Foo</em>Listener</code>s157* are registered using the <code>add<em>Foo</em>Listener</code> method.158* <p>159* You can specify the <code>listenerType</code> argument160* with a class literal, such as <code><em>Foo</em>Listener.class</code>.161* For example, you can query a <code>DefaultSingleSelectionModel</code>162* instance <code>m</code>163* for its change listeners164* with the following code:165*166* <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>167*168* If no such listeners exist,169* this method returns an empty array.170*171* @param <T> the type of {@code EventListener} class being requested172* @param listenerType the type of listeners requested;173* this parameter should specify an interface174* that descends from <code>java.util.EventListener</code>175* @return an array of all objects registered as176* <code><em>Foo</em>Listener</code>s177* on this model,178* or an empty array if no such179* listeners have been added180* @exception ClassCastException if <code>listenerType</code> doesn't181* specify a class or interface that implements182* <code>java.util.EventListener</code>183*184* @see #getChangeListeners185*186* @since 1.3187*/188public <T extends EventListener> T[] getListeners(Class<T> listenerType) {189return listenerList.getListeners(listenerType);190}191}192193194