1 package autotest.afe; 2 3 import autotest.common.SimpleCallback; 4 import autotest.common.StaticDataRepository; 5 import autotest.common.Utils; 6 import autotest.common.table.BooleanFilter; 7 import autotest.common.table.CheckboxFilter; 8 import autotest.common.table.ListFilter; 9 import autotest.common.table.SearchFilter; 10 import autotest.common.table.TableDecorator; 11 12 import com.google.gwt.json.client.JSONArray; 13 import com.google.gwt.json.client.JSONBoolean; 14 import com.google.gwt.json.client.JSONObject; 15 import com.google.gwt.json.client.JSONString; 16 import com.google.gwt.json.client.JSONValue; 17 18 import java.util.ArrayList; 19 import java.util.List; 20 21 class HostTableDecorator extends TableDecorator implements SimpleCallback { 22 SearchFilter hostnameFilter; 23 LabelFilter labelFilter; 24 ListFilter statusFilter; 25 BooleanFilter lockedFilter; 26 ListFilter lockedByFilter; 27 AclAccessibleFilter aclFilter; 28 29 static class AclAccessibleFilter extends CheckboxFilter { 30 private JSONValue username; 31 AclAccessibleFilter()32 public AclAccessibleFilter() { 33 super("aclgroup__users__login"); 34 username = new JSONString(StaticDataRepository.getRepository().getCurrentUserLogin()); 35 } 36 37 @Override getMatchValue()38 public JSONValue getMatchValue() { 39 return username; 40 } 41 } 42 HostTableDecorator(HostTable table, int rowsPerPage)43 public HostTableDecorator(HostTable table, int rowsPerPage) { 44 super(table); 45 table.sortOnColumn("hostname"); /* Case sensitive name */ 46 table.setRowsPerPage(rowsPerPage); 47 addPaginators(); 48 49 StaticDataRepository staticData = StaticDataRepository.getRepository(); 50 JSONArray statuses = staticData.getData("host_statuses").isArray(); 51 String[] statusStrings = Utils.JSONtoStrings(statuses); 52 53 JSONArray users = staticData.getData("users").isArray(); 54 List<String> userStrings = new ArrayList<String>(); 55 for (int i = 0; i < users.size(); i++) { 56 JSONObject user = users.get(i).isObject(); 57 userStrings.add(Utils.jsonToString(user.get("login"))); 58 } 59 60 hostnameFilter = new SearchFilter("hostname", true); 61 labelFilter = new LabelFilter(); 62 statusFilter = new ListFilter("status"); 63 statusFilter.setChoices(statusStrings); 64 lockedFilter = new BooleanFilter("locked"); 65 lockedByFilter = new ListFilter("locked_by__login"); 66 lockedByFilter.setChoices(userStrings.toArray(new String[userStrings.size()])); 67 aclFilter = new AclAccessibleFilter(); 68 69 updateLockedByEnabled(); 70 lockedFilter.addCallback(this); 71 72 addFilter("Hostname", hostnameFilter); 73 addControl("Platform", labelFilter.getPlatformWidget()); 74 addFilter("Label", labelFilter); 75 addFilter("Status", statusFilter); 76 addFilter("Locked", lockedFilter); 77 addFilter("Locked By", lockedByFilter); 78 addFilter("ACL accessible only", aclFilter); 79 } 80 81 @Override doCallback(Object source)82 public void doCallback(Object source) { 83 assert source == lockedFilter; 84 updateLockedByEnabled(); 85 } 86 updateLockedByEnabled()87 private void updateLockedByEnabled() { 88 if (lockedFilter.isActive() && lockedFilter.isSelected()) { 89 lockedByFilter.setEnabled(true); 90 } else { 91 lockedByFilter.setEnabled(false); 92 } 93 } 94 } 95