選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VCaption.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  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 boolean iconOnloadHandled = 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. /**
  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(), 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 += " " + "v-disabled";
  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. iconOnloadHandled = false;
  86. icon.setUri(uidl.getStringAttribute(ATTRIBUTE_ICON));
  87. } else if (icon != null) {
  88. // Remove existing
  89. DOM.removeChild(getElement(), icon.getElement());
  90. icon = null;
  91. }
  92. if (hasText) {
  93. // A caption text should be shown if the attribute is set
  94. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  95. // avoid ending up here.
  96. if (captionText == null) {
  97. captionText = DOM.createDiv();
  98. captionText.setClassName("v-captiontext");
  99. DOM.insertChild(getElement(), captionText,
  100. getInsertPosition(ATTRIBUTE_CAPTION));
  101. }
  102. // Update caption text
  103. String c = uidl.getStringAttribute(ATTRIBUTE_CAPTION);
  104. // A text forces the caption to be above the component.
  105. placedAfterComponent = false;
  106. if (c == null || c.trim().equals("")) {
  107. // Not sure if c even can be null. Should not.
  108. // This is required to ensure that the caption uses space in all
  109. // browsers when it is set to the empty string. If there is an
  110. // icon, error indicator or required indicator they will ensure
  111. // that space is reserved.
  112. if (!hasIcon && !showRequired && !showError) {
  113. captionText.setInnerHTML("&nbsp;");
  114. }
  115. } else {
  116. DOM.setInnerText(captionText, c);
  117. }
  118. } else if (captionText != null) {
  119. // Remove existing
  120. DOM.removeChild(getElement(), captionText);
  121. captionText = null;
  122. }
  123. if (hasDescription) {
  124. if (captionText != null) {
  125. addStyleDependentName("hasdescription");
  126. } else {
  127. removeStyleDependentName("hasdescription");
  128. }
  129. }
  130. if (showRequired) {
  131. if (requiredFieldIndicator == null) {
  132. requiredFieldIndicator = DOM.createDiv();
  133. requiredFieldIndicator
  134. .setClassName("v-required-field-indicator");
  135. DOM.setInnerText(requiredFieldIndicator, "*");
  136. DOM.insertChild(getElement(), requiredFieldIndicator,
  137. getInsertPosition(ATTRIBUTE_REQUIRED));
  138. }
  139. } else if (requiredFieldIndicator != null) {
  140. // Remove existing
  141. DOM.removeChild(getElement(), requiredFieldIndicator);
  142. requiredFieldIndicator = null;
  143. }
  144. if (showError) {
  145. if (errorIndicatorElement == null) {
  146. errorIndicatorElement = DOM.createDiv();
  147. DOM.setInnerHTML(errorIndicatorElement, "&nbsp;");
  148. DOM.setElementProperty(errorIndicatorElement, "className",
  149. "v-errorindicator");
  150. DOM.insertChild(getElement(), errorIndicatorElement,
  151. getInsertPosition(ATTRIBUTE_ERROR));
  152. }
  153. } else if (errorIndicatorElement != null) {
  154. // Remove existing
  155. DOM.removeChild(getElement(), errorIndicatorElement);
  156. errorIndicatorElement = null;
  157. }
  158. if (clearElement == null) {
  159. clearElement = DOM.createDiv();
  160. DOM.setStyleAttribute(clearElement, "clear", "both");
  161. DOM.setStyleAttribute(clearElement, "width", "0");
  162. DOM.setStyleAttribute(clearElement, "height", "0");
  163. DOM.setStyleAttribute(clearElement, "overflow", "hidden");
  164. DOM.appendChild(getElement(), clearElement);
  165. }
  166. return (wasPlacedAfterComponent != placedAfterComponent);
  167. }
  168. private int getInsertPosition(String element) {
  169. int pos = 0;
  170. if (element.equals(ATTRIBUTE_ICON)) {
  171. return pos;
  172. }
  173. if (icon != null) {
  174. pos++;
  175. }
  176. if (element.equals(ATTRIBUTE_CAPTION)) {
  177. return pos;
  178. }
  179. if (captionText != null) {
  180. pos++;
  181. }
  182. if (element.equals(ATTRIBUTE_REQUIRED)) {
  183. return pos;
  184. }
  185. if (requiredFieldIndicator != null) {
  186. pos++;
  187. }
  188. // if (element.equals(ATTRIBUTE_ERROR)) {
  189. // }
  190. return pos;
  191. }
  192. @Override
  193. public void onBrowserEvent(Event event) {
  194. super.onBrowserEvent(event);
  195. final Element target = DOM.eventGetTarget(event);
  196. if (client != null && owner != null && target != getElement()) {
  197. client.handleTooltipEvent(event, owner);
  198. }
  199. if (DOM.eventGetType(event) == Event.ONLOAD
  200. && icon.getElement() == target && !iconOnloadHandled) {
  201. icon.setWidth("");
  202. icon.setHeight("");
  203. /*
  204. * IE6 pngFix causes two onload events to be fired and we want to
  205. * react only to the first one
  206. */
  207. iconOnloadHandled = true;
  208. // if max width defined, recalculate
  209. if (maxWidth != -1) {
  210. setMaxWidth(maxWidth);
  211. } else {
  212. String width = getElement().getStyle().getProperty("width");
  213. if (width != null && !width.equals("")) {
  214. setWidth(getRequiredWidth() + "px");
  215. }
  216. }
  217. /*
  218. * The size of the icon might affect the size of the component so we
  219. * must report the size change to the parent TODO consider moving
  220. * the responsibility of reacting to ONLOAD from VCaption to layouts
  221. */
  222. if (owner != null) {
  223. Util.notifyParentOfSizeChange(owner, true);
  224. } else {
  225. ApplicationConnection
  226. .getConsole()
  227. .log("Warning: Icon load event was not propagated because VCaption owner is unknown.");
  228. }
  229. }
  230. }
  231. public static boolean isNeeded(UIDL uidl) {
  232. if (uidl.getStringAttribute(ATTRIBUTE_CAPTION) != null) {
  233. return true;
  234. }
  235. if (uidl.hasAttribute(ATTRIBUTE_ERROR)) {
  236. return true;
  237. }
  238. if (uidl.hasAttribute(ATTRIBUTE_ICON)) {
  239. return true;
  240. }
  241. if (uidl.hasAttribute(ATTRIBUTE_REQUIRED)) {
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. * Returns Paintable for which this Caption belongs to.
  248. *
  249. * @return owner Widget
  250. */
  251. public Paintable getOwner() {
  252. return owner;
  253. }
  254. public boolean shouldBePlacedAfterComponent() {
  255. return placedAfterComponent;
  256. }
  257. public int getRenderedWidth() {
  258. int width = 0;
  259. if (icon != null) {
  260. width += Util.getRequiredWidth(icon.getElement());
  261. }
  262. if (captionText != null) {
  263. width += Util.getRequiredWidth(captionText);
  264. }
  265. if (requiredFieldIndicator != null) {
  266. width += Util.getRequiredWidth(requiredFieldIndicator);
  267. }
  268. if (errorIndicatorElement != null) {
  269. width += Util.getRequiredWidth(errorIndicatorElement);
  270. }
  271. return width;
  272. }
  273. public int getRequiredWidth() {
  274. int width = 0;
  275. if (icon != null) {
  276. width += Util.getRequiredWidth(icon.getElement());
  277. }
  278. if (captionText != null) {
  279. int textWidth = captionText.getScrollWidth();
  280. if (BrowserInfo.get().isFF3()) {
  281. /*
  282. * In Firefox3 the caption might require more space than the
  283. * scrollWidth returns as scrollWidth is rounded down.
  284. */
  285. int requiredWidth = Util.getRequiredWidth(captionText);
  286. if (requiredWidth > textWidth) {
  287. textWidth = requiredWidth;
  288. }
  289. }
  290. width += textWidth;
  291. }
  292. if (requiredFieldIndicator != null) {
  293. width += Util.getRequiredWidth(requiredFieldIndicator);
  294. }
  295. if (errorIndicatorElement != null) {
  296. width += Util.getRequiredWidth(errorIndicatorElement);
  297. }
  298. return width;
  299. }
  300. public int getHeight() {
  301. int height = 0;
  302. int h;
  303. if (icon != null) {
  304. h = Util.getRequiredHeight(icon.getElement());
  305. if (h > height) {
  306. height = h;
  307. }
  308. }
  309. if (captionText != null) {
  310. h = Util.getRequiredHeight(captionText);
  311. if (h > height) {
  312. height = h;
  313. }
  314. }
  315. if (requiredFieldIndicator != null) {
  316. h = Util.getRequiredHeight(requiredFieldIndicator);
  317. if (h > height) {
  318. height = h;
  319. }
  320. }
  321. if (errorIndicatorElement != null) {
  322. h = Util.getRequiredHeight(errorIndicatorElement);
  323. if (h > height) {
  324. height = h;
  325. }
  326. }
  327. return height;
  328. }
  329. public void setAlignment(String alignment) {
  330. DOM.setStyleAttribute(getElement(), "textAlign", alignment);
  331. }
  332. public void setMaxWidth(int maxWidth) {
  333. this.maxWidth = maxWidth;
  334. DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  335. if (icon != null) {
  336. DOM.setStyleAttribute(icon.getElement(), "width", "");
  337. }
  338. if (captionText != null) {
  339. DOM.setStyleAttribute(captionText, "width", "");
  340. }
  341. int requiredWidth = getRequiredWidth();
  342. /*
  343. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  344. * maxWidth + ", requiredWidth: " + requiredWidth);
  345. */
  346. if (requiredWidth > maxWidth) {
  347. // Needs to truncate and clip
  348. int availableWidth = maxWidth;
  349. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  350. if (requiredFieldIndicator != null) {
  351. availableWidth -= Util.getRequiredWidth(requiredFieldIndicator);
  352. }
  353. if (errorIndicatorElement != null) {
  354. availableWidth -= Util.getRequiredWidth(errorIndicatorElement);
  355. }
  356. if (availableWidth < 0) {
  357. availableWidth = 0;
  358. }
  359. if (icon != null) {
  360. int iconRequiredWidth = Util
  361. .getRequiredWidth(icon.getElement());
  362. if (availableWidth > iconRequiredWidth) {
  363. availableWidth -= iconRequiredWidth;
  364. } else {
  365. DOM.setStyleAttribute(icon.getElement(), "width",
  366. availableWidth + "px");
  367. availableWidth = 0;
  368. }
  369. }
  370. if (captionText != null) {
  371. int captionWidth = Util.getRequiredWidth(captionText);
  372. if (availableWidth > captionWidth) {
  373. availableWidth -= captionWidth;
  374. } else {
  375. DOM.setStyleAttribute(captionText, "width", availableWidth
  376. + "px");
  377. availableWidth = 0;
  378. }
  379. }
  380. }
  381. }
  382. protected Element getTextElement() {
  383. return captionText;
  384. }
  385. public static String getCaptionOwnerPid(Element e) {
  386. return getOwnerPid(e);
  387. }
  388. private native static void setOwnerPid(Element el, String pid)
  389. /*-{
  390. el.vOwnerPid = pid;
  391. }-*/;
  392. public native static String getOwnerPid(Element el)
  393. /*-{
  394. return el.vOwnerPid;
  395. }-*/;
  396. }