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