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

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