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.9KB

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