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.

VErrorMessage.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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;
  17. import com.google.gwt.user.client.DOM;
  18. import com.google.gwt.user.client.Element;
  19. import com.google.gwt.user.client.ui.FlowPanel;
  20. import com.google.gwt.user.client.ui.HTML;
  21. import com.google.gwt.user.client.ui.Widget;
  22. import com.vaadin.client.ui.VOverlay;
  23. public class VErrorMessage extends FlowPanel {
  24. public static final String CLASSNAME = "v-errormessage";
  25. private Widget owner;
  26. public VErrorMessage() {
  27. super();
  28. setStyleName(CLASSNAME);
  29. // Needed for binding with WAI-ARIA attributes
  30. getElement().setId(DOM.createUniqueId());
  31. }
  32. /**
  33. * Set the owner, i.e the Widget that created this {@link VErrorMessage}.
  34. * The owner must be set if the {@link VErrorMessage} is created
  35. * 'stand-alone' (not within a {@link VOverlay}), or theming might not work
  36. * properly.
  37. *
  38. * @see VOverlay#setOwner(Widget)
  39. * @param owner
  40. * the owner (creator Widget)
  41. */
  42. public void setOwner(Widget owner) {
  43. this.owner = owner;
  44. }
  45. public void updateMessage(String htmlErrorMessage) {
  46. clear();
  47. if (htmlErrorMessage == null || htmlErrorMessage.length() == 0) {
  48. add(new HTML(" "));
  49. } else {
  50. // pre-formatted on the server as div per child
  51. add(new HTML(htmlErrorMessage));
  52. }
  53. }
  54. /**
  55. * Shows this error message next to given element.
  56. *
  57. * @param indicatorElement
  58. */
  59. public void showAt(Element indicatorElement) {
  60. VOverlay errorContainer = (VOverlay) getParent();
  61. if (errorContainer == null) {
  62. errorContainer = new VOverlay();
  63. errorContainer.setWidget(this);
  64. errorContainer.setOwner(owner);
  65. }
  66. errorContainer.setPopupPosition(
  67. DOM.getAbsoluteLeft(indicatorElement)
  68. + 2
  69. * DOM.getElementPropertyInt(indicatorElement,
  70. "offsetHeight"),
  71. DOM.getAbsoluteTop(indicatorElement)
  72. + 2
  73. * DOM.getElementPropertyInt(indicatorElement,
  74. "offsetHeight"));
  75. errorContainer.show();
  76. }
  77. public void hide() {
  78. final VOverlay errorContainer = (VOverlay) getParent();
  79. if (errorContainer != null) {
  80. errorContainer.hide();
  81. }
  82. }
  83. }