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.

Caption.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.itmill.toolkit.terminal.gwt.client;
  2. import com.google.gwt.user.client.DOM;
  3. import com.google.gwt.user.client.Element;
  4. import com.google.gwt.user.client.Event;
  5. import com.google.gwt.user.client.ui.HTML;
  6. import com.google.gwt.user.client.ui.PopupPanel;
  7. public class Caption extends HTML {
  8. public static final String CLASSNAME = "i-caption";
  9. private Paintable owner;
  10. private Element errorIndicatorElement;
  11. private Element captionText;
  12. private ErrorMessage errorMessage;
  13. private PopupPanel errorContainer;
  14. /* Caption must be attached to a Paintable */
  15. private Caption() {
  16. };
  17. public Caption(Paintable component) {
  18. super();
  19. owner = component;
  20. setStyleName(CLASSNAME);
  21. }
  22. public void updateCaption(UIDL uidl) {
  23. setVisible(!uidl.getBooleanAttribute("invisible"));
  24. if (uidl.hasAttribute("error")) {
  25. UIDL errorUidl = uidl.getErrors();
  26. if (errorIndicatorElement == null) {
  27. errorIndicatorElement = DOM.createDiv();
  28. DOM.setElementProperty(errorIndicatorElement, "className",
  29. "i-errorindicator");
  30. DOM.insertChild(getElement(), errorIndicatorElement, 0);
  31. }
  32. if (errorMessage == null)
  33. errorMessage = new ErrorMessage();
  34. errorMessage.updateFromUIDL(errorUidl);
  35. } else if (errorIndicatorElement != null) {
  36. DOM.setStyleAttribute(errorIndicatorElement, "display", "none");
  37. }
  38. if (uidl.hasAttribute("caption")) {
  39. if (captionText == null) {
  40. captionText = DOM.createSpan();
  41. DOM.appendChild(getElement(), captionText);
  42. }
  43. DOM.setInnerText(captionText, uidl.getStringAttribute("caption"));
  44. }
  45. if (uidl.hasAttribute("description")) {
  46. if (captionText != null) {
  47. DOM.setElementProperty(captionText, "title", uidl
  48. .getStringAttribute("description"));
  49. } else {
  50. setTitle(uidl.getStringAttribute("description"));
  51. }
  52. }
  53. }
  54. public void onBrowserEvent(Event event) {
  55. Element target = DOM.eventGetTarget(event);
  56. if (errorIndicatorElement != null
  57. && DOM.compare(target, errorIndicatorElement)) {
  58. switch (DOM.eventGetType(event)) {
  59. case Event.ONMOUSEOVER:
  60. showErrorMessage();
  61. break;
  62. case Event.ONMOUSEOUT:
  63. hideErrorMessage();
  64. break;
  65. case Event.ONCLICK:
  66. ApplicationConnection.getConsole().log(
  67. DOM.getInnerHTML(errorMessage.getElement()));
  68. default:
  69. break;
  70. }
  71. }
  72. }
  73. private void hideErrorMessage() {
  74. if (errorContainer != null) {
  75. errorContainer.hide();
  76. }
  77. }
  78. private void showErrorMessage() {
  79. if (errorMessage != null) {
  80. if (errorContainer == null) {
  81. errorContainer = new PopupPanel();
  82. errorContainer.setWidget(errorMessage);
  83. }
  84. errorContainer.setPopupPosition(DOM
  85. .getAbsoluteLeft(errorIndicatorElement)
  86. + 2
  87. * DOM.getElementPropertyInt(errorIndicatorElement,
  88. "offsetHeight"), DOM
  89. .getAbsoluteTop(errorIndicatorElement)
  90. + 2
  91. * DOM.getElementPropertyInt(errorIndicatorElement,
  92. "offsetHeight"));
  93. errorContainer.show();
  94. }
  95. }
  96. public static boolean isNeeded(UIDL uidl) {
  97. if (uidl.getStringAttribute("caption") != null)
  98. return true;
  99. if (uidl.hasAttribute("error"))
  100. return true;
  101. // TODO Description ??
  102. return false;
  103. }
  104. /**
  105. * Returns Paintable for which this Caption belongs to.
  106. *
  107. * @return owner Widget
  108. */
  109. public Paintable getOwner() {
  110. return owner;
  111. }
  112. }