2
|
1 |
/*
|
|
2 |
* Copyright 1997-2004 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 javax.swing.event.*;
|
|
29 |
import java.io.Serializable;
|
|
30 |
import java.util.EventListener;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A generic implementation of SingleSelectionModel.
|
|
34 |
* <p>
|
|
35 |
* <strong>Warning:</strong>
|
|
36 |
* Serialized objects of this class will not be compatible with
|
|
37 |
* future Swing releases. The current serialization support is
|
|
38 |
* appropriate for short term storage or RMI between applications running
|
|
39 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
40 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
41 |
* has been added to the <code>java.beans</code> package.
|
|
42 |
* Please see {@link java.beans.XMLEncoder}.
|
|
43 |
*
|
|
44 |
* @author Dave Moore
|
|
45 |
*/
|
|
46 |
public class DefaultSingleSelectionModel implements SingleSelectionModel,
|
|
47 |
Serializable {
|
|
48 |
/* Only one ModelChangeEvent is needed per model instance since the
|
|
49 |
* event's only (read-only) state is the source property. The source
|
|
50 |
* of events generated here is always "this".
|
|
51 |
*/
|
|
52 |
protected transient ChangeEvent changeEvent = null;
|
|
53 |
/** The collection of registered listeners */
|
|
54 |
protected EventListenerList listenerList = new EventListenerList();
|
|
55 |
|
|
56 |
private int index = -1;
|
|
57 |
|
|
58 |
// implements javax.swing.SingleSelectionModel
|
|
59 |
public int getSelectedIndex() {
|
|
60 |
return index;
|
|
61 |
}
|
|
62 |
|
|
63 |
// implements javax.swing.SingleSelectionModel
|
|
64 |
public void setSelectedIndex(int index) {
|
|
65 |
if (this.index != index) {
|
|
66 |
this.index = index;
|
|
67 |
fireStateChanged();
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
// implements javax.swing.SingleSelectionModel
|
|
72 |
public void clearSelection() {
|
|
73 |
setSelectedIndex(-1);
|
|
74 |
}
|
|
75 |
|
|
76 |
// implements javax.swing.SingleSelectionModel
|
|
77 |
public boolean isSelected() {
|
|
78 |
boolean ret = false;
|
|
79 |
if (getSelectedIndex() != -1) {
|
|
80 |
ret = true;
|
|
81 |
}
|
|
82 |
return ret;
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Adds a <code>ChangeListener</code> to the button.
|
|
87 |
*/
|
|
88 |
public void addChangeListener(ChangeListener l) {
|
|
89 |
listenerList.add(ChangeListener.class, l);
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Removes a <code>ChangeListener</code> from the button.
|
|
94 |
*/
|
|
95 |
public void removeChangeListener(ChangeListener l) {
|
|
96 |
listenerList.remove(ChangeListener.class, l);
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Returns an array of all the change listeners
|
|
101 |
* registered on this <code>DefaultSingleSelectionModel</code>.
|
|
102 |
*
|
|
103 |
* @return all of this model's <code>ChangeListener</code>s
|
|
104 |
* or an empty
|
|
105 |
* array if no change listeners are currently registered
|
|
106 |
*
|
|
107 |
* @see #addChangeListener
|
|
108 |
* @see #removeChangeListener
|
|
109 |
*
|
|
110 |
* @since 1.4
|
|
111 |
*/
|
|
112 |
public ChangeListener[] getChangeListeners() {
|
|
113 |
return (ChangeListener[])listenerList.getListeners(
|
|
114 |
ChangeListener.class);
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* Notifies all listeners that have registered interest for
|
|
119 |
* notification on this event type. The event instance
|
|
120 |
* is created lazily.
|
|
121 |
* @see EventListenerList
|
|
122 |
*/
|
|
123 |
protected void fireStateChanged() {
|
|
124 |
// Guaranteed to return a non-null array
|
|
125 |
Object[] listeners = listenerList.getListenerList();
|
|
126 |
// Process the listeners last to first, notifying
|
|
127 |
// those that are interested in this event
|
|
128 |
for (int i = listeners.length-2; i>=0; i-=2) {
|
|
129 |
if (listeners[i]==ChangeListener.class) {
|
|
130 |
// Lazily create the event:
|
|
131 |
if (changeEvent == null)
|
|
132 |
changeEvent = new ChangeEvent(this);
|
|
133 |
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Returns an array of all the objects currently registered as
|
|
140 |
* <code><em>Foo</em>Listener</code>s
|
|
141 |
* upon this model.
|
|
142 |
* <code><em>Foo</em>Listener</code>s
|
|
143 |
* are registered using the <code>add<em>Foo</em>Listener</code> method.
|
|
144 |
* <p>
|
|
145 |
* You can specify the <code>listenerType</code> argument
|
|
146 |
* with a class literal, such as <code><em>Foo</em>Listener.class</code>.
|
|
147 |
* For example, you can query a <code>DefaultSingleSelectionModel</code>
|
|
148 |
* instance <code>m</code>
|
|
149 |
* for its change listeners
|
|
150 |
* with the following code:
|
|
151 |
*
|
|
152 |
* <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
|
|
153 |
*
|
|
154 |
* If no such listeners exist,
|
|
155 |
* this method returns an empty array.
|
|
156 |
*
|
|
157 |
* @param listenerType the type of listeners requested;
|
|
158 |
* this parameter should specify an interface
|
|
159 |
* that descends from <code>java.util.EventListener</code>
|
|
160 |
* @return an array of all objects registered as
|
|
161 |
* <code><em>Foo</em>Listener</code>s
|
|
162 |
* on this model,
|
|
163 |
* or an empty array if no such
|
|
164 |
* listeners have been added
|
|
165 |
* @exception ClassCastException if <code>listenerType</code> doesn't
|
|
166 |
* specify a class or interface that implements
|
|
167 |
* <code>java.util.EventListener</code>
|
|
168 |
*
|
|
169 |
* @see #getChangeListeners
|
|
170 |
*
|
|
171 |
* @since 1.3
|
|
172 |
*/
|
|
173 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
|
|
174 |
return listenerList.getListeners(listenerType);
|
|
175 |
}
|
|
176 |
}
|