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.

TestBenchSection.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 java.util.HashMap;
  18. import java.util.Map;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.event.dom.client.KeyCodes;
  21. import com.google.gwt.event.dom.client.MouseOutEvent;
  22. import com.google.gwt.event.dom.client.MouseOutHandler;
  23. import com.google.gwt.event.dom.client.MouseOverEvent;
  24. import com.google.gwt.event.dom.client.MouseOverHandler;
  25. import com.google.gwt.event.shared.HandlerRegistration;
  26. import com.google.gwt.user.client.Event;
  27. import com.google.gwt.user.client.Event.NativePreviewHandler;
  28. import com.google.gwt.user.client.ui.Button;
  29. import com.google.gwt.user.client.ui.FlowPanel;
  30. import com.google.gwt.user.client.ui.HTML;
  31. import com.google.gwt.user.client.ui.RootPanel;
  32. import com.google.gwt.user.client.ui.Widget;
  33. import com.vaadin.client.ApplicationConfiguration;
  34. import com.vaadin.client.ApplicationConnection;
  35. import com.vaadin.client.ComponentConnector;
  36. import com.vaadin.client.ServerConnector;
  37. import com.vaadin.client.Util;
  38. import com.vaadin.client.ValueMap;
  39. import com.vaadin.client.WidgetUtil;
  40. /**
  41. * Provides functionality for picking selectors for Vaadin TestBench.
  42. *
  43. * @since 7.1.x
  44. * @author Vaadin Ltd
  45. */
  46. public class TestBenchSection implements Section {
  47. /**
  48. * Selector widget showing a selector in a program-usable form.
  49. */
  50. private static class SelectorWidget extends HTML
  51. implements MouseOverHandler, MouseOutHandler {
  52. private final SelectorPath path;
  53. public SelectorWidget(final SelectorPath path) {
  54. this.path = path;
  55. String html = "<div class=\"" + VDebugWindow.STYLENAME
  56. + "-selector\"><span class=\"tb-selector\">"
  57. + WidgetUtil.escapeHTML(path.getElementQuery())
  58. + "</span></div>";
  59. setHTML(html);
  60. addMouseOverHandler(this);
  61. addMouseOutHandler(this);
  62. }
  63. @Override
  64. public void onMouseOver(MouseOverEvent event) {
  65. Highlight.hideAll();
  66. Element element = path.getElement();
  67. if (null != element) {
  68. Highlight.show(element);
  69. }
  70. }
  71. @Override
  72. public void onMouseOut(MouseOutEvent event) {
  73. Highlight.hideAll();
  74. }
  75. }
  76. private final DebugButton tabButton = new DebugButton(Icon.TESTBENCH,
  77. "Pick Vaadin TestBench selectors");
  78. private final FlowPanel content = new FlowPanel();
  79. private final FlowPanel selectorPanel = new FlowPanel();
  80. // map from full path to SelectorWidget to enable reuse of old selectors
  81. private Map<SelectorPath, SelectorWidget> selectorWidgets = new HashMap<>();
  82. private final FlowPanel controls = new FlowPanel();
  83. private final Button find = new DebugButton(Icon.HIGHLIGHT,
  84. "Pick an element and generate a query for it");
  85. private final Button clear = new DebugButton(Icon.CLEAR,
  86. "Clear current elements");
  87. private HandlerRegistration highlightModeRegistration = null;
  88. public TestBenchSection() {
  89. controls.add(find);
  90. find.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  91. find.addClickHandler(event -> toggleFind());
  92. controls.add(clear);
  93. clear.setStylePrimaryName(VDebugWindow.STYLENAME_BUTTON);
  94. clear.addClickHandler(event -> clearResults());
  95. content.setStylePrimaryName(VDebugWindow.STYLENAME + "-testbench");
  96. content.add(selectorPanel);
  97. }
  98. @Override
  99. public DebugButton getTabButton() {
  100. return tabButton;
  101. }
  102. @Override
  103. public Widget getControls() {
  104. return controls;
  105. }
  106. @Override
  107. public Widget getContent() {
  108. return content;
  109. }
  110. @Override
  111. public void show() {
  112. }
  113. @Override
  114. public void hide() {
  115. stopFind();
  116. }
  117. @Override
  118. public void meta(ApplicationConnection ac, ValueMap meta) {
  119. // NOP
  120. }
  121. @Override
  122. public void uidl(ApplicationConnection ac, ValueMap uidl) {
  123. // NOP
  124. }
  125. private boolean isFindMode() {
  126. return (highlightModeRegistration != null);
  127. }
  128. private void toggleFind() {
  129. if (isFindMode()) {
  130. stopFind();
  131. } else {
  132. startFind();
  133. }
  134. }
  135. private void startFind() {
  136. Highlight.hideAll();
  137. if (!isFindMode()) {
  138. highlightModeRegistration = Event
  139. .addNativePreviewHandler(highlightModeHandler);
  140. find.addStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  141. }
  142. }
  143. private void stopFind() {
  144. if (isFindMode()) {
  145. highlightModeRegistration.removeHandler();
  146. highlightModeRegistration = null;
  147. find.removeStyleDependentName(VDebugWindow.STYLENAME_ACTIVE);
  148. }
  149. Highlight.hideAll();
  150. }
  151. private void pickSelector(ServerConnector connector, Element element) {
  152. SelectorPath p = new SelectorPath(connector,
  153. Util.findPaintable(connector.getConnection(), element)
  154. .getWidget().getElement());
  155. SelectorWidget w = new SelectorWidget(p);
  156. content.add(w);
  157. }
  158. private final NativePreviewHandler highlightModeHandler = event -> {
  159. if (event.getTypeInt() == Event.ONKEYDOWN
  160. && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE) {
  161. stopFind();
  162. Highlight.hideAll();
  163. return;
  164. }
  165. if (event.getTypeInt() == Event.ONMOUSEMOVE
  166. || event.getTypeInt() == Event.ONCLICK) {
  167. Element eventTarget = WidgetUtil.getElementFromPoint(
  168. event.getNativeEvent().getClientX(),
  169. event.getNativeEvent().getClientY());
  170. if (VDebugWindow.get().getElement().isOrHasChild(eventTarget)) {
  171. if (isFindMode() && event.getTypeInt() == Event.ONCLICK) {
  172. stopFind();
  173. event.cancel();
  174. }
  175. return;
  176. }
  177. // make sure that not finding the highlight element only
  178. Highlight.hideAll();
  179. eventTarget = WidgetUtil.getElementFromPoint(
  180. event.getNativeEvent().getClientX(),
  181. event.getNativeEvent().getClientY());
  182. ComponentConnector connector = findConnector(eventTarget);
  183. if (event.getTypeInt() == Event.ONMOUSEMOVE) {
  184. if (connector != null) {
  185. Highlight.showOnly(connector);
  186. event.cancel();
  187. event.consume();
  188. event.getNativeEvent().stopPropagation();
  189. return;
  190. }
  191. } else if (event.getTypeInt() == Event.ONCLICK) {
  192. event.cancel();
  193. event.consume();
  194. event.getNativeEvent().stopPropagation();
  195. if (connector != null) {
  196. Highlight.showOnly(connector);
  197. pickSelector(connector, eventTarget);
  198. return;
  199. }
  200. }
  201. }
  202. event.cancel();
  203. };
  204. private ComponentConnector findConnector(Element element) {
  205. for (ApplicationConnection a : ApplicationConfiguration
  206. .getRunningApplications()) {
  207. ComponentConnector connector = Util.getConnectorForElement(a,
  208. a.getUIConnector().getWidget(), element);
  209. if (connector == null) {
  210. connector = Util.getConnectorForElement(a, RootPanel.get(),
  211. element);
  212. }
  213. if (connector != null) {
  214. return connector;
  215. }
  216. }
  217. return null;
  218. }
  219. private void clearResults() {
  220. content.clear();
  221. }
  222. }