Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestBenchSection.java 9.0KB

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