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