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

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