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.

HierarchySection.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2000-2014 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 com.google.gwt.dom.client.Element;
  18. import com.google.gwt.event.dom.client.ClickEvent;
  19. import com.google.gwt.event.dom.client.ClickHandler;
  20. import com.google.gwt.event.dom.client.KeyCodes;
  21. import com.google.gwt.event.shared.HandlerRegistration;
  22. import com.google.gwt.user.client.Event;
  23. import com.google.gwt.user.client.Event.NativePreviewEvent;
  24. import com.google.gwt.user.client.Event.NativePreviewHandler;
  25. import com.google.gwt.user.client.ui.Button;
  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.RootPanel;
  29. import com.google.gwt.user.client.ui.SimplePanel;
  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.ServerConnector;
  35. import com.vaadin.client.Util;
  36. import com.vaadin.client.ValueMap;
  37. /**
  38. * Provides functionality for examining the UI component hierarchy.
  39. *
  40. * @since 7.1
  41. * @author Vaadin Ltd
  42. */
  43. public class HierarchySection implements Section {
  44. private final DebugButton tabButton = new DebugButton(Icon.HIERARCHY,
  45. "Examine component hierarchy");
  46. private final SimplePanel content = new SimplePanel();
  47. // TODO highlighting logic is split between these, should be refactored
  48. private final FlowPanel helpPanel = new FlowPanel();
  49. private final ConnectorInfoPanel infoPanel = new ConnectorInfoPanel();
  50. private final HierarchyPanel hierarchyPanel = new HierarchyPanel();
  51. private final OptimizedWidgetsetPanel widgetsetPanel = new OptimizedWidgetsetPanel();
  52. private final AnalyzeLayoutsPanel analyzeLayoutsPanel = new AnalyzeLayoutsPanel();
  53. private final FlowPanel controls = new FlowPanel();
  54. private final Button find = new DebugButton(Icon.HIGHLIGHT,
  55. "Select a component on the page to inspect it");
  56. private final Button analyze = new DebugButton(Icon.ANALYZE,
  57. "Check layouts for potential problems");
  58. private final Button generateWS = new DebugButton(Icon.OPTIMIZE,
  59. "Show used connectors and how to optimize widgetset");
  60. private final Button showHierarchy = new DebugButton(Icon.HIERARCHY,
  61. "Show the connector hierarchy tree");
  62. private HandlerRegistration highlightModeRegistration = null;
  63. public HierarchySection() {
  64. controls.add(showHierarchy);
  65. showHierarchy.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  66. showHierarchy.addClickHandler(new ClickHandler() {
  67. @Override
  68. public void onClick(ClickEvent event) {
  69. showHierarchy();
  70. }
  71. });
  72. controls.add(find);
  73. find.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  74. find.addClickHandler(new ClickHandler() {
  75. @Override
  76. public void onClick(ClickEvent event) {
  77. toggleFind();
  78. }
  79. });
  80. controls.add(analyze);
  81. analyze.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  82. analyze.addClickHandler(new ClickHandler() {
  83. @Override
  84. public void onClick(ClickEvent event) {
  85. stopFind();
  86. analyzeLayouts();
  87. }
  88. });
  89. controls.add(generateWS);
  90. generateWS.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  91. generateWS.addClickHandler(new ClickHandler() {
  92. @Override
  93. public void onClick(ClickEvent event) {
  94. generateWidgetset();
  95. }
  96. });
  97. hierarchyPanel.addListener(new SelectConnectorListener() {
  98. @Override
  99. public void select(ServerConnector connector, Element element) {
  100. printState(connector, true);
  101. }
  102. });
  103. analyzeLayoutsPanel.addListener(new SelectConnectorListener() {
  104. @Override
  105. public void select(ServerConnector connector, Element element) {
  106. printState(connector, true);
  107. }
  108. });
  109. content.setStylePrimaryName(VDebugWindow.STYLENAME + "-hierarchy");
  110. initializeHelpPanel();
  111. content.setWidget(helpPanel);
  112. }
  113. private void initializeHelpPanel() {
  114. HTML info = new HTML(showHierarchy.getHTML() + " "
  115. + showHierarchy.getTitle() + "<br/>" + find.getHTML() + " "
  116. + find.getTitle() + "<br/>" + analyze.getHTML() + " "
  117. + analyze.getTitle() + "<br/>" + generateWS.getHTML() + " "
  118. + generateWS.getTitle() + "<br/>");
  119. info.setStyleName(VDebugWindow.STYLENAME + "-info");
  120. helpPanel.add(info);
  121. }
  122. private void showHierarchy() {
  123. Highlight.hideAll();
  124. hierarchyPanel.update();
  125. content.setWidget(hierarchyPanel);
  126. }
  127. @Override
  128. public DebugButton getTabButton() {
  129. return tabButton;
  130. }
  131. @Override
  132. public Widget getControls() {
  133. return controls;
  134. }
  135. @Override
  136. public Widget getContent() {
  137. return content;
  138. }
  139. @Override
  140. public void show() {
  141. }
  142. @Override
  143. public void hide() {
  144. stopFind();
  145. }
  146. private void generateWidgetset() {
  147. widgetsetPanel.update();
  148. content.setWidget(widgetsetPanel);
  149. }
  150. private void analyzeLayouts() {
  151. analyzeLayoutsPanel.update();
  152. content.setWidget(analyzeLayoutsPanel);
  153. }
  154. @Override
  155. public void meta(ApplicationConnection ac, ValueMap meta) {
  156. // show the results of analyzeLayouts
  157. analyzeLayoutsPanel.meta(ac, meta);
  158. }
  159. @Override
  160. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  161. // NOP
  162. }
  163. private boolean isFindMode() {
  164. return (highlightModeRegistration != null);
  165. }
  166. private void toggleFind() {
  167. if (isFindMode()) {
  168. stopFind();
  169. } else {
  170. startFind();
  171. }
  172. }
  173. private void startFind() {
  174. Highlight.hideAll();
  175. if (!isFindMode()) {
  176. highlightModeRegistration = Event
  177. .addNativePreviewHandler(highlightModeHandler);
  178. find.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  179. }
  180. }
  181. private void stopFind() {
  182. if (isFindMode()) {
  183. highlightModeRegistration.removeHandler();
  184. highlightModeRegistration = null;
  185. find.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  186. }
  187. }
  188. private void printState(ServerConnector connector, boolean serverDebug) {
  189. Highlight.showOnly(connector);
  190. if (serverDebug) {
  191. HierarchyPanel.showServerDebugInfo(connector);
  192. }
  193. infoPanel.update(connector);
  194. content.setWidget(infoPanel);
  195. }
  196. private final NativePreviewHandler highlightModeHandler = new NativePreviewHandler() {
  197. @Override
  198. public void onPreviewNativeEvent(NativePreviewEvent event) {
  199. if (event.getTypeInt() == Event.ONKEYDOWN
  200. && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
  201. stopFind();
  202. Highlight.hideAll();
  203. return;
  204. }
  205. if (event.getTypeInt() == Event.ONMOUSEMOVE) {
  206. Highlight.hideAll();
  207. Element eventTarget = Util.getElementFromPoint(event
  208. .getNativeEvent().getClientX(), event.getNativeEvent()
  209. .getClientY());
  210. if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) {
  211. infoPanel.clear();
  212. return;
  213. }
  214. for (ApplicationConnection a : ApplicationConfiguration
  215. .getRunningApplications()) {
  216. ComponentConnector connector = Util.getConnectorForElement(
  217. a, a.getUIConnector().getWidget(), eventTarget);
  218. if (connector == null) {
  219. connector = Util.getConnectorForElement(a,
  220. RootPanel.get(), eventTarget);
  221. }
  222. if (connector != null) {
  223. printState(connector, false);
  224. event.cancel();
  225. event.consume();
  226. event.getNativeEvent().stopPropagation();
  227. return;
  228. }
  229. }
  230. infoPanel.clear();
  231. }
  232. if (event.getTypeInt() == Event.ONCLICK) {
  233. Highlight.hideAll();
  234. event.cancel();
  235. event.consume();
  236. event.getNativeEvent().stopPropagation();
  237. stopFind();
  238. Element eventTarget = Util.getElementFromPoint(event
  239. .getNativeEvent().getClientX(), event.getNativeEvent()
  240. .getClientY());
  241. for (ApplicationConnection a : ApplicationConfiguration
  242. .getRunningApplications()) {
  243. ComponentConnector connector = Util.getConnectorForElement(
  244. a, a.getUIConnector().getWidget(), eventTarget);
  245. if (connector == null) {
  246. connector = Util.getConnectorForElement(a,
  247. RootPanel.get(), eventTarget);
  248. }
  249. if (connector != null) {
  250. printState(connector, true);
  251. return;
  252. }
  253. }
  254. }
  255. event.cancel();
  256. }
  257. };
  258. }