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

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