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

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