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.

ICaption.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.Event;
  8. import com.google.gwt.user.client.ui.HTML;
  9. import com.itmill.toolkit.terminal.gwt.client.ui.Icon;
  10. public class ICaption extends HTML {
  11. public static final String CLASSNAME = "i-caption";
  12. private final Paintable owner;
  13. private Element errorIndicatorElement;
  14. private Element requiredFieldIndicator;
  15. private Icon icon;
  16. private Element captionText;
  17. private final ApplicationConnection client;
  18. private boolean placedAfterComponent = false;
  19. /**
  20. *
  21. * @param component
  22. * optional owner of caption. If not set, getOwner will return
  23. * null
  24. * @param client
  25. */
  26. public ICaption(Paintable component, ApplicationConnection client) {
  27. super();
  28. this.client = client;
  29. owner = component;
  30. setStyleName(CLASSNAME);
  31. }
  32. public void updateCaption(UIDL uidl) {
  33. setVisible(!uidl.getBooleanAttribute("invisible"));
  34. setStyleName(getElement(), "i-disabled", uidl.hasAttribute("disabled"));
  35. boolean isEmpty = true;
  36. placedAfterComponent = true;
  37. if (uidl.hasAttribute("icon")) {
  38. if (icon == null) {
  39. icon = new Icon(client);
  40. placedAfterComponent = false;
  41. DOM.insertChild(getElement(), icon.getElement(), 0);
  42. }
  43. icon.setUri(uidl.getStringAttribute("icon"));
  44. isEmpty = false;
  45. } else {
  46. if (icon != null) {
  47. DOM.removeChild(getElement(), icon.getElement());
  48. icon = null;
  49. }
  50. }
  51. if (uidl.hasAttribute("caption")) {
  52. if (captionText == null) {
  53. captionText = DOM.createSpan();
  54. DOM
  55. .insertChild(getElement(), captionText,
  56. icon == null ? 0 : 1);
  57. }
  58. String c = uidl.getStringAttribute("caption");
  59. if (c == null) {
  60. c = "";
  61. } else {
  62. isEmpty = false;
  63. placedAfterComponent = false;
  64. }
  65. DOM.setInnerText(captionText, c);
  66. } else {
  67. // TODO should span also be removed
  68. }
  69. if (uidl.hasAttribute("description")) {
  70. if (captionText != null) {
  71. addStyleDependentName("hasdescription");
  72. } else {
  73. removeStyleDependentName("hasdescription");
  74. }
  75. }
  76. if (uidl.getBooleanAttribute("required")) {
  77. isEmpty = false;
  78. if (requiredFieldIndicator == null) {
  79. requiredFieldIndicator = DOM.createSpan();
  80. DOM.setInnerText(requiredFieldIndicator, "*");
  81. DOM.setElementProperty(requiredFieldIndicator, "className",
  82. "i-required-field-indicator");
  83. DOM.appendChild(getElement(), requiredFieldIndicator);
  84. }
  85. } else {
  86. if (requiredFieldIndicator != null) {
  87. DOM.removeChild(getElement(), requiredFieldIndicator);
  88. requiredFieldIndicator = null;
  89. }
  90. }
  91. if (uidl.hasAttribute("error")) {
  92. isEmpty = false;
  93. if (errorIndicatorElement == null) {
  94. errorIndicatorElement = DOM.createDiv();
  95. DOM.setInnerHTML(errorIndicatorElement, " ");
  96. DOM.setElementProperty(errorIndicatorElement, "className",
  97. "i-errorindicator");
  98. DOM.appendChild(getElement(), errorIndicatorElement);
  99. }
  100. } else if (errorIndicatorElement != null) {
  101. DOM.removeChild(getElement(), errorIndicatorElement);
  102. errorIndicatorElement = null;
  103. }
  104. // Workaround for IE weirdness, sometimes returns bad height in some
  105. // circumstances when Caption is empty. See #1444
  106. // IE7 bugs more often. I wonder what happens when IE8 arrives...
  107. if (Util.isIE()) {
  108. if (isEmpty) {
  109. setHeight("0px");
  110. DOM.setStyleAttribute(getElement(), "overflow", "hidden");
  111. } else {
  112. setHeight("");
  113. DOM.setStyleAttribute(getElement(), "overflow", "");
  114. }
  115. }
  116. }
  117. public void onBrowserEvent(Event event) {
  118. super.onBrowserEvent(event);
  119. final Element target = DOM.eventGetTarget(event);
  120. if (client != null && !DOM.compare(target, getElement())) {
  121. client.handleTooltipEvent(event, owner);
  122. }
  123. }
  124. public static boolean isNeeded(UIDL uidl) {
  125. if (uidl.getStringAttribute("caption") != null) {
  126. return true;
  127. }
  128. if (uidl.hasAttribute("error")) {
  129. return true;
  130. }
  131. if (uidl.hasAttribute("icon")) {
  132. return true;
  133. }
  134. if (uidl.hasAttribute("required")) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Returns Paintable for which this Caption belongs to.
  141. *
  142. * @return owner Widget
  143. */
  144. public Paintable getOwner() {
  145. return owner;
  146. }
  147. public boolean shouldBePlacedAfterComponent() {
  148. return placedAfterComponent;
  149. }
  150. }