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.

AnalyzeLayoutsPanel.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.debug.internal;
  17. import java.util.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Set;
  21. import com.google.gwt.core.client.JsArray;
  22. import com.google.gwt.dom.client.Style.TextDecoration;
  23. import com.google.gwt.event.dom.client.ClickEvent;
  24. import com.google.gwt.event.dom.client.ClickHandler;
  25. import com.google.gwt.event.dom.client.MouseOutEvent;
  26. import com.google.gwt.event.dom.client.MouseOutHandler;
  27. import com.google.gwt.event.dom.client.MouseOverEvent;
  28. import com.google.gwt.event.dom.client.MouseOverHandler;
  29. import com.google.gwt.user.client.ui.FlowPanel;
  30. import com.google.gwt.user.client.ui.HTML;
  31. import com.google.gwt.user.client.ui.Label;
  32. import com.google.gwt.user.client.ui.VerticalPanel;
  33. import com.google.gwt.user.client.ui.Widget;
  34. import com.vaadin.client.ApplicationConfiguration;
  35. import com.vaadin.client.ApplicationConnection;
  36. import com.vaadin.client.ComponentConnector;
  37. import com.vaadin.client.ComputedStyle;
  38. import com.vaadin.client.ConnectorMap;
  39. import com.vaadin.client.ServerConnector;
  40. import com.vaadin.client.SimpleTree;
  41. import com.vaadin.client.ValueMap;
  42. /**
  43. * Analyze layouts view panel of the debug window.
  44. *
  45. * @since 7.1.4
  46. */
  47. public class AnalyzeLayoutsPanel extends FlowPanel {
  48. private List<SelectConnectorListener> listeners = new ArrayList<>();
  49. public void update() {
  50. clear();
  51. add(new Label("Analyzing layouts..."));
  52. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  53. .getRunningApplications();
  54. for (ApplicationConnection applicationConnection : runningApplications) {
  55. applicationConnection.analyzeLayouts();
  56. }
  57. }
  58. public void meta(ApplicationConnection ac, ValueMap meta) {
  59. clear();
  60. JsArray<ValueMap> valueMapArray = meta
  61. .getJSValueMapArray("invalidLayouts");
  62. int size = valueMapArray.length();
  63. if (size > 0) {
  64. SimpleTree root = new SimpleTree(
  65. "Layouts analyzed, " + size + " top level problems");
  66. for (int i = 0; i < size; i++) {
  67. printLayoutError(ac, valueMapArray.get(i), root);
  68. }
  69. root.open(false);
  70. add(root);
  71. } else {
  72. add(new Label("Layouts analyzed, no top level problems"));
  73. }
  74. Set<ComponentConnector> zeroHeightComponents = new HashSet<>();
  75. Set<ComponentConnector> zeroWidthComponents = new HashSet<>();
  76. findZeroSizeComponents(zeroHeightComponents, zeroWidthComponents,
  77. ac.getUIConnector());
  78. if (zeroHeightComponents.size() > 0 || zeroWidthComponents.size() > 0) {
  79. add(new HTML("<h4> Client side notifications</h4>"
  80. + " <em>The following relative sized components were "
  81. + "rendered to a zero size container on the client side."
  82. + " Note that these are not necessarily invalid "
  83. + "states, but reported here as they might be.</em>"));
  84. if (zeroHeightComponents.size() > 0) {
  85. add(new HTML("<p><strong>Vertically zero size:</strong></p>"));
  86. printClientSideDetectedIssues(zeroHeightComponents, ac);
  87. }
  88. if (zeroWidthComponents.size() > 0) {
  89. add(new HTML(
  90. "<p><strong>Horizontally zero size:</strong></p>"));
  91. printClientSideDetectedIssues(zeroWidthComponents, ac);
  92. }
  93. }
  94. }
  95. private void printClientSideDetectedIssues(
  96. Set<ComponentConnector> zeroSized, ApplicationConnection ac) {
  97. // keep track of already highlighted parents
  98. HashSet<String> parents = new HashSet<>();
  99. for (final ComponentConnector connector : zeroSized) {
  100. final ServerConnector parent = connector.getParent();
  101. final String parentId = parent.getConnectorId();
  102. final Label errorDetails = new Label(
  103. connector.getClass().getSimpleName() + "["
  104. + connector.getConnectorId() + "]" + " inside "
  105. + parent.getClass().getSimpleName());
  106. if (parent instanceof ComponentConnector) {
  107. final ComponentConnector parentConnector = (ComponentConnector) parent;
  108. if (!parents.contains(parentId)) {
  109. parents.add(parentId);
  110. Highlight.show(parentConnector, "yellow");
  111. }
  112. errorDetails.addMouseOverHandler(new MouseOverHandler() {
  113. @Override
  114. public void onMouseOver(MouseOverEvent event) {
  115. Highlight.hideAll();
  116. Highlight.show(parentConnector, "yellow");
  117. Highlight.show(connector);
  118. errorDetails.getElement().getStyle()
  119. .setTextDecoration(TextDecoration.UNDERLINE);
  120. }
  121. });
  122. errorDetails.addMouseOutHandler(new MouseOutHandler() {
  123. @Override
  124. public void onMouseOut(MouseOutEvent event) {
  125. Highlight.hideAll();
  126. errorDetails.getElement().getStyle()
  127. .setTextDecoration(TextDecoration.NONE);
  128. }
  129. });
  130. errorDetails.addClickHandler(new ClickHandler() {
  131. @Override
  132. public void onClick(ClickEvent event) {
  133. fireSelectEvent(connector);
  134. }
  135. });
  136. }
  137. Highlight.show(connector);
  138. add(errorDetails);
  139. }
  140. }
  141. private void printLayoutError(ApplicationConnection ac, ValueMap valueMap,
  142. SimpleTree root) {
  143. final String pid = valueMap.getString("id");
  144. // find connector
  145. final ComponentConnector connector = (ComponentConnector) ConnectorMap
  146. .get(ac).getConnector(pid);
  147. if (connector == null) {
  148. root.add(new SimpleTree("[" + pid + "] NOT FOUND"));
  149. return;
  150. }
  151. Highlight.show(connector);
  152. final SimpleTree errorNode = new SimpleTree(
  153. connector.getClass().getSimpleName() + " id: " + pid);
  154. errorNode.addDomHandler(new MouseOverHandler() {
  155. @Override
  156. public void onMouseOver(MouseOverEvent event) {
  157. Highlight.showOnly(connector);
  158. ((Widget) event.getSource()).getElement().getStyle()
  159. .setTextDecoration(TextDecoration.UNDERLINE);
  160. }
  161. }, MouseOverEvent.getType());
  162. errorNode.addDomHandler(new MouseOutHandler() {
  163. @Override
  164. public void onMouseOut(MouseOutEvent event) {
  165. Highlight.hideAll();
  166. ((Widget) event.getSource()).getElement().getStyle()
  167. .setTextDecoration(TextDecoration.NONE);
  168. }
  169. }, MouseOutEvent.getType());
  170. errorNode.addDomHandler(new ClickHandler() {
  171. @Override
  172. public void onClick(ClickEvent event) {
  173. if (event.getNativeEvent().getEventTarget().cast() == errorNode
  174. .getElement().getChild(1).cast()) {
  175. fireSelectEvent(connector);
  176. }
  177. }
  178. }, ClickEvent.getType());
  179. VerticalPanel errorDetails = new VerticalPanel();
  180. if (valueMap.containsKey("heightMsg")) {
  181. errorDetails.add(new Label(
  182. "Height problem: " + valueMap.getString("heightMsg")));
  183. }
  184. if (valueMap.containsKey("widthMsg")) {
  185. errorDetails.add(new Label(
  186. "Width problem: " + valueMap.getString("widthMsg")));
  187. }
  188. if (errorDetails.getWidgetCount() > 0) {
  189. errorNode.add(errorDetails);
  190. }
  191. if (valueMap.containsKey("subErrors")) {
  192. HTML l = new HTML(
  193. "<em>Expand this node to show problems that may be dependent on this problem.</em>");
  194. errorDetails.add(l);
  195. JsArray<ValueMap> suberrors = valueMap
  196. .getJSValueMapArray("subErrors");
  197. for (int i = 0; i < suberrors.length(); i++) {
  198. ValueMap value = suberrors.get(i);
  199. printLayoutError(ac, value, errorNode);
  200. }
  201. }
  202. root.add(errorNode);
  203. }
  204. private void findZeroSizeComponents(
  205. Set<ComponentConnector> zeroHeightComponents,
  206. Set<ComponentConnector> zeroWidthComponents,
  207. ComponentConnector connector) {
  208. Widget widget = connector.getWidget();
  209. ComputedStyle computedStyle = new ComputedStyle(widget.getElement());
  210. if (computedStyle.getIntProperty("height") == 0) {
  211. zeroHeightComponents.add(connector);
  212. }
  213. if (computedStyle.getIntProperty("width") == 0) {
  214. zeroWidthComponents.add(connector);
  215. }
  216. List<ServerConnector> children = connector.getChildren();
  217. for (ServerConnector serverConnector : children) {
  218. if (serverConnector instanceof ComponentConnector) {
  219. findZeroSizeComponents(zeroHeightComponents,
  220. zeroWidthComponents,
  221. (ComponentConnector) serverConnector);
  222. }
  223. }
  224. }
  225. public void addListener(SelectConnectorListener listener) {
  226. listeners.add(listener);
  227. }
  228. public void removeListener(SelectConnectorListener listener) {
  229. listeners.remove(listener);
  230. }
  231. private void fireSelectEvent(ServerConnector connector) {
  232. for (SelectConnectorListener listener : listeners) {
  233. listener.select(connector, null);
  234. }
  235. }
  236. }