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

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