Home | History | Annotate | Download | only in ui
      1 package autotest.common.ui;
      2 
      3 import autotest.common.JsonRpcProxy;
      4 import autotest.common.Utils;
      5 import autotest.common.CustomHistory.HistoryToken;
      6 
      7 import com.google.gwt.event.dom.client.ClickEvent;
      8 import com.google.gwt.event.dom.client.ClickHandler;
      9 import com.google.gwt.event.dom.client.KeyCodes;
     10 import com.google.gwt.event.dom.client.KeyPressEvent;
     11 import com.google.gwt.event.dom.client.KeyPressHandler;
     12 import com.google.gwt.json.client.JSONObject;
     13 import com.google.gwt.user.client.ui.Button;
     14 import com.google.gwt.user.client.ui.TextBox;
     15 
     16 import java.util.Map;
     17 
     18 
     19 
     20 public abstract class DetailView extends TabView {
     21     protected static final String NO_OBJECT = "";
     22 
     23     protected JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
     24     protected TextBox idInput = new TextBox();
     25     protected Button idFetchButton = new Button("Go");
     26 
     27     protected abstract String getNoObjectText();
     28     protected abstract String getFetchControlsElementId();
     29     protected abstract String getDataElementId();
     30     protected abstract String getTitleElementId();
     31     protected abstract String getObjectId();
     32     protected abstract void setObjectId(String id); // throws IllegalArgumentException
     33     protected abstract void fetchData();
     34 
     35     @Override
     36     public void initialize() {
     37         super.initialize();
     38         resetPage();
     39 
     40         addWidget(idInput, getFetchControlsElementId());
     41         addWidget(idFetchButton, getFetchControlsElementId());
     42 
     43         idInput.addKeyPressHandler(new KeyPressHandler() {
     44             public void onKeyPress (KeyPressEvent event) {
     45                 if (event.getCharCode() == (char) KeyCodes.KEY_ENTER)
     46                     fetchById(idInput.getText());
     47             }
     48         });
     49         idFetchButton.addClickHandler(new ClickHandler() {
     50             public void onClick(ClickEvent event) {
     51                 fetchById(idInput.getText());
     52             }
     53         });
     54     }
     55 
     56     protected void showText(String text, String elementId) {
     57         getElementById(elementId).setInnerText(text);
     58     }
     59 
     60     protected void showField(JSONObject object, String field, String elementId) {
     61         String value = Utils.jsonToString(object.get(field));
     62         showText(value, elementId);
     63     }
     64 
     65     public void resetPage() {
     66         showText(getNoObjectText(), getTitleElementId());
     67         Utils.setElementVisible(getDataElementId(), false);
     68     }
     69 
     70     public void updateObjectId(String id) {
     71         try {
     72             setObjectId(id);
     73         }
     74         catch (IllegalArgumentException exc) {
     75             String error = "Invalid input: " + id;
     76             NotifyManager.getInstance().showError(error);
     77             return;
     78         }
     79         idInput.setText(id);
     80     }
     81 
     82     public void fetchById(String id) {
     83         updateObjectId(id);
     84         updateHistory();
     85         refresh();
     86     }
     87 
     88     @Override
     89     public void refresh() {
     90         super.refresh();
     91         if (!getObjectId().equals(NO_OBJECT))
     92             fetchData();
     93     }
     94 
     95     protected void displayObjectData(String title) {
     96         showText(title, getTitleElementId());
     97         Utils.setElementVisible(getDataElementId(), true);
     98     }
     99 
    100     @Override
    101     public HistoryToken getHistoryArguments() {
    102         HistoryToken arguments = super.getHistoryArguments();
    103         String objectId = getObjectId();
    104         if (!objectId.equals(NO_OBJECT)) {
    105             arguments.put("object_id", objectId);
    106         }
    107         return arguments;
    108     }
    109 
    110     @Override
    111     public void handleHistoryArguments(Map<String, String> arguments) {
    112         String objectId = arguments.get("object_id");
    113         if (objectId == null) {
    114             resetPage();
    115             return;
    116         }
    117 
    118         try {
    119             updateObjectId(objectId);
    120         }
    121         catch (IllegalArgumentException exc) {
    122             return;
    123         }
    124     }
    125 }
    126