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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2000-2013 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,
  100. Element element) {
  101. printState(connector, true);
  102. }
  103. });
  104. analyzeLayoutsPanel.addListener(new SelectConnectorListener() {
  105. @Override
  106. public void select(ServerConnector connector,
  107. Element element) {
  108. printState(connector, true);
  109. }
  110. });
  111. content.setStylePrimaryName(VDebugWindow.STYLENAME + "-hierarchy");
  112. initializeHelpPanel();
  113. content.setWidget(helpPanel);
  114. }
  115. private void initializeHelpPanel() {
  116. HTML info = new HTML(showHierarchy.getHTML() + " "
  117. + showHierarchy.getTitle() + "<br/>" + find.getHTML() + " "
  118. + find.getTitle() + "<br/>" + analyze.getHTML() + " "
  119. + analyze.getTitle() + "<br/>" + generateWS.getHTML() + " "
  120. + generateWS.getTitle() + "<br/>");
  121. info.setStyleName(VDebugWindow.STYLENAME + "-info");
  122. helpPanel.add(info);
  123. }
  124. private void showHierarchy() {
  125. Highlight.hideAll();
  126. hierarchyPanel.update();
  127. content.setWidget(hierarchyPanel);
  128. }
  129. @Override
  130. public DebugButton getTabButton() {
  131. return tabButton;
  132. }
  133. @Override
  134. public Widget getControls() {
  135. return controls;
  136. }
  137. @Override
  138. public Widget getContent() {
  139. return content;
  140. }
  141. @Override
  142. public void show() {
  143. }
  144. @Override
  145. public void hide() {
  146. stopFind();
  147. }
  148. private void generateWidgetset() {
  149. widgetsetPanel.update();
  150. content.setWidget(widgetsetPanel);
  151. }
  152. private void analyzeLayouts() {
  153. analyzeLayoutsPanel.update();
  154. content.setWidget(analyzeLayoutsPanel);
  155. }
  156. @Override
  157. public void meta(ApplicationConnection ac, ValueMap meta) {
  158. // show the results of analyzeLayouts
  159. analyzeLayoutsPanel.meta(ac, meta);
  160. }
  161. @Override
  162. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  163. // NOP
  164. }
  165. private boolean isFindMode() {
  166. return (highlightModeRegistration != null);
  167. }
  168. private void toggleFind() {
  169. if (isFindMode()) {
  170. stopFind();
  171. } else {
  172. startFind();
  173. }
  174. }
  175. private void startFind() {
  176. Highlight.hideAll();
  177. if (!isFindMode()) {
  178. highlightModeRegistration = Event
  179. .addNativePreviewHandler(highlightModeHandler);
  180. find.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  181. }
  182. }
  183. private void stopFind() {
  184. if (isFindMode()) {
  185. highlightModeRegistration.removeHandler();
  186. highlightModeRegistration = null;
  187. find.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  188. }
  189. }
  190. private void printState(ServerConnector connector, boolean serverDebug) {
  191. Highlight.showOnly(connector);
  192. if (serverDebug) {
  193. HierarchyPanel.showServerDebugInfo(connector);
  194. }
  195. infoPanel.update(connector);
  196. content.setWidget(infoPanel);
  197. }
  198. private final NativePreviewHandler highlightModeHandler = new NativePreviewHandler() {
  199. @Override
  200. public void onPreviewNativeEvent(NativePreviewEvent event) {
  201. if (event.getTypeInt() == Event.ONKEYDOWN
  202. && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
  203. stopFind();
  204. Highlight.hideAll();
  205. return;
  206. }
  207. if (event.getTypeInt() == Event.ONMOUSEMOVE) {
  208. Highlight.hideAll();
  209. Element eventTarget = Util.getElementFromPoint(event
  210. .getNativeEvent().getClientX(), event.getNativeEvent()
  211. .getClientY());
  212. if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) {
  213. infoPanel.clear();
  214. return;
  215. }
  216. for (ApplicationConnection a : ApplicationConfiguration
  217. .getRunningApplications()) {
  218. ComponentConnector connector = Util.getConnectorForElement(
  219. a, a.getUIConnector().getWidget(), eventTarget);
  220. if (connector == null) {
  221. connector = Util.getConnectorForElement(a,
  222. RootPanel.get(), eventTarget);
  223. }
  224. if (connector != null) {
  225. printState(connector, false);
  226. event.cancel();
  227. event.consume();
  228. event.getNativeEvent().stopPropagation();
  229. return;
  230. }
  231. }
  232. infoPanel.clear();
  233. }
  234. if (event.getTypeInt() == Event.ONCLICK) {
  235. Highlight.hideAll();
  236. event.cancel();
  237. event.consume();
  238. event.getNativeEvent().stopPropagation();
  239. stopFind();
  240. Element eventTarget = Util.getElementFromPoint(event
  241. .getNativeEvent().getClientX(), event.getNativeEvent()
  242. .getClientY());
  243. for (ApplicationConnection a : ApplicationConfiguration
  244. .getRunningApplications()) {
  245. ComponentConnector connector = Util.getConnectorForElement(
  246. a, a.getUIConnector().getWidget(), eventTarget);
  247. if (connector == null) {
  248. connector = Util.getConnectorForElement(a,
  249. RootPanel.get(), eventTarget);
  250. }
  251. if (connector != null) {
  252. printState(connector, true);
  253. return;
  254. }
  255. }
  256. }
  257. event.cancel();
  258. }
  259. };
  260. }