001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import javax.swing.event.*;
029 import java.io.Serializable;
030 import java.util.EventListener;
031
032 /**
033 * A generic implementation of SingleSelectionModel.
034 * <p>
035 * <strong>Warning:</strong>
036 * Serialized objects of this class will not be compatible with
037 * future Swing releases. The current serialization support is
038 * appropriate for short term storage or RMI between applications running
039 * the same version of Swing. As of 1.4, support for long term storage
040 * of all JavaBeans<sup><font size="-2">TM</font></sup>
041 * has been added to the <code>java.beans</code> package.
042 * Please see {@link java.beans.XMLEncoder}.
043 *
044 * @version 1.42 05/05/07
045 * @author Dave Moore
046 */
047 public class DefaultSingleSelectionModel implements
048 SingleSelectionModel, Serializable {
049 /* Only one ModelChangeEvent is needed per model instance since the
050 * event's only (read-only) state is the source property. The source
051 * of events generated here is always "this".
052 */
053 protected transient ChangeEvent changeEvent = null;
054 /** The collection of registered listeners */
055 protected EventListenerList listenerList = new EventListenerList();
056
057 private int index = -1;
058
059 // implements javax.swing.SingleSelectionModel
060 public int getSelectedIndex() {
061 return index;
062 }
063
064 // implements javax.swing.SingleSelectionModel
065 public void setSelectedIndex(int index) {
066 if (this .index != index) {
067 this .index = index;
068 fireStateChanged();
069 }
070 }
071
072 // implements javax.swing.SingleSelectionModel
073 public void clearSelection() {
074 setSelectedIndex(-1);
075 }
076
077 // implements javax.swing.SingleSelectionModel
078 public boolean isSelected() {
079 boolean ret = false;
080 if (getSelectedIndex() != -1) {
081 ret = true;
082 }
083 return ret;
084 }
085
086 /**
087 * Adds a <code>ChangeListener</code> to the button.
088 */
089 public void addChangeListener(ChangeListener l) {
090 listenerList.add(ChangeListener.class, l);
091 }
092
093 /**
094 * Removes a <code>ChangeListener</code> from the button.
095 */
096 public void removeChangeListener(ChangeListener l) {
097 listenerList.remove(ChangeListener.class, l);
098 }
099
100 /**
101 * Returns an array of all the change listeners
102 * registered on this <code>DefaultSingleSelectionModel</code>.
103 *
104 * @return all of this model's <code>ChangeListener</code>s
105 * or an empty
106 * array if no change listeners are currently registered
107 *
108 * @see #addChangeListener
109 * @see #removeChangeListener
110 *
111 * @since 1.4
112 */
113 public ChangeListener[] getChangeListeners() {
114 return (ChangeListener[]) listenerList
115 .getListeners(ChangeListener.class);
116 }
117
118 /**
119 * Notifies all listeners that have registered interest for
120 * notification on this event type. The event instance
121 * is created lazily.
122 * @see EventListenerList
123 */
124 protected void fireStateChanged() {
125 // Guaranteed to return a non-null array
126 Object[] listeners = listenerList.getListenerList();
127 // Process the listeners last to first, notifying
128 // those that are interested in this event
129 for (int i = listeners.length - 2; i >= 0; i -= 2) {
130 if (listeners[i] == ChangeListener.class) {
131 // Lazily create the event:
132 if (changeEvent == null)
133 changeEvent = new ChangeEvent(this );
134 ((ChangeListener) listeners[i + 1])
135 .stateChanged(changeEvent);
136 }
137 }
138 }
139
140 /**
141 * Returns an array of all the objects currently registered as
142 * <code><em>Foo</em>Listener</code>s
143 * upon this model.
144 * <code><em>Foo</em>Listener</code>s
145 * are registered using the <code>add<em>Foo</em>Listener</code> method.
146 * <p>
147 * You can specify the <code>listenerType</code> argument
148 * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
149 * For example, you can query a <code>DefaultSingleSelectionModel</code>
150 * instance <code>m</code>
151 * for its change listeners
152 * with the following code:
153 *
154 * <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>
155 *
156 * If no such listeners exist,
157 * this method returns an empty array.
158 *
159 * @param listenerType the type of listeners requested;
160 * this parameter should specify an interface
161 * that descends from <code>java.util.EventListener</code>
162 * @return an array of all objects registered as
163 * <code><em>Foo</em>Listener</code>s
164 * on this model,
165 * or an empty array if no such
166 * listeners have been added
167 * @exception ClassCastException if <code>listenerType</code> doesn't
168 * specify a class or interface that implements
169 * <code>java.util.EventListener</code>
170 *
171 * @see #getChangeListeners
172 *
173 * @since 1.3
174 */
175 public <T extends EventListener> T[] getListeners(
176 Class<T> listenerType) {
177 return listenerList.getListeners(listenerType);
178 }
179 }
|