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.

VUIDLBrowser.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. /**
  5. *
  6. */
  7. package com.vaadin.terminal.gwt.client;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Set;
  11. import com.google.gwt.core.client.JsArray;
  12. import com.google.gwt.core.client.JsArrayString;
  13. import com.google.gwt.core.client.Scheduler;
  14. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  15. import com.google.gwt.dom.client.Document;
  16. import com.google.gwt.dom.client.Element;
  17. import com.google.gwt.dom.client.Style;
  18. import com.google.gwt.dom.client.Style.Position;
  19. import com.google.gwt.dom.client.Style.Unit;
  20. import com.google.gwt.event.dom.client.ClickEvent;
  21. import com.google.gwt.event.dom.client.MouseOutEvent;
  22. import com.google.gwt.event.dom.client.MouseOutHandler;
  23. import com.google.gwt.user.client.ui.RootPanel;
  24. import com.google.gwt.user.client.ui.Widget;
  25. import com.vaadin.terminal.gwt.client.ui.VUnknownComponentPaintable;
  26. import com.vaadin.terminal.gwt.client.ui.VWindow;
  27. public class VUIDLBrowser extends SimpleTree {
  28. private static final String HELP = "Shift click handle to open recursively. Click components to hightlight them on client side. Shift click components to highlight them also on the server side.";
  29. private ApplicationConfiguration conf;
  30. private String highlightedPid;
  31. public VUIDLBrowser(final UIDL uidl, ApplicationConfiguration conf) {
  32. this.conf = conf;
  33. final UIDLItem root = new UIDLItem(uidl, conf);
  34. add(root);
  35. }
  36. public VUIDLBrowser(ValueMap u, ApplicationConfiguration conf) {
  37. this.conf = conf;
  38. ValueMap valueMap = u.getValueMap("meta");
  39. if (valueMap.containsKey("hl")) {
  40. highlightedPid = valueMap.getString("hl");
  41. }
  42. Set<String> keySet = u.getKeySet();
  43. for (String key : keySet) {
  44. if (key.equals("state")) {
  45. // TODO print updated shared states
  46. } else if (key.equals("changes")) {
  47. JsArray<UIDL> jsValueMapArray = u.getJSValueMapArray("changes")
  48. .cast();
  49. for (int i = 0; i < jsValueMapArray.length(); i++) {
  50. UIDL uidl = jsValueMapArray.get(i);
  51. UIDLItem change = new UIDLItem(uidl, conf);
  52. change.setTitle("change " + i);
  53. add(change);
  54. }
  55. } else if (key.equals("meta")) {
  56. } else {
  57. // TODO consider pretty printing other request data
  58. // addItem(key + " : " + u.getAsString(key));
  59. }
  60. }
  61. open(highlightedPid != null);
  62. setTitle(HELP);
  63. }
  64. class UIDLItem extends SimpleTree {
  65. private UIDL uidl;
  66. UIDLItem(UIDL uidl, ApplicationConfiguration conf) {
  67. setTitle(HELP);
  68. this.uidl = uidl;
  69. try {
  70. String name = uidl.getTag();
  71. try {
  72. Integer.parseInt(name);
  73. name = getNodeName(uidl, conf, name);
  74. } catch (Exception e) {
  75. // NOP
  76. }
  77. setText(name);
  78. addItem("LOADING");
  79. } catch (Exception e) {
  80. setText(uidl.toString());
  81. }
  82. addDomHandler(new MouseOutHandler() {
  83. public void onMouseOut(MouseOutEvent event) {
  84. deHiglight();
  85. }
  86. }, MouseOutEvent.getType());
  87. }
  88. private String getNodeName(UIDL uidl, ApplicationConfiguration conf,
  89. String name) {
  90. Class<? extends ComponentConnector> widgetClassByDecodedTag = conf
  91. .getWidgetClassByEncodedTag(name);
  92. if (widgetClassByDecodedTag == VUnknownComponentPaintable.class) {
  93. return conf.getUnknownServerClassNameByEncodedTagName(name)
  94. + "(NO CLIENT IMPLEMENTATION FOUND)";
  95. } else {
  96. return widgetClassByDecodedTag.getName();
  97. }
  98. }
  99. @Override
  100. public void open(boolean recursive) {
  101. if (getWidgetCount() == 1
  102. && getWidget(0).getElement().getInnerText()
  103. .equals("LOADING")) {
  104. dir();
  105. }
  106. super.open(recursive);
  107. }
  108. @Override
  109. protected void select(ClickEvent event) {
  110. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  111. .getRunningApplications();
  112. // TODO this does not work properly with multiple application on
  113. // same
  114. // host page
  115. for (ApplicationConnection applicationConnection : runningApplications) {
  116. ComponentConnector paintable = (ComponentConnector) ConnectorMap
  117. .get(applicationConnection).getConnector(uidl.getId());
  118. highlight(paintable);
  119. if (event != null && event.getNativeEvent().getShiftKey()) {
  120. applicationConnection.highlightComponent(paintable);
  121. }
  122. }
  123. super.select(event);
  124. }
  125. public void dir() {
  126. remove(0);
  127. String nodeName = uidl.getTag();
  128. try {
  129. Integer.parseInt(nodeName);
  130. nodeName = getNodeName(uidl, conf, nodeName);
  131. } catch (Exception e) {
  132. // NOP
  133. }
  134. Set<String> attributeNames = uidl.getAttributeNames();
  135. for (String name : attributeNames) {
  136. if (uidl.isMapAttribute(name)) {
  137. try {
  138. ValueMap map = uidl.getMapAttribute(name);
  139. JsArrayString keyArray = map.getKeyArray();
  140. nodeName += " " + name + "=" + "{";
  141. for (int i = 0; i < keyArray.length(); i++) {
  142. nodeName += keyArray.get(i) + ":"
  143. + map.getAsString(keyArray.get(i)) + ",";
  144. }
  145. nodeName += "}";
  146. } catch (Exception e) {
  147. }
  148. } else {
  149. final String value = uidl.getAttribute(name);
  150. nodeName += " " + name + "=" + value;
  151. }
  152. }
  153. setText(nodeName);
  154. try {
  155. SimpleTree tmp = null;
  156. Set<String> variableNames = uidl.getVariableNames();
  157. for (String name : variableNames) {
  158. String value = "";
  159. try {
  160. value = uidl.getVariable(name);
  161. } catch (final Exception e) {
  162. try {
  163. String[] stringArrayAttribute = uidl
  164. .getStringArrayAttribute(name);
  165. value = stringArrayAttribute.toString();
  166. } catch (final Exception e2) {
  167. try {
  168. final int intVal = uidl.getIntVariable(name);
  169. value = String.valueOf(intVal);
  170. } catch (final Exception e3) {
  171. value = "unknown";
  172. }
  173. }
  174. }
  175. if (tmp == null) {
  176. tmp = new SimpleTree("variables");
  177. }
  178. tmp.addItem(name + "=" + value);
  179. }
  180. if (tmp != null) {
  181. add(tmp);
  182. }
  183. } catch (final Exception e) {
  184. // Ignored, no variables
  185. }
  186. final Iterator<Object> i = uidl.getChildIterator();
  187. while (i.hasNext()) {
  188. final Object child = i.next();
  189. try {
  190. final UIDL c = (UIDL) child;
  191. final UIDLItem childItem = new UIDLItem(c, conf);
  192. add(childItem);
  193. } catch (final Exception e) {
  194. addItem(child.toString());
  195. }
  196. }
  197. if (highlightedPid != null && highlightedPid.equals(uidl.getId())) {
  198. getElement().getStyle().setBackgroundColor("#fdd");
  199. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  200. public void execute() {
  201. getElement().scrollIntoView();
  202. }
  203. });
  204. }
  205. }
  206. }
  207. static Element highlight = Document.get().createDivElement();
  208. static {
  209. Style style = highlight.getStyle();
  210. style.setPosition(Position.ABSOLUTE);
  211. style.setZIndex(VWindow.Z_INDEX + 1000);
  212. style.setBackgroundColor("red");
  213. style.setOpacity(0.2);
  214. if (BrowserInfo.get().isIE()) {
  215. style.setProperty("filter", "alpha(opacity=20)");
  216. }
  217. }
  218. static void highlight(ComponentConnector paintable) {
  219. if (paintable != null) {
  220. Widget w = paintable.getWidget();
  221. Style style = highlight.getStyle();
  222. style.setTop(w.getAbsoluteTop(), Unit.PX);
  223. style.setLeft(w.getAbsoluteLeft(), Unit.PX);
  224. style.setWidth(w.getOffsetWidth(), Unit.PX);
  225. style.setHeight(w.getOffsetHeight(), Unit.PX);
  226. RootPanel.getBodyElement().appendChild(highlight);
  227. }
  228. }
  229. static void deHiglight() {
  230. if (highlight.getParentElement() != null) {
  231. highlight.getParentElement().removeChild(highlight);
  232. }
  233. }
  234. }