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

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