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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. public class VCaption extends HTML {
  11. public static final String CLASSNAME = "v-caption";
  12. private final Paintable owner;
  13. private Element errorIndicatorElement;
  14. private Element requiredFieldIndicator;
  15. private Icon icon;
  16. private Element captionText;
  17. private Element clearElement;
  18. private final ApplicationConnection client;
  19. private boolean placedAfterComponent = false;
  20. private int maxWidth = -1;
  21. protected static final String ATTRIBUTE_ICON = "icon";
  22. protected static final String ATTRIBUTE_CAPTION = "caption";
  23. protected static final String ATTRIBUTE_DESCRIPTION = "description";
  24. protected static final String ATTRIBUTE_REQUIRED = "required";
  25. protected static final String ATTRIBUTE_ERROR = "error";
  26. protected static final String ATTRIBUTE_HIDEERRORS = "hideErrors";
  27. private static final String CLASSNAME_CLEAR = CLASSNAME + "-clearelem";
  28. /**
  29. *
  30. * @param component
  31. * optional owner of caption. If not set, getOwner will return
  32. * null
  33. * @param client
  34. */
  35. public VCaption(Paintable component, ApplicationConnection client) {
  36. super();
  37. this.client = client;
  38. owner = component;
  39. if (client != null && owner != null) {
  40. setOwnerPid(getElement(), PaintableMap.get(client).getPid(owner));
  41. }
  42. setStyleName(CLASSNAME);
  43. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  44. }
  45. /**
  46. * Updates the caption from UIDL.
  47. *
  48. * @param uidl
  49. * @return true if the position where the caption should be placed has
  50. * changed
  51. */
  52. public boolean updateCaption(UIDL uidl) {
  53. setVisible(!uidl.getBooleanAttribute("invisible"));
  54. boolean wasPlacedAfterComponent = placedAfterComponent;
  55. // Caption is placed after component unless there is some part which
  56. // moves it above.
  57. placedAfterComponent = true;
  58. String style = CLASSNAME;
  59. if (uidl.hasAttribute("style")) {
  60. final String[] styles = uidl.getStringAttribute("style").split(" ");
  61. for (int i = 0; i < styles.length; i++) {
  62. style += " " + CLASSNAME + "-" + styles[i];
  63. }
  64. }
  65. if (uidl.hasAttribute("disabled")) {
  66. style += " " + ApplicationConnection.DISABLED_CLASSNAME;
  67. }
  68. setStyleName(style);
  69. boolean hasIcon = uidl.hasAttribute(ATTRIBUTE_ICON);
  70. boolean hasText = uidl.hasAttribute(ATTRIBUTE_CAPTION);
  71. boolean hasDescription = uidl.hasAttribute(ATTRIBUTE_DESCRIPTION);
  72. boolean showRequired = uidl.getBooleanAttribute(ATTRIBUTE_REQUIRED);
  73. boolean showError = uidl.hasAttribute(ATTRIBUTE_ERROR)
  74. && !uidl.getBooleanAttribute(ATTRIBUTE_HIDEERRORS);
  75. if (hasIcon) {
  76. if (icon == null) {
  77. icon = new Icon(client);
  78. icon.setWidth("0");
  79. icon.setHeight("0");
  80. DOM.insertChild(getElement(), icon.getElement(),
  81. getInsertPosition(ATTRIBUTE_ICON));
  82. }
  83. // Icon forces the caption to be above the component
  84. placedAfterComponent = false;
  85. icon.setUri(uidl.getStringAttribute(ATTRIBUTE_ICON));
  86. } else if (icon != null) {
  87. // Remove existing
  88. DOM.removeChild(getElement(), icon.getElement());
  89. icon = null;
  90. }
  91. if (hasText) {
  92. // A caption text should be shown if the attribute is set
  93. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  94. // avoid ending up here.
  95. if (captionText == null) {
  96. captionText = DOM.createDiv();
  97. captionText.setClassName("v-captiontext");
  98. DOM.insertChild(getElement(), captionText,
  99. getInsertPosition(ATTRIBUTE_CAPTION));
  100. }
  101. // Update caption text
  102. String c = uidl.getStringAttribute(ATTRIBUTE_CAPTION);
  103. // A text forces the caption to be above the component.
  104. placedAfterComponent = false;
  105. if (c == null || c.trim().equals("")) {
  106. // Not sure if c even can be null. Should not.
  107. // This is required to ensure that the caption uses space in all
  108. // browsers when it is set to the empty string. If there is an
  109. // icon, error indicator or required indicator they will ensure
  110. // that space is reserved.
  111. if (!hasIcon && !showRequired && !showError) {
  112. captionText.setInnerHTML("&nbsp;");
  113. }
  114. } else {
  115. DOM.setInnerText(captionText, c);
  116. }
  117. } else if (captionText != null) {
  118. // Remove existing
  119. DOM.removeChild(getElement(), captionText);
  120. captionText = null;
  121. }
  122. if (hasDescription) {
  123. if (captionText != null) {
  124. addStyleDependentName("hasdescription");
  125. } else {
  126. removeStyleDependentName("hasdescription");
  127. }
  128. }
  129. if (showRequired) {
  130. if (requiredFieldIndicator == null) {
  131. requiredFieldIndicator = DOM.createDiv();
  132. requiredFieldIndicator
  133. .setClassName("v-required-field-indicator");
  134. DOM.setInnerText(requiredFieldIndicator, "*");
  135. DOM.insertChild(getElement(), requiredFieldIndicator,
  136. getInsertPosition(ATTRIBUTE_REQUIRED));
  137. }
  138. } else if (requiredFieldIndicator != null) {
  139. // Remove existing
  140. DOM.removeChild(getElement(), requiredFieldIndicator);
  141. requiredFieldIndicator = null;
  142. }
  143. if (showError) {
  144. if (errorIndicatorElement == null) {
  145. errorIndicatorElement = DOM.createDiv();
  146. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  147. DOM.setElementProperty(errorIndicatorElement, "className",
  148. "v-errorindicator");
  149. DOM.insertChild(getElement(), errorIndicatorElement,
  150. getInsertPosition(ATTRIBUTE_ERROR));
  151. }
  152. } else if (errorIndicatorElement != null) {
  153. // Remove existing
  154. getElement().removeChild(errorIndicatorElement);
  155. errorIndicatorElement = null;
  156. }
  157. if (clearElement == null) {
  158. clearElement = DOM.createDiv();
  159. clearElement.setClassName(CLASSNAME_CLEAR);
  160. getElement().appendChild(clearElement);
  161. }
  162. return (wasPlacedAfterComponent != placedAfterComponent);
  163. }
  164. private int getInsertPosition(String element) {
  165. int pos = 0;
  166. if (element.equals(ATTRIBUTE_ICON)) {
  167. return pos;
  168. }
  169. if (icon != null) {
  170. pos++;
  171. }
  172. if (element.equals(ATTRIBUTE_CAPTION)) {
  173. return pos;
  174. }
  175. if (captionText != null) {
  176. pos++;
  177. }
  178. if (element.equals(ATTRIBUTE_REQUIRED)) {
  179. return pos;
  180. }
  181. if (requiredFieldIndicator != null) {
  182. pos++;
  183. }
  184. // if (element.equals(ATTRIBUTE_ERROR)) {
  185. // }
  186. return pos;
  187. }
  188. @Override
  189. public void onBrowserEvent(Event event) {
  190. super.onBrowserEvent(event);
  191. final Element target = DOM.eventGetTarget(event);
  192. if (client != null && owner != null && target != getElement()) {
  193. client.handleTooltipEvent(event, owner);
  194. }
  195. if (DOM.eventGetType(event) == Event.ONLOAD
  196. && icon.getElement() == target) {
  197. icon.setWidth("");
  198. icon.setHeight("");
  199. // if max width defined, recalculate
  200. if (maxWidth != -1) {
  201. setMaxWidth(maxWidth);
  202. } else {
  203. String width = getElement().getStyle().getProperty("width");
  204. if (width != null && !width.equals("")) {
  205. setWidth(getRequiredWidth() + "px");
  206. }
  207. }
  208. /*
  209. * The size of the icon might affect the size of the component so we
  210. * must report the size change to the parent TODO consider moving
  211. * the responsibility of reacting to ONLOAD from VCaption to layouts
  212. */
  213. if (owner != null) {
  214. Util.notifyParentOfSizeChange(owner, true);
  215. } else {
  216. VConsole.log("Warning: Icon load event was not propagated because VCaption owner is unknown.");
  217. }
  218. }
  219. }
  220. public static boolean isNeeded(UIDL uidl) {
  221. if (uidl.getStringAttribute(ATTRIBUTE_CAPTION) != null) {
  222. return true;
  223. }
  224. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  225. return true;
  226. }
  227. if (uidl.hasAttribute(ATTRIBUTE_ICON)) {
  228. return true;
  229. }
  230. if (uidl.hasAttribute(ATTRIBUTE_REQUIRED)) {
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * Returns Paintable for which this Caption belongs to.
  237. *
  238. * @return owner Widget
  239. */
  240. public Paintable getOwner() {
  241. return owner;
  242. }
  243. public boolean shouldBePlacedAfterComponent() {
  244. return placedAfterComponent;
  245. }
  246. public int getRenderedWidth() {
  247. int width = 0;
  248. if (icon != null) {
  249. width += Util.getRequiredWidth(icon.getElement());
  250. }
  251. if (captionText != null) {
  252. width += Util.getRequiredWidth(captionText);
  253. }
  254. if (requiredFieldIndicator != null) {
  255. width += Util.getRequiredWidth(requiredFieldIndicator);
  256. }
  257. if (errorIndicatorElement != null) {
  258. width += Util.getRequiredWidth(errorIndicatorElement);
  259. }
  260. return width;
  261. }
  262. public int getRequiredWidth() {
  263. int width = 0;
  264. if (icon != null) {
  265. width += Util.getRequiredWidth(icon.getElement());
  266. }
  267. if (captionText != null) {
  268. int textWidth = captionText.getScrollWidth();
  269. if (BrowserInfo.get().isFirefox()) {
  270. /*
  271. * In Firefox3 the caption might require more space than the
  272. * scrollWidth returns as scrollWidth is rounded down.
  273. */
  274. int requiredWidth = Util.getRequiredWidth(captionText);
  275. if (requiredWidth > textWidth) {
  276. textWidth = requiredWidth;
  277. }
  278. }
  279. width += textWidth;
  280. }
  281. if (requiredFieldIndicator != null) {
  282. width += Util.getRequiredWidth(requiredFieldIndicator);
  283. }
  284. if (errorIndicatorElement != null) {
  285. width += Util.getRequiredWidth(errorIndicatorElement);
  286. }
  287. return width;
  288. }
  289. public int getHeight() {
  290. int height = 0;
  291. int h;
  292. if (icon != null) {
  293. h = Util.getRequiredHeight(icon.getElement());
  294. if (h > height) {
  295. height = h;
  296. }
  297. }
  298. if (captionText != null) {
  299. h = Util.getRequiredHeight(captionText);
  300. if (h > height) {
  301. height = h;
  302. }
  303. }
  304. if (requiredFieldIndicator != null) {
  305. h = Util.getRequiredHeight(requiredFieldIndicator);
  306. if (h > height) {
  307. height = h;
  308. }
  309. }
  310. if (errorIndicatorElement != null) {
  311. h = Util.getRequiredHeight(errorIndicatorElement);
  312. if (h > height) {
  313. height = h;
  314. }
  315. }
  316. return height;
  317. }
  318. public void setAlignment(String alignment) {
  319. DOM.setStyleAttribute(getElement(), "textAlign", alignment);
  320. }
  321. public void setMaxWidth(int maxWidth) {
  322. this.maxWidth = maxWidth;
  323. DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  324. if (icon != null) {
  325. DOM.setStyleAttribute(icon.getElement(), "width", "");
  326. }
  327. if (captionText != null) {
  328. DOM.setStyleAttribute(captionText, "width", "");
  329. }
  330. int requiredWidth = getRequiredWidth();
  331. /*
  332. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  333. * maxWidth + ", requiredWidth: " + requiredWidth);
  334. */
  335. if (requiredWidth > maxWidth) {
  336. // Needs to truncate and clip
  337. int availableWidth = maxWidth;
  338. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  339. if (requiredFieldIndicator != null) {
  340. availableWidth -= Util.getRequiredWidth(requiredFieldIndicator);
  341. }
  342. if (errorIndicatorElement != null) {
  343. availableWidth -= Util.getRequiredWidth(errorIndicatorElement);
  344. }
  345. if (availableWidth < 0) {
  346. availableWidth = 0;
  347. }
  348. if (icon != null) {
  349. int iconRequiredWidth = Util
  350. .getRequiredWidth(icon.getElement());
  351. if (availableWidth > iconRequiredWidth) {
  352. availableWidth -= iconRequiredWidth;
  353. } else {
  354. DOM.setStyleAttribute(icon.getElement(), "width",
  355. availableWidth + "px");
  356. availableWidth = 0;
  357. }
  358. }
  359. if (captionText != null) {
  360. int captionWidth = Util.getRequiredWidth(captionText);
  361. if (availableWidth > captionWidth) {
  362. availableWidth -= captionWidth;
  363. } else {
  364. DOM.setStyleAttribute(captionText, "width", availableWidth
  365. + "px");
  366. availableWidth = 0;
  367. }
  368. }
  369. }
  370. }
  371. protected Element getTextElement() {
  372. return captionText;
  373. }
  374. public static String getCaptionOwnerPid(Element e) {
  375. return getOwnerPid(e);
  376. }
  377. private native static void setOwnerPid(Element el, String pid)
  378. /*-{
  379. el.vOwnerPid = pid;
  380. }-*/;
  381. public native static String getOwnerPid(Element el)
  382. /*-{
  383. return el.vOwnerPid;
  384. }-*/;
  385. }