Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/AbstractSpinnerModel.java
38829 views
/*1* Copyright (c) 2000, 2008, 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 java.util.*;28import javax.swing.event.*;29import java.io.Serializable;303132/**33* This class provides the ChangeListener part of the34* SpinnerModel interface that should be suitable for most concrete SpinnerModel35* implementations. Subclasses must provide an implementation of the36* <code>setValue</code>, <code>getValue</code>, <code>getNextValue</code> and37* <code>getPreviousValue</code> methods.38*39* @see JSpinner40* @see SpinnerModel41* @see SpinnerListModel42* @see SpinnerNumberModel43* @see SpinnerDateModel44*45* @author Hans Muller46* @since 1.447*/48public abstract class AbstractSpinnerModel implements SpinnerModel, Serializable49{5051/**52* Only one ChangeEvent is needed per model instance since the53* event's only (read-only) state is the source property. The source54* of events generated here is always "this".55*/56private transient ChangeEvent changeEvent = null;575859/**60* The list of ChangeListeners for this model. Subclasses may61* store their own listeners here.62*/63protected EventListenerList listenerList = new EventListenerList();646566/**67* Adds a ChangeListener to the model's listener list. The68* ChangeListeners must be notified when the models value changes.69*70* @param l the ChangeListener to add71* @see #removeChangeListener72* @see SpinnerModel#addChangeListener73*/74public void addChangeListener(ChangeListener l) {75listenerList.add(ChangeListener.class, l);76}777879/**80* Removes a ChangeListener from the model's listener list.81*82* @param l the ChangeListener to remove83* @see #addChangeListener84* @see SpinnerModel#removeChangeListener85*/86public void removeChangeListener(ChangeListener l) {87listenerList.remove(ChangeListener.class, l);88}899091/**92* Returns an array of all the <code>ChangeListener</code>s added93* to this AbstractSpinnerModel with addChangeListener().94*95* @return all of the <code>ChangeListener</code>s added or an empty96* array if no listeners have been added97* @since 1.498*/99public ChangeListener[] getChangeListeners() {100return listenerList.getListeners(ChangeListener.class);101}102103104/**105* Run each ChangeListeners stateChanged() method.106*107* @see #setValue108* @see EventListenerList109*/110protected void fireStateChanged()111{112Object[] listeners = listenerList.getListenerList();113for (int i = listeners.length - 2; i >= 0; i -=2 ) {114if (listeners[i] == ChangeListener.class) {115if (changeEvent == null) {116changeEvent = new ChangeEvent(this);117}118((ChangeListener)listeners[i+1]).stateChanged(changeEvent);119}120}121}122123124/**125* Return an array of all the listeners of the given type that126* were added to this model. For example to find all of the127* ChangeListeners added to this model:128* <pre>129* myAbstractSpinnerModel.getListeners(ChangeListener.class);130* </pre>131*132* @param listenerType the type of listeners to return, e.g. ChangeListener.class133* @return all of the objects receiving <em>listenerType</em> notifications134* from this model135*/136public <T extends EventListener> T[] getListeners(Class<T> listenerType) {137return listenerList.getListeners(listenerType);138}139}140141142