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 getNoObjectText()27 protected abstract String getNoObjectText(); getFetchControlsElementId()28 protected abstract String getFetchControlsElementId(); getDataElementId()29 protected abstract String getDataElementId(); getTitleElementId()30 protected abstract String getTitleElementId(); getObjectId()31 protected abstract String getObjectId(); setObjectId(String id)32 protected abstract void setObjectId(String id); // throws IllegalArgumentException fetchData()33 protected abstract void fetchData(); 34 35 @Override initialize()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 showText(String text, String elementId)56 protected void showText(String text, String elementId) { 57 getElementById(elementId).setInnerText(text); 58 } 59 showField(JSONObject object, String field, String elementId)60 protected void showField(JSONObject object, String field, String elementId) { 61 String value = Utils.jsonToString(object.get(field)); 62 showText(value, elementId); 63 } 64 resetPage()65 public void resetPage() { 66 showText(getNoObjectText(), getTitleElementId()); 67 Utils.setElementVisible(getDataElementId(), false); 68 } 69 updateObjectId(String id)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 fetchById(String id)82 public void fetchById(String id) { 83 updateObjectId(id); 84 updateHistory(); 85 refresh(); 86 } 87 88 @Override refresh()89 public void refresh() { 90 super.refresh(); 91 if (!getObjectId().equals(NO_OBJECT)) 92 fetchData(); 93 } 94 displayObjectData(String title)95 protected void displayObjectData(String title) { 96 showText(title, getTitleElementId()); 97 Utils.setElementVisible(getDataElementId(), true); 98 } 99 100 @Override getHistoryArguments()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 handleHistoryArguments(Map<String, String> arguments)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