Home | History | Annotate | Download | only in table
      1 package autotest.common.table;
      2 
      3 import autotest.common.ui.ExtendedListBox;
      4 
      5 import com.google.gwt.event.dom.client.ChangeEvent;
      6 import com.google.gwt.event.dom.client.ChangeHandler;
      7 import com.google.gwt.json.client.JSONString;
      8 import com.google.gwt.json.client.JSONValue;
      9 import com.google.gwt.user.client.ui.ListBox;
     10 import com.google.gwt.user.client.ui.Widget;
     11 
     12 public class ListFilter extends FieldFilter {
     13     protected ListBox select;
     14     protected String allValuesText = "All values";
     15 
     16     public ListFilter(String fieldName) {
     17         super(fieldName);
     18         select = new ExtendedListBox();
     19         select.setMultipleSelect(isMultipleSelect());
     20         select.setStylePrimaryName("filter-box");
     21         select.addChangeHandler(new ChangeHandler() {
     22             public void onChange(ChangeEvent event) {
     23                 notifyListeners();
     24             }
     25         });
     26     }
     27 
     28     /**
     29      * Subclasses should override this if they wish to use a multi-select listbox
     30      * @return true if and only if the listbox should be multiple select
     31      */
     32     protected boolean isMultipleSelect() {
     33         return false;
     34     }
     35 
     36     /**
     37      * Set the text for that option that matches any value for this filter.
     38      */
     39     public void setMatchAllText(String text) {
     40         allValuesText = text;
     41         if (select.getItemCount() > 0) {
     42             select.setItemText(0, allValuesText);
     43         }
     44     }
     45 
     46     @Override
     47     public void setExactMatch(boolean isExactMatch) {
     48         this.isExactMatch = isExactMatch;
     49     }
     50 
     51     protected String getItemText(int index) {
     52         return select.getItemText(index);
     53     }
     54 
     55     protected String getSelectedText() {
     56         int selected = select.getSelectedIndex();
     57         if (selected == -1) {
     58             return "";
     59         }
     60         return getItemText(selected);
     61     }
     62 
     63     @Override
     64     public JSONValue getMatchValue() {
     65         return new JSONString(getSelectedText());
     66     }
     67 
     68     @Override
     69     public boolean isActive() {
     70         return select.isEnabled() && !getSelectedText().equals(allValuesText);
     71     }
     72 
     73     @Override
     74     public Widget getWidget() {
     75         return select;
     76     }
     77 
     78     public void setChoices(String[] choices) {
     79         String selectedValue = null;
     80         if (select.getSelectedIndex() != -1) {
     81             selectedValue = getSelectedText();
     82         }
     83 
     84         select.clear();
     85         select.addItem(allValuesText);
     86         for (int i = 0; i < choices.length; i++) {
     87             select.addItem(choices[i]);
     88         }
     89 
     90         if (selectedValue != null) {
     91             setSelectedChoice(selectedValue);
     92         }
     93     }
     94 
     95     public void setSelectedChoice(String choice) {
     96         for(int i = 0; i < select.getItemCount(); i++) {
     97             if(select.getItemText(i).equals(choice)) {
     98                 select.setSelectedIndex(i);
     99                 return;
    100             }
    101         }
    102 
    103         select.addItem(choice);
    104         select.setSelectedIndex(select.getItemCount() - 1);
    105     }
    106 
    107     public void setEnabled(boolean enabled) {
    108         select.setEnabled(enabled);
    109     }
    110 }
    111