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.

Highlight.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.HashSet;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.Style;
  20. import com.google.gwt.dom.client.Style.Position;
  21. import com.google.gwt.dom.client.Style.Unit;
  22. import com.google.gwt.user.client.DOM;
  23. import com.google.gwt.user.client.ui.RootPanel;
  24. import com.google.gwt.user.client.ui.Widget;
  25. import com.vaadin.client.BrowserInfo;
  26. import com.vaadin.client.ComponentConnector;
  27. import com.vaadin.client.ServerConnector;
  28. import com.vaadin.client.ui.VWindow;
  29. /**
  30. * Highlights a widget in the UI by overlaying a semi-transparent colored div.
  31. * <p>
  32. * Multiple highlights can be added, then selectively removed with
  33. * {@link #hide(Element)} or all at once with {@link #hideAll()}.
  34. * </p>
  35. * <p>
  36. * Note that highlights are intended to be short-term; highlights do not move or
  37. * disappear with the highlighted widget, and it is also fairly likely that
  38. * someone else calls {@link #hideAll()} eventually.
  39. * </p>
  40. *
  41. * @since 7.1
  42. * @author Vaadin Ltd
  43. */
  44. public class Highlight {
  45. private static final String DEFAULT_COLOR = "red";
  46. private static final double DEFAULT_OPACITY = 0.3;
  47. private static final int MIN_WIDTH = 3;
  48. private static final int MIN_HEIGHT = 3;
  49. static HashSet<Element> highlights;
  50. /**
  51. * Highlight the {@link Widget} for the given {@link ComponentConnector}.
  52. * <p>
  53. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  54. * this particular highlight.
  55. * </p>
  56. *
  57. * @param connector
  58. * ComponentConnector
  59. * @return Highlight element
  60. */
  61. static Element show(ComponentConnector connector) {
  62. return show(connector, DEFAULT_COLOR);
  63. }
  64. /**
  65. * Highlight the {@link Widget} for the given connector if it is a
  66. * {@link ComponentConnector}. Hide any other highlight.
  67. * <p>
  68. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  69. * this particular highlight.
  70. * </p>
  71. *
  72. * @since 7.1
  73. *
  74. * @param connector
  75. * the server connector to highlight
  76. * @return Highlight element, or <code>null</code> if the connector isn't a
  77. * component
  78. */
  79. static Element showOnly(ServerConnector connector) {
  80. hideAll();
  81. if (connector instanceof ComponentConnector) {
  82. return show((ComponentConnector) connector);
  83. } else {
  84. return null;
  85. }
  86. }
  87. /**
  88. * Highlights the {@link Widget} for the given {@link ComponentConnector}
  89. * using the given color.
  90. * <p>
  91. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  92. * this particular highlight.
  93. * </p>
  94. *
  95. * @param connector
  96. * ComponentConnector
  97. * @param color
  98. * Color of highlight
  99. * @return Highlight element
  100. */
  101. static Element show(ComponentConnector connector, String color) {
  102. if (connector != null) {
  103. Widget w = connector.getWidget();
  104. return show(w, color);
  105. }
  106. return null;
  107. }
  108. /**
  109. * Highlights the given {@link Widget}.
  110. * <p>
  111. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  112. * this particular highlight.
  113. * </p>
  114. *
  115. * @param widget
  116. * Widget to highlight
  117. * @return Highlight element
  118. */
  119. static Element show(Widget widget) {
  120. return show(widget, DEFAULT_COLOR);
  121. }
  122. /**
  123. * Highlight the given {@link Widget} using the given color.
  124. * <p>
  125. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  126. * this particular highlight.
  127. * </p>
  128. *
  129. * @param widget
  130. * Widget to highlight
  131. * @param color
  132. * Color of highlight
  133. * @return Highlight element
  134. */
  135. static Element show(Widget widget, String color) {
  136. if (widget != null) {
  137. show(widget.getElement(), color);
  138. }
  139. return null;
  140. }
  141. /**
  142. * Highlights the given {@link Element}.
  143. * <p>
  144. * Pass the returned {@link Element} to {@link #hide(Element)} to remove
  145. * this particular highlight.
  146. * </p>
  147. *
  148. * @param element
  149. * Element to highlight
  150. * @return Highlight element
  151. */
  152. static Element show(Element element) {
  153. return show(element, DEFAULT_COLOR);
  154. }
  155. /**
  156. * Highlight the given {@link Element} using the given color.
  157. * <p>
  158. * Pass the returned highlight {@link Element} to {@link #hide(Element)} to
  159. * remove this particular highlight.
  160. * </p>
  161. *
  162. * @param element
  163. * Element to highlight
  164. * @param color
  165. * Color of highlight
  166. * @return Highlight element
  167. */
  168. static Element show(Element element, String color) {
  169. if (element != null) {
  170. if (highlights == null) {
  171. highlights = new HashSet<>();
  172. }
  173. Element highlight = DOM.createDiv();
  174. Style style = highlight.getStyle();
  175. style.setTop(element.getAbsoluteTop(), Unit.PX);
  176. style.setLeft(element.getAbsoluteLeft(), Unit.PX);
  177. int width = element.getOffsetWidth();
  178. if (width < MIN_WIDTH) {
  179. width = MIN_WIDTH;
  180. }
  181. style.setWidth(width, Unit.PX);
  182. int height = element.getOffsetHeight();
  183. if (height < MIN_HEIGHT) {
  184. height = MIN_HEIGHT;
  185. }
  186. style.setHeight(height, Unit.PX);
  187. RootPanel.getBodyElement().appendChild(highlight);
  188. style.setPosition(Position.ABSOLUTE);
  189. style.setZIndex(VWindow.Z_INDEX + 1000);
  190. style.setBackgroundColor(color);
  191. style.setOpacity(DEFAULT_OPACITY);
  192. if (BrowserInfo.get().isIE()) {
  193. style.setProperty("filter",
  194. "alpha(opacity=" + (DEFAULT_OPACITY * 100) + ")");
  195. }
  196. highlights.add(highlight);
  197. return highlight;
  198. }
  199. return null;
  200. }
  201. /**
  202. * Hides the given highlight.
  203. *
  204. * @param highlight
  205. * Highlight to hide
  206. */
  207. static void hide(Element highlight) {
  208. if (highlight != null && highlight.getParentElement() != null) {
  209. highlight.getParentElement().removeChild(highlight);
  210. highlights.remove(highlight);
  211. }
  212. }
  213. /**
  214. * Hides all highlights
  215. */
  216. static void hideAll() {
  217. if (highlights != null) {
  218. for (Element highlight : highlights) {
  219. if (highlight.getParentElement() != null) {
  220. highlight.getParentElement().removeChild(highlight);
  221. }
  222. }
  223. highlights = null;
  224. }
  225. }
  226. }