1 29 30 package nextapp.echo2.app; 31 32 import nextapp.echo2.app.list.AbstractListComponent; 33 import nextapp.echo2.app.list.DefaultListModel; 34 import nextapp.echo2.app.list.ListModel; 35 import nextapp.echo2.app.list.ListSelectionModel; 36 37 38 41 public class ListBox extends AbstractListComponent { 42 43 49 public ListBox() { 50 super(); 51 } 52 53 60 public ListBox(ListModel model) { 61 super(model, null); 62 } 63 64 72 public ListBox(ListModel model, ListSelectionModel selectionModel) { 73 super(model, selectionModel); 74 } 75 76 85 public ListBox(Object [] itemArray) { 86 super(new DefaultListModel(itemArray), null); 87 } 88 89 94 public int getMaxSelectedIndex() { 95 return getSelectionModel().getMaxSelectedIndex(); 96 } 97 98 103 public int getMinSelectedIndex() { 104 return getSelectionModel().getMinSelectedIndex(); 105 } 106 107 112 public int[] getSelectedIndices() { 113 ListModel model = getModel(); 114 int min = getMinSelectedIndex(); 115 if (min == -1) { 116 return new int[0]; 118 } 119 120 int selectionCount = 0; 121 int max = getMaxSelectedIndex(); 122 int size = model.size(); 123 if (max >= size - 1) { 124 max = size - 1; 125 } 126 127 for (int index = min; index <= max; ++index) { 128 if (isSelectedIndex(index)) { 129 ++selectionCount; 130 } 131 } 132 133 int[] selectedIndices = new int[selectionCount]; 134 selectionCount = 0; 135 for (int index = min; index <= max; ++index) { 136 if (isSelectedIndex(index)) { 137 selectedIndices[selectionCount] = index; 138 ++selectionCount; 139 } 140 } 141 142 return selectedIndices; 143 } 144 145 153 public Object getSelectedValue() { 154 ListModel model = getModel(); 155 int selectedIndex = getMinSelectedIndex(); 156 Object value; 157 158 if (selectedIndex == -1) { 159 value = null; 160 } else { 161 value = model.get(getMinSelectedIndex()); 162 } 163 164 return value; 165 } 166 167 172 public Object [] getSelectedValues() { 173 ListModel model = getModel(); 174 int min = getMinSelectedIndex(); 175 176 if (min == -1) { 177 return new Object [0]; 179 } 180 181 int selectionCount = 0; 182 int max = getMaxSelectedIndex(); 183 int size = model.size(); 184 if (max >= size - 1) { 185 max = size - 1; 186 } 187 188 for (int index = min; index <= max; ++index) { 189 if (isSelectedIndex(index)) { 190 ++selectionCount; 191 } 192 } 193 194 Object [] selectedValues = new Object [selectionCount]; 195 selectionCount = 0; 196 for (int index = min; index <= max; ++index) { 197 if (isSelectedIndex(index)) { 198 selectedValues[selectionCount] = model.get(index); 199 ++selectionCount; 200 } 201 } 202 203 return selectedValues; 204 } 205 206 217 public int getSelectionMode() { 218 return getSelectionModel().getSelectionMode(); 219 } 220 221 227 public boolean isSelectedIndex(int index) { 228 return getSelectionModel().isSelectedIndex(index); 229 } 230 231 236 public void setSelectedIndex(int index) { 237 ListSelectionModel selectionModel = getSelectionModel(); 238 selectionModel.clearSelection(); 239 selectionModel.setSelectedIndex(index, true); 240 } 241 242 248 public void setSelectedIndex(int index, boolean selected) { 249 getSelectionModel().setSelectedIndex(index, selected); 250 } 251 252 257 public void setSelectedIndices(int[] indices) { 258 ListSelectionModel selectionModel = getSelectionModel(); 259 selectionModel.clearSelection(); 260 for (int i = 0; i < indices.length; ++i) { 261 selectionModel.setSelectedIndex(indices[i], true); 262 } 263 } 264 265 276 public void setSelectionMode(int newValue) { 277 getSelectionModel().setSelectionMode(newValue); 278 } 279 } 280 | Popular Tags |