Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VCaption.java 18KB

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