KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > AbstractSpinnerModel


1 /*
2  * @(#)AbstractSpinnerModel.java 1.7 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.util.*;
11 import javax.swing.event.*;
12
13
14 /**
15  * This class provides the ChangeListener part of the
16  * SpinnerModel interface that should be suitable for most concrete SpinnerModel
17  * implementations. Subclasses must provide an implementation of the
18  * minimum, maximum, and value properties and the getNextValue
19  * and getPreviousValue methods.
20  *
21  * @see JSpinner
22  * @see SpinnerModel
23  * @see SpinnerListModel
24  * @see SpinnerNumberModel
25  * @see SpinnerDateModel
26  *
27  * @version 1.7 05/05/04
28  * @author Hans Muller
29  * @since 1.4
30  */

31 public abstract class AbstractSpinnerModel implements SpinnerModel JavaDoc
32 {
33
34     /**
35      * Only one ChangeEvent is needed per model instance since the
36      * event's only (read-only) state is the source property. The source
37      * of events generated here is always "this".
38      */

39     private transient ChangeEvent changeEvent = null;
40
41
42     /**
43      * The list of ChangeListeners for this model. Subclasses may
44      * store their own listeners here.
45      */

46     protected EventListenerList listenerList = new EventListenerList();
47
48
49     /**
50      * Adds a ChangeListener to the model's listener list. The
51      * ChangeListeners must be notified when the models value changes.
52      *
53      * @param l the ChangeListener to add
54      * @see #removeChangeListener
55      * @see SpinnerModel#addChangeListener
56      */

57     public void addChangeListener(ChangeListener l) {
58         listenerList.add(ChangeListener.class, l);
59     }
60     
61
62     /**
63      * Removes a ChangeListener from the model's listener list.
64      *
65      * @param l the ChangeListener to remove
66      * @see #addChangeListener
67      * @see SpinnerModel#removeChangeListener
68      */

69     public void removeChangeListener(ChangeListener l) {
70         listenerList.remove(ChangeListener.class, l);
71     }
72
73
74     /**
75      * Returns an array of all the <code>ChangeListener</code>s added
76      * to this AbstractSpinnerModel with addChangeListener().
77      *
78      * @return all of the <code>ChangeListener</code>s added or an empty
79      * array if no listeners have been added
80      * @since 1.4
81      */

82     public ChangeListener[] getChangeListeners() {
83         return (ChangeListener[])listenerList.getListeners(
84                 ChangeListener.class);
85     }
86
87
88     /**
89      * Run each ChangeListeners stateChanged() method.
90      *
91      * @see #setValue
92      * @see EventListenerList
93      */

94     protected void fireStateChanged()
95     {
96         Object JavaDoc[] listeners = listenerList.getListenerList();
97         for (int i = listeners.length - 2; i >= 0; i -=2 ) {
98             if (listeners[i] == ChangeListener.class) {
99                 if (changeEvent == null) {
100                     changeEvent = new ChangeEvent(this);
101                 }
102                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
103             }
104         }
105     }
106
107
108     /**
109      * Return an array of all the listeners of the given type that
110      * were added to this model. For example to find all of the
111      * ChangeListeners added to this model:
112      * <pre>
113      * myAbstractSpinnerModel.getListeners(ChangeListener.class);
114      * </pre>
115      *
116      * @param listenerType the type of listeners to return, e.g. ChangeListener.class
117      * @return all of the objects receiving <em>listenerType</em> notifications
118      * from this model
119      */

120     public <T extends EventListener> T[] getListeners(Class JavaDoc<T> listenerType) {
121     return listenerList.getListeners(listenerType);
122     }
123 }
124
125
Popular Tags