Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestBenchSection.java 8.9KB

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