J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.swing; |
| 27 | |
| 28 | import java.util.*; |
| 29 | import javax.swing.event.*; |
| 30 | import java.io.Serializable; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * This class provides the ChangeListener part of the |
| 35 | * SpinnerModel interface that should be suitable for most concrete SpinnerModel |
| 36 | * implementations. Subclasses must provide an implementation of the |
| 37 | * <code>setValue</code>, <code>getValue</code>, <code>getNextValue</code> and |
| 38 | * <code>getPreviousValue</code> methods. |
| 39 | * |
| 40 | * @see JSpinner |
| 41 | * @see SpinnerModel |
| 42 | * @see SpinnerListModel |
| 43 | * @see SpinnerNumberModel |
| 44 | * @see SpinnerDateModel |
| 45 | * |
| 46 | * @author Hans Muller |
| 47 | * @since 1.4 |
| 48 | */ |
| 49 | public abstract class AbstractSpinnerModel implements SpinnerModel, Serializable |
| 50 | { |
| 51 | |
| 52 | /** |
| 53 | * Only one ChangeEvent is needed per model instance since the |
| 54 | * event's only (read-only) state is the source property. The source |
| 55 | * of events generated here is always "this". |
| 56 | */ |
| 57 | private transient ChangeEvent changeEvent = null; |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * The list of ChangeListeners for this model. Subclasses may |
| 62 | * store their own listeners here. |
| 63 | */ |
| 64 | protected EventListenerList listenerList = new EventListenerList(); |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * Adds a ChangeListener to the model's listener list. The |
| 69 | * ChangeListeners must be notified when the models value changes. |
| 70 | * |
| 71 | * @param l the ChangeListener to add |
| 72 | * @see #removeChangeListener |
| 73 | * @see SpinnerModel#addChangeListener |
| 74 | */ |
| 75 | public void addChangeListener(ChangeListener l) { |
| 76 | listenerList.add(ChangeListener.class, l); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Removes a ChangeListener from the model's listener list. |
| 82 | * |
| 83 | * @param l the ChangeListener to remove |
| 84 | * @see #addChangeListener |
| 85 | * @see SpinnerModel#removeChangeListener |
| 86 | */ |
| 87 | public void removeChangeListener(ChangeListener l) { |
| 88 | listenerList.remove(ChangeListener.class, l); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * Returns an array of all the <code>ChangeListener</code>s added |
| 94 | * to this AbstractSpinnerModel with addChangeListener(). |
| 95 | * |
| 96 | * @return all of the <code>ChangeListener</code>s added or an empty |
| 97 | * array if no listeners have been added |
| 98 | * @since 1.4 |
| 99 | */ |
| 100 | public ChangeListener[] getChangeListeners() { |
| 101 | return (ChangeListener[])listenerList.getListeners( |
| 102 | ChangeListener.class); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Run each ChangeListeners stateChanged() method. |
| 108 | * |
| 109 | * @see #setValue |
| 110 | * @see EventListenerList |
| 111 | */ |
| 112 | protected void fireStateChanged() |
| 113 | { |
| 114 | Object[] listeners = listenerList.getListenerList(); |
| 115 | for (int i = listeners.length - 2; i >= 0; i -=2 ) { |
| 116 | if (listeners[i] == ChangeListener.class) { |
| 117 | if (changeEvent == null) { |
| 118 | changeEvent = new ChangeEvent(this); |
| 119 | } |
| 120 | ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Return an array of all the listeners of the given type that |
| 128 | * were added to this model. For example to find all of the |
| 129 | * ChangeListeners added to this model: |
| 130 | * <pre> |
| 131 | * myAbstractSpinnerModel.getListeners(ChangeListener.class); |
| 132 | * </pre> |
| 133 | * |
| 134 | * @param listenerType the type of listeners to return, e.g. ChangeListener.class |
| 135 | * @return all of the objects receiving <em>listenerType</em> notifications |
| 136 | * from this model |
| 137 | */ |
| 138 | public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
| 139 | return listenerList.getListeners(listenerType); |
| 140 | } |
| 141 | } |