您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VUIDLBrowser.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  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.JsArrayString;
  12. import com.google.gwt.dom.client.Document;
  13. import com.google.gwt.dom.client.Element;
  14. import com.google.gwt.dom.client.Style;
  15. import com.google.gwt.dom.client.Style.Position;
  16. import com.google.gwt.dom.client.Style.Unit;
  17. import com.google.gwt.event.dom.client.MouseOutEvent;
  18. import com.google.gwt.event.dom.client.MouseOutHandler;
  19. import com.google.gwt.event.logical.shared.OpenEvent;
  20. import com.google.gwt.event.logical.shared.OpenHandler;
  21. import com.google.gwt.event.logical.shared.SelectionEvent;
  22. import com.google.gwt.event.logical.shared.SelectionHandler;
  23. import com.google.gwt.user.client.DOM;
  24. import com.google.gwt.user.client.ui.RootPanel;
  25. import com.google.gwt.user.client.ui.Tree;
  26. import com.google.gwt.user.client.ui.TreeItem;
  27. import com.google.gwt.user.client.ui.Widget;
  28. import com.vaadin.terminal.gwt.client.ui.VUnknownComponent;
  29. import com.vaadin.terminal.gwt.client.ui.VView;
  30. import com.vaadin.terminal.gwt.client.ui.VWindow;
  31. public class VUIDLBrowser extends Tree implements MouseOutHandler {
  32. /**
  33. *
  34. */
  35. private final UIDL uidl;
  36. private ApplicationConfiguration conf;
  37. public VUIDLBrowser(final UIDL uidl, ApplicationConfiguration conf) {
  38. this.conf = conf;
  39. this.uidl = uidl;
  40. DOM.setStyleAttribute(getElement(), "position", "");
  41. final UIDLItem root = new UIDLItem(this.uidl, conf);
  42. addItem(root);
  43. addOpenHandler(new OpenHandler<TreeItem>() {
  44. public void onOpen(OpenEvent<TreeItem> event) {
  45. TreeItem item = event.getTarget();
  46. if (item.getChildCount() == 1
  47. && item.getChild(0).getText().equals("LOADING")) {
  48. ((UIDLItem) item).dir();
  49. }
  50. }
  51. });
  52. addSelectionHandler(new SelectionHandler<TreeItem>() {
  53. public void onSelection(SelectionEvent<TreeItem> event) {
  54. UIDLItem selectedItem = (UIDLItem) event.getSelectedItem();
  55. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  56. .getRunningApplications();
  57. // TODO this does not work properly with multiple application on
  58. // same
  59. // host page
  60. for (ApplicationConnection applicationConnection : runningApplications) {
  61. Paintable paintable = applicationConnection
  62. .getPaintable(selectedItem.uidl.getId());
  63. highlight(paintable);
  64. }
  65. }
  66. });
  67. addMouseOutHandler(VUIDLBrowser.this);
  68. }
  69. @Override
  70. protected boolean isKeyboardNavigationEnabled(TreeItem currentItem) {
  71. return false;
  72. }
  73. class UIDLItem extends TreeItem {
  74. private UIDL uidl;
  75. UIDLItem(UIDL uidl, ApplicationConfiguration conf) {
  76. this.uidl = uidl;
  77. try {
  78. String name = uidl.getTag();
  79. try {
  80. Integer.parseInt(name);
  81. name = getNodeName(uidl, conf, name);
  82. } catch (Exception e) {
  83. // NOP
  84. }
  85. setText(name);
  86. addItem("LOADING");
  87. } catch (Exception e) {
  88. setText(uidl.toString());
  89. }
  90. }
  91. private String getNodeName(UIDL uidl, ApplicationConfiguration conf,
  92. String name) {
  93. Class<? extends Paintable> widgetClassByDecodedTag = conf
  94. .getWidgetClassByEncodedTag(name);
  95. if (widgetClassByDecodedTag == VUnknownComponent.class) {
  96. return conf.getUnknownServerClassNameByEncodedTagName(name)
  97. + "(NO CLIENT IMPLEMENTATION FOUND)";
  98. } else if (widgetClassByDecodedTag == VView.class
  99. && uidl.hasAttribute("sub")) {
  100. return "com.vaadin.terminal.gwt.ui.VWindow";
  101. } else {
  102. return widgetClassByDecodedTag.getName();
  103. }
  104. }
  105. public void dir() {
  106. TreeItem temp = getChild(0);
  107. removeItem(temp);
  108. String nodeName = uidl.getTag();
  109. try {
  110. Integer.parseInt(nodeName);
  111. nodeName = getNodeName(uidl, conf, nodeName);
  112. } catch (Exception e) {
  113. // NOP
  114. }
  115. Set<String> attributeNames = uidl.getAttributeNames();
  116. for (String name : attributeNames) {
  117. if (uidl.isMapAttribute(name)) {
  118. try {
  119. ValueMap map = uidl.getMapAttribute(name);
  120. JsArrayString keyArray = map.getKeyArray();
  121. nodeName += " " + name + "=" + "{";
  122. for (int i = 0; i < keyArray.length(); i++) {
  123. nodeName += keyArray.get(i) + ":"
  124. + map.getAsString(keyArray.get(i)) + ",";
  125. }
  126. nodeName += "}";
  127. } catch (Exception e) {
  128. }
  129. } else {
  130. final String value = uidl.getAttribute(name);
  131. nodeName += " " + name + "=" + value;
  132. }
  133. }
  134. setText(nodeName);
  135. try {
  136. TreeItem tmp = null;
  137. Set<String> variableNames = uidl.getVariableNames();
  138. for (String name : variableNames) {
  139. String value = "";
  140. try {
  141. value = uidl.getVariable(name);
  142. } catch (final Exception e) {
  143. try {
  144. String[] stringArrayAttribute = uidl
  145. .getStringArrayAttribute(name);
  146. value = stringArrayAttribute.toString();
  147. } catch (final Exception e2) {
  148. try {
  149. final int intVal = uidl.getIntVariable(name);
  150. value = String.valueOf(intVal);
  151. } catch (final Exception e3) {
  152. value = "unknown";
  153. }
  154. }
  155. }
  156. if (tmp == null) {
  157. tmp = new TreeItem("variables");
  158. }
  159. tmp.addItem(name + "=" + value);
  160. }
  161. if (tmp != null) {
  162. addItem(tmp);
  163. }
  164. } catch (final Exception e) {
  165. // Ignored, no variables
  166. }
  167. final Iterator<Object> i = uidl.getChildIterator();
  168. while (i.hasNext()) {
  169. final Object child = i.next();
  170. try {
  171. final UIDL c = (UIDL) child;
  172. final TreeItem childItem = new UIDLItem(c, conf);
  173. addItem(childItem);
  174. } catch (final Exception e) {
  175. addItem(child.toString());
  176. }
  177. }
  178. }
  179. }
  180. static Element highlight = Document.get().createDivElement();
  181. static {
  182. Style style = highlight.getStyle();
  183. style.setPosition(Position.ABSOLUTE);
  184. style.setZIndex(VWindow.Z_INDEX + 1000);
  185. style.setBackgroundColor("red");
  186. style.setOpacity(0.2);
  187. if (BrowserInfo.get().isIE()) {
  188. style.setProperty("filter", "alpha(opacity=20)");
  189. }
  190. }
  191. private static void highlight(Paintable paintable) {
  192. Widget w = (Widget) paintable;
  193. if (w != null) {
  194. Style style = highlight.getStyle();
  195. style.setTop(w.getAbsoluteTop(), Unit.PX);
  196. style.setLeft(w.getAbsoluteLeft(), Unit.PX);
  197. style.setWidth(w.getOffsetWidth(), Unit.PX);
  198. style.setHeight(w.getOffsetHeight(), Unit.PX);
  199. RootPanel.getBodyElement().appendChild(highlight);
  200. }
  201. }
  202. private static void deHiglight() {
  203. if (highlight.getParentElement() != null) {
  204. highlight.getParentElement().removeChild(highlight);
  205. }
  206. }
  207. public void onMouseOut(MouseOutEvent event) {
  208. deHiglight();
  209. }
  210. }