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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.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.vaadin.terminal.gwt.client.ui.AbstractComponentConnector;
  10. import com.vaadin.terminal.gwt.client.ui.Icon;
  11. import com.vaadin.terminal.gwt.client.ui.TabsheetBaseConnector;
  12. public class VCaption extends HTML {
  13. public static final String CLASSNAME = "v-caption";
  14. private final ComponentConnector owner;
  15. private Element errorIndicatorElement;
  16. private Element requiredFieldIndicator;
  17. private Icon icon;
  18. private Element captionText;
  19. private final ApplicationConnection client;
  20. private boolean placedAfterComponent = false;
  21. private int maxWidth = -1;
  22. protected static final String ATTRIBUTE_ICON = "icon";
  23. protected static final String ATTRIBUTE_CAPTION = "caption";
  24. protected static final String ATTRIBUTE_DESCRIPTION = "description";
  25. protected static final String ATTRIBUTE_REQUIRED = "required";
  26. protected static final String ATTRIBUTE_ERROR = "error";
  27. protected static final String ATTRIBUTE_HIDEERRORS = "hideErrors";
  28. private enum InsertPosition {
  29. ICON, CAPTION, REQUIRED, ERROR
  30. }
  31. /**
  32. * Creates a caption that is not linked to a {@link ComponentConnector}.
  33. *
  34. * When using this constructor, {@link #getOwner()} returns null.
  35. *
  36. * @param client
  37. * ApplicationConnection
  38. * @deprecated all captions should be associated with a paintable widget and
  39. * be updated from shared state, not UIDL
  40. */
  41. @Deprecated
  42. public VCaption(ApplicationConnection client) {
  43. super();
  44. this.client = client;
  45. owner = null;
  46. setStyleName(CLASSNAME);
  47. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  48. }
  49. /**
  50. * Creates a caption for a {@link ComponentConnector}.
  51. *
  52. * @param component
  53. * owner of caption, not null
  54. * @param client
  55. * ApplicationConnection
  56. */
  57. public VCaption(ComponentConnector component, ApplicationConnection client) {
  58. super();
  59. this.client = client;
  60. owner = component;
  61. if (client != null && owner != null) {
  62. setOwnerPid(getElement(),
  63. ConnectorMap.get(client).getConnectorId(owner));
  64. }
  65. setStyleName(CLASSNAME);
  66. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  67. }
  68. /**
  69. * Updates the caption from UIDL.
  70. *
  71. * This method may only be called when the caption has an owner - otherwise,
  72. * use {@link #updateCaptionWithoutOwner(UIDL, String, boolean, boolean)}.
  73. *
  74. * @param uidl
  75. * @return true if the position where the caption should be placed has
  76. * changed
  77. */
  78. public boolean updateCaption(UIDL uidl) {
  79. setVisible(!uidl.getBooleanAttribute("invisible"));
  80. boolean wasPlacedAfterComponent = placedAfterComponent;
  81. // Caption is placed after component unless there is some part which
  82. // moves it above.
  83. placedAfterComponent = true;
  84. String style = CLASSNAME;
  85. if (owner.getState().hasStyles()) {
  86. final String[] styles = owner.getState().getStyle().split(" ");
  87. for (int i = 0; i < styles.length; i++) {
  88. style += " " + CLASSNAME + "-" + styles[i];
  89. }
  90. }
  91. if (owner.getState().isDisabled()) {
  92. style += " " + ApplicationConnection.DISABLED_CLASSNAME;
  93. }
  94. setStyleName(style);
  95. boolean hasIcon = uidl
  96. .hasAttribute(AbstractComponentConnector.ATTRIBUTE_ICON);
  97. boolean showRequired = uidl
  98. .getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED);
  99. boolean showError = uidl
  100. .hasAttribute(AbstractComponentConnector.ATTRIBUTE_ERROR)
  101. && !uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_HIDEERRORS);
  102. if (hasIcon) {
  103. if (icon == null) {
  104. icon = new Icon(client);
  105. icon.setWidth("0");
  106. icon.setHeight("0");
  107. DOM.insertChild(getElement(), icon.getElement(),
  108. getInsertPosition(InsertPosition.ICON));
  109. }
  110. // Icon forces the caption to be above the component
  111. placedAfterComponent = false;
  112. icon.setUri(uidl
  113. .getStringAttribute(AbstractComponentConnector.ATTRIBUTE_ICON));
  114. } else if (icon != null) {
  115. // Remove existing
  116. DOM.removeChild(getElement(), icon.getElement());
  117. icon = null;
  118. }
  119. if (owner.getState().getCaption() != null) {
  120. // A caption text should be shown if the attribute is set
  121. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  122. // avoid ending up here.
  123. if (captionText == null) {
  124. captionText = DOM.createDiv();
  125. captionText.setClassName("v-captiontext");
  126. DOM.insertChild(getElement(), captionText,
  127. getInsertPosition(InsertPosition.CAPTION));
  128. }
  129. // Update caption text
  130. String c = owner.getState().getCaption();
  131. // A text forces the caption to be above the component.
  132. placedAfterComponent = false;
  133. if (c == null || c.trim().equals("")) {
  134. // Not sure if c even can be null. Should not.
  135. // This is required to ensure that the caption uses space in all
  136. // browsers when it is set to the empty string. If there is an
  137. // icon, error indicator or required indicator they will ensure
  138. // that space is reserved.
  139. if (!hasIcon && !showRequired && !showError) {
  140. captionText.setInnerHTML("&nbsp;");
  141. }
  142. } else {
  143. DOM.setInnerText(captionText, c);
  144. }
  145. } else if (captionText != null) {
  146. // Remove existing
  147. DOM.removeChild(getElement(), captionText);
  148. captionText = null;
  149. }
  150. if (owner.getState().hasDescription() && captionText != null) {
  151. addStyleDependentName("hasdescription");
  152. } else {
  153. removeStyleDependentName("hasdescription");
  154. }
  155. if (showRequired) {
  156. if (requiredFieldIndicator == null) {
  157. requiredFieldIndicator = DOM.createDiv();
  158. requiredFieldIndicator
  159. .setClassName("v-required-field-indicator");
  160. DOM.setInnerText(requiredFieldIndicator, "*");
  161. DOM.insertChild(getElement(), requiredFieldIndicator,
  162. getInsertPosition(InsertPosition.REQUIRED));
  163. }
  164. } else if (requiredFieldIndicator != null) {
  165. // Remove existing
  166. DOM.removeChild(getElement(), requiredFieldIndicator);
  167. requiredFieldIndicator = null;
  168. }
  169. if (showError) {
  170. if (errorIndicatorElement == null) {
  171. errorIndicatorElement = DOM.createDiv();
  172. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  173. DOM.setElementProperty(errorIndicatorElement, "className",
  174. "v-errorindicator");
  175. DOM.insertChild(getElement(), errorIndicatorElement,
  176. getInsertPosition(InsertPosition.ERROR));
  177. }
  178. } else if (errorIndicatorElement != null) {
  179. // Remove existing
  180. getElement().removeChild(errorIndicatorElement);
  181. errorIndicatorElement = null;
  182. }
  183. return (wasPlacedAfterComponent != placedAfterComponent);
  184. }
  185. private int getInsertPosition(InsertPosition element) {
  186. int pos = 0;
  187. if (InsertPosition.ICON.equals(element)) {
  188. return pos;
  189. }
  190. if (icon != null) {
  191. pos++;
  192. }
  193. if (InsertPosition.CAPTION.equals(element)) {
  194. return pos;
  195. }
  196. if (captionText != null) {
  197. pos++;
  198. }
  199. if (InsertPosition.REQUIRED.equals(element)) {
  200. return pos;
  201. }
  202. if (requiredFieldIndicator != null) {
  203. pos++;
  204. }
  205. // if (InsertPosition.ERROR.equals(element)) {
  206. // }
  207. return pos;
  208. }
  209. @Deprecated
  210. public boolean updateCaptionWithoutOwner(UIDL uidl, String caption,
  211. boolean disabled, boolean hasDescription) {
  212. // TODO temporary method, needed because some tabsheet and accordion
  213. // internal captions do not have an owner or shared state. Simplified to
  214. // only support those cases
  215. setVisible(!uidl.getBooleanAttribute("invisible"));
  216. boolean wasPlacedAfterComponent = placedAfterComponent;
  217. // Caption is placed after component unless there is some part which
  218. // moves it above.
  219. placedAfterComponent = true;
  220. String style = VCaption.CLASSNAME;
  221. if (disabled) {
  222. style += " " + ApplicationConnection.DISABLED_CLASSNAME;
  223. }
  224. setStyleName(style);
  225. if (hasDescription) {
  226. if (captionText != null) {
  227. addStyleDependentName("hasdescription");
  228. } else {
  229. removeStyleDependentName("hasdescription");
  230. }
  231. }
  232. boolean hasIcon = uidl
  233. .hasAttribute(AbstractComponentConnector.ATTRIBUTE_ICON);
  234. boolean showError = uidl
  235. .hasAttribute(AbstractComponentConnector.ATTRIBUTE_ERROR)
  236. && !uidl.getBooleanAttribute(AbstractComponentConnector.ATTRIBUTE_HIDEERRORS);
  237. if (hasIcon) {
  238. if (icon == null) {
  239. icon = new Icon(client);
  240. icon.setWidth("0");
  241. icon.setHeight("0");
  242. DOM.insertChild(getElement(), icon.getElement(),
  243. getInsertPosition(InsertPosition.ICON));
  244. }
  245. // Icon forces the caption to be above the component
  246. placedAfterComponent = false;
  247. icon.setUri(uidl
  248. .getStringAttribute(AbstractComponentConnector.ATTRIBUTE_ICON));
  249. } else if (icon != null) {
  250. // Remove existing
  251. DOM.removeChild(getElement(), icon.getElement());
  252. icon = null;
  253. }
  254. if (caption != null) {
  255. // A caption text should be shown if the attribute is set
  256. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  257. // avoid ending up here.
  258. if (captionText == null) {
  259. captionText = DOM.createDiv();
  260. captionText.setClassName("v-captiontext");
  261. DOM.insertChild(getElement(), captionText,
  262. getInsertPosition(InsertPosition.CAPTION));
  263. }
  264. // Update caption text
  265. // A text forces the caption to be above the component.
  266. placedAfterComponent = false;
  267. if (caption.trim().equals("")) {
  268. // This is required to ensure that the caption uses space in all
  269. // browsers when it is set to the empty string. If there is an
  270. // icon, error indicator or required indicator they will ensure
  271. // that space is reserved.
  272. if (!hasIcon && !showError) {
  273. captionText.setInnerHTML("&nbsp;");
  274. }
  275. } else {
  276. DOM.setInnerText(captionText, caption);
  277. }
  278. } else if (captionText != null) {
  279. // Remove existing
  280. DOM.removeChild(getElement(), captionText);
  281. captionText = null;
  282. }
  283. if (showError) {
  284. if (errorIndicatorElement == null) {
  285. errorIndicatorElement = DOM.createDiv();
  286. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  287. DOM.setElementProperty(errorIndicatorElement, "className",
  288. "v-errorindicator");
  289. DOM.insertChild(getElement(), errorIndicatorElement,
  290. getInsertPosition(InsertPosition.ERROR));
  291. }
  292. } else if (errorIndicatorElement != null) {
  293. // Remove existing
  294. getElement().removeChild(errorIndicatorElement);
  295. errorIndicatorElement = null;
  296. }
  297. return (wasPlacedAfterComponent != placedAfterComponent);
  298. }
  299. @Override
  300. public void onBrowserEvent(Event event) {
  301. super.onBrowserEvent(event);
  302. final Element target = DOM.eventGetTarget(event);
  303. if (client != null && owner != null && target != getElement()) {
  304. client.handleTooltipEvent(event, owner);
  305. }
  306. if (DOM.eventGetType(event) == Event.ONLOAD
  307. && icon.getElement() == target) {
  308. icon.setWidth("");
  309. icon.setHeight("");
  310. // if max width defined, recalculate
  311. if (maxWidth != -1) {
  312. setMaxWidth(maxWidth);
  313. } else {
  314. String width = getElement().getStyle().getProperty("width");
  315. if (width != null && !width.equals("")) {
  316. setWidth(getRequiredWidth() + "px");
  317. }
  318. }
  319. /*
  320. * The size of the icon might affect the size of the component so we
  321. * must report the size change to the parent TODO consider moving
  322. * the responsibility of reacting to ONLOAD from VCaption to layouts
  323. */
  324. if (owner != null) {
  325. Util.notifyParentOfSizeChange(owner.getWidget(), true);
  326. } else {
  327. VConsole.log("Warning: Icon load event was not propagated because VCaption owner is unknown.");
  328. }
  329. }
  330. }
  331. public static boolean isNeeded(UIDL uidl, ComponentState state) {
  332. if (state != null) {
  333. if (state.getCaption() != null) {
  334. return true;
  335. }
  336. } else {
  337. // TODO fallback for cases where the caption has no owner (Tabsheet,
  338. // Accordion)
  339. if (uidl.getStringAttribute(TabsheetBaseConnector.ATTRIBUTE_TAB_CAPTION) != null) {
  340. return true;
  341. }
  342. }
  343. if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_ERROR)) {
  344. return true;
  345. }
  346. if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_ICON)) {
  347. return true;
  348. }
  349. if (uidl.hasAttribute(AbstractComponentConnector.ATTRIBUTE_REQUIRED)) {
  350. return true;
  351. }
  352. return false;
  353. }
  354. /**
  355. * Returns Paintable for which this Caption belongs to.
  356. *
  357. * @return owner Widget
  358. */
  359. public ComponentConnector getOwner() {
  360. return owner;
  361. }
  362. public boolean shouldBePlacedAfterComponent() {
  363. return placedAfterComponent;
  364. }
  365. public int getRenderedWidth() {
  366. int width = 0;
  367. if (icon != null) {
  368. width += Util.getRequiredWidth(icon.getElement());
  369. }
  370. if (captionText != null) {
  371. width += Util.getRequiredWidth(captionText);
  372. }
  373. if (requiredFieldIndicator != null) {
  374. width += Util.getRequiredWidth(requiredFieldIndicator);
  375. }
  376. if (errorIndicatorElement != null) {
  377. width += Util.getRequiredWidth(errorIndicatorElement);
  378. }
  379. return width;
  380. }
  381. public int getRequiredWidth() {
  382. int width = 0;
  383. if (icon != null) {
  384. width += Util.getRequiredWidth(icon.getElement());
  385. }
  386. if (captionText != null) {
  387. int textWidth = captionText.getScrollWidth();
  388. if (BrowserInfo.get().isFirefox()) {
  389. /*
  390. * In Firefox3 the caption might require more space than the
  391. * scrollWidth returns as scrollWidth is rounded down.
  392. */
  393. int requiredWidth = Util.getRequiredWidth(captionText);
  394. if (requiredWidth > textWidth) {
  395. textWidth = requiredWidth;
  396. }
  397. }
  398. width += textWidth;
  399. }
  400. if (requiredFieldIndicator != null) {
  401. width += Util.getRequiredWidth(requiredFieldIndicator);
  402. }
  403. if (errorIndicatorElement != null) {
  404. width += Util.getRequiredWidth(errorIndicatorElement);
  405. }
  406. return width;
  407. }
  408. public int getHeight() {
  409. int height = 0;
  410. int h;
  411. if (icon != null) {
  412. h = Util.getRequiredHeight(icon.getElement());
  413. if (h > height) {
  414. height = h;
  415. }
  416. }
  417. if (captionText != null) {
  418. h = Util.getRequiredHeight(captionText);
  419. if (h > height) {
  420. height = h;
  421. }
  422. }
  423. if (requiredFieldIndicator != null) {
  424. h = Util.getRequiredHeight(requiredFieldIndicator);
  425. if (h > height) {
  426. height = h;
  427. }
  428. }
  429. if (errorIndicatorElement != null) {
  430. h = Util.getRequiredHeight(errorIndicatorElement);
  431. if (h > height) {
  432. height = h;
  433. }
  434. }
  435. return height;
  436. }
  437. public void setAlignment(String alignment) {
  438. DOM.setStyleAttribute(getElement(), "textAlign", alignment);
  439. }
  440. public void setMaxWidth(int maxWidth) {
  441. this.maxWidth = maxWidth;
  442. DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  443. if (icon != null) {
  444. DOM.setStyleAttribute(icon.getElement(), "width", "");
  445. }
  446. if (captionText != null) {
  447. DOM.setStyleAttribute(captionText, "width", "");
  448. }
  449. int requiredWidth = getRequiredWidth();
  450. /*
  451. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  452. * maxWidth + ", requiredWidth: " + requiredWidth);
  453. */
  454. if (requiredWidth > maxWidth) {
  455. // Needs to truncate and clip
  456. int availableWidth = maxWidth;
  457. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  458. if (requiredFieldIndicator != null) {
  459. availableWidth -= Util.getRequiredWidth(requiredFieldIndicator);
  460. }
  461. if (errorIndicatorElement != null) {
  462. availableWidth -= Util.getRequiredWidth(errorIndicatorElement);
  463. }
  464. if (availableWidth < 0) {
  465. availableWidth = 0;
  466. }
  467. if (icon != null) {
  468. int iconRequiredWidth = Util
  469. .getRequiredWidth(icon.getElement());
  470. if (availableWidth > iconRequiredWidth) {
  471. availableWidth -= iconRequiredWidth;
  472. } else {
  473. DOM.setStyleAttribute(icon.getElement(), "width",
  474. availableWidth + "px");
  475. availableWidth = 0;
  476. }
  477. }
  478. if (captionText != null) {
  479. int captionWidth = Util.getRequiredWidth(captionText);
  480. if (availableWidth > captionWidth) {
  481. availableWidth -= captionWidth;
  482. } else {
  483. DOM.setStyleAttribute(captionText, "width", availableWidth
  484. + "px");
  485. availableWidth = 0;
  486. }
  487. }
  488. }
  489. }
  490. protected Element getTextElement() {
  491. return captionText;
  492. }
  493. public static String getCaptionOwnerPid(Element e) {
  494. return getOwnerPid(e);
  495. }
  496. private native static void setOwnerPid(Element el, String pid)
  497. /*-{
  498. el.vOwnerPid = pid;
  499. }-*/;
  500. public native static String getOwnerPid(Element el)
  501. /*-{
  502. return el.vOwnerPid;
  503. }-*/;
  504. }