Path: blob/master/src/java.desktop/share/classes/javax/swing/AbstractSpinnerModel.java
41153 views
/*1* Copyright (c) 2000, 2014, 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*/48@SuppressWarnings("serial") // Field contents are not serializable across versions49public abstract class AbstractSpinnerModel implements SpinnerModel, Serializable50{5152/**53* Only one ChangeEvent is needed per model instance since the54* event's only (read-only) state is the source property. The source55* of events generated here is always "this".56*/57private transient ChangeEvent changeEvent = null;585960/**61* The list of ChangeListeners for this model. Subclasses may62* store their own listeners here.63*/64protected EventListenerList listenerList = new EventListenerList();6566/**67* Constructor for subclasses to call.68*/69protected AbstractSpinnerModel() {}7071/**72* Adds a ChangeListener to the model's listener list. The73* ChangeListeners must be notified when the models value changes.74*75* @param l the ChangeListener to add76* @see #removeChangeListener77* @see SpinnerModel#addChangeListener78*/79public void addChangeListener(ChangeListener l) {80listenerList.add(ChangeListener.class, l);81}828384/**85* Removes a ChangeListener from the model's listener list.86*87* @param l the ChangeListener to remove88* @see #addChangeListener89* @see SpinnerModel#removeChangeListener90*/91public void removeChangeListener(ChangeListener l) {92listenerList.remove(ChangeListener.class, l);93}949596/**97* Returns an array of all the <code>ChangeListener</code>s added98* to this AbstractSpinnerModel with addChangeListener().99*100* @return all of the <code>ChangeListener</code>s added or an empty101* array if no listeners have been added102* @since 1.4103*/104public ChangeListener[] getChangeListeners() {105return listenerList.getListeners(ChangeListener.class);106}107108109/**110* Run each ChangeListeners stateChanged() method.111*112* @see #setValue113* @see EventListenerList114*/115protected void fireStateChanged()116{117Object[] listeners = listenerList.getListenerList();118for (int i = listeners.length - 2; i >= 0; i -=2 ) {119if (listeners[i] == ChangeListener.class) {120if (changeEvent == null) {121changeEvent = new ChangeEvent(this);122}123((ChangeListener)listeners[i+1]).stateChanged(changeEvent);124}125}126}127128129/**130* Return an array of all the listeners of the given type that131* were added to this model. For example to find all of the132* ChangeListeners added to this model:133* <pre>134* myAbstractSpinnerModel.getListeners(ChangeListener.class);135* </pre>136*137* @param <T> the type of requested listeners138* @param listenerType the type of listeners to return, e.g. ChangeListener.class139* @return all of the objects receiving <em>listenerType</em> notifications140* from this model141*/142public <T extends EventListener> T[] getListeners(Class<T> listenerType) {143return listenerList.getListeners(listenerType);144}145}146147148