123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.itmill.toolkit.terminal.gwt.client;
-
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.ui.HTML;
- import com.itmill.toolkit.terminal.gwt.client.ui.Icon;
-
- public class ICaption extends HTML {
-
- public static final String CLASSNAME = "i-caption";
-
- private final Paintable owner;
-
- private Element errorIndicatorElement;
-
- private Element requiredFieldIndicator;
-
- private Icon icon;
-
- private Element captionText;
-
- private final ApplicationConnection client;
-
- private boolean placedAfterComponent = false;
-
- /**
- *
- * @param component
- * optional owner of caption. If not set, getOwner will return
- * null
- * @param client
- */
- public ICaption(Paintable component, ApplicationConnection client) {
- super();
- this.client = client;
- owner = component;
- setStyleName(CLASSNAME);
- }
-
- public void updateCaption(UIDL uidl) {
- setVisible(!uidl.getBooleanAttribute("invisible"));
-
- setStyleName(getElement(), "i-disabled", uidl.hasAttribute("disabled"));
-
- boolean isEmpty = true;
-
- placedAfterComponent = true;
-
- if (uidl.hasAttribute("icon")) {
- if (icon == null) {
- icon = new Icon(client);
- placedAfterComponent = false;
- DOM.insertChild(getElement(), icon.getElement(), 0);
- }
- icon.setUri(uidl.getStringAttribute("icon"));
- isEmpty = false;
- } else {
- if (icon != null) {
- DOM.removeChild(getElement(), icon.getElement());
- icon = null;
- }
- }
-
- if (uidl.hasAttribute("caption")) {
- if (captionText == null) {
- captionText = DOM.createSpan();
- DOM
- .insertChild(getElement(), captionText,
- icon == null ? 0 : 1);
- }
- String c = uidl.getStringAttribute("caption");
- if (c == null) {
- c = "";
- } else {
- isEmpty = false;
- placedAfterComponent = false;
- }
- DOM.setInnerText(captionText, c);
- } else {
- // TODO should span also be removed
- }
-
- if (uidl.hasAttribute("description")) {
- if (captionText != null) {
- addStyleDependentName("hasdescription");
- } else {
- removeStyleDependentName("hasdescription");
- }
- }
-
- if (uidl.getBooleanAttribute("required")) {
- isEmpty = false;
- if (requiredFieldIndicator == null) {
- requiredFieldIndicator = DOM.createSpan();
- DOM.setInnerText(requiredFieldIndicator, "*");
- DOM.setElementProperty(requiredFieldIndicator, "className",
- "i-required-field-indicator");
- DOM.appendChild(getElement(), requiredFieldIndicator);
- }
- } else {
- if (requiredFieldIndicator != null) {
- DOM.removeChild(getElement(), requiredFieldIndicator);
- requiredFieldIndicator = null;
- }
- }
-
- if (uidl.hasAttribute("error")) {
- isEmpty = false;
- if (errorIndicatorElement == null) {
- errorIndicatorElement = DOM.createDiv();
- DOM.setInnerHTML(errorIndicatorElement, " ");
- DOM.setElementProperty(errorIndicatorElement, "className",
- "i-errorindicator");
- DOM.appendChild(getElement(), errorIndicatorElement);
- }
- } else if (errorIndicatorElement != null) {
- DOM.removeChild(getElement(), errorIndicatorElement);
- errorIndicatorElement = null;
- }
-
- // Workaround for IE weirdness, sometimes returns bad height in some
- // circumstances when Caption is empty. See #1444
- // IE7 bugs more often. I wonder what happens when IE8 arrives...
- if (Util.isIE()) {
- if (isEmpty) {
- setHeight("0px");
- DOM.setStyleAttribute(getElement(), "overflow", "hidden");
- } else {
- setHeight("");
- DOM.setStyleAttribute(getElement(), "overflow", "");
- }
-
- }
-
- }
-
- public void onBrowserEvent(Event event) {
- super.onBrowserEvent(event);
- final Element target = DOM.eventGetTarget(event);
- if (client != null && !DOM.compare(target, getElement())) {
- client.handleTooltipEvent(event, owner);
- }
- }
-
- public static boolean isNeeded(UIDL uidl) {
- if (uidl.getStringAttribute("caption") != null) {
- return true;
- }
- if (uidl.hasAttribute("error")) {
- return true;
- }
- if (uidl.hasAttribute("icon")) {
- return true;
- }
- if (uidl.hasAttribute("required")) {
- return true;
- }
-
- return false;
- }
-
- /**
- * Returns Paintable for which this Caption belongs to.
- *
- * @return owner Widget
- */
- public Paintable getOwner() {
- return owner;
- }
-
- public boolean shouldBePlacedAfterComponent() {
- return placedAfterComponent;
- }
- }
|