Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VErrorMessage.java 2.9KB

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