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

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