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

VCaption.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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.shared.AbstractFieldState;
  10. import com.vaadin.shared.ComponentState;
  11. import com.vaadin.terminal.gwt.client.ui.AbstractFieldConnector;
  12. import com.vaadin.terminal.gwt.client.ui.Icon;
  13. public class VCaption extends HTML {
  14. public static final String CLASSNAME = "v-caption";
  15. private final ComponentConnector owner;
  16. private Element errorIndicatorElement;
  17. private Element requiredFieldIndicator;
  18. private Icon icon;
  19. private Element captionText;
  20. private final ApplicationConnection client;
  21. private boolean placedAfterComponent = false;
  22. private int maxWidth = -1;
  23. private enum InsertPosition {
  24. ICON, CAPTION, REQUIRED, ERROR
  25. }
  26. private TooltipInfo tooltipInfo = null;
  27. /**
  28. * Creates a caption that is not linked to a {@link ComponentConnector}.
  29. *
  30. * When using this constructor, {@link #getOwner()} returns null.
  31. *
  32. * @param client
  33. * ApplicationConnection
  34. * @deprecated all captions should be associated with a paintable widget and
  35. * be updated from shared state, not UIDL
  36. */
  37. @Deprecated
  38. public VCaption(ApplicationConnection client) {
  39. super();
  40. this.client = client;
  41. owner = null;
  42. setStyleName(CLASSNAME);
  43. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  44. }
  45. /**
  46. * Creates a caption for a {@link ComponentConnector}.
  47. *
  48. * @param component
  49. * owner of caption, not null
  50. * @param client
  51. * ApplicationConnection
  52. */
  53. public VCaption(ComponentConnector component, ApplicationConnection client) {
  54. super();
  55. this.client = client;
  56. owner = component;
  57. if (client != null && owner != null) {
  58. setOwnerPid(getElement(), owner.getConnectorId());
  59. }
  60. setStyleName(CLASSNAME);
  61. }
  62. /**
  63. * Updates the caption from UIDL.
  64. *
  65. * This method may only be called when the caption has an owner - otherwise,
  66. * use {@link #updateCaptionWithoutOwner(UIDL, String, boolean, boolean)}.
  67. *
  68. * @return true if the position where the caption should be placed has
  69. * changed
  70. */
  71. public boolean updateCaption() {
  72. boolean wasPlacedAfterComponent = placedAfterComponent;
  73. // Caption is placed after component unless there is some part which
  74. // moves it above.
  75. placedAfterComponent = true;
  76. String style = CLASSNAME;
  77. if (owner.getState().hasStyles()) {
  78. for (String customStyle : owner.getState().getStyles()) {
  79. style += " " + CLASSNAME + "-" + customStyle;
  80. }
  81. }
  82. if (!owner.isEnabled()) {
  83. style += " " + ApplicationConnection.DISABLED_CLASSNAME;
  84. }
  85. setStyleName(style);
  86. boolean hasIcon = owner.getState().getIcon() != null;
  87. boolean showRequired = false;
  88. boolean showError = owner.getState().getErrorMessage() != null;
  89. if (owner.getState() instanceof AbstractFieldState) {
  90. AbstractFieldState abstractFieldState = (AbstractFieldState) owner
  91. .getState();
  92. showError = showError && !abstractFieldState.isHideErrors();
  93. }
  94. if (owner instanceof AbstractFieldConnector) {
  95. showRequired = ((AbstractFieldConnector) owner).isRequired();
  96. }
  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(String caption, boolean disabled,
  205. boolean hasDescription, boolean hasError, String iconURL) {
  206. boolean wasPlacedAfterComponent = placedAfterComponent;
  207. // Caption is placed after component unless there is some part which
  208. // moves it above.
  209. placedAfterComponent = true;
  210. String style = VCaption.CLASSNAME;
  211. if (disabled) {
  212. style += " " + ApplicationConnection.DISABLED_CLASSNAME;
  213. }
  214. setStyleName(style);
  215. if (hasDescription) {
  216. if (captionText != null) {
  217. addStyleDependentName("hasdescription");
  218. } else {
  219. removeStyleDependentName("hasdescription");
  220. }
  221. }
  222. boolean hasIcon = iconURL != null;
  223. if (hasIcon) {
  224. if (icon == null) {
  225. icon = new Icon(client);
  226. icon.setWidth("0");
  227. icon.setHeight("0");
  228. DOM.insertChild(getElement(), icon.getElement(),
  229. getInsertPosition(InsertPosition.ICON));
  230. }
  231. // Icon forces the caption to be above the component
  232. placedAfterComponent = false;
  233. icon.setUri(iconURL);
  234. } else if (icon != null) {
  235. // Remove existing
  236. DOM.removeChild(getElement(), icon.getElement());
  237. icon = null;
  238. }
  239. if (caption != null) {
  240. // A caption text should be shown if the attribute is set
  241. // If the caption is null the ATTRIBUTE_CAPTION should not be set to
  242. // avoid ending up here.
  243. if (captionText == null) {
  244. captionText = DOM.createDiv();
  245. captionText.setClassName("v-captiontext");
  246. DOM.insertChild(getElement(), captionText,
  247. getInsertPosition(InsertPosition.CAPTION));
  248. }
  249. // Update caption text
  250. // A text forces the caption to be above the component.
  251. placedAfterComponent = false;
  252. if (caption.trim().equals("")) {
  253. // This is required to ensure that the caption uses space in all
  254. // browsers when it is set to the empty string. If there is an
  255. // icon, error indicator or required indicator they will ensure
  256. // that space is reserved.
  257. if (!hasIcon && !hasError) {
  258. captionText.setInnerHTML(" ");
  259. }
  260. } else {
  261. DOM.setInnerText(captionText, caption);
  262. }
  263. } else if (captionText != null) {
  264. // Remove existing
  265. DOM.removeChild(getElement(), captionText);
  266. captionText = null;
  267. }
  268. if (hasError) {
  269. if (errorIndicatorElement == null) {
  270. errorIndicatorElement = DOM.createDiv();
  271. DOM.setInnerHTML(errorIndicatorElement, " ");
  272. DOM.setElementProperty(errorIndicatorElement, "className",
  273. "v-errorindicator");
  274. DOM.insertChild(getElement(), errorIndicatorElement,
  275. getInsertPosition(InsertPosition.ERROR));
  276. }
  277. } else if (errorIndicatorElement != null) {
  278. // Remove existing
  279. getElement().removeChild(errorIndicatorElement);
  280. errorIndicatorElement = null;
  281. }
  282. return (wasPlacedAfterComponent != placedAfterComponent);
  283. }
  284. @Override
  285. public void onBrowserEvent(Event event) {
  286. super.onBrowserEvent(event);
  287. final Element target = DOM.eventGetTarget(event);
  288. if (DOM.eventGetType(event) == Event.ONLOAD
  289. && icon.getElement() == target) {
  290. icon.setWidth("");
  291. icon.setHeight("");
  292. // if max width defined, recalculate
  293. if (maxWidth != -1) {
  294. setMaxWidth(maxWidth);
  295. } else {
  296. String width = getElement().getStyle().getProperty("width");
  297. if (width != null && !width.equals("")) {
  298. setWidth(getRequiredWidth() + "px");
  299. }
  300. }
  301. /*
  302. * The size of the icon might affect the size of the component so we
  303. * must report the size change to the parent TODO consider moving
  304. * the responsibility of reacting to ONLOAD from VCaption to layouts
  305. */
  306. if (owner != null) {
  307. Util.notifyParentOfSizeChange(owner.getWidget(), true);
  308. } else {
  309. VConsole.log("Warning: Icon load event was not propagated because VCaption owner is unknown.");
  310. }
  311. }
  312. }
  313. public static boolean isNeeded(ComponentState state) {
  314. if (state.getCaption() != null) {
  315. return true;
  316. }
  317. if (state.getIcon() != null) {
  318. return true;
  319. }
  320. if (state.getErrorMessage() != null) {
  321. return true;
  322. }
  323. return false;
  324. }
  325. /**
  326. * Returns Paintable for which this Caption belongs to.
  327. *
  328. * @return owner Widget
  329. */
  330. public ComponentConnector getOwner() {
  331. return owner;
  332. }
  333. public boolean shouldBePlacedAfterComponent() {
  334. return placedAfterComponent;
  335. }
  336. public int getRenderedWidth() {
  337. int width = 0;
  338. if (icon != null) {
  339. width += Util.getRequiredWidth(icon.getElement());
  340. }
  341. if (captionText != null) {
  342. width += Util.getRequiredWidth(captionText);
  343. }
  344. if (requiredFieldIndicator != null) {
  345. width += Util.getRequiredWidth(requiredFieldIndicator);
  346. }
  347. if (errorIndicatorElement != null) {
  348. width += Util.getRequiredWidth(errorIndicatorElement);
  349. }
  350. return width;
  351. }
  352. public int getRequiredWidth() {
  353. int width = 0;
  354. if (icon != null) {
  355. width += Util.getRequiredWidth(icon.getElement());
  356. }
  357. if (captionText != null) {
  358. int textWidth = captionText.getScrollWidth();
  359. if (BrowserInfo.get().isFirefox()) {
  360. /*
  361. * In Firefox3 the caption might require more space than the
  362. * scrollWidth returns as scrollWidth is rounded down.
  363. */
  364. int requiredWidth = Util.getRequiredWidth(captionText);
  365. if (requiredWidth > textWidth) {
  366. textWidth = requiredWidth;
  367. }
  368. }
  369. width += textWidth;
  370. }
  371. if (requiredFieldIndicator != null) {
  372. width += Util.getRequiredWidth(requiredFieldIndicator);
  373. }
  374. if (errorIndicatorElement != null) {
  375. width += Util.getRequiredWidth(errorIndicatorElement);
  376. }
  377. return width;
  378. }
  379. public int getHeight() {
  380. int height = 0;
  381. int h;
  382. if (icon != null) {
  383. h = Util.getRequiredHeight(icon.getElement());
  384. if (h > height) {
  385. height = h;
  386. }
  387. }
  388. if (captionText != null) {
  389. h = Util.getRequiredHeight(captionText);
  390. if (h > height) {
  391. height = h;
  392. }
  393. }
  394. if (requiredFieldIndicator != null) {
  395. h = Util.getRequiredHeight(requiredFieldIndicator);
  396. if (h > height) {
  397. height = h;
  398. }
  399. }
  400. if (errorIndicatorElement != null) {
  401. h = Util.getRequiredHeight(errorIndicatorElement);
  402. if (h > height) {
  403. height = h;
  404. }
  405. }
  406. return height;
  407. }
  408. public void setAlignment(String alignment) {
  409. DOM.setStyleAttribute(getElement(), "textAlign", alignment);
  410. }
  411. public void setMaxWidth(int maxWidth) {
  412. this.maxWidth = maxWidth;
  413. DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  414. if (icon != null) {
  415. DOM.setStyleAttribute(icon.getElement(), "width", "");
  416. }
  417. if (captionText != null) {
  418. DOM.setStyleAttribute(captionText, "width", "");
  419. }
  420. int requiredWidth = getRequiredWidth();
  421. /*
  422. * ApplicationConnection.getConsole().log( "Caption maxWidth: " +
  423. * maxWidth + ", requiredWidth: " + requiredWidth);
  424. */
  425. if (requiredWidth > maxWidth) {
  426. // Needs to truncate and clip
  427. int availableWidth = maxWidth;
  428. // DOM.setStyleAttribute(getElement(), "width", maxWidth + "px");
  429. if (requiredFieldIndicator != null) {
  430. availableWidth -= Util.getRequiredWidth(requiredFieldIndicator);
  431. }
  432. if (errorIndicatorElement != null) {
  433. availableWidth -= Util.getRequiredWidth(errorIndicatorElement);
  434. }
  435. if (availableWidth < 0) {
  436. availableWidth = 0;
  437. }
  438. if (icon != null) {
  439. int iconRequiredWidth = Util
  440. .getRequiredWidth(icon.getElement());
  441. if (availableWidth > iconRequiredWidth) {
  442. availableWidth -= iconRequiredWidth;
  443. } else {
  444. DOM.setStyleAttribute(icon.getElement(), "width",
  445. availableWidth + "px");
  446. availableWidth = 0;
  447. }
  448. }
  449. if (captionText != null) {
  450. int captionWidth = Util.getRequiredWidth(captionText);
  451. if (availableWidth > captionWidth) {
  452. availableWidth -= captionWidth;
  453. } else {
  454. DOM.setStyleAttribute(captionText, "width", availableWidth
  455. + "px");
  456. availableWidth = 0;
  457. }
  458. }
  459. }
  460. }
  461. /**
  462. * Sets the tooltip that should be shown for the caption
  463. *
  464. * @param tooltipInfo
  465. * The tooltip that should be shown or null if no tooltip should
  466. * be shown
  467. */
  468. public void setTooltipInfo(TooltipInfo tooltipInfo) {
  469. this.tooltipInfo = tooltipInfo;
  470. }
  471. /**
  472. * Returns the tooltip that should be shown for the caption
  473. *
  474. * @return The tooltip to show or null if no tooltip should be shown
  475. */
  476. public TooltipInfo getTooltipInfo() {
  477. return tooltipInfo;
  478. }
  479. protected Element getTextElement() {
  480. return captionText;
  481. }
  482. public static String getCaptionOwnerPid(Element e) {
  483. return getOwnerPid(e);
  484. }
  485. private native static void setOwnerPid(Element el, String pid)
  486. /*-{
  487. el.vOwnerPid = pid;
  488. }-*/;
  489. public native static String getOwnerPid(Element el)
  490. /*-{
  491. return el.vOwnerPid;
  492. }-*/;
  493. }