1 package autotest.afe; 2 3 import autotest.common.SimpleCallback; 4 import autotest.common.CustomHistory.HistoryToken; 5 import autotest.common.table.Filter; 6 import autotest.common.table.RadioButtonSetFilter; 7 import autotest.common.table.SearchFilter; 8 import autotest.common.table.SelectionManager; 9 import autotest.common.table.TableDecorator; 10 import autotest.common.table.DynamicTable.DynamicTableListener; 11 import autotest.common.ui.ContextMenu; 12 import autotest.common.ui.NotifyManager; 13 import autotest.common.ui.TabView; 14 import autotest.common.ui.TableActionsPanel.TableActionsListener; 15 import autotest.common.ui.ToolTip; 16 17 import com.google.gwt.json.client.JSONArray; 18 import com.google.gwt.json.client.JSONBoolean; 19 import com.google.gwt.json.client.JSONObject; 20 import com.google.gwt.user.client.Command; 21 import com.google.gwt.user.client.ui.HorizontalPanel; 22 import com.google.gwt.user.client.ui.HTMLPanel; 23 24 import java.util.Map; 25 import java.util.Set; 26 27 28 public class JobListView extends TabView implements TableActionsListener { 29 protected static final String SELECTED_LINK_STYLE = "selected-link"; 30 protected static final int JOBS_PER_PAGE = 30; 31 protected static final int STATUS_QUEUED = 0, STATUS_RUNNING = 1, 32 STATUS_FINISHED = 2, STATUS_ALL = 3, 33 STATUS_RADIO_BUTTON_COUNT = 4; 34 protected static final int STATUS_DEFAULT_BUTTON = STATUS_ALL; 35 protected static final int TYPE_SUITE = 0, TYPE_SUB = 1, TYPE_STANDALONE = 2, 36 TYPE_ALL = 3, TYPE_RADIO_BUTTON_COUNT = 4; 37 protected static final int TYPE_DEFAULT_BUTTON = TYPE_ALL; 38 private static final String[] statusHistoryTokens = {"queued", "running", 39 "finished", "all"}; 40 private static final String[] statusRadioButtonLabels = {"Queued Jobs", "Running Jobs", 41 "Finished Jobs", "All Jobs"}; 42 private static final String[] statusFilterStrings = {"not_yet_run", "running", 43 "finished"}; 44 private static final String[] typeHistoryTokens = {"suite", "sub", "standalone", 45 "all"}; 46 private static final String[] typeRadioButtonLabels = {"Parent Jobs", "Child Jobs", 47 "Standalone Jobs", 48 "All Jobs"}; 49 private static final String[] typeFilterStrings = {"suite", "sub", "standalone"}; 50 51 private JobSelectListener selectListener; 52 53 private JobTable jobTable; 54 private TableDecorator tableDecorator; 55 private JobStateFilter jobStateFilter; 56 private JobTypeFilter jobTypeFilter; 57 private Filter ownerFilter; 58 private SearchFilter nameFilter; 59 private SelectionManager selectionManager; 60 61 interface JobSelectListener { onJobSelected(int jobId)62 public void onJobSelected(int jobId); 63 } 64 65 static class JobStateFilter extends RadioButtonSetFilter { JobStateFilter()66 public JobStateFilter() { 67 super("job-status"); 68 } 69 70 @Override addParams(JSONObject params)71 public void addParams(JSONObject params) { 72 params.put(statusFilterStrings[getSelectedButtonIndex()], 73 JSONBoolean.getInstance(true)); 74 } 75 76 @Override isActive()77 public boolean isActive() { 78 return getSelectedButtonIndex() < STATUS_ALL; 79 } 80 } 81 82 static class JobTypeFilter extends RadioButtonSetFilter { JobTypeFilter()83 public JobTypeFilter() { 84 super("job-type"); 85 } 86 87 @Override addParams(JSONObject params)88 public void addParams(JSONObject params) { 89 params.put(typeFilterStrings[getSelectedButtonIndex()], 90 JSONBoolean.getInstance(true)); 91 } 92 93 @Override isActive()94 public boolean isActive() { 95 return getSelectedButtonIndex() < TYPE_ALL; 96 } 97 } 98 abortSelectedJobs()99 public void abortSelectedJobs() { 100 Set<JSONObject> selectedSet = selectionManager.getSelectedObjects(); 101 if (selectedSet.isEmpty()) { 102 NotifyManager.getInstance().showError("No jobs selected"); 103 return; 104 } 105 106 JSONArray ids = new JSONArray(); 107 for(JSONObject jsonObj : selectedSet) { 108 ids.set(ids.size(), jsonObj.get("id")); 109 } 110 111 JSONObject params = new JSONObject(); 112 params.put("job__id__in", ids); 113 AfeUtils.callAbort(params, new SimpleCallback() { 114 public void doCallback(Object source) { 115 refresh(); 116 } 117 }); 118 } 119 120 @Override getElementId()121 public String getElementId() { 122 return "job_list"; 123 } 124 125 @Override refresh()126 public void refresh() { 127 super.refresh(); 128 jobTable.refresh(); 129 } 130 JobListView(JobSelectListener listener)131 public JobListView(JobSelectListener listener) { 132 selectListener = listener; 133 } 134 135 @Override initialize()136 public void initialize() { 137 super.initialize(); 138 jobTable = new JobTable(); 139 jobTable.setRowsPerPage(JOBS_PER_PAGE); 140 jobTable.setClickable(true); 141 jobTable.addListener(new DynamicTableListener() { 142 public void onRowClicked(int rowIndex, JSONObject row, boolean isRightClick) { 143 int jobId = (int) row.get("id").isNumber().doubleValue(); 144 selectListener.onJobSelected(jobId); 145 } 146 147 public void onTableRefreshed() {} 148 }); 149 150 tableDecorator = new TableDecorator(jobTable); 151 tableDecorator.addPaginators(); 152 selectionManager = tableDecorator.addSelectionManager(false); 153 jobTable.setWidgetFactory(selectionManager); 154 tableDecorator.addTableActionsPanel(this, true); 155 addWidget(tableDecorator, "job_table"); 156 157 ownerFilter = new JobOwnerFilter("owner"); 158 jobTable.addFilter(ownerFilter); 159 addWidget(ownerFilter.getWidget(), "user_list"); 160 161 nameFilter = new SearchFilter("name", "Filter", false); 162 jobTable.addFilter(nameFilter); 163 addWidget(nameFilter.getWidget(), "jl_name_search"); 164 165 jobStateFilter = new JobStateFilter(); 166 for (int i = 0; i < STATUS_RADIO_BUTTON_COUNT; i++) 167 jobStateFilter.addRadioButon(statusRadioButtonLabels[i]); 168 // All Jobs is selected by default 169 jobStateFilter.setSelectedButton(STATUS_DEFAULT_BUTTON); 170 jobStateFilter.addCallback(new SimpleCallback() { 171 public void doCallback(Object source) { 172 updateHistory(); 173 } 174 }); 175 jobTable.addFilter(jobStateFilter); 176 addWidget(jobStateFilter.getWidget(), "job_status_controls"); 177 178 jobTypeFilter = new JobTypeFilter(); 179 for (int i = 0; i < TYPE_RADIO_BUTTON_COUNT; i++) 180 jobTypeFilter.addRadioButon(typeRadioButtonLabels[i]); 181 // All Jobs is selected by default 182 jobTypeFilter.setSelectedButton(TYPE_DEFAULT_BUTTON); 183 jobTypeFilter.addCallback(new SimpleCallback() { 184 public void doCallback(Object source) { 185 updateHistory(); 186 } 187 }); 188 jobTable.addFilter(jobTypeFilter); 189 addWidget(new ToolTip("?", "Suite jobs: jobs with child jobs. " + 190 "Sub jobs: jobs with a parent jobs. " + 191 "Standalone jobs: jobs without a parent or child job."), 192 "job_type_tooltip"); 193 addWidget(jobTypeFilter.getWidget(), "job_type_controls"); 194 } 195 196 @Override getHistoryArguments()197 public HistoryToken getHistoryArguments() { 198 HistoryToken arguments = super.getHistoryArguments(); 199 arguments.put("state_filter", 200 statusHistoryTokens[jobStateFilter.getSelectedButtonIndex()]); 201 arguments.put("type_filter", 202 typeHistoryTokens[jobTypeFilter.getSelectedButtonIndex()]); 203 return arguments; 204 } 205 206 @Override handleHistoryArguments(Map<String, String> arguments)207 public void handleHistoryArguments(Map<String, String> arguments) { 208 String stateFilter = arguments.get("state_filter"); 209 if (stateFilter == null) { 210 jobStateFilter.setSelectedButton(STATUS_DEFAULT_BUTTON); 211 } else { 212 for (int i = 0; i < STATUS_RADIO_BUTTON_COUNT; i++) { 213 if (stateFilter.equals(statusHistoryTokens[i])) { 214 jobStateFilter.setSelectedButton(i); 215 break; 216 } 217 } 218 } 219 220 String typeFilter = arguments.get("type_filter"); 221 if (typeFilter == null) { 222 jobTypeFilter.setSelectedButton(TYPE_DEFAULT_BUTTON); 223 } else { 224 for (int i = 0; i < TYPE_RADIO_BUTTON_COUNT; i++) { 225 if (typeFilter.equals(typeHistoryTokens[i])) { 226 jobTypeFilter.setSelectedButton(i); 227 break; 228 } 229 } 230 } 231 } 232 getActionMenu()233 public ContextMenu getActionMenu() { 234 ContextMenu menu = new ContextMenu(); 235 menu.addItem("Abort jobs", new Command() { 236 public void execute() { 237 abortSelectedJobs(); 238 } 239 }); 240 if (selectionManager.isEmpty()) 241 menu.setEnabled(false); 242 return menu; 243 } 244 } 245