You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Client.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import java.util.HashMap;
  3. import java.util.Vector;
  4. import com.google.gwt.core.client.EntryPoint;
  5. import com.google.gwt.http.client.Request;
  6. import com.google.gwt.http.client.RequestBuilder;
  7. import com.google.gwt.http.client.RequestCallback;
  8. import com.google.gwt.http.client.RequestException;
  9. import com.google.gwt.http.client.Response;
  10. import com.google.gwt.json.client.JSONArray;
  11. import com.google.gwt.json.client.JSONObject;
  12. import com.google.gwt.json.client.JSONParser;
  13. import com.google.gwt.json.client.JSONValue;
  14. import com.google.gwt.user.client.ui.RootPanel;
  15. import com.google.gwt.user.client.ui.Widget;
  16. /**
  17. * Entry point classes define <code>onModuleLoad()</code>.
  18. *
  19. * TODO IDEA: Should be extend Widget here !?!?!
  20. */
  21. public class Client implements EntryPoint {
  22. private String appUri = "http://127.0.0.1:8080/tk/HelloWorld";
  23. // TODO remove repaintAll things start to pile up
  24. private RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, appUri
  25. + "/UIDL/?repaintAll=1&");
  26. private Console console;
  27. private Vector pendingVariables = new Vector();
  28. private HashMap paintables = new HashMap();
  29. private int requestCount = 0;
  30. private WidgetFactory widgetFactory = new DefaultWidgetFactory();
  31. /**
  32. * This is the entry point method.
  33. */
  34. public void onModuleLoad() {
  35. console = new Console(RootPanel.get("itmtk-loki"));
  36. console.log("Makin fake UIDL Request to fool servlet of an app init");
  37. RequestBuilder rb2 = new RequestBuilder(RequestBuilder.GET, appUri);
  38. try {
  39. rb2.sendRequest("", new RequestCallback() {
  40. public void onResponseReceived(Request request,
  41. Response response) {
  42. console
  43. .log("Got fake response... sending initial UIDL request");
  44. makeUidlRequest("repaintAll=1");
  45. }
  46. public void onError(Request request, Throwable exception) {
  47. // TODO Auto-generated method stub
  48. }
  49. });
  50. } catch (RequestException e1) {
  51. e1.printStackTrace();
  52. }
  53. }
  54. private void makeUidlRequest(String requestData) {
  55. console.log("Making UIDL Request");
  56. rb = new RequestBuilder(RequestBuilder.GET, appUri
  57. + "/UIDL/?requestId=" + (++requestCount) + "&" + requestData);
  58. try {
  59. rb.sendRequest(requestData, new RequestCallback() {
  60. public void onError(Request request, Throwable exception) {
  61. console.error("Got error");
  62. }
  63. public void onResponseReceived(Request request,
  64. Response response) {
  65. handleReceivedJSONMessage(response);
  66. }
  67. });
  68. console.log("Request sent");
  69. } catch (RequestException e) {
  70. console.error(e.getMessage());
  71. }
  72. }
  73. private void handleReceivedJSONMessage(Response response) {
  74. JSONValue json = JSONParser
  75. .parse(response.getText().substring(3) + "}");
  76. // Process changes
  77. JSONArray changes = (JSONArray) ((JSONObject) json).get("changes");
  78. for (int i = 0; i < changes.size(); i++) {
  79. try {
  80. UIDL change = new UIDL((JSONArray) changes.get(i));
  81. console.log("Received the following change: " + change);
  82. UIDL uidl = change.getChildUIDL(0);
  83. Paintable paintable = getPaintable(uidl.getId());
  84. if (paintable != null)
  85. paintable.updateFromUIDL(uidl, this);
  86. else {
  87. if (!uidl.getTag().equals("window"))
  88. throw new IllegalStateException("Received update for "
  89. + uidl.getTag()
  90. + ", but there is no such paintable ("
  91. + uidl.getId() + ") registered yet.");
  92. Widget window = createWidgetFromUIDL(uidl);
  93. // We should also handle other windows
  94. RootPanel.get("itmtk-ajax-window").add(window);
  95. }
  96. } catch (Throwable e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101. public void registerPaintable(String id, Paintable paintable) {
  102. paintables.put(id, paintable);
  103. }
  104. public Paintable getPaintable(String id) {
  105. return (Paintable) paintables.get(id);
  106. }
  107. public Widget createWidgetFromUIDL(UIDL uidlForChild) {
  108. Widget w = widgetFactory.createWidget(uidlForChild.getTag(), null);
  109. if (w instanceof Paintable) {
  110. registerPaintable(uidlForChild.getId(), (Paintable) w);
  111. ((Paintable)w).updateFromUIDL(uidlForChild, this);
  112. }
  113. return w;
  114. }
  115. private void addVariableToQueue(String paintableId, String variableName,
  116. String encodedValue, boolean immediate) {
  117. String id = paintableId + "_" + variableName;
  118. for (int i = 0; i < pendingVariables.size(); i += 2)
  119. if ((pendingVariables.get(i)).equals(id)) {
  120. pendingVariables.remove(i);
  121. pendingVariables.remove(i);
  122. break;
  123. }
  124. pendingVariables.add(id);
  125. pendingVariables.add(encodedValue);
  126. if (immediate)
  127. sendPendingVariableChanges();
  128. }
  129. public void sendPendingVariableChanges() {
  130. StringBuffer req = new StringBuffer();
  131. for (int i = 0; i < pendingVariables.size(); i++) {
  132. req.append(pendingVariables.get(i++));
  133. req.append("=");
  134. req.append(pendingVariables.get(i));
  135. }
  136. pendingVariables.clear();
  137. makeUidlRequest(req.toString());
  138. }
  139. private String escapeString(String value) {
  140. // TODO
  141. return value;
  142. }
  143. public void updateVariable(String paintableId, String variableName,
  144. String newValue, boolean immediate) {
  145. addVariableToQueue(paintableId, variableName, escapeString(newValue),
  146. immediate);
  147. }
  148. public void updateVariable(String paintableId, String variableName,
  149. int newValue, boolean immediate) {
  150. addVariableToQueue(paintableId, variableName, "" + newValue, immediate);
  151. }
  152. public void updateVariable(String paintableId, String variableName,
  153. boolean newValue, boolean immediate) {
  154. addVariableToQueue(paintableId, variableName, newValue ? "true"
  155. : "false", immediate);
  156. }
  157. public WidgetFactory getWidgetFactory() {
  158. return widgetFactory;
  159. }
  160. public void setWidgetFactory(WidgetFactory widgetFactory) {
  161. this.widgetFactory = widgetFactory;
  162. }
  163. }