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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2000-2021 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.dom.client.Element;
  18. import com.google.gwt.user.client.DOM;
  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. import com.vaadin.client.WidgetUtil.ErrorUtil;
  24. import com.vaadin.shared.ui.ErrorLevel;
  25. public class VErrorMessage extends FlowPanel {
  26. public static final String CLASSNAME = "v-errormessage";
  27. private Widget owner;
  28. public VErrorMessage() {
  29. super();
  30. setStyleName(CLASSNAME);
  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.isEmpty()) {
  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. * Sets the correct error level style name for the error message and removes
  56. * all previous style names.
  57. *
  58. * @param errorLevel
  59. * error level
  60. * @since 8.2
  61. */
  62. public void updateErrorLevel(ErrorLevel errorLevel) {
  63. ErrorUtil.setErrorLevelStyle(getStyleElement(), CLASSNAME, errorLevel);
  64. }
  65. /**
  66. * Shows this error message next to given element.
  67. *
  68. * @param indicatorElement
  69. * @deprecated As of 7.2, call and override {@link #showAt(Element)} instead
  70. */
  71. @Deprecated
  72. public void showAt(com.google.gwt.user.client.Element indicatorElement) {
  73. VOverlay errorContainer = (VOverlay) getParent();
  74. if (errorContainer == null) {
  75. errorContainer = new VOverlay();
  76. errorContainer.setWidget(this);
  77. errorContainer.setOwner(owner);
  78. }
  79. errorContainer.setPopupPosition(
  80. DOM.getAbsoluteLeft(indicatorElement)
  81. + 2 * DOM.getElementPropertyInt(indicatorElement,
  82. "offsetHeight"),
  83. DOM.getAbsoluteTop(indicatorElement)
  84. + 2 * DOM.getElementPropertyInt(indicatorElement,
  85. "offsetHeight"));
  86. errorContainer.show();
  87. }
  88. /**
  89. * Shows this error message next to given element.
  90. *
  91. * @param indicatorElement
  92. *
  93. * @since 7.2
  94. */
  95. public void showAt(Element indicatorElement) {
  96. showAt(DOM.asOld(indicatorElement));
  97. }
  98. public void hide() {
  99. final VOverlay errorContainer = (VOverlay) getParent();
  100. if (errorContainer != null) {
  101. errorContainer.hide();
  102. }
  103. }
  104. }