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 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright 2000-2021 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.MouseOutEvent;
  25. import com.google.gwt.event.dom.client.MouseOverEvent;
  26. import com.google.gwt.user.client.ui.FlowPanel;
  27. import com.google.gwt.user.client.ui.HTML;
  28. import com.google.gwt.user.client.ui.Label;
  29. import com.google.gwt.user.client.ui.VerticalPanel;
  30. import com.google.gwt.user.client.ui.Widget;
  31. import com.vaadin.client.ApplicationConfiguration;
  32. import com.vaadin.client.ApplicationConnection;
  33. import com.vaadin.client.ComponentConnector;
  34. import com.vaadin.client.ComputedStyle;
  35. import com.vaadin.client.ConnectorMap;
  36. import com.vaadin.client.ServerConnector;
  37. import com.vaadin.client.SimpleTree;
  38. import com.vaadin.client.ValueMap;
  39. /**
  40. * Analyze layouts view panel of the debug window.
  41. *
  42. * @since 7.1.4
  43. */
  44. @SuppressWarnings("deprecation")
  45. public class AnalyzeLayoutsPanel extends FlowPanel {
  46. private List<SelectConnectorListener> listeners = new ArrayList<>();
  47. public void update() {
  48. clear();
  49. add(new Label("Analyzing layouts..."));
  50. List<ApplicationConnection> runningApplications = ApplicationConfiguration
  51. .getRunningApplications();
  52. for (ApplicationConnection applicationConnection : runningApplications) {
  53. applicationConnection.analyzeLayouts();
  54. }
  55. }
  56. public void meta(ApplicationConnection ac, ValueMap meta) {
  57. clear();
  58. JsArray<ValueMap> valueMapArray = meta
  59. .getJSValueMapArray("invalidLayouts");
  60. int size = valueMapArray.length();
  61. if (size > 0) {
  62. SimpleTree root = new SimpleTree(
  63. "Layouts analyzed, " + size + " top level problems");
  64. for (int i = 0; i < size; i++) {
  65. printLayoutError(ac, valueMapArray.get(i), root);
  66. }
  67. root.open(false);
  68. add(root);
  69. } else {
  70. add(new Label("Layouts analyzed, no top level problems"));
  71. }
  72. Set<ComponentConnector> zeroHeightComponents = new HashSet<>();
  73. Set<ComponentConnector> zeroWidthComponents = new HashSet<>();
  74. findZeroSizeComponents(zeroHeightComponents, zeroWidthComponents,
  75. ac.getUIConnector());
  76. if (!zeroHeightComponents.isEmpty() || !zeroWidthComponents.isEmpty()) {
  77. add(new HTML("<h4> Client side notifications</h4>"
  78. + " <em>The following relative sized components were "
  79. + "rendered to a zero size container on the client side."
  80. + " Note that these are not necessarily invalid "
  81. + "states, but reported here as they might be.</em>"));
  82. if (!zeroHeightComponents.isEmpty()) {
  83. add(new HTML("<p><strong>Vertically zero size:</strong></p>"));
  84. printClientSideDetectedIssues(zeroHeightComponents);
  85. }
  86. if (!zeroWidthComponents.isEmpty()) {
  87. add(new HTML(
  88. "<p><strong>Horizontally zero size:</strong></p>"));
  89. printClientSideDetectedIssues(zeroWidthComponents);
  90. }
  91. }
  92. }
  93. private void printClientSideDetectedIssues(
  94. Set<ComponentConnector> zeroSized) {
  95. // keep track of already highlighted parents
  96. HashSet<String> parents = new HashSet<>();
  97. for (final ComponentConnector connector : zeroSized) {
  98. final ServerConnector parent = connector.getParent();
  99. final String parentId = parent.getConnectorId();
  100. final Label errorDetails = new Label(
  101. connector.getClass().getSimpleName() + "["
  102. + connector.getConnectorId() + "]" + " inside "
  103. + parent.getClass().getSimpleName());
  104. if (parent instanceof ComponentConnector) {
  105. final ComponentConnector parentConnector = (ComponentConnector) parent;
  106. if (!parents.contains(parentId)) {
  107. parents.add(parentId);
  108. Highlight.show(parentConnector, "yellow");
  109. }
  110. errorDetails.addMouseOverHandler(event -> {
  111. Highlight.hideAll();
  112. Highlight.show(parentConnector, "yellow");
  113. Highlight.show(connector);
  114. errorDetails.getElement().getStyle()
  115. .setTextDecoration(TextDecoration.UNDERLINE);
  116. });
  117. errorDetails.addMouseOutHandler(event -> {
  118. Highlight.hideAll();
  119. errorDetails.getElement().getStyle()
  120. .setTextDecoration(TextDecoration.NONE);
  121. });
  122. errorDetails
  123. .addClickHandler(event -> fireSelectEvent(connector));
  124. }
  125. Highlight.show(connector);
  126. add(errorDetails);
  127. }
  128. }
  129. private void printLayoutError(ApplicationConnection ac, ValueMap valueMap,
  130. SimpleTree root) {
  131. final String pid = valueMap.getString("id");
  132. // find connector
  133. final ComponentConnector connector = (ComponentConnector) ConnectorMap
  134. .get(ac).getConnector(pid);
  135. if (connector == null) {
  136. root.add(new SimpleTree("[" + pid + "] NOT FOUND"));
  137. return;
  138. }
  139. Highlight.show(connector);
  140. final SimpleTree errorNode = new SimpleTree(
  141. connector.getClass().getSimpleName() + " id: " + pid);
  142. errorNode.addDomHandler(event -> {
  143. Highlight.showOnly(connector);
  144. ((Widget) event.getSource()).getElement().getStyle()
  145. .setTextDecoration(TextDecoration.UNDERLINE);
  146. }, MouseOverEvent.getType());
  147. errorNode.addDomHandler(event -> {
  148. Highlight.hideAll();
  149. ((Widget) event.getSource()).getElement().getStyle()
  150. .setTextDecoration(TextDecoration.NONE);
  151. }, MouseOutEvent.getType());
  152. errorNode.addDomHandler(event -> {
  153. if (event.getNativeEvent().getEventTarget().cast() == errorNode
  154. .getElement().getChild(1).cast()) {
  155. fireSelectEvent(connector);
  156. }
  157. }, ClickEvent.getType());
  158. VerticalPanel errorDetails = new VerticalPanel();
  159. if (valueMap.containsKey("heightMsg")) {
  160. errorDetails.add(new Label(
  161. "Height problem: " + valueMap.getString("heightMsg")));
  162. }
  163. if (valueMap.containsKey("widthMsg")) {
  164. errorDetails.add(new Label(
  165. "Width problem: " + valueMap.getString("widthMsg")));
  166. }
  167. if (errorDetails.getWidgetCount() > 0) {
  168. errorNode.add(errorDetails);
  169. }
  170. if (valueMap.containsKey("subErrors")) {
  171. HTML l = new HTML(
  172. "<em>Expand this node to show problems that may be dependent on this problem.</em>");
  173. errorDetails.add(l);
  174. JsArray<ValueMap> suberrors = valueMap
  175. .getJSValueMapArray("subErrors");
  176. for (int i = 0; i < suberrors.length(); i++) {
  177. ValueMap value = suberrors.get(i);
  178. printLayoutError(ac, value, errorNode);
  179. }
  180. }
  181. root.add(errorNode);
  182. }
  183. private void findZeroSizeComponents(
  184. Set<ComponentConnector> zeroHeightComponents,
  185. Set<ComponentConnector> zeroWidthComponents,
  186. ComponentConnector connector) {
  187. Widget widget = connector.getWidget();
  188. ComputedStyle computedStyle = new ComputedStyle(widget.getElement());
  189. if (computedStyle.getIntProperty("height") == 0) {
  190. zeroHeightComponents.add(connector);
  191. }
  192. if (computedStyle.getIntProperty("width") == 0) {
  193. zeroWidthComponents.add(connector);
  194. }
  195. List<ServerConnector> children = connector.getChildren();
  196. for (ServerConnector serverConnector : children) {
  197. if (serverConnector instanceof ComponentConnector) {
  198. findZeroSizeComponents(zeroHeightComponents,
  199. zeroWidthComponents,
  200. (ComponentConnector) serverConnector);
  201. }
  202. }
  203. }
  204. public void addListener(SelectConnectorListener listener) {
  205. listeners.add(listener);
  206. }
  207. public void removeListener(SelectConnectorListener listener) {
  208. listeners.remove(listener);
  209. }
  210. private void fireSelectEvent(ServerConnector connector) {
  211. for (SelectConnectorListener listener : listeners) {
  212. listener.select(connector, null);
  213. }
  214. }
  215. }