Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultSingleSelectionModel.java
38829 views
/*1* Copyright (c) 1997, 2013, 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 JavaBeans™40* has been added to the <code>java.beans</code> package.41* Please see {@link java.beans.XMLEncoder}.42*43* @author Dave Moore44*/45public class DefaultSingleSelectionModel implements SingleSelectionModel,46Serializable {47/* Only one ModelChangeEvent is needed per model instance since the48* event's only (read-only) state is the source property. The source49* of events generated here is always "this".50*/51protected transient ChangeEvent changeEvent = null;52/** The collection of registered listeners */53protected EventListenerList listenerList = new EventListenerList();5455private int index = -1;5657// implements javax.swing.SingleSelectionModel58public int getSelectedIndex() {59return index;60}6162// implements javax.swing.SingleSelectionModel63public void setSelectedIndex(int index) {64if (this.index != index) {65this.index = index;66fireStateChanged();67}68}6970// implements javax.swing.SingleSelectionModel71public void clearSelection() {72setSelectedIndex(-1);73}7475// implements javax.swing.SingleSelectionModel76public boolean isSelected() {77boolean ret = false;78if (getSelectedIndex() != -1) {79ret = true;80}81return ret;82}8384/**85* Adds a <code>ChangeListener</code> to the button.86*/87public void addChangeListener(ChangeListener l) {88listenerList.add(ChangeListener.class, l);89}9091/**92* Removes a <code>ChangeListener</code> from the button.93*/94public void removeChangeListener(ChangeListener l) {95listenerList.remove(ChangeListener.class, l);96}9798/**99* Returns an array of all the change listeners100* registered on this <code>DefaultSingleSelectionModel</code>.101*102* @return all of this model's <code>ChangeListener</code>s103* or an empty104* array if no change listeners are currently registered105*106* @see #addChangeListener107* @see #removeChangeListener108*109* @since 1.4110*/111public ChangeListener[] getChangeListeners() {112return listenerList.getListeners(ChangeListener.class);113}114115/**116* Notifies all listeners that have registered interest for117* notification on this event type. The event instance118* is created lazily.119* @see EventListenerList120*/121protected void fireStateChanged() {122// Guaranteed to return a non-null array123Object[] listeners = listenerList.getListenerList();124// Process the listeners last to first, notifying125// those that are interested in this event126for (int i = listeners.length-2; i>=0; i-=2) {127if (listeners[i]==ChangeListener.class) {128// Lazily create the event:129if (changeEvent == null)130changeEvent = new ChangeEvent(this);131((ChangeListener)listeners[i+1]).stateChanged(changeEvent);132}133}134}135136/**137* Returns an array of all the objects currently registered as138* <code><em>Foo</em>Listener</code>s139* upon this model.140* <code><em>Foo</em>Listener</code>s141* are registered using the <code>add<em>Foo</em>Listener</code> method.142* <p>143* You can specify the <code>listenerType</code> argument144* with a class literal, such as <code><em>Foo</em>Listener.class</code>.145* For example, you can query a <code>DefaultSingleSelectionModel</code>146* instance <code>m</code>147* for its change listeners148* with the following code:149*150* <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>151*152* If no such listeners exist,153* this method returns an empty array.154*155* @param listenerType the type of listeners requested;156* this parameter should specify an interface157* that descends from <code>java.util.EventListener</code>158* @return an array of all objects registered as159* <code><em>Foo</em>Listener</code>s160* on this model,161* or an empty array if no such162* listeners have been added163* @exception ClassCastException if <code>listenerType</code> doesn't164* specify a class or interface that implements165* <code>java.util.EventListener</code>166*167* @see #getChangeListeners168*169* @since 1.3170*/171public <T extends EventListener> T[] getListeners(Class<T> listenerType) {172return listenerList.getListeners(listenerType);173}174}175176177