1package autotest.tko; 2 3import autotest.common.CustomHistory; 4import autotest.common.JsonRpcCallback; 5import autotest.common.JsonRpcProxy; 6import autotest.common.Utils; 7import autotest.common.CustomHistory.CustomHistoryListener; 8import autotest.common.CustomHistory.HistoryToken; 9import autotest.common.ui.NotifyManager; 10import autotest.common.ui.SimpleDialog; 11import autotest.tko.TableView.TableSwitchListener; 12 13import com.google.gwt.event.dom.client.ClickEvent; 14import com.google.gwt.event.dom.client.ClickHandler; 15import com.google.gwt.json.client.JSONNumber; 16import com.google.gwt.json.client.JSONObject; 17import com.google.gwt.json.client.JSONString; 18import com.google.gwt.json.client.JSONValue; 19import com.google.gwt.user.client.Window; 20import com.google.gwt.user.client.ui.Anchor; 21import com.google.gwt.user.client.ui.Composite; 22import com.google.gwt.user.client.ui.FlexTable; 23import com.google.gwt.user.client.ui.HasVerticalAlignment; 24import com.google.gwt.user.client.ui.TextBox; 25import com.google.gwt.user.client.ui.Widget; 26 27import java.util.Map; 28 29public abstract class GraphingFrontend extends Composite 30 implements CustomHistoryListener, ClickHandler { 31 public static final String HISTORY_TOKEN = "embedded_query"; 32 33 protected FlexTable table = new FlexTable(); 34 protected JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy(); 35 protected Anchor embeddingLink = new Anchor("[Link to this graph]"); 36 protected TableSwitchListener listener; 37 38 public abstract void refresh(); 39 public abstract void addToHistory(Map<String, String> args); 40 public abstract void handleHistoryArguments(Map<String, String> args); 41 42 /** 43 * This function allows subclasses to add parameters to the call to get_embedding_id() RPC, 44 * called when a user requests an embeddable link to a graph. 45 */ 46 protected abstract void addAdditionalEmbeddingParams(JSONObject params); 47 48 /** 49 * @return a short text ID for the frontend 50 */ 51 public abstract String getFrontendId(); 52 53 protected GraphingFrontend() { 54 CustomHistory.addHistoryListener(this); 55 embeddingLink.addClickHandler(this); 56 } 57 58 public void onClick(ClickEvent event) { 59 assert event.getSource() == embeddingLink; 60 JSONObject params = new JSONObject(); 61 params.put("url_token", new JSONString(CustomHistory.getLastHistoryToken().toString())); 62 addAdditionalEmbeddingParams(params); 63 64 rpcProxy.rpcCall("get_embedding_id", params, new JsonRpcCallback() { 65 @Override 66 public void onSuccess(JSONValue result) { 67 String id = Utils.jsonToString(result); 68 showEmbeddedGraphHtml(id); 69 } 70 }); 71 } 72 73 private void showEmbeddedGraphHtml(String embeddedGraphId) { 74 StringBuilder link = new StringBuilder(); 75 link.append("<a href=\"http://"); 76 link.append(Window.Location.getHost()); 77 link.append(Window.Location.getPath()); 78 link.append("#"); 79 link.append(HISTORY_TOKEN); 80 link.append("="); 81 link.append(embeddedGraphId); 82 83 link.append("\"><img border=\"0\" src=\"http://"); 84 link.append(Window.Location.getHost()); 85 86 link.append(JsonRpcProxy.TKO_BASE_URL); 87 link.append("plot/?id="); 88 link.append(embeddedGraphId); 89 90 link.append("&max_age=10"); 91 92 link.append("\"></a>"); 93 94 TextBox linkBox = new TextBox(); 95 linkBox.setText(link.toString()); 96 linkBox.setWidth("100%"); 97 linkBox.setSelectionRange(0, link.length()); 98 99 new SimpleDialog("Paste HTML to embed in website:", linkBox).center(); 100 } 101 102 protected void setListener(TableSwitchListener listener) { 103 this.listener = listener; 104 } 105 106 protected void addControl(String text, Widget control) { 107 int row = TkoUtils.addControlRow(table, text, control); 108 table.getFlexCellFormatter().setColSpan(row, 1, 2); 109 table.getFlexCellFormatter().setWidth(row, 1, "100%"); 110 table.getFlexCellFormatter().setVerticalAlignment(row, 0, HasVerticalAlignment.ALIGN_TOP); 111 } 112 113 // TODO(showard): merge this with the code from SavedQueriesControl 114 public void onHistoryChanged(Map<String, String> arguments) { 115 final String idString = arguments.get(HISTORY_TOKEN); 116 if (idString == null) { 117 return; 118 } 119 120 JSONObject args = new JSONObject(); 121 args.put("id", new JSONNumber(Integer.parseInt(idString))); 122 rpcProxy.rpcCall("get_embedded_query_url_token", args, new JsonRpcCallback() { 123 @Override 124 public void onSuccess(JSONValue result) { 125 String tokenString = Utils.jsonToString(result); 126 HistoryToken token; 127 try { 128 token = HistoryToken.fromString(tokenString); 129 } catch (IllegalArgumentException exc) { 130 NotifyManager.getInstance().showError("Invalid embedded query token " + 131 tokenString); 132 return; 133 } 134 135 // since this is happening asynchronously, the history may have changed, so ensure 136 // it's set back to what it should be. 137 HistoryToken shortToken = new HistoryToken(); 138 shortToken.put(HISTORY_TOKEN, idString); 139 CustomHistory.newItem(shortToken); 140 141 CustomHistory.simulateHistoryToken(token); 142 } 143 }); 144 } 145} 146