1 29 30 package nextapp.echo2.app.list; 31 32 import java.io.Serializable ; 33 import java.util.BitSet ; 34 import java.util.EventListener ; 35 36 import nextapp.echo2.app.event.EventListenerList; 37 import nextapp.echo2.app.event.ChangeEvent; 38 import nextapp.echo2.app.event.ChangeListener; 39 40 43 public class DefaultListSelectionModel 44 implements ListSelectionModel, Serializable { 45 46 private EventListenerList listenerList = new EventListenerList(); 47 private int selectionMode = SINGLE_SELECTION; 48 private BitSet selection = new BitSet (); 49 private int minSelectedIndex = -1; 50 51 55 public void addChangeListener(ChangeListener l) { 56 listenerList.addListener(ChangeListener.class, l); 57 } 58 59 62 public void clearSelection() { 63 selection = new BitSet (); 64 minSelectedIndex = -1; 65 fireValueChanged(); 66 } 67 68 72 protected void fireValueChanged() { 73 ChangeEvent e = new ChangeEvent(this); 74 EventListener [] listeners = listenerList.getListeners(ChangeListener.class); 75 for (int index = 0; index < listeners.length; ++index) { 76 ((ChangeListener) listeners[index]).stateChanged(e); 77 } 78 } 79 80 83 public int getMaxSelectedIndex() { 84 return selection.length() - 1; 85 } 86 87 90 public int getMinSelectedIndex() { 91 return minSelectedIndex; 92 } 93 94 97 public int getSelectionMode() { 98 return selectionMode; 99 } 100 101 104 public boolean isSelectedIndex(int index) { 105 return selection.get(index); 106 } 107 108 111 public boolean isSelectionEmpty() { 112 return selection.length() == 0; 113 } 114 115 118 public void removeChangeListener(ChangeListener l) { 119 listenerList.removeListener(ChangeListener.class, l); 120 } 121 122 125 public void setSelectedIndex(int index, boolean newValue) { 126 boolean oldValue = isSelectedIndex(index); 127 128 if (newValue ^ oldValue) { 129 if (newValue) { 130 if (selectionMode == SINGLE_SELECTION && getMinSelectedIndex() != -1) { 131 setSelectedIndex(getMinSelectedIndex(), false); 132 } 133 selection.set(index); 134 if (index < minSelectedIndex || minSelectedIndex == -1) { 135 minSelectedIndex = index; 136 } 137 } else { 138 selection.clear(index); 139 if (index == minSelectedIndex) { 140 int max = getMaxSelectedIndex(); 142 minSelectedIndex = -1; 143 for (int i = 0; i <= max; ++i) { 144 if (selection.get(i)) { 145 minSelectedIndex = i; 146 break; 147 } 148 } 149 } 150 } 151 fireValueChanged(); 152 } 153 } 154 155 158 public void setSelectionMode(int selectionMode) { 159 if (selectionMode != MULTIPLE_SELECTION && this.selectionMode == MULTIPLE_SELECTION) { 160 int maxSelectedIndex = getMaxSelectedIndex(); 162 for (int i = minSelectedIndex + 1; i <= maxSelectedIndex; ++i) { 163 setSelectedIndex(i, false); 164 } 165 } 166 this.selectionMode = selectionMode; 167 fireValueChanged(); 168 } 169 } 170 | Popular Tags |