1 package autotest.moblab; 2 3 import autotest.common.JsonRpcCallback; 4 import autotest.common.SimpleCallback; 5 import autotest.common.Utils; 6 import autotest.common.ui.TabView; 7 import autotest.moblab.rpc.MoblabRpcHelper; 8 import autotest.common.ui.NotifyManager; 9 10 import com.google.gwt.event.dom.client.ClickHandler; 11 import com.google.gwt.event.dom.client.ClickEvent; 12 import com.google.gwt.json.client.JSONArray; 13 import com.google.gwt.json.client.JSONObject; 14 import com.google.gwt.json.client.JSONString; 15 import com.google.gwt.json.client.JSONValue; 16 import com.google.gwt.user.client.ui.Anchor; 17 import com.google.gwt.user.client.ui.Button; 18 import com.google.gwt.user.client.ui.FlexTable; 19 import com.google.gwt.user.client.ui.TextBox; 20 import com.google.gwt.user.client.ui.Label; 21 import com.google.gwt.user.client.ui.PopupPanel; 22 import com.google.gwt.user.client.ui.VerticalPanel; 23 24 import java.util.HashMap; 25 import java.util.HashSet; 26 import java.util.Map.Entry; 27 import java.util.Set; 28 29 public class ConfigSettingsView extends TabView { 30 private static final Set<String> GOOGLE_STORAGE_CONFIG_KEY = new HashSet<String>(); 31 static { 32 GOOGLE_STORAGE_CONFIG_KEY.add("image_storage_server"); 33 GOOGLE_STORAGE_CONFIG_KEY.add("results_storage_server"); 34 GOOGLE_STORAGE_CONFIG_KEY.add("canary_channel_server"); 35 GOOGLE_STORAGE_CONFIG_KEY.add("chromeos_image_archive"); 36 } 37 38 private Button submitButton; 39 private Button resetButton; 40 private HashMap<String, HashMap<String, TextBox> > configValueTextBoxes; 41 private FlexTable configValueTable; 42 private PopupPanel resetConfirmPanel; 43 private Button resetConfirmButton; 44 private PopupPanel submitConfirmPanel; 45 private Button submitConfirmButton; 46 47 @Override refresh()48 public void refresh() { 49 super.refresh(); 50 configValueTable.removeAllRows(); 51 fetchConfigData(new SimpleCallback() { 52 @Override 53 public void doCallback(Object source) { 54 loadData((JSONValue) source); 55 } 56 }); 57 resetConfirmPanel.hide(); 58 } 59 60 @Override getElementId()61 public String getElementId() { 62 return "config_settings"; 63 } 64 getAlertPanel(String alertMessage, Button confirmButton)65 private PopupPanel getAlertPanel(String alertMessage, Button confirmButton){ 66 PopupPanel alertPanel = new PopupPanel(true); 67 VerticalPanel alertInnerPanel = new VerticalPanel(); 68 alertInnerPanel.add(new Label(alertMessage)); 69 alertInnerPanel.add(confirmButton); 70 alertPanel.setWidget(alertInnerPanel); 71 return alertPanel; 72 } 73 74 @Override initialize()75 public void initialize() { 76 super.initialize(); 77 configValueTable = new FlexTable(); 78 79 resetConfirmButton = new Button("Confirm Reset", new ClickHandler() { 80 @Override 81 public void onClick(ClickEvent event) { 82 rpcCallReset(); 83 resetConfirmPanel.hide(); 84 } 85 }); 86 87 resetConfirmPanel =getAlertPanel( 88 "Restoring Default Settings requires rebooting the MobLab. Are you sure?", 89 resetConfirmButton); 90 91 submitConfirmButton = new Button("Confirm Save", new ClickHandler() { 92 @Override 93 public void onClick(ClickEvent event) { 94 JSONObject configValues = new JSONObject(); 95 for (Entry<String, HashMap<String, TextBox> > sections : configValueTextBoxes.entrySet()) { 96 JSONArray sectionValue = new JSONArray(); 97 for (Entry<String, TextBox> configValue : sections.getValue().entrySet()) { 98 JSONArray configValuePair = new JSONArray(); 99 configValuePair.set(0, new JSONString(configValue.getKey())); 100 configValuePair.set(1, new JSONString(configValue.getValue().getText())); 101 sectionValue.set(sectionValue.size(), configValuePair); 102 } 103 configValues.put(sections.getKey(), sectionValue); 104 } 105 rpcCallSubmit(configValues); 106 submitConfirmPanel.hide(); 107 } 108 }); 109 110 submitConfirmPanel = getAlertPanel( 111 "Saving settings requires restarting services on Moblab. Are you sure?", 112 submitConfirmButton); 113 114 submitButton = new Button("Submit", new ClickHandler() { 115 @Override 116 public void onClick(ClickEvent event) { 117 submitConfirmPanel.center(); 118 } 119 }); 120 121 resetButton = new Button("Restore Defaults", new ClickHandler() { 122 @Override 123 public void onClick(ClickEvent event) { 124 resetConfirmPanel.center(); 125 } 126 }); 127 128 addWidget(configValueTable, "view_config_values"); 129 addWidget(submitButton, "view_submit"); 130 addWidget(resetButton, "view_reset"); 131 } 132 fetchConfigData(final SimpleCallback callBack)133 private void fetchConfigData(final SimpleCallback callBack) { 134 MoblabRpcHelper.fetchConfigData(callBack); 135 } 136 loadData(JSONValue result)137 private void loadData(JSONValue result) { 138 configValueTextBoxes = new HashMap<String, HashMap<String, TextBox> >(); 139 JSONObject resultObject = result.isObject(); 140 for (String section : resultObject.keySet()) { 141 JSONArray sectionArray = resultObject.get(section).isArray(); 142 HashMap<String, TextBox> sectionKeyValues = new HashMap<String, TextBox>(); 143 144 Label sectionLabel = new Label(section); 145 sectionLabel.addStyleName("field-name"); 146 configValueTable.setWidget(configValueTable.getRowCount(), 0, sectionLabel); 147 148 for (int i = 0; i < sectionArray.size(); i++) { 149 JSONArray configPair = sectionArray.get(i).isArray(); 150 String configKey = configPair.get(0).isString().stringValue(); 151 String configValue = configPair.get(1).isString().stringValue(); 152 153 TextBox configInput = new TextBox(); 154 configInput.setVisibleLength(100); 155 156 int row = configValueTable.getRowCount(); 157 configValueTable.setWidget(row, 0, new Label(configKey)); 158 configValueTable.setWidget(row, 1, configInput); 159 configInput.setText(configValue); 160 if (GOOGLE_STORAGE_CONFIG_KEY.contains(configKey)) { 161 Anchor a = Utils.createGoogleStorageHttpUrlLink("link", configValue); 162 if (a != null) { 163 configValueTable.setWidget(row, 2, a); 164 } 165 } 166 sectionKeyValues.put(configKey, configInput); 167 } 168 169 if (sectionArray.size() == 0) { 170 configValueTable.setText(configValueTable.getRowCount(), 0, 171 "No config values in this section."); 172 } 173 174 configValueTextBoxes.put(section, sectionKeyValues); 175 // Add an empty row after each section. 176 configValueTable.setText(configValueTable.getRowCount(), 0, ""); 177 } 178 } 179 rpcCallSubmit(JSONObject configValues)180 public void rpcCallSubmit(JSONObject configValues) { 181 MoblabRpcHelper.submitConfigData(configValues, new JsonRpcCallback() { 182 @Override 183 public void onSuccess(JSONValue result) { 184 NotifyManager.getInstance().showMessage("Setup completed."); 185 } 186 }); 187 } 188 rpcCallReset()189 public void rpcCallReset() { 190 MoblabRpcHelper.resetConfigData(new JsonRpcCallback() { 191 @Override 192 public void onSuccess(JSONValue result) { 193 NotifyManager.getInstance().showMessage("Reset completed."); 194 } 195 }); 196 } 197 198 } 199