Home | History | Annotate | Download | only in afe
      1 package autotest.afe;
      2 
      3 import autotest.common.StaticDataRepository;
      4 import autotest.common.Utils;
      5 import autotest.common.table.FieldFilter;
      6 import autotest.common.ui.ExtendedListBox;
      7 
      8 import com.google.gwt.event.dom.client.ChangeEvent;
      9 import com.google.gwt.event.dom.client.ChangeHandler;
     10 import com.google.gwt.event.logical.shared.ValueChangeEvent;
     11 import com.google.gwt.event.logical.shared.ValueChangeHandler;
     12 import com.google.gwt.json.client.JSONArray;
     13 import com.google.gwt.json.client.JSONString;
     14 import com.google.gwt.json.client.JSONValue;
     15 import com.google.gwt.user.client.ui.HorizontalPanel;
     16 import com.google.gwt.user.client.ui.Panel;
     17 import com.google.gwt.user.client.ui.RadioButton;
     18 import com.google.gwt.user.client.ui.VerticalPanel;
     19 import com.google.gwt.user.client.ui.Widget;
     20 
     21 
     22 public class JobOwnerFilter extends FieldFilter
     23                             implements ValueChangeHandler<Boolean>, ChangeHandler {
     24     private static int nameCounter = 0;
     25 
     26     private ExtendedListBox userList = new ExtendedListBox();
     27     private RadioButton allUsersRadio, selectUserRadio;
     28     private Panel parentPanel;
     29 
     30     public JobOwnerFilter(String field) {
     31         super(field);
     32         String radioGroupName = getFreshName();
     33         allUsersRadio = new RadioButton(radioGroupName, "All users");
     34         selectUserRadio = new RadioButton(radioGroupName);
     35         allUsersRadio.addValueChangeHandler(this);
     36         selectUserRadio.addValueChangeHandler(this);
     37 
     38         populateUserList();
     39         userList.addChangeHandler(this);
     40 
     41         Panel selectUserPanel = new HorizontalPanel();
     42         selectUserPanel.add(selectUserRadio);
     43         selectUserPanel.add(userList);
     44         parentPanel = new VerticalPanel();
     45         parentPanel.add(allUsersRadio);
     46         parentPanel.add(selectUserPanel);
     47 
     48         selectUserRadio.setValue(true);
     49     }
     50 
     51     @Override
     52     // radio button selection changes
     53     public void onValueChange(ValueChangeEvent<Boolean> event) {
     54         userList.setEnabled(event.getSource() == selectUserRadio);
     55         notifyListeners();
     56     }
     57 
     58     @Override
     59     // user list changes
     60     public void onChange(ChangeEvent event) {
     61         notifyListeners();
     62     }
     63 
     64     private static String getFreshName() {
     65         nameCounter++;
     66         return "JobOwnerFilter" + Integer.toString(nameCounter);
     67     }
     68 
     69     private void populateUserList() {
     70         StaticDataRepository staticData = StaticDataRepository.getRepository();
     71         JSONArray userArray = staticData.getData("users").isArray();
     72         for (String user : Utils.JSONObjectsToStrings(userArray, "login")) {
     73             userList.addItem(user);
     74         }
     75         String currentUser = staticData.getCurrentUserLogin();
     76         userList.selectByName(currentUser);
     77     }
     78 
     79     @Override
     80     public JSONValue getMatchValue() {
     81         assert selectUserRadio.getValue();
     82         return new JSONString(userList.getSelectedName());
     83     }
     84 
     85     @Override
     86     public Widget getWidget() {
     87         return parentPanel;
     88     }
     89 
     90     @Override
     91     public boolean isActive() {
     92         return selectUserRadio.getValue();
     93     }
     94 }
     95