Home | History | Annotate | Download | only in ui
      1 package autotest.common.ui;
      2 
      3 import com.google.gwt.dom.client.Document;
      4 import com.google.gwt.dom.client.OptionElement;
      5 import com.google.gwt.dom.client.SelectElement;
      6 import com.google.gwt.user.client.ui.ListBox;
      7 
      8 public class ExtendedListBox extends ListBox implements SimplifiedList {
      9     private int findItemByName(String name) {
     10         for (int i = 0; i < getItemCount(); i++) {
     11             if (getItemText(i).equals(name)) {
     12                 return i;
     13             }
     14         }
     15         throw new IllegalArgumentException("No such name found: " + name);
     16     }
     17 
     18     private int findItemByValue(String value) {
     19         for (int i = 0; i < getItemCount(); i++) {
     20             if (getValue(i).equals(value)) {
     21                 return i;
     22             }
     23         }
     24         throw new IllegalArgumentException("No such value found: " + value);
     25     }
     26 
     27     private native void selectAppend(SelectElement select,
     28                                      OptionElement option) /*-{
     29         select.appendChild(option);
     30     }-*/;
     31 
     32     public void addItem(String name) {
     33         addItem(name, name);
     34     }
     35 
     36     public void addItem(String name, String value) {
     37         SelectElement select = getElement().cast();
     38         OptionElement option = Document.get().createOptionElement();
     39         setOptionText(option, name, null);
     40         option.setValue(value);
     41         selectAppend(select, option);
     42     }
     43 
     44     public void removeItemByName(String name) {
     45         removeItem(findItemByName(name));
     46     }
     47 
     48     private boolean isNothingSelected() {
     49         return getSelectedIndex() == -1;
     50     }
     51 
     52     public String getSelectedName() {
     53         if (isNothingSelected()) {
     54             return null;
     55         }
     56         return getItemText(getSelectedIndex());
     57     }
     58 
     59     public String getSelectedValue() {
     60         if (isNothingSelected()) {
     61             return null;
     62         }
     63         return getValue(getSelectedIndex());
     64     }
     65 
     66     public void selectByName(String name) {
     67         setSelectedIndex(findItemByName(name));
     68     }
     69 
     70     public void selectByValue(String value) {
     71         setSelectedIndex(findItemByValue(value));
     72     }
     73 }
     74