KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultSingleSelectionModel


1 /*
2  * @(#)DefaultSingleSelectionModel.java 1.35 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 javax.swing.event.*;
11 import java.io.Serializable JavaDoc;
12 import java.util.EventListener JavaDoc;
13
14 /**
15  * A generic implementation of SingleSelectionModel.
16  * <p>
17  * <strong>Warning:</strong>
18  * Serialized objects of this class will not be compatible with
19  * future Swing releases. The current serialization support is
20  * appropriate for short term storage or RMI between applications running
21  * the same version of Swing. As of 1.4, support for long term storage
22  * of all JavaBeans<sup><font size="-2">TM</font></sup>
23  * has been added to the <code>java.beans</code> package.
24  * Please see {@link java.beans.XMLEncoder}.
25  *
26  * @version 1.35 05/05/04
27  * @author Dave Moore
28  */

29 public class DefaultSingleSelectionModel implements SingleSelectionModel JavaDoc,
30 Serializable JavaDoc {
31     /* Only one ModelChangeEvent is needed per model instance since the
32      * event's only (read-only) state is the source property. The source
33      * of events generated here is always "this".
34      */

35     protected transient ChangeEvent changeEvent = null;
36     /** The collection of registered listeners */
37     protected EventListenerList listenerList = new EventListenerList();
38
39     private int index = -1;
40
41     // implements javax.swing.SingleSelectionModel
42
public int getSelectedIndex() {
43         return index;
44     }
45
46     // implements javax.swing.SingleSelectionModel
47
public void setSelectedIndex(int index) {
48         if (this.index != index) {
49             this.index = index;
50         fireStateChanged();
51         }
52     }
53
54     // implements javax.swing.SingleSelectionModel
55
public void clearSelection() {
56         setSelectedIndex(-1);
57     }
58
59     // implements javax.swing.SingleSelectionModel
60
public boolean isSelected() {
61     boolean ret = false;
62     if (getSelectedIndex() != -1) {
63         ret = true;
64     }
65     return ret;
66     }
67
68     /**
69      * Adds a <code>ChangeListener</code> to the button.
70      */

71     public void addChangeListener(ChangeListener l) {
72     listenerList.add(ChangeListener.class, l);
73     }
74     
75     /**
76      * Removes a <code>ChangeListener</code> from the button.
77      */

78     public void removeChangeListener(ChangeListener l) {
79     listenerList.remove(ChangeListener.class, l);
80     }
81
82     /**
83      * Returns an array of all the change listeners
84      * registered on this <code>DefaultSingleSelectionModel</code>.
85      *
86      * @return all of this model's <code>ChangeListener</code>s
87      * or an empty
88      * array if no change listeners are currently registered
89      *
90      * @see #addChangeListener
91      * @see #removeChangeListener
92      *
93      * @since 1.4
94      */

95     public ChangeListener[] getChangeListeners() {
96         return (ChangeListener[])listenerList.getListeners(
97                 ChangeListener.class);
98     }
99
100     /**
101      * Notifies all listeners that have registered interest for
102      * notification on this event type. The event instance
103      * is created lazily.
104      * @see EventListenerList
105      */

106     protected void fireStateChanged() {
107     // Guaranteed to return a non-null array
108
Object JavaDoc[] listeners = listenerList.getListenerList();
109     // Process the listeners last to first, notifying
110
// those that are interested in this event
111
for (int i = listeners.length-2; i>=0; i-=2) {
112         if (listeners[i]==ChangeListener.class) {
113         // Lazily create the event:
114
if (changeEvent == null)
115             changeEvent = new ChangeEvent(this);
116         ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
117         }
118     }
119     }
120
121     /**
122      * Returns an array of all the objects currently registered as
123      * <code><em>Foo</em>Listener</code>s
124      * upon this model.
125      * <code><em>Foo</em>Listener</code>s
126      * are registered using the <code>add<em>Foo</em>Listener</code> method.
127      * <p>
128      * You can specify the <code>listenerType</code> argument
129      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
130      * For example, you can query a <code>DefaultSingleSelectionModel</code>
131      * instance <code>m</code>
132      * for its change listeners
133      * with the following code:
134      *
135      * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
136      *
137      * If no such listeners exist,
138      * this method returns an empty array.
139      *
140      * @param listenerType the type of listeners requested;
141      * this parameter should specify an interface
142      * that descends from <code>java.util.EventListener</code>
143      * @return an array of all objects registered as
144      * <code><em>Foo</em>Listener</code>s
145      * on this model,
146      * or an empty array if no such
147      * listeners have been added
148      * @exception ClassCastException if <code>listenerType</code> doesn't
149      * specify a class or interface that implements
150      * <code>java.util.EventListener</code>
151      *
152      * @see #getChangeListeners
153      *
154      * @since 1.3
155      */

156     public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
157     return listenerList.getListeners(listenerType);
158     }
159 }
160
161
162
Popular Tags