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 26KB

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