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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2000-2018 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.KeyCodes;
  19. import com.google.gwt.event.shared.HandlerRegistration;
  20. import com.google.gwt.user.client.Event;
  21. import com.google.gwt.user.client.Event.NativePreviewHandler;
  22. import com.google.gwt.user.client.ui.Button;
  23. import com.google.gwt.user.client.ui.FlowPanel;
  24. import com.google.gwt.user.client.ui.HTML;
  25. import com.google.gwt.user.client.ui.RootPanel;
  26. import com.google.gwt.user.client.ui.SimplePanel;
  27. import com.google.gwt.user.client.ui.Widget;
  28. import com.vaadin.client.ApplicationConfiguration;
  29. import com.vaadin.client.ApplicationConnection;
  30. import com.vaadin.client.ComponentConnector;
  31. import com.vaadin.client.ServerConnector;
  32. import com.vaadin.client.Util;
  33. import com.vaadin.client.ValueMap;
  34. import com.vaadin.client.WidgetUtil;
  35. /**
  36. * Provides functionality for examining the UI component hierarchy.
  37. *
  38. * @since 7.1
  39. * @author Vaadin Ltd
  40. */
  41. public class HierarchySection implements Section {
  42. private final DebugButton tabButton = new DebugButton(Icon.HIERARCHY,
  43. "Examine component hierarchy");
  44. private final SimplePanel content = new SimplePanel();
  45. // TODO highlighting logic is split between these, should be refactored
  46. private final FlowPanel helpPanel = new FlowPanel();
  47. private final ConnectorInfoPanel infoPanel = new ConnectorInfoPanel();
  48. private final HierarchyPanel hierarchyPanel = new HierarchyPanel();
  49. private final OptimizedWidgetsetPanel widgetsetPanel = new OptimizedWidgetsetPanel();
  50. private final AnalyzeLayoutsPanel analyzeLayoutsPanel = new AnalyzeLayoutsPanel();
  51. private final FlowPanel controls = new FlowPanel();
  52. private final Button find = new DebugButton(Icon.HIGHLIGHT,
  53. "Select a component on the page to inspect it");
  54. private final Button analyze = new DebugButton(Icon.ANALYZE,
  55. "Check layouts for potential problems");
  56. private final Button generateWS = new DebugButton(Icon.OPTIMIZE,
  57. "Show used connectors and how to optimize widgetset");
  58. private final Button showHierarchy = new DebugButton(Icon.HIERARCHY,
  59. "Show the connector hierarchy tree");
  60. private final Button generateDesign = new DebugButton(Icon.SHOW_DESIGN,
  61. "Generate a declarative design for the given component sub tree");
  62. private HandlerRegistration highlightModeRegistration = null;
  63. public interface FindHandler {
  64. /**
  65. * Called when the user hovers over a connector, which is highlighted.
  66. * Also called when hovering outside the tree, e.g. over the debug
  67. * console, but in this case the connector is null
  68. *
  69. * @param connector
  70. */
  71. void onHover(ComponentConnector connector);
  72. /**
  73. * Called when the user clicks on a highlighted connector.
  74. *
  75. * @param connector
  76. */
  77. void onSelected(ComponentConnector connector);
  78. }
  79. private FindHandler inspectComponent = new FindHandler() {
  80. @Override
  81. public void onHover(ComponentConnector connector) {
  82. if (connector == null) {
  83. infoPanel.clear();
  84. } else {
  85. printState(connector, false);
  86. }
  87. }
  88. @Override
  89. public void onSelected(ComponentConnector connector) {
  90. stopFind();
  91. printState(connector, true);
  92. }
  93. };
  94. private FindHandler showComponentDesign = new FindHandler() {
  95. @Override
  96. public void onHover(ComponentConnector connector) {
  97. Highlight.showOnly(connector);
  98. }
  99. @Override
  100. public void onSelected(ComponentConnector connector) {
  101. stopFind();
  102. connector.getConnection().getUIConnector()
  103. .showServerDesign(connector);
  104. content.setWidget(
  105. new HTML("Design file for component sent to server log"));
  106. }
  107. };
  108. private FindHandler activeFindHandler;
  109. public HierarchySection() {
  110. controls.add(showHierarchy);
  111. showHierarchy.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  112. showHierarchy.addClickHandler(event -> showHierarchy());
  113. controls.add(find);
  114. find.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  115. find.addClickHandler(event -> toggleFind(inspectComponent));
  116. controls.add(analyze);
  117. analyze.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  118. analyze.addClickHandler(event -> {
  119. stopFind();
  120. analyzeLayouts();
  121. });
  122. controls.add(generateWS);
  123. generateWS.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  124. generateWS.addClickHandler(event -> generateWidgetset());
  125. controls.add(generateDesign);
  126. generateDesign.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  127. generateDesign.addClickHandler(event -> {
  128. content.setWidget(new HTML(
  129. "Select a layout or component to generate the declarative design"));
  130. toggleFind(showComponentDesign);
  131. });
  132. hierarchyPanel.addListener(
  133. (connector, element) -> printState(connector, true));
  134. analyzeLayoutsPanel.addListener(
  135. (connector, element) -> printState(connector, true));
  136. content.setStylePrimaryName(VDebugWindow.STYLENAME + "-hierarchy");
  137. initializeHelpPanel();
  138. content.setWidget(helpPanel);
  139. }
  140. private void initializeHelpPanel() {
  141. HTML info = new HTML(showHierarchy.getHTML() + " "
  142. + showHierarchy.getTitle() + "<br/>" + find.getHTML() + " "
  143. + find.getTitle() + "<br/>" + analyze.getHTML() + " "
  144. + analyze.getTitle() + "<br/>" + generateWS.getHTML() + " "
  145. + generateWS.getTitle() + "<br/>" + generateDesign.getHTML()
  146. + " " + generateDesign.getTitle() + "<br/>");
  147. info.setStyleName(VDebugWindow.STYLENAME + "-info");
  148. helpPanel.add(info);
  149. }
  150. private void showHierarchy() {
  151. Highlight.hideAll();
  152. hierarchyPanel.update();
  153. content.setWidget(hierarchyPanel);
  154. }
  155. @Override
  156. public DebugButton getTabButton() {
  157. return tabButton;
  158. }
  159. @Override
  160. public Widget getControls() {
  161. return controls;
  162. }
  163. @Override
  164. public Widget getContent() {
  165. return content;
  166. }
  167. @Override
  168. public void show() {
  169. }
  170. @Override
  171. public void hide() {
  172. stopFind();
  173. }
  174. private void generateWidgetset() {
  175. widgetsetPanel.update();
  176. content.setWidget(widgetsetPanel);
  177. }
  178. private void analyzeLayouts() {
  179. analyzeLayoutsPanel.update();
  180. content.setWidget(analyzeLayoutsPanel);
  181. }
  182. @Override
  183. public void meta(ApplicationConnection ac, ValueMap meta) {
  184. // show the results of analyzeLayouts
  185. analyzeLayoutsPanel.meta(ac, meta);
  186. }
  187. @Override
  188. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  189. // NOP
  190. }
  191. private boolean isFindMode(FindHandler handler) {
  192. return activeFindHandler == handler;
  193. }
  194. private boolean isFindMode() {
  195. return (activeFindHandler != null);
  196. }
  197. private void toggleFind(FindHandler handler) {
  198. if (isFindMode()) {
  199. // Currently finding something
  200. if (isFindMode(handler)) {
  201. // Toggle off, stop finding
  202. stopFind();
  203. return;
  204. } else {
  205. // Stop finding something else, start finding this
  206. stopFind();
  207. startFind(handler);
  208. }
  209. } else {
  210. // Not currently finding anything
  211. startFind(handler);
  212. }
  213. }
  214. private void startFind(FindHandler handler) {
  215. if (isFindMode()) {
  216. stopFind();
  217. }
  218. Highlight.hideAll();
  219. highlightModeRegistration = Event
  220. .addNativePreviewHandler(highlightModeHandler);
  221. activeFindHandler = handler;
  222. if (handler == inspectComponent) {
  223. find.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  224. } else if (handler == showComponentDesign) {
  225. generateDesign.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  226. }
  227. }
  228. /**
  229. * Stop any current find operation, regardless of the handler
  230. */
  231. private void stopFind() {
  232. if (!isFindMode()) {
  233. return;
  234. }
  235. highlightModeRegistration.removeHandler();
  236. highlightModeRegistration = null;
  237. find.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  238. generateDesign.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  239. activeFindHandler = null;
  240. }
  241. private void printState(ServerConnector connector, boolean serverDebug) {
  242. Highlight.showOnly(connector);
  243. if (serverDebug) {
  244. HierarchyPanel.showServerDebugInfo(connector);
  245. }
  246. infoPanel.update(connector);
  247. content.setWidget(infoPanel);
  248. }
  249. private final NativePreviewHandler highlightModeHandler = event -> {
  250. if (event.getTypeInt() == Event.ONKEYDOWN
  251. && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
  252. stopFind();
  253. Highlight.hideAll();
  254. return;
  255. }
  256. if (event.getTypeInt() == Event.ONMOUSEMOVE) {
  257. Highlight.hideAll();
  258. Element eventTarget = WidgetUtil.getElementFromPoint(
  259. event.getNativeEvent().getClientX(),
  260. event.getNativeEvent().getClientY());
  261. if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) {
  262. // Do not prevent using debug window controls
  263. infoPanel.clear();
  264. return;
  265. }
  266. for (ApplicationConnection a : ApplicationConfiguration
  267. .getRunningApplications()) {
  268. ComponentConnector connector = Util.getConnectorForElement(a,
  269. a.getUIConnector().getWidget(), eventTarget);
  270. if (connector == null) {
  271. connector = Util.getConnectorForElement(a, RootPanel.get(),
  272. eventTarget);
  273. }
  274. if (connector != null) {
  275. activeFindHandler.onHover(connector);
  276. event.cancel();
  277. event.consume();
  278. event.getNativeEvent().stopPropagation();
  279. return;
  280. }
  281. }
  282. // Not over any connector
  283. activeFindHandler.onHover(null);
  284. }
  285. if (event.getTypeInt() == Event.ONCLICK) {
  286. Highlight.hideAll();
  287. event.cancel();
  288. event.consume();
  289. event.getNativeEvent().stopPropagation();
  290. Element eventTarget = WidgetUtil.getElementFromPoint(
  291. event.getNativeEvent().getClientX(),
  292. event.getNativeEvent().getClientY());
  293. for (ApplicationConnection a : ApplicationConfiguration
  294. .getRunningApplications()) {
  295. ComponentConnector connector = Util.getConnectorForElement(a,
  296. a.getUIConnector().getWidget(), eventTarget);
  297. if (connector == null) {
  298. connector = Util.getConnectorForElement(a, RootPanel.get(),
  299. eventTarget);
  300. }
  301. if (connector != null) {
  302. activeFindHandler.onSelected(connector);
  303. return;
  304. }
  305. }
  306. // Click on something else -> stop find operation
  307. stopFind();
  308. }
  309. event.cancel();
  310. };
  311. }