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.

VCaption.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.logging.Logger;
  18. import com.google.gwt.aria.client.Roles;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.dom.client.Style.Unit;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.Event;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.google.gwt.user.client.ui.HasHTML;
  25. import com.vaadin.client.communication.StateChangeEvent;
  26. import com.vaadin.client.ui.HasErrorIndicator;
  27. import com.vaadin.client.ui.HasRequiredIndicator;
  28. import com.vaadin.client.ui.Icon;
  29. import com.vaadin.client.ui.ImageIcon;
  30. import com.vaadin.client.ui.aria.AriaHelper;
  31. import com.vaadin.shared.AbstractComponentState;
  32. import com.vaadin.shared.ComponentConstants;
  33. import com.vaadin.shared.ui.ComponentStateUtil;
  34. public class VCaption extends HTML {
  35. public static final String CLASSNAME = "v-caption";
  36. private final ComponentConnector owner;
  37. private Element errorIndicatorElement;
  38. private Element requiredFieldIndicator;
  39. private Icon icon;
  40. private String iconAltText = "";
  41. private Element captionText;
  42. private final ApplicationConnection client;
  43. private boolean placedAfterComponent = false;
  44. private int maxWidth = -1;
  45. private enum InsertPosition {
  46. ICON, CAPTION, REQUIRED, ERROR
  47. }
  48. private TooltipInfo tooltipInfo = null;
  49. private boolean captionAsHtml = false;
  50. /**
  51. * Creates a caption that is not linked to a {@link ComponentConnector}.
  52. *
  53. * When using this constructor, {@link #getOwner()} returns null.
  54. *
  55. * @param client
  56. * ApplicationConnection
  57. * @deprecated all captions should be associated with a paintable widget and
  58. * be updated from shared state, not UIDL
  59. */
  60. @Deprecated
  61. public VCaption(ApplicationConnection client) {
  62. super();
  63. this.client = client;
  64. owner = null;
  65. setStyleName(CLASSNAME);
  66. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  67. }
  68. /**
  69. * Creates a caption for a {@link ComponentConnector}.
  70. *
  71. * @param component
  72. * owner of caption, not null
  73. * @param client
  74. * ApplicationConnection
  75. */
  76. public VCaption(ComponentConnector component,
  77. ApplicationConnection client) {
  78. super();
  79. this.client = client;
  80. owner = component;
  81. if (client != null && owner != null) {
  82. setOwnerPid(getElement(), owner.getConnectorId());
  83. }
  84. setStyleName(CLASSNAME);
  85. }
  86. @Override
  87. protected void onAttach() {
  88. super.onAttach();
  89. if (null != owner) {
  90. AriaHelper.bindCaption(owner.getWidget(), getElement());
  91. }
  92. }
  93. @Override
  94. protected void onDetach() {
  95. super.onDetach();
  96. if (null != owner) {
  97. AriaHelper.bindCaption(owner.getWidget(), null);
  98. AriaHelper.handleInputInvalid(owner.getWidget(), false);
  99. AriaHelper.handleInputRequired(owner.getWidget(), false);
  100. }
  101. }
  102. /**
  103. * Updates the caption from UIDL.
  104. *
  105. * This method may only be called when the caption has an owner - otherwise,
  106. * use {@link #updateCaptionWithoutOwner(UIDL, String, boolean, boolean)}.
  107. *
  108. * @return true if the position where the caption should be placed has
  109. * changed
  110. */
  111. public boolean updateCaption() {
  112. boolean wasPlacedAfterComponent = placedAfterComponent;
  113. // Caption is placed after component unless there is some part which
  114. // moves it above.
  115. placedAfterComponent = true;
  116. String style = CLASSNAME;
  117. if (ComponentStateUtil.hasStyles(owner.getState())) {
  118. for (String customStyle : owner.getState().styles) {
  119. style += " " + CLASSNAME + "-" + customStyle;
  120. }
  121. }
  122. if (!owner.isEnabled()) {
  123. style += " " + StyleConstants.DISABLED;
  124. }
  125. setStyleName(style);
  126. boolean hasIcon = owner.getState().resources
  127. .containsKey(ComponentConstants.ICON_RESOURCE);
  128. boolean showRequired = false;
  129. boolean showError = false;
  130. if (owner instanceof HasRequiredIndicator) {
  131. showRequired = ((HasRequiredIndicator) owner)
  132. .isRequiredIndicatorVisible();
  133. }
  134. if (owner instanceof HasErrorIndicator) {
  135. showError = ((HasErrorIndicator) owner).isErrorIndicatorVisible();
  136. }
  137. if (icon != null) {
  138. getElement().removeChild(icon.getElement());
  139. icon = null;
  140. }
  141. if (hasIcon) {
  142. String uri = owner.getState().resources
  143. .get(ComponentConstants.ICON_RESOURCE).getURL();
  144. icon = client.getIcon(uri);
  145. if (icon instanceof ImageIcon) {
  146. // onload will set appropriate size later
  147. icon.setWidth("0");
  148. icon.setHeight("0");
  149. }
  150. DOM.insertChild(getElement(), icon.getElement(),
  151. getInsertPosition(InsertPosition.ICON));
  152. // Icon forces the caption to be above the component
  153. placedAfterComponent = false;
  154. }
  155. if (owner.getState().caption != null) {
  156. // A caption text should be shown if the attribute is set
  157. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  158. // avoid ending up here.
  159. if (captionText == null) {
  160. captionText = DOM.createDiv();
  161. captionText.setClassName("v-captiontext");
  162. DOM.insertChild(getElement(), captionText,
  163. getInsertPosition(InsertPosition.CAPTION));
  164. }
  165. // Update caption text
  166. String c = owner.getState().caption;
  167. // A text forces the caption to be above the component.
  168. placedAfterComponent = false;
  169. if (c == null || c.trim().isEmpty()) {
  170. // Not sure if c even can be null. Should not.
  171. // This is required to ensure that the caption uses space in all
  172. // browsers when it is set to the empty string. If there is an
  173. // icon, error indicator or required indicator they will ensure
  174. // that space is reserved.
  175. if (!hasIcon && !showRequired && !showError) {
  176. captionText.setInnerHTML(" ");
  177. }
  178. } else {
  179. setCaptionText(captionText, owner.getState());
  180. }
  181. } else if (captionText != null) {
  182. // Remove existing
  183. DOM.removeChild(getElement(), captionText);
  184. captionText = null;
  185. }
  186. if (ComponentStateUtil.hasDescription(owner.getState())
  187. && captionText != null) {
  188. addStyleDependentName("hasdescription");
  189. } else {
  190. removeStyleDependentName("hasdescription");
  191. }
  192. AriaHelper.handleInputRequired(owner.getWidget(), showRequired);
  193. if (showRequired) {
  194. if (requiredFieldIndicator == null) {
  195. requiredFieldIndicator = DOM.createDiv();
  196. requiredFieldIndicator
  197. .setClassName("v-required-field-indicator");
  198. DOM.setInnerText(requiredFieldIndicator, "*");
  199. DOM.insertChild(getElement(), requiredFieldIndicator,
  200. getInsertPosition(InsertPosition.REQUIRED));
  201. // Hide the required indicator from assistive device
  202. Roles.getTextboxRole()
  203. .setAriaHiddenState(requiredFieldIndicator, true);
  204. }
  205. } else if (requiredFieldIndicator != null) {
  206. // Remove existing
  207. DOM.removeChild(getElement(), requiredFieldIndicator);
  208. requiredFieldIndicator = null;
  209. }
  210. AriaHelper.handleInputInvalid(owner.getWidget(), showError);
  211. if (showError) {
  212. if (errorIndicatorElement == null) {
  213. errorIndicatorElement = DOM.createDiv();
  214. DOM.setInnerHTML(errorIndicatorElement, " ");
  215. DOM.setElementProperty(errorIndicatorElement, "className",
  216. "v-errorindicator");
  217. DOM.insertChild(getElement(), errorIndicatorElement,
  218. getInsertPosition(InsertPosition.ERROR));
  219. // Hide error indicator from assistive devices
  220. Roles.getTextboxRole().setAriaHiddenState(errorIndicatorElement,
  221. true);
  222. }
  223. } else if (errorIndicatorElement != null) {
  224. // Remove existing
  225. getElement().removeChild(errorIndicatorElement);
  226. errorIndicatorElement = null;
  227. }
  228. return (wasPlacedAfterComponent != placedAfterComponent);
  229. }
  230. private int getInsertPosition(InsertPosition element) {
  231. int pos = 0;
  232. if (InsertPosition.ICON.equals(element)) {
  233. return pos;
  234. }
  235. if (icon != null) {
  236. pos++;
  237. }
  238. if (InsertPosition.CAPTION.equals(element)) {
  239. return pos;
  240. }
  241. if (captionText != null) {
  242. pos++;
  243. }
  244. if (InsertPosition.REQUIRED.equals(element)) {
  245. return pos;
  246. }
  247. if (requiredFieldIndicator != null) {
  248. pos++;
  249. }
  250. // if (InsertPosition.ERROR.equals(element)) {
  251. // }
  252. return pos;
  253. }
  254. @Deprecated
  255. public boolean updateCaptionWithoutOwner(String caption, boolean disabled,
  256. boolean hasDescription, boolean hasError, String iconURL) {
  257. return updateCaptionWithoutOwner(caption, disabled, hasDescription,
  258. hasError, iconURL, "");
  259. }
  260. @Deprecated
  261. public boolean updateCaptionWithoutOwner(String caption, boolean disabled,
  262. boolean hasDescription, boolean hasError, String iconURL,
  263. String iconAltText) {
  264. boolean wasPlacedAfterComponent = placedAfterComponent;
  265. // Caption is placed after component unless there is some part which
  266. // moves it above.
  267. placedAfterComponent = true;
  268. String style = VCaption.CLASSNAME;
  269. if (disabled) {
  270. style += " " + StyleConstants.DISABLED;
  271. }
  272. setStyleName(style);
  273. if (hasDescription) {
  274. if (captionText != null) {
  275. addStyleDependentName("hasdescription");
  276. } else {
  277. removeStyleDependentName("hasdescription");
  278. }
  279. }
  280. boolean hasIcon = iconURL != null;
  281. if (icon != null) {
  282. getElement().removeChild(icon.getElement());
  283. icon = null;
  284. }
  285. if (hasIcon) {
  286. icon = client.getIcon(iconURL);
  287. if (icon instanceof ImageIcon) {
  288. // onload sets appropriate size later
  289. icon.setWidth("0");
  290. icon.setHeight("0");
  291. }
  292. icon.setAlternateText(iconAltText);
  293. DOM.insertChild(getElement(), icon.getElement(),
  294. getInsertPosition(InsertPosition.ICON));
  295. // Icon forces the caption to be above the component
  296. placedAfterComponent = false;
  297. }
  298. if (caption != null) {
  299. // A caption text should be shown if the attribute is set
  300. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  301. // avoid ending up here.
  302. if (captionText == null) {
  303. captionText = DOM.createDiv();
  304. captionText.setClassName("v-captiontext");
  305. DOM.insertChild(getElement(), captionText,
  306. getInsertPosition(InsertPosition.CAPTION));
  307. }
  308. // Update caption text
  309. // A text forces the caption to be above the component.
  310. placedAfterComponent = false;
  311. if (caption.trim().isEmpty()) {
  312. // This is required to ensure that the caption uses space in all
  313. // browsers when it is set to the empty string. If there is an
  314. // icon, error indicator or required indicator they will ensure
  315. // that space is reserved.
  316. if (!hasIcon && !hasError) {
  317. captionText.setInnerHTML(" ");
  318. }
  319. } else {
  320. if (captionAsHtml) {
  321. captionText.setInnerHTML(caption);
  322. } else {
  323. captionText.setInnerText(caption);
  324. }
  325. }
  326. } else if (captionText != null) {
  327. // Remove existing
  328. DOM.removeChild(getElement(), captionText);
  329. captionText = null;
  330. }
  331. if (hasError) {
  332. if (errorIndicatorElement == null) {
  333. errorIndicatorElement = DOM.createDiv();
  334. DOM.setInnerHTML(errorIndicatorElement, " ");
  335. DOM.setElementProperty(errorIndicatorElement, "className",
  336. "v-errorindicator");
  337. DOM.insertChild(getElement(), errorIndicatorElement,
  338. getInsertPosition(InsertPosition.ERROR));
  339. }
  340. } else if (errorIndicatorElement != null) {
  341. // Remove existing
  342. getElement().removeChild(errorIndicatorElement);
  343. errorIndicatorElement = null;
  344. }
  345. return (wasPlacedAfterComponent != placedAfterComponent);
  346. }
  347. @Override
  348. public void onBrowserEvent(Event event) {
  349. super.onBrowserEvent(event);
  350. final Element target = DOM.eventGetTarget(event);
  351. if (DOM.eventGetType(event) == Event.ONLOAD
  352. && icon.getElement() == target) {
  353. icon.setWidth("");
  354. icon.setHeight("");
  355. // if max width defined, recalculate
  356. if (maxWidth != -1) {
  357. setMaxWidth(maxWidth);
  358. } else {
  359. String width = getElement().getStyle().getProperty("width");
  360. if (width != null && !width.isEmpty()) {
  361. setWidth(getRequiredWidth() + "px");
  362. }
  363. }
  364. /*
  365. * The size of the icon might affect the size of the component so we
  366. * must report the size change to the parent TODO consider moving
  367. * the responsibility of reacting to ONLOAD from VCaption to layouts
  368. */
  369. if (owner != null) {
  370. Util.notifyParentOfSizeChange(owner.getWidget(), true);
  371. } else {
  372. getLogger().warning(
  373. "Warning: Icon load event was not propagated because VCaption owner is unknown.");
  374. }
  375. }
  376. }
  377. public static boolean isNeeded(ComponentConnector connector) {
  378. AbstractComponentState state = connector.getState();
  379. if (state.caption != null) {
  380. return true;
  381. }
  382. if (state.resources.containsKey(ComponentConstants.ICON_RESOURCE)) {
  383. return true;
  384. }
  385. if (connector instanceof HasErrorIndicator
  386. && ((HasErrorIndicator) connector).isErrorIndicatorVisible()) {
  387. return true;
  388. }
  389. if (connector instanceof HasRequiredIndicator
  390. && ((HasRequiredIndicator) connector)
  391. .isRequiredIndicatorVisible()) {
  392. return true;
  393. }
  394. return false;
  395. }
  396. /**
  397. * Checks whether anything in a given state change might cause the caption
  398. * to change.
  399. *
  400. * @param event
  401. * the state change event to check
  402. * @return <code>true</code> if the caption might have changed; otherwise
  403. * <code>false</code>
  404. */
  405. public static boolean mightChange(StateChangeEvent event) {
  406. if (event.hasPropertyChanged("caption")) {
  407. return true;
  408. }
  409. if (event.hasPropertyChanged("resources")) {
  410. return true;
  411. }
  412. if (event.hasPropertyChanged("errorMessage")) {
  413. return true;
  414. }
  415. return false;
  416. }
  417. /**
  418. * Returns Paintable for which this Caption belongs to.
  419. *
  420. * @return owner Widget
  421. */
  422. public ComponentConnector getOwner() {
  423. return owner;
  424. }
  425. public boolean shouldBePlacedAfterComponent() {
  426. return placedAfterComponent;
  427. }
  428. public int getRenderedWidth() {
  429. int width = 0;
  430. if (icon != null) {
  431. width += WidgetUtil.getRequiredWidth(icon.getElement());
  432. }
  433. if (captionText != null) {
  434. width += WidgetUtil.getRequiredWidth(captionText);
  435. }
  436. if (requiredFieldIndicator != null) {
  437. width += WidgetUtil.getRequiredWidth(requiredFieldIndicator);
  438. }
  439. if (errorIndicatorElement != null) {
  440. width += WidgetUtil.getRequiredWidth(errorIndicatorElement);
  441. }
  442. return width;
  443. }
  444. public int getRequiredWidth() {
  445. int width = 0;
  446. if (icon != null) {
  447. width += WidgetUtil.getRequiredWidth(icon.getElement());
  448. }
  449. if (captionText != null) {
  450. int textWidth = captionText.getScrollWidth();
  451. if (BrowserInfo.get().isFirefox() || BrowserInfo.get().isChrome()) {
  452. /*
  453. * The caption might require more space than the scrollWidth
  454. * returns as scrollWidth is rounded down.
  455. */
  456. int requiredWidth = WidgetUtil.getRequiredWidth(captionText);
  457. if (requiredWidth > textWidth) {
  458. textWidth = requiredWidth;
  459. }
  460. }
  461. width += textWidth;
  462. }
  463. if (requiredFieldIndicator != null) {
  464. width += WidgetUtil.getRequiredWidth(requiredFieldIndicator);
  465. }
  466. if (errorIndicatorElement != null) {
  467. width += WidgetUtil.getRequiredWidth(errorIndicatorElement);
  468. }
  469. return width;
  470. }
  471. public int getHeight() {
  472. int height = 0;
  473. int h;
  474. if (icon != null) {
  475. h = WidgetUtil.getRequiredHeight(icon.getElement());
  476. if (h > height) {
  477. height = h;
  478. }
  479. }
  480. if (captionText != null) {
  481. h = WidgetUtil.getRequiredHeight(captionText);
  482. if (h > height) {
  483. height = h;
  484. }
  485. }
  486. if (requiredFieldIndicator != null) {
  487. h = WidgetUtil.getRequiredHeight(requiredFieldIndicator);
  488. if (h > height) {
  489. height = h;
  490. }
  491. }
  492. if (errorIndicatorElement != null) {
  493. h = WidgetUtil.getRequiredHeight(errorIndicatorElement);
  494. if (h > height) {
  495. height = h;
  496. }
  497. }
  498. return height;
  499. }
  500. public void setAlignment(String alignment) {
  501. getElement().getStyle().setProperty("textAlign", alignment);
  502. }
  503. public void setMaxWidth(int maxWidth) {
  504. this.maxWidth = maxWidth;
  505. getElement().getStyle().setWidth(maxWidth, Unit.PX);
  506. if (icon != null) {
  507. icon.getElement().getStyle().clearWidth();
  508. }
  509. if (captionText != null) {
  510. captionText.getStyle().clearWidth();
  511. }
  512. int requiredWidth = getRequiredWidth();
  513. /*
  514. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  515. * maxWidth + ", requiredWidth: " + requiredWidth);
  516. */
  517. if (requiredWidth > maxWidth) {
  518. // Needs to truncate and clip
  519. int availableWidth = maxWidth;
  520. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  521. if (requiredFieldIndicator != null) {
  522. availableWidth -= WidgetUtil
  523. .getRequiredWidth(requiredFieldIndicator);
  524. }
  525. if (errorIndicatorElement != null) {
  526. availableWidth -= WidgetUtil
  527. .getRequiredWidth(errorIndicatorElement);
  528. }
  529. if (availableWidth < 0) {
  530. availableWidth = 0;
  531. }
  532. if (icon != null) {
  533. int iconRequiredWidth = WidgetUtil
  534. .getRequiredWidth(icon.getElement());
  535. if (availableWidth > iconRequiredWidth) {
  536. availableWidth -= iconRequiredWidth;
  537. } else {
  538. icon.getElement().getStyle().setWidth(availableWidth,
  539. Unit.PX);
  540. availableWidth = 0;
  541. }
  542. }
  543. if (captionText != null) {
  544. int captionWidth = WidgetUtil.getRequiredWidth(captionText);
  545. if (availableWidth > captionWidth) {
  546. availableWidth -= captionWidth;
  547. } else {
  548. captionText.getStyle().setWidth(availableWidth, Unit.PX);
  549. availableWidth = 0;
  550. }
  551. }
  552. }
  553. }
  554. /**
  555. * Sets the tooltip that should be shown for the caption
  556. *
  557. * @param tooltipInfo
  558. * The tooltip that should be shown or null if no tooltip should
  559. * be shown
  560. */
  561. public void setTooltipInfo(TooltipInfo tooltipInfo) {
  562. this.tooltipInfo = tooltipInfo;
  563. }
  564. /**
  565. * Returns the tooltip that should be shown for the caption
  566. *
  567. * @return The tooltip to show or null if no tooltip should be shown
  568. */
  569. public TooltipInfo getTooltipInfo() {
  570. return tooltipInfo;
  571. }
  572. protected com.google.gwt.user.client.Element getTextElement() {
  573. return DOM.asOld(captionText);
  574. }
  575. public static String getCaptionOwnerPid(Element e) {
  576. return getOwnerPid(e);
  577. }
  578. private static native void setOwnerPid(Element el, String pid)
  579. /*-{
  580. el.vOwnerPid = pid;
  581. }-*/;
  582. public static native String getOwnerPid(Element el)
  583. /*-{
  584. return el.vOwnerPid;
  585. }-*/;
  586. /**
  587. * Sets whether the caption is rendered as HTML.
  588. * <p>
  589. * Default is false
  590. *
  591. * @param captionAsHtml
  592. * true if the captions are rendered as HTML, false if rendered
  593. * as plain text
  594. */
  595. public void setCaptionAsHtml(boolean captionAsHtml) {
  596. this.captionAsHtml = captionAsHtml;
  597. }
  598. /**
  599. * Checks whether captions are rendered as HTML.
  600. * <p>
  601. * Default is false
  602. *
  603. * @return true if the captions are rendered as HTML, false if rendered as
  604. * plain text
  605. */
  606. public boolean isCaptionAsHtml() {
  607. return captionAsHtml;
  608. }
  609. /**
  610. * Sets the text of the given caption element to the caption found in the
  611. * state.
  612. * <p>
  613. * Uses {@link AbstractComponentState#captionAsHtml} to determine whether to
  614. * set the caption as html or plain text
  615. *
  616. * @since 7.4
  617. * @param captionElement
  618. * the target element
  619. * @param state
  620. * the state from which to read the caption text and mode
  621. */
  622. public static void setCaptionText(Element captionElement,
  623. AbstractComponentState state) {
  624. if (state.captionAsHtml) {
  625. captionElement.setInnerHTML(state.caption);
  626. } else {
  627. captionElement.setInnerText(state.caption);
  628. }
  629. }
  630. /**
  631. * Sets the text of the given widget to the caption found in the state.
  632. * <p>
  633. * Uses {@link AbstractComponentState#captionAsHtml} to determine whether to
  634. * set the caption as html or plain text
  635. *
  636. * @since 7.4
  637. * @param widget
  638. * the target widget
  639. * @param state
  640. * the state from which to read the caption text and mode
  641. */
  642. public static void setCaptionText(HasHTML widget,
  643. AbstractComponentState state) {
  644. if (state.captionAsHtml) {
  645. widget.setHTML(state.caption);
  646. } else {
  647. widget.setText(state.caption);
  648. }
  649. }
  650. private static Logger getLogger() {
  651. return Logger.getLogger(VCaption.class.getName());
  652. }
  653. }