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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 VPaintableWidget 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(VPaintableWidget component, ApplicationConnection client) {
  36. super();
  37. this.client = client;
  38. owner = component;
  39. if (client != null && owner != null) {
  40. setOwnerPid(getElement(), VPaintableMap.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.getWidgetForPaintable(),
  215. true);
  216. } else {
  217. VConsole.log("Warning: Icon load event was not propagated because VCaption owner is unknown.");
  218. }
  219. }
  220. }
  221. public static boolean isNeeded(UIDL uidl) {
  222. if (uidl.getStringAttribute(ATTRIBUTE_CAPTION) != null) {
  223. return true;
  224. }
  225. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  226. return true;
  227. }
  228. if (uidl.hasAttribute(ATTRIBUTE_ICON)) {
  229. return true;
  230. }
  231. if (uidl.hasAttribute(ATTRIBUTE_REQUIRED)) {
  232. return true;
  233. }
  234. return false;
  235. }
  236. /**
  237. * Returns Paintable for which this Caption belongs to.
  238. *
  239. * @return owner Widget
  240. */
  241. public VPaintableWidget getOwner() {
  242. return owner;
  243. }
  244. public boolean shouldBePlacedAfterComponent() {
  245. return placedAfterComponent;
  246. }
  247. public int getRenderedWidth() {
  248. int width = 0;
  249. if (icon != null) {
  250. width += Util.getRequiredWidth(icon.getElement());
  251. }
  252. if (captionText != null) {
  253. width += Util.getRequiredWidth(captionText);
  254. }
  255. if (requiredFieldIndicator != null) {
  256. width += Util.getRequiredWidth(requiredFieldIndicator);
  257. }
  258. if (errorIndicatorElement != null) {
  259. width += Util.getRequiredWidth(errorIndicatorElement);
  260. }
  261. return width;
  262. }
  263. public int getRequiredWidth() {
  264. int width = 0;
  265. if (icon != null) {
  266. width += Util.getRequiredWidth(icon.getElement());
  267. }
  268. if (captionText != null) {
  269. int textWidth = captionText.getScrollWidth();
  270. if (BrowserInfo.get().isFirefox()) {
  271. /*
  272. * In Firefox3 the caption might require more space than the
  273. * scrollWidth returns as scrollWidth is rounded down.
  274. */
  275. int requiredWidth = Util.getRequiredWidth(captionText);
  276. if (requiredWidth > textWidth) {
  277. textWidth = requiredWidth;
  278. }
  279. }
  280. width += textWidth;
  281. }
  282. if (requiredFieldIndicator != null) {
  283. width += Util.getRequiredWidth(requiredFieldIndicator);
  284. }
  285. if (errorIndicatorElement != null) {
  286. width += Util.getRequiredWidth(errorIndicatorElement);
  287. }
  288. return width;
  289. }
  290. public int getHeight() {
  291. int height = 0;
  292. int h;
  293. if (icon != null) {
  294. h = Util.getRequiredHeight(icon.getElement());
  295. if (h > height) {
  296. height = h;
  297. }
  298. }
  299. if (captionText != null) {
  300. h = Util.getRequiredHeight(captionText);
  301. if (h > height) {
  302. height = h;
  303. }
  304. }
  305. if (requiredFieldIndicator != null) {
  306. h = Util.getRequiredHeight(requiredFieldIndicator);
  307. if (h > height) {
  308. height = h;
  309. }
  310. }
  311. if (errorIndicatorElement != null) {
  312. h = Util.getRequiredHeight(errorIndicatorElement);
  313. if (h > height) {
  314. height = h;
  315. }
  316. }
  317. return height;
  318. }
  319. public void setAlignment(String alignment) {
  320. DOM.setStyleAttribute(getElement(), "textAlign", alignment);
  321. }
  322. public void setMaxWidth(int maxWidth) {
  323. this.maxWidth = maxWidth;
  324. DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  325. if (icon != null) {
  326. DOM.setStyleAttribute(icon.getElement(), "width", "");
  327. }
  328. if (captionText != null) {
  329. DOM.setStyleAttribute(captionText, "width", "");
  330. }
  331. int requiredWidth = getRequiredWidth();
  332. /*
  333. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  334. * maxWidth + ", requiredWidth: " + requiredWidth);
  335. */
  336. if (requiredWidth > maxWidth) {
  337. // Needs to truncate and clip
  338. int availableWidth = maxWidth;
  339. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  340. if (requiredFieldIndicator != null) {
  341. availableWidth -= Util.getRequiredWidth(requiredFieldIndicator);
  342. }
  343. if (errorIndicatorElement != null) {
  344. availableWidth -= Util.getRequiredWidth(errorIndicatorElement);
  345. }
  346. if (availableWidth < 0) {
  347. availableWidth = 0;
  348. }
  349. if (icon != null) {
  350. int iconRequiredWidth = Util
  351. .getRequiredWidth(icon.getElement());
  352. if (availableWidth > iconRequiredWidth) {
  353. availableWidth -= iconRequiredWidth;
  354. } else {
  355. DOM.setStyleAttribute(icon.getElement(), "width",
  356. availableWidth + "px");
  357. availableWidth = 0;
  358. }
  359. }
  360. if (captionText != null) {
  361. int captionWidth = Util.getRequiredWidth(captionText);
  362. if (availableWidth > captionWidth) {
  363. availableWidth -= captionWidth;
  364. } else {
  365. DOM.setStyleAttribute(captionText, "width", availableWidth
  366. + "px");
  367. availableWidth = 0;
  368. }
  369. }
  370. }
  371. }
  372. protected Element getTextElement() {
  373. return captionText;
  374. }
  375. public static String getCaptionOwnerPid(Element e) {
  376. return getOwnerPid(e);
  377. }
  378. private native static void setOwnerPid(Element el, String pid)
  379. /*-{
  380. el.vOwnerPid = pid;
  381. }-*/;
  382. public native static String getOwnerPid(Element el)
  383. /*-{
  384. return el.vOwnerPid;
  385. }-*/;
  386. }