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