001 /* DefaultSingleSelectionModel.java -- 002 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package javax.swing; 040 041 import java.io.Serializable; 042 import java.util.EventListener; 043 044 import javax.swing.event.ChangeEvent; 045 import javax.swing.event.ChangeListener; 046 import javax.swing.event.EventListenerList; 047 048 /** 049 * The default implementation of {@link SingleSelectionModel}, used in 050 * {@link JTabbedPane}, {@link JMenuBar} and {@link JPopupMenu}. 051 * 052 * @author Andrew Selkirk 053 */ 054 public class DefaultSingleSelectionModel 055 implements SingleSelectionModel, Serializable 056 { 057 private static final long serialVersionUID = 3676229404753786004L; 058 059 /** 060 * changeEvent 061 */ 062 protected transient ChangeEvent changeEvent; 063 064 /** 065 * listenerList 066 */ 067 protected EventListenerList listenerList = new EventListenerList(); 068 069 /** 070 * The selected index (or -1 for no selection). 071 */ 072 private int index = -1; 073 074 /** 075 * Creates a new <code>DefaultSingleSelectionModel</code> with no current 076 * selection. 077 */ 078 public DefaultSingleSelectionModel() 079 { 080 // Do nothing. 081 } 082 083 /** 084 * Returns the selected index or <code>-1</code> if there is no selection. 085 * 086 * @return The selected index. 087 * 088 * @see #setSelectedIndex(int) 089 */ 090 public int getSelectedIndex() 091 { 092 return index; 093 } 094 095 /** 096 * Sets the selected index and, if this is different to the previous 097 * selection, sends a {@link ChangeEvent} to all registered listeners. 098 * 099 * @param index the index (use <code>-1</code> to represent no selection). 100 * 101 * @see #getSelectedIndex() 102 * @see #clearSelection 103 */ 104 public void setSelectedIndex(int index) 105 { 106 if (this.index != index) 107 { 108 this.index = index; 109 fireStateChanged(); 110 } 111 } 112 113 /** 114 * Clears the selection by setting the selected index to <code>-1</code> and 115 * sends a {@link ChangeEvent} to all registered listeners. If the selected 116 * index is already <code>-1</code>, this method does nothing. 117 */ 118 public void clearSelection() 119 { 120 setSelectedIndex(-1); 121 } 122 123 /** 124 * Returns <code>true</code> if there is a selection, and <code>false</code> 125 * otherwise. 126 * 127 * @return A boolean. 128 */ 129 public boolean isSelected() 130 { 131 return index != -1; 132 } 133 134 /** 135 * Registers a listener to receive {@link ChangeEvent} notifications from 136 * this model whenever the selected index changes. 137 * 138 * @param listener the listener to add. 139 */ 140 public void addChangeListener(ChangeListener listener) 141 { 142 listenerList.add(ChangeListener.class, listener); 143 } 144 145 /** 146 * Deregisters a listener so that it no longer receives {@link ChangeEvent} 147 * notifications from this model. 148 * 149 * @param listener the listener to remove. 150 */ 151 public void removeChangeListener(ChangeListener listener) 152 { 153 listenerList.remove(ChangeListener.class, listener); 154 } 155 156 /** 157 * fireStateChanged 158 */ 159 protected void fireStateChanged() 160 { 161 if (changeEvent == null) 162 changeEvent = new ChangeEvent(this); 163 ChangeListener[] listeners = getChangeListeners(); 164 for (int i = 0; i < listeners.length; i++) 165 listeners[i].stateChanged(changeEvent); 166 } 167 168 /** 169 * getListeners 170 * 171 * @param listenerClass the type fo listener 172 * 173 * @return an array of listeners 174 * 175 * @since 1.3 176 */ 177 public <T extends EventListener> T[] getListeners(Class<T> listenerClass) 178 { 179 return listenerList.getListeners(listenerClass); 180 } 181 182 /** 183 * getChangeListeners 184 * 185 * @since 1.4 186 */ 187 public ChangeListener[] getChangeListeners() 188 { 189 return (ChangeListener[]) getListeners(ChangeListener.class); 190 } 191 }