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

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