1 package autotest.afe; 2 3 import autotest.afe.create.CreateJobViewPresenter.JobCreateListener; 4 import autotest.common.SimpleCallback; 5 import autotest.common.table.DynamicTable.DynamicTableListener; 6 import autotest.common.table.SelectionManager; 7 import autotest.common.ui.ContextMenu; 8 import autotest.common.ui.NotifyManager; 9 import autotest.common.ui.TabView; 10 import autotest.common.ui.TableActionsPanel.TableActionsListener; 11 12 import com.google.gwt.json.client.JSONArray; 13 import com.google.gwt.json.client.JSONObject; 14 import com.google.gwt.user.client.Command; 15 16 import java.util.Set; 17 18 public class HostListView extends TabView implements TableActionsListener { 19 protected static final int HOSTS_PER_PAGE = 30; 20 21 public interface HostListListener { onHostSelected(String id)22 public void onHostSelected(String id); 23 } 24 25 protected HostListListener hostListListener = null; 26 private JobCreateListener jobCreateListener = null; 27 HostListView(HostListListener hostListListener, JobCreateListener jobCreateListener)28 public HostListView(HostListListener hostListListener, JobCreateListener jobCreateListener) { 29 this.hostListListener = hostListListener; 30 this.jobCreateListener = jobCreateListener; 31 } 32 33 @Override getElementId()34 public String getElementId() { 35 return "hosts"; 36 } 37 38 protected HostTable table; 39 protected HostTableDecorator hostTableDecorator; 40 protected SelectionManager selectionManager; 41 42 @Override initialize()43 public void initialize() { 44 super.initialize(); 45 46 table = new HostTable(new HostDataSource(), true); 47 hostTableDecorator = new HostTableDecorator(table, HOSTS_PER_PAGE); 48 49 selectionManager = hostTableDecorator.addSelectionManager(false); 50 table.setWidgetFactory(selectionManager); 51 hostTableDecorator.addTableActionsPanel(this, true); 52 53 table.setClickable(true); 54 table.addListener(new DynamicTableListener() { 55 public void onRowClicked(int rowIndex, JSONObject row, boolean isRightClick) { 56 String hostId = row.get("id").toString(); 57 hostListListener.onHostSelected(hostId); 58 } 59 60 public void onTableRefreshed() {} 61 }); 62 63 addWidget(hostTableDecorator, "hosts_list"); 64 } 65 66 @Override refresh()67 public void refresh() { 68 super.refresh(); 69 table.refresh(); 70 } 71 reverifySelectedHosts()72 private void reverifySelectedHosts() { 73 JSONObject params = new JSONObject(); 74 JSONArray hostIds = getSelectedHostIds(); 75 if (hostIds == null) { 76 return; 77 } 78 79 params.put("id__in", hostIds); 80 AfeUtils.callReverify(params, new SimpleCallback() { 81 public void doCallback(Object source) { 82 refresh(); 83 } 84 }, "Hosts"); 85 } 86 changeLockStatus(final boolean lock, String lockReason)87 private void changeLockStatus(final boolean lock, String lockReason) { 88 JSONArray hostIds = getSelectedHostIds(); 89 if (hostIds == null) { 90 return; 91 } 92 93 AfeUtils.changeHostLocks(hostIds, lock, lockReason, "Hosts", new SimpleCallback() { 94 public void doCallback(Object source) { 95 refresh(); 96 } 97 }); 98 } 99 getSelectedHosts()100 private Set<JSONObject> getSelectedHosts() { 101 Set<JSONObject> selectedSet = selectionManager.getSelectedObjects(); 102 if (selectedSet.isEmpty()) { 103 NotifyManager.getInstance().showError("No hosts selected"); 104 return null; 105 } 106 return selectedSet; 107 } 108 getSelectedHostIds()109 private JSONArray getSelectedHostIds() { 110 Set<JSONObject> selectedSet = getSelectedHosts(); 111 if (selectedSet == null) { 112 return null; 113 } 114 115 JSONArray ids = new JSONArray(); 116 for (JSONObject jsonObj : selectedSet) { 117 ids.set(ids.size(), jsonObj.get("id")); 118 } 119 120 return ids; 121 } 122 getActionMenu()123 public ContextMenu getActionMenu() { 124 ContextMenu menu = new ContextMenu(); 125 menu.addItem("Reverify hosts", new Command() { 126 public void execute() { 127 reverifySelectedHosts(); 128 } 129 }); 130 menu.addItem("Lock hosts for testing", new Command() { 131 public void execute() { 132 changeLockStatus(true, "Device is under test"); 133 } 134 }); 135 menu.addItem("Lock hosts for recovery", new Command() { 136 public void execute() { 137 changeLockStatus(true, "Device is recovering"); 138 } 139 }); 140 menu.addItem("Lock hosts for resource exclusion", new Command() { 141 public void execute() { 142 changeLockStatus(true, "Device is an excluded resource"); 143 } 144 }); 145 menu.addItem("Lock hosts for defects", new Command() { 146 public void execute() { 147 changeLockStatus(true, "Device is defective"); 148 } 149 }); 150 menu.addItem("Unlock hosts", new Command() { 151 public void execute() { 152 changeLockStatus(false, ""); 153 } 154 }); 155 156 if (selectionManager.isEmpty()) 157 menu.setEnabled(false); 158 return menu; 159 } 160 } 161