ExtendedListBox.java revision 02ed4bd47471fe77270e5507a7f9759955b3753c
1package autotest.common.ui;
2
3import com.google.gwt.user.client.ui.ListBox;
4
5public class ExtendedListBox extends ListBox implements SimplifiedList {
6    private int findItemByName(String name) {
7        for (int i = 0; i < getItemCount(); i++) {
8            if (getItemText(i).equals(name)) {
9                return i;
10            }
11        }
12        throw new IllegalArgumentException("No such name found: " + name);
13    }
14
15    private int findItemByValue(String value) {
16        for (int i = 0; i < getItemCount(); i++) {
17            if (getValue(i).equals(value)) {
18                return i;
19            }
20        }
21        throw new IllegalArgumentException("No such value found: " + value);
22    }
23
24    public void removeItemByName(String name) {
25        removeItem(findItemByName(name));
26    }
27
28    private boolean isNothingSelected() {
29        return getSelectedIndex() == -1;
30    }
31
32    public String getSelectedName() {
33        if (isNothingSelected()) {
34            return null;
35        }
36        return getItemText(getSelectedIndex());
37    }
38
39    public String getSelectedValue() {
40        if (isNothingSelected()) {
41            return null;
42        }
43        return getValue(getSelectedIndex());
44    }
45
46    public void selectByName(String name) {
47        setSelectedIndex(findItemByName(name));
48    }
49
50    public void selectByValue(String value) {
51        setSelectedIndex(findItemByValue(value));
52    }
53}
54