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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Set;
  5. import java.util.Vector;
  6. import com.google.gwt.core.client.EntryPoint;
  7. import com.google.gwt.http.client.Request;
  8. import com.google.gwt.http.client.RequestBuilder;
  9. import com.google.gwt.http.client.RequestCallback;
  10. import com.google.gwt.http.client.RequestException;
  11. import com.google.gwt.http.client.Response;
  12. import com.google.gwt.http.client.URL;
  13. import com.google.gwt.json.client.JSONArray;
  14. import com.google.gwt.json.client.JSONObject;
  15. import com.google.gwt.json.client.JSONParser;
  16. import com.google.gwt.json.client.JSONValue;
  17. import com.google.gwt.user.client.ui.RootPanel;
  18. import com.itmill.toolkit.terminal.gwt.client.ui.Component;
  19. import com.itmill.toolkit.terminal.gwt.client.ui.RootWindow;
  20. /**
  21. * Entry point classes define <code>onModuleLoad()</code>.
  22. */
  23. public class Client implements EntryPoint {
  24. private String appUri = "http://127.0.0.1:8080/tk/HelloWorld";
  25. // TODO remove repaintAll things start to pile up
  26. private RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, appUri
  27. + "/UIDL/?repaintAll=1&");
  28. private Console console;
  29. private RootWindow rw;
  30. private Vector pendingVariables = new Vector();
  31. private HashMap components = new HashMap();
  32. private int requestCount = 0;
  33. /**
  34. * This is the entry point method.
  35. */
  36. public void onModuleLoad() {
  37. console = new Console(RootPanel.get("itmtk-loki"));
  38. console.log("muutos");
  39. console.log("Starting app");
  40. console.log("Makin fake UIDL Request to fool servlet of an app init");
  41. RequestBuilder rb2 = new RequestBuilder(RequestBuilder.GET, appUri);
  42. try {
  43. rb2.sendRequest("", new RequestCallback() {
  44. public void onResponseReceived(Request request,
  45. Response response) {
  46. console
  47. .log("Got fake response... sending initial UIDL request");
  48. makeUidlRequest("repaintAll=1");
  49. }
  50. public void onError(Request request, Throwable exception) {
  51. // TODO Auto-generated method stub
  52. }
  53. });
  54. } catch (RequestException e1) {
  55. // TODO Auto-generated catch block
  56. e1.printStackTrace();
  57. }
  58. }
  59. private void makeUidlRequest(String requestData) {
  60. console.log("Making UIDL Request");
  61. rb = new RequestBuilder(RequestBuilder.GET, appUri
  62. + "/UIDL/?requestId=" + (++requestCount) + "&" + requestData);
  63. try {
  64. rb.sendRequest(requestData, new RequestCallback() {
  65. public void onError(Request request, Throwable exception) {
  66. console.error("Got error");
  67. }
  68. public void onResponseReceived(Request request,
  69. Response response) {
  70. console.log("Got response:" + response.getText() + "\n");
  71. JSONValue update = JSONParser.parse(response.getText()
  72. .substring(3)
  73. + "}");
  74. // TEST
  75. console.log(update.toString());
  76. JSONArray changes = (JSONArray) ((JSONObject) update)
  77. .get("changes");
  78. for (int i = 0; i < changes.size(); i++) {
  79. try {
  80. console.log("Change " + i);
  81. UIDL u = new UIDL((JSONArray) changes.get(i));
  82. console.log("\nUIDL = " + u);
  83. } catch (Throwable e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. // handleUIDL(update.isObject());
  88. }
  89. });
  90. console.log("Request sent");
  91. } catch (RequestException e) {
  92. console.error(e.getMessage());
  93. }
  94. }
  95. private void handleUIDL(JSONObject update) {
  96. JSONObject changes;
  97. if (update.containsKey("meta")) {
  98. JSONObject meta = update.get("meta").isObject();
  99. }
  100. if ((changes = update.isObject()) != null) {
  101. Set keys = changes.keySet();
  102. Iterator it = keys.iterator();
  103. while (it.hasNext()) {
  104. String key = (String) it.next();
  105. if (key.startsWith("change")) {
  106. JSONObject change = changes.get(key).isObject();
  107. JSONArray children;
  108. if ((children = change.isArray()) != null) {
  109. for (int i = 0; i < children.size(); i++) {
  110. applyChange(children.get(i).isObject());
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. private void applyChange(JSONObject change) {
  118. if (change.get("attr").isObject().get("id").isString().equals("PID0")) {
  119. console.log("Rendering main window");
  120. // rw = new RootWindow(change, this);
  121. rw.setClient(this);
  122. } else {
  123. // int pid = Component.getIdFromUidl(change);
  124. // console.log("Updating node: " + change.getNodeName() + ",
  125. // PID:"+pid);
  126. // Component c = getPaintable(pid);
  127. // c.updateFromUidl(change);
  128. }
  129. }
  130. /**
  131. * Queues a changed variable to be sent to server
  132. *
  133. * @param variable
  134. */
  135. public void updateVariable(Variable variable) {
  136. // remove variable first so we will maintain the correct order (in case
  137. // of "double change")
  138. pendingVariables.remove(variable);
  139. pendingVariables.add(variable);
  140. }
  141. /**
  142. * Sends queued variables to server
  143. *
  144. */
  145. public void flushVariables() {
  146. StringBuffer sb = new StringBuffer();
  147. int i = 0;
  148. while (!pendingVariables.isEmpty()) {
  149. Variable v = (Variable) pendingVariables.lastElement();
  150. pendingVariables.removeElement(v);
  151. if (i > 0) {
  152. sb.append("&");
  153. }
  154. // encode the characters in the name
  155. String encodedName = URL.encodeComponent(v.getId());
  156. sb.append(encodedName);
  157. sb.append("=");
  158. // encode the characters in the value
  159. String encodedValue = URL.encodeComponent(v.getEncodedValue());
  160. sb.append(encodedValue);
  161. }
  162. String buf = sb.toString();
  163. console.log("Making following request to server:");
  164. console.log(buf);
  165. makeUidlRequest(buf);
  166. }
  167. public void registerComponent(Component component) {
  168. components.put("" + component.getId(), component);
  169. }
  170. public Component getPaintable(int pid) {
  171. return (Component) components.get("" + pid);
  172. }
  173. }