1 7 8 package javax.swing; 9 10 import javax.swing.event.*; 11 import java.io.Serializable ; 12 import java.util.EventListener ; 13 14 29 public class DefaultSingleSelectionModel implements SingleSelectionModel , 30 Serializable { 31 35 protected transient ChangeEvent changeEvent = null; 36 37 protected EventListenerList listenerList = new EventListenerList(); 38 39 private int index = -1; 40 41 public int getSelectedIndex() { 43 return index; 44 } 45 46 public void setSelectedIndex(int index) { 48 if (this.index != index) { 49 this.index = index; 50 fireStateChanged(); 51 } 52 } 53 54 public void clearSelection() { 56 setSelectedIndex(-1); 57 } 58 59 public boolean isSelected() { 61 boolean ret = false; 62 if (getSelectedIndex() != -1) { 63 ret = true; 64 } 65 return ret; 66 } 67 68 71 public void addChangeListener(ChangeListener l) { 72 listenerList.add(ChangeListener.class, l); 73 } 74 75 78 public void removeChangeListener(ChangeListener l) { 79 listenerList.remove(ChangeListener.class, l); 80 } 81 82 95 public ChangeListener[] getChangeListeners() { 96 return (ChangeListener[])listenerList.getListeners( 97 ChangeListener.class); 98 } 99 100 106 protected void fireStateChanged() { 107 Object [] listeners = listenerList.getListenerList(); 109 for (int i = listeners.length-2; i>=0; i-=2) { 112 if (listeners[i]==ChangeListener.class) { 113 if (changeEvent == null) 115 changeEvent = new ChangeEvent(this); 116 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 117 } 118 } 119 } 120 121 156 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 157 return listenerList.getListeners(listenerType); 158 } 159 } 160 161 162 | Popular Tags |