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.

Component.java 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.util.Locale;
  18. import org.jsoup.nodes.Element;
  19. import com.vaadin.event.ConnectorEvent;
  20. import com.vaadin.event.ConnectorEventListener;
  21. import com.vaadin.event.FieldEvents;
  22. import com.vaadin.server.ClientConnector;
  23. import com.vaadin.server.ErrorMessage;
  24. import com.vaadin.server.Resource;
  25. import com.vaadin.server.Sizeable;
  26. import com.vaadin.server.VariableOwner;
  27. import com.vaadin.shared.Registration;
  28. import com.vaadin.ui.declarative.DesignContext;
  29. /**
  30. * {@code Component} is the top-level interface that is and must be implemented
  31. * by all Vaadin components. {@code Component} is paired with
  32. * {@link AbstractComponent}, which provides a default implementation for all
  33. * the methods defined in this interface.
  34. *
  35. * <p>
  36. * Components are laid out in the user interface hierarchically. The layout is
  37. * managed by layout components, or more generally by components that implement
  38. * the {@link ComponentContainer} interface. Such a container is the
  39. * <i>parent</i> of the contained components.
  40. * </p>
  41. *
  42. * <p>
  43. * The {@link #getParent()} method allows retrieving the parent component of a
  44. * component. While there is a {@link #setParent(HasComponents)}, you rarely
  45. * need it as you normally add components with the
  46. * {@link ComponentContainer#addComponent(Component) addComponent()} method of
  47. * the layout or other {@code ComponentContainer}, which automatically sets the
  48. * parent.
  49. * </p>
  50. *
  51. * <p>
  52. * A component becomes <i>attached</i> to an application (and the
  53. * {@link #attach()} is called) when it or one of its parents is attached to the
  54. * main window of the application through its containment hierarchy.
  55. * </p>
  56. *
  57. * @author Vaadin Ltd.
  58. * @since 3.0
  59. */
  60. public interface Component extends ClientConnector, Sizeable {
  61. /**
  62. * Gets all user-defined CSS style names of a component. If the component
  63. * has multiple style names defined, the return string is a space-separated
  64. * list of style names. Built-in style names defined in Vaadin or GWT are
  65. * not returned.
  66. *
  67. * <p>
  68. * The style names are returned only in the basic form in which they were
  69. * added; each user-defined style name shows as two CSS style class names in
  70. * the rendered HTML: one as it was given and one prefixed with the
  71. * component-specific style name. Only the former is returned.
  72. * </p>
  73. *
  74. * @return the style name or a space-separated list of user-defined style
  75. * names of the component
  76. * @see #setStyleName(String)
  77. * @see #addStyleName(String)
  78. * @see #removeStyleName(String)
  79. */
  80. public String getStyleName();
  81. /**
  82. * Sets one or more user-defined style names of the component, replacing any
  83. * previous user-defined styles. Multiple styles can be specified as a
  84. * space-separated list of style names. The style names must be valid CSS
  85. * class names and should not conflict with any built-in style names in
  86. * Vaadin or GWT.
  87. *
  88. * <pre>
  89. * Label label = new Label(&quot;This text has a lot of style&quot;);
  90. * label.setStyleName(&quot;myonestyle myotherstyle&quot;);
  91. * </pre>
  92. *
  93. * <p>
  94. * Each style name will occur in two versions: one as specified and one that
  95. * is prefixed with the style name of the component. For example, if you
  96. * have a {@code Button} component and give it "{@code mystyle}" style, the
  97. * component will have both "{@code mystyle}" and "{@code v-button-mystyle}"
  98. * styles. You could then style the component either with:
  99. * </p>
  100. *
  101. * <pre>
  102. * .myonestyle {background: blue;}
  103. * </pre>
  104. *
  105. * <p>
  106. * or
  107. * </p>
  108. *
  109. * <pre>
  110. * .v-button-myonestyle {background: blue;}
  111. * </pre>
  112. *
  113. * <p>
  114. * It is normally a good practice to use {@link #addStyleName(String)
  115. * addStyleName()} rather than this setter, as different software
  116. * abstraction layers can then add their own styles without accidentally
  117. * removing those defined in other layers.
  118. * </p>
  119. *
  120. * @param style
  121. * the new style or styles of the component as a space-separated
  122. * list
  123. * @see #getStyleName()
  124. * @see #addStyleName(String)
  125. * @see #removeStyleName(String)
  126. */
  127. public void setStyleName(String style);
  128. /**
  129. * Adds or removes a style name. Multiple styles can be specified as a
  130. * space-separated list of style names.
  131. *
  132. * If the {@code add} parameter is true, the style name is added to the
  133. * component. If the {@code add} parameter is false, the style name is
  134. * removed from the component.
  135. * <p>
  136. * Functionally this is equivalent to using {@link #addStyleName(String)} or
  137. * {@link #removeStyleName(String)}
  138. *
  139. * @since 8.5
  140. * @param style
  141. * the style name to be added or removed
  142. * @param add
  143. * <code>true</code> to add the given style, <code>false</code>
  144. * to remove it
  145. * @see #addStyleName(String)
  146. * @see #removeStyleName(String)
  147. */
  148. public default void setStyleName(String style, boolean add) {
  149. if (add) {
  150. addStyleName(style);
  151. } else {
  152. removeStyleName(style);
  153. }
  154. }
  155. /**
  156. * Adds one or more style names to this component. Multiple styles can be
  157. * specified as a space-separated list of style names. The style name will
  158. * be rendered as a HTML class name, which can be used in a CSS definition.
  159. *
  160. * <pre>
  161. * Label label = new Label(&quot;This text has style&quot;);
  162. * label.addStyleName(&quot;mystyle&quot;);
  163. * </pre>
  164. *
  165. * <p>
  166. * Each style name will occur in two versions: one as specified and one that
  167. * is prefixed with the style name of the component. For example, if you
  168. * have a {@code Button} component and give it "{@code mystyle}" style, the
  169. * component will have both "{@code mystyle}" and "{@code v-button-mystyle}"
  170. * styles. You could then style the component either with:
  171. * </p>
  172. *
  173. * <pre>
  174. * .mystyle {font-style: italic;}
  175. * </pre>
  176. *
  177. * <p>
  178. * or
  179. * </p>
  180. *
  181. * <pre>
  182. * .v-button-mystyle {font-style: italic;}
  183. * </pre>
  184. *
  185. * @param style
  186. * the new style to be added to the component
  187. * @see #getStyleName()
  188. * @see #setStyleName(String)
  189. * @see #removeStyleName(String)
  190. */
  191. public void addStyleName(String style);
  192. /**
  193. * Adds one or more style names to this component by using one or multiple
  194. * parameters.
  195. *
  196. * @param styles
  197. * the style name or style names to be added to the component
  198. * @see #addStyleName(String)
  199. * @see #setStyleName(String)
  200. * @see #removeStyleName(String)
  201. * @since 8.1
  202. */
  203. public default void addStyleNames(String... styles) {
  204. for (String style : styles) {
  205. addStyleName(style);
  206. }
  207. }
  208. /**
  209. * Removes one or more style names from component. Multiple styles can be
  210. * specified as a space-separated list of style names.
  211. *
  212. * <p>
  213. * The parameter must be a valid CSS style name. Only user-defined style
  214. * names added with {@link #addStyleName(String) addStyleName()} or
  215. * {@link #setStyleName(String) setStyleName()} can be removed; built-in
  216. * style names defined in Vaadin or GWT can not be removed.
  217. * </p>
  218. *
  219. * @param style
  220. * the style name or style names to be removed
  221. * @see #getStyleName()
  222. * @see #setStyleName(String)
  223. * @see #addStyleName(String)
  224. */
  225. public void removeStyleName(String style);
  226. /**
  227. * Removes one or more style names from component. Multiple styles can be
  228. * specified by using multiple parameters.
  229. *
  230. * @param styles
  231. * the style name or style names to be removed
  232. * @see #removeStyleName(String)
  233. * @see #setStyleName(String)
  234. * @see #addStyleName(String)
  235. * @since 8.1
  236. */
  237. public default void removeStyleNames(String... styles) {
  238. for (String style : styles) {
  239. removeStyleName(style);
  240. }
  241. }
  242. /**
  243. * Gets the primary style name of the component. See
  244. * {@link Component#setPrimaryStyleName(String)} for a better description of
  245. * the primary stylename.
  246. */
  247. public String getPrimaryStyleName();
  248. /**
  249. * Changes the primary style name of the component.
  250. *
  251. * <p>
  252. * The primary style name identifies the component when applying the CSS
  253. * theme to the Component. By changing the style name all CSS rules targeted
  254. * for that style name will no longer apply, and might result in the
  255. * component not working as intended.
  256. * </p>
  257. *
  258. * <p>
  259. * To preserve the original style of the component when changing to a new
  260. * primary style you should make your new primary style inherit the old
  261. * primary style using the SASS @include directive. See more in the SASS
  262. * tutorials.
  263. * </p>
  264. *
  265. * @param style
  266. * The new primary style name
  267. */
  268. public void setPrimaryStyleName(String style);
  269. /**
  270. * Tests whether the component is enabled or not. A user can not interact
  271. * with disabled components. Disabled components are rendered in a style
  272. * that indicates the status, usually in gray color. Children of a disabled
  273. * component are also disabled. Components are enabled by default.
  274. *
  275. * <p>
  276. * As a security feature, all updates for disabled components are blocked on
  277. * the server-side.
  278. * </p>
  279. *
  280. * <p>
  281. * Note that this method only returns the status of the component and does
  282. * not take parents into account. Even though this method returns true the
  283. * component can be disabled to the user if a parent is disabled.
  284. * </p>
  285. *
  286. * @return <code>true</code> if the component and its parent are enabled,
  287. * <code>false</code> otherwise.
  288. * @see VariableOwner#isEnabled()
  289. */
  290. public boolean isEnabled();
  291. /**
  292. * Enables or disables the component. The user can not interact with
  293. * disabled components, which are shown with a style that indicates the
  294. * status, usually shaded in light gray color. Components are enabled by
  295. * default.
  296. *
  297. * <pre>
  298. * Button enabled = new Button(&quot;Enabled&quot;);
  299. * enabled.setEnabled(true); // The default
  300. * layout.addComponent(enabled);
  301. *
  302. * Button disabled = new Button(&quot;Disabled&quot;);
  303. * disabled.setEnabled(false);
  304. * layout.addComponent(disabled);
  305. * </pre>
  306. *
  307. * @param enabled
  308. * a boolean value specifying if the component should be enabled
  309. * or not
  310. */
  311. public void setEnabled(boolean enabled);
  312. /**
  313. * Tests the <i>visibility</i> property of the component.
  314. *
  315. * <p>
  316. * Visible components are drawn in the user interface, while invisible ones
  317. * are not. The effect is not merely a cosmetic CSS change - no information
  318. * about an invisible component will be sent to the client. The effect is
  319. * thus the same as removing the component from its parent. Making a
  320. * component invisible through this property can alter the positioning of
  321. * other components.
  322. * </p>
  323. *
  324. * <p>
  325. * A component is visible only if all its parents are also visible. This is
  326. * not checked by this method though, so even if this method returns true,
  327. * the component can be hidden from the user because a parent is set to
  328. * invisible.
  329. * </p>
  330. *
  331. * @return <code>true</code> if the component has been set to be visible in
  332. * the user interface, <code>false</code> if not
  333. * @see #setVisible(boolean)
  334. * @see #attach()
  335. */
  336. public boolean isVisible();
  337. /**
  338. * Sets the visibility of the component.
  339. *
  340. * <p>
  341. * Visible components are drawn in the user interface, while invisible ones
  342. * are not. The effect is not merely a cosmetic CSS change - no information
  343. * about an invisible component will be sent to the client. The effect is
  344. * thus the same as removing the component from its parent.
  345. * </p>
  346. *
  347. * <pre>
  348. * TextField readonly = new TextField(&quot;Read-Only&quot;);
  349. * readonly.setValue(&quot;You can't see this!&quot;);
  350. * readonly.setVisible(false);
  351. * layout.addComponent(readonly);
  352. * </pre>
  353. *
  354. * <p>
  355. * A component is visible only if all of its parents are also visible. If a
  356. * component is explicitly set to be invisible, changes in the visibility of
  357. * its parents will not change the visibility of the component.
  358. * </p>
  359. *
  360. * @param visible
  361. * the boolean value specifying if the component should be
  362. * visible after the call or not.
  363. * @see #isVisible()
  364. */
  365. public void setVisible(boolean visible);
  366. /**
  367. * Sets the parent connector of the component.
  368. *
  369. * <p>
  370. * This method automatically calls {@link #attach()} if the component
  371. * becomes attached to the session, regardless of whether it was attached
  372. * previously. Conversely, if the component currently is attached to the
  373. * session, {@link #detach()} is called for the connector before attaching
  374. * it to a new parent.
  375. * </p>
  376. * <p>
  377. * This method is rarely called directly.
  378. * {@link ComponentContainer#addComponent(Component)} or a
  379. * {@link HasComponents} specific method is normally used for adding
  380. * components to a parent and the used method will call this method
  381. * implicitly.
  382. * </p>
  383. *
  384. * @param parent
  385. * the parent connector
  386. * @throws IllegalStateException
  387. * if a parent is given even though the connector already has a
  388. * parent
  389. */
  390. public void setParent(HasComponents parent);
  391. /**
  392. * Gets the parent component of the component.
  393. *
  394. * <p>
  395. * Components can be nested but a component can have only one parent. A
  396. * component that contains other components, that is, can be a parent,
  397. * should usually inherit the {@link ComponentContainer} interface.
  398. * </p>
  399. *
  400. * @return the parent component
  401. */
  402. @Override
  403. public HasComponents getParent();
  404. /**
  405. * Gets the caption of the component.
  406. *
  407. * <p>
  408. * See {@link #setCaption(String)} for a detailed description of the
  409. * caption.
  410. * </p>
  411. *
  412. * @return the caption of the component or {@code null} if the caption is
  413. * not set.
  414. * @see #setCaption(String)
  415. */
  416. public String getCaption();
  417. /**
  418. * Sets the caption of the component.
  419. *
  420. * <p>
  421. * A <i>caption</i> is an explanatory textual label accompanying a user
  422. * interface component, usually shown above, left of, or inside the
  423. * component. <i>Icon</i> (see {@link #setIcon(Resource) setIcon()} is
  424. * closely related to caption and is usually displayed horizontally before
  425. * or after it, depending on the component and the containing layout.
  426. * </p>
  427. *
  428. * <p>
  429. * The caption can usually also be given as the first parameter to a
  430. * constructor, though some components do not support it.
  431. * </p>
  432. *
  433. * <pre>
  434. * RichTextArea area = new RichTextArea();
  435. * area.setCaption(&quot;You can edit stuff here&quot;);
  436. * area.setValue(&quot;&lt;h1&gt;Helpful Heading&lt;/h1&gt;&quot;
  437. * + &quot;&lt;p&gt;All this is for you to edit.&lt;/p&gt;&quot;);
  438. * </pre>
  439. *
  440. * <p>
  441. * The contents of a caption are automatically quoted, so no raw HTML can be
  442. * rendered in a caption. The validity of the used character encoding,
  443. * usually UTF-8, is not checked.
  444. * </p>
  445. *
  446. * <p>
  447. * The caption of a component is, by default, managed and displayed by the
  448. * layout component or component container in which the component is placed.
  449. * For example, the {@link VerticalLayout} component shows the captions
  450. * left-aligned above the contained components, while the {@link FormLayout}
  451. * component shows the captions on the left side of the vertically laid
  452. * components, with the captions and their associated components
  453. * left-aligned in their own columns. The {@link CustomComponent} does not
  454. * manage the caption of its composition root, so if the root component has
  455. * a caption, it will not be rendered. Some components, such as
  456. * {@link Button} and {@link Panel}, manage the caption themselves and
  457. * display it inside the component.
  458. * </p>
  459. *
  460. * @param caption
  461. * the new caption for the component. If the caption is
  462. * {@code null}, no caption is shown and it does not normally
  463. * take any space
  464. */
  465. public void setCaption(String caption);
  466. /**
  467. * Gets the icon resource of the component.
  468. *
  469. * <p>
  470. * See {@link #setIcon(Resource)} for a detailed description of the icon.
  471. * </p>
  472. *
  473. * @return the icon resource of the component or {@code null} if the
  474. * component has no icon
  475. * @see #setIcon(Resource)
  476. */
  477. public Resource getIcon();
  478. /**
  479. * Sets the icon of the component.
  480. *
  481. * <p>
  482. * An icon is an explanatory graphical label accompanying a user interface
  483. * component, usually shown above, left of, or inside the component. Icon is
  484. * closely related to caption (see {@link #setCaption(String) setCaption()})
  485. * and is usually displayed horizontally before or after it, depending on
  486. * the component and the containing layout.
  487. * </p>
  488. *
  489. * <p>
  490. * The image is loaded by the browser from a resource, typically a
  491. * {@link com.vaadin.server.ThemeResource}.
  492. * </p>
  493. *
  494. * <pre>
  495. * // Component with an icon from a custom theme
  496. * TextField name = new TextField(&quot;Name&quot;);
  497. * name.setIcon(new ThemeResource(&quot;icons/user.png&quot;));
  498. * layout.addComponent(name);
  499. *
  500. * // Component with an icon from another theme ('runo')
  501. * Button ok = new Button(&quot;OK&quot;);
  502. * ok.setIcon(new ThemeResource(&quot;../runo/icons/16/ok.png&quot;));
  503. * layout.addComponent(ok);
  504. * </pre>
  505. *
  506. * <p>
  507. * The icon of a component is, by default, managed and displayed by the
  508. * layout component or component container in which the component is placed.
  509. * For example, the {@link VerticalLayout} component shows the icons
  510. * left-aligned above the contained components, while the {@link FormLayout}
  511. * component shows the icons on the left side of the vertically laid
  512. * components, with the icons and their associated components left-aligned
  513. * in their own columns. The {@link CustomComponent} does not manage the
  514. * icon of its composition root, so if the root component has an icon, it
  515. * will not be rendered.
  516. * </p>
  517. *
  518. * <p>
  519. * An icon will be rendered inside an HTML element that has the
  520. * {@code v-icon} CSS style class. The containing layout may enclose an icon
  521. * and a caption inside elements related to the caption, such as
  522. * {@code v-caption} .
  523. * </p>
  524. *
  525. * @param icon
  526. * the icon of the component. If null, no icon is shown and it
  527. * does not normally take any space.
  528. * @see #getIcon()
  529. * @see #setCaption(String)
  530. */
  531. public void setIcon(Resource icon);
  532. /**
  533. * Gets the UI the component is attached to.
  534. *
  535. * <p>
  536. * If the component is not attached to a UI through a component containment
  537. * hierarchy, <code>null</code> is returned.
  538. * </p>
  539. *
  540. * @return the UI of the component or <code>null</code> if it is not
  541. * attached to a UI
  542. */
  543. @Override
  544. public UI getUI();
  545. /**
  546. * {@inheritDoc}
  547. *
  548. * <p>
  549. * Reimplementing the {@code attach()} method is useful for tasks that need
  550. * to get a reference to the parent, window, or application object with the
  551. * {@link #getParent()} and {@link #getUI()} methods. A component does not
  552. * yet know these objects in the constructor, so in such case, the methods
  553. * will return {@code null}. For example, the following is invalid:
  554. * </p>
  555. *
  556. * <pre>
  557. * public class AttachExample extends CustomComponent {
  558. * public AttachExample() {
  559. * // ERROR: We can't access the application object yet.
  560. * ClassResource r = new ClassResource(&quot;smiley.jpg&quot;,
  561. * getApplication());
  562. * Embedded image = new Embedded(&quot;Image:&quot;, r);
  563. * setCompositionRoot(image);
  564. * }
  565. * }
  566. * </pre>
  567. *
  568. * <p>
  569. * Adding a component to an application triggers calling the
  570. * {@link #attach()} method for the component. Correspondingly, removing a
  571. * component from a container triggers calling the {@link #detach()} method.
  572. * If the parent of an added component is already connected to the
  573. * application, the {@code attach()} is called immediately from
  574. * {@link #setParent(HasComponents)}.
  575. * </p>
  576. *
  577. * <pre>
  578. * public class AttachExample extends CustomComponent {
  579. * public AttachExample() {
  580. * }
  581. *
  582. * &#064;Override
  583. * public void attach() {
  584. * super.attach(); // Must call.
  585. *
  586. * // Now we know who ultimately owns us.
  587. * ClassResource r = new ClassResource(&quot;smiley.jpg&quot;,
  588. * getApplication());
  589. * Embedded image = new Embedded(&quot;Image:&quot;, r);
  590. * setCompositionRoot(image);
  591. * }
  592. * }
  593. * </pre>
  594. */
  595. @Override
  596. public void attach();
  597. /**
  598. * Gets the locale of the component.
  599. *
  600. * <p>
  601. * If a component does not have a locale set, the locale of its parent is
  602. * returned, and so on. Eventually, if no parent has locale set, the locale
  603. * of the application is returned. If the application does not have a locale
  604. * set, it is determined by <code>Locale.getDefault()</code>.
  605. * </p>
  606. *
  607. * <p>
  608. * As the component must be attached before its locale can be acquired,
  609. * using this method in the internationalization of component captions, etc.
  610. * is generally not feasible. For such use case, we recommend using an
  611. * otherwise acquired reference to the application locale.
  612. * </p>
  613. *
  614. * @return Locale of this component or {@code null} if the component and
  615. * none of its parents has a locale set and the component is not yet
  616. * attached to an application.
  617. */
  618. public Locale getLocale();
  619. /**
  620. * Adds an unique id for component that is used in the client-side for
  621. * testing purposes. Keeping identifiers unique is the responsibility of the
  622. * programmer.
  623. *
  624. * @param id
  625. * An alphanumeric id
  626. */
  627. public void setId(String id);
  628. /**
  629. * Gets currently set debug identifier.
  630. *
  631. * @return current id, null if not set
  632. */
  633. public String getId();
  634. /**
  635. * <p>
  636. * Gets the components description, used in tooltips and can be displayed
  637. * directly in certain other components such as forms. The description can
  638. * be used to briefly describe the state of the component to the user. The
  639. * description string may contain certain XML tags:
  640. * </p>
  641. *
  642. * <p>
  643. * <table border=1>
  644. * <tr>
  645. * <td width=120><b>Tag</b></td>
  646. * <td width=120><b>Description</b></td>
  647. * <td width=120><b>Example</b></td>
  648. * </tr>
  649. * <tr>
  650. * <td>&lt;b&gt;</td>
  651. * <td>bold</td>
  652. * <td><b>bold text</b></td>
  653. * </tr>
  654. * <tr>
  655. * <td>&lt;i&gt;</td>
  656. * <td>italic</td>
  657. * <td><i>italic text</i></td>
  658. * </tr>
  659. * <tr>
  660. * <td>&lt;u&gt;</td>
  661. * <td>underlined</td>
  662. * <td><u>underlined text</u></td>
  663. * </tr>
  664. * <tr>
  665. * <td>&lt;br></td>
  666. * <td>linebreak</td>
  667. * <td>N/A</td>
  668. * </tr>
  669. * <tr>
  670. * <td>&lt;ul&gt;<br>
  671. * &lt;li&gt;item1<br>
  672. * &lt;li&gt;item1<br>
  673. * &lt;/ul&gt;</td>
  674. * <td>item list</td>
  675. * <td>
  676. * <ul>
  677. * <li>item1
  678. * <li>item2
  679. * </ul>
  680. * </td>
  681. * </tr>
  682. * </table>
  683. * </p>
  684. *
  685. * <p>
  686. * These tags may be nested.
  687. * </p>
  688. *
  689. * @return component's description <code>String</code>
  690. */
  691. public String getDescription();
  692. /* Declarative support */
  693. /**
  694. * Reads the component state from the given design.
  695. * <p>
  696. * The component is responsible not only for updating its own state but also
  697. * for ensuring that its children update their state based on the design.
  698. * <p>
  699. * It is assumed that the component is in its default state when this method
  700. * is called. Reading should only take into consideration attributes
  701. * specified in the design and not reset any unspecified attributes to their
  702. * defaults.
  703. * <p>
  704. * This method must not modify the design.
  705. *
  706. * @since 7.4
  707. * @param design
  708. * The element to obtain the state from
  709. * @param designContext
  710. * The DesignContext instance used for parsing the design
  711. */
  712. public void readDesign(Element design, DesignContext designContext);
  713. /**
  714. * Writes the component state to the given design.
  715. * <p>
  716. * The component is responsible not only for writing its own state but also
  717. * for ensuring that its children write their state to the design.
  718. * <p>
  719. * This method must not modify the component state.
  720. *
  721. * @since 7.4
  722. * @param design
  723. * The element to write the component state to. Any previous
  724. * attributes or child nodes are <i>not</i> cleared.
  725. * @param designContext
  726. * The DesignContext instance used for writing the design
  727. *
  728. */
  729. public void writeDesign(Element design, DesignContext designContext);
  730. /* Component event framework */
  731. /**
  732. * Superclass of all component originated events.
  733. *
  734. * <p>
  735. * Events are the basis of all user interaction handling in Vaadin. To
  736. * handle events, you provide a listener object that receives the events of
  737. * the particular event type.
  738. * </p>
  739. *
  740. * <pre>
  741. * Button button = new Button(&quot;Click Me!&quot;);
  742. * button.addListener(new Button.ClickListener() {
  743. * public void buttonClick(ClickEvent event) {
  744. * getWindow().showNotification(&quot;Thank You!&quot;);
  745. * }
  746. * });
  747. * layout.addComponent(button);
  748. * </pre>
  749. *
  750. * <p>
  751. * Notice that while each of the event types have their corresponding
  752. * listener types; the listener interfaces are not required to inherit the
  753. * {@code Component.Listener} interface.
  754. * </p>
  755. *
  756. * @see Component.Listener
  757. */
  758. @SuppressWarnings("serial")
  759. public static class Event extends ConnectorEvent {
  760. /**
  761. * Constructs a new event with the specified source component.
  762. *
  763. * @param source
  764. * the source component of the event
  765. */
  766. public Event(Component source) {
  767. super(source);
  768. }
  769. /**
  770. * Gets the component where the event occurred.
  771. *
  772. * @return the source component of the event
  773. */
  774. public Component getComponent() {
  775. return (Component) getSource();
  776. }
  777. }
  778. /**
  779. * Listener interface for receiving <code>Component.Event</code>s.
  780. *
  781. * <p>
  782. * Listener interfaces are the basis of all user interaction handling in
  783. * Vaadin. You have or create a listener object that receives the events.
  784. * All event types have their corresponding listener types; they are not,
  785. * however, required to inherit the {@code Component.Listener} interface,
  786. * and they rarely do so.
  787. * </p>
  788. *
  789. * <p>
  790. * This generic listener interface is useful typically when you wish to
  791. * handle events from different component types in a single listener method
  792. * ({@code componentEvent()}. If you handle component events in an anonymous
  793. * listener class, you normally use the component specific listener class,
  794. * such as {@link com.vaadin.ui.Button.ClickEvent}.
  795. * </p>
  796. *
  797. * <pre>
  798. * class Listening extends CustomComponent implements Listener {
  799. * Button ok; // Stored for determining the source of an event
  800. *
  801. * Label status; // For displaying info about the event
  802. *
  803. * public Listening() {
  804. * VerticalLayout layout = new VerticalLayout();
  805. *
  806. * // Some miscellaneous component
  807. * TextField name = new TextField(&quot;Say it all here&quot;);
  808. * name.addListener(this);
  809. * layout.addComponent(name);
  810. *
  811. * // Handle button clicks as generic events instead
  812. * // of Button.ClickEvent events
  813. * ok = new Button(&quot;OK&quot;);
  814. * ok.addListener(this);
  815. * layout.addComponent(ok);
  816. *
  817. * // For displaying information about an event
  818. * status = new Label(&quot;&quot;);
  819. * layout.addComponent(status);
  820. *
  821. * setCompositionRoot(layout);
  822. * }
  823. *
  824. * public void componentEvent(Event event) {
  825. * // Act according to the source of the event
  826. * if (event.getSource() == ok
  827. * &amp;&amp; event.getClass() == Button.ClickEvent.class)
  828. * getWindow().showNotification(&quot;Click!&quot;);
  829. *
  830. * // Display source component and event class names
  831. * status.setValue(
  832. * &quot;Event from &quot; + event.getSource().getClass().getName()
  833. * + &quot;: &quot; + event.getClass().getName());
  834. * }
  835. * }
  836. *
  837. * Listening listening = new Listening();
  838. * layout.addComponent(listening);
  839. * </pre>
  840. *
  841. * @see Component#addListener(Listener)
  842. */
  843. public interface Listener extends ConnectorEventListener {
  844. /**
  845. * Notifies the listener of a component event.
  846. *
  847. * <p>
  848. * As the event can typically come from one of many source components,
  849. * you may need to differentiate between the event source by component
  850. * reference, class, etc.
  851. * </p>
  852. *
  853. * <pre>
  854. * public void componentEvent(Event event) {
  855. * // Act according to the source of the event
  856. * if (event.getSource() == ok
  857. * &amp;&amp; event.getClass() == Button.ClickEvent.class)
  858. * getWindow().showNotification(&quot;Click!&quot;);
  859. *
  860. * // Display source component and event class names
  861. * status.setValue(
  862. * &quot;Event from &quot; + event.getSource().getClass().getName() + &quot;: &quot;
  863. * + event.getClass().getName());
  864. * }
  865. * </pre>
  866. *
  867. * @param event
  868. * the event that has occurred.
  869. */
  870. public void componentEvent(Component.Event event);
  871. }
  872. /**
  873. * Registers a new (generic) component event listener for the component.
  874. *
  875. * <pre>
  876. * class Listening extends CustomComponent implements Listener {
  877. * // Stored for determining the source of an event
  878. * Button ok;
  879. *
  880. * Label status; // For displaying info about the event
  881. *
  882. * public Listening() {
  883. * VerticalLayout layout = new VerticalLayout();
  884. *
  885. * // Some miscellaneous component
  886. * TextField name = new TextField(&quot;Say it all here&quot;);
  887. * name.addListener(this);
  888. * layout.addComponent(name);
  889. *
  890. * // Handle button clicks as generic events instead
  891. * // of Button.ClickEvent events
  892. * ok = new Button(&quot;OK&quot;);
  893. * ok.addListener(this);
  894. * layout.addComponent(ok);
  895. *
  896. * // For displaying information about an event
  897. * status = new Label(&quot;&quot;);
  898. * layout.addComponent(status);
  899. *
  900. * setCompositionRoot(layout);
  901. * }
  902. *
  903. * public void componentEvent(Event event) {
  904. * // Act according to the source of the event
  905. * if (event.getSource() == ok)
  906. * getWindow().showNotification(&quot;Click!&quot;);
  907. *
  908. * status.setValue(
  909. * &quot;Event from &quot; + event.getSource().getClass().getName()
  910. * + &quot;: &quot; + event.getClass().getName());
  911. * }
  912. * }
  913. *
  914. * Listening listening = new Listening();
  915. * layout.addComponent(listening);
  916. * </pre>
  917. *
  918. * @param listener
  919. * the new Listener to be registered.
  920. * @return a registration object for removing this listener
  921. * @see Component.Event
  922. * @see Registration
  923. * @since 8.0
  924. */
  925. public Registration addListener(Component.Listener listener);
  926. /**
  927. * Removes a previously registered component event listener from this
  928. * component.
  929. *
  930. * @param listener
  931. * the listener to be removed.
  932. * @see #addListener(Listener)
  933. *
  934. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in the
  935. * registration object returned from
  936. * {@link #addListener(Component.Listener)}.
  937. */
  938. @Deprecated
  939. public void removeListener(Component.Listener listener);
  940. /**
  941. * Class of all component originated error events.
  942. *
  943. * <p>
  944. * The component error event is normally fired by
  945. * {@link AbstractComponent#setComponentError(ErrorMessage)}. The component
  946. * errors are set by the framework in some situations and can be set by user
  947. * code. They are indicated in a component with an error indicator.
  948. * </p>
  949. */
  950. @SuppressWarnings("serial")
  951. public static class ErrorEvent extends Event {
  952. private final ErrorMessage message;
  953. /**
  954. * Constructs a new event with a specified source component.
  955. *
  956. * @param message
  957. * the error message.
  958. * @param component
  959. * the source component.
  960. */
  961. public ErrorEvent(ErrorMessage message, Component component) {
  962. super(component);
  963. this.message = message;
  964. }
  965. /**
  966. * Gets the error message.
  967. *
  968. * @return the error message.
  969. */
  970. public ErrorMessage getErrorMessage() {
  971. return message;
  972. }
  973. }
  974. /**
  975. * A sub-interface implemented by components that can obtain input focus.
  976. * This includes all {@code LegacyField} components as well as some other
  977. * components, such as {@code Upload}.
  978. *
  979. * <p>
  980. * Focus can be set with {@link #focus()}. This interface does not provide
  981. * an accessor that would allow finding out the currently focused component;
  982. * focus information can be acquired for some (but not all)
  983. * {@code LegacyField} components through the
  984. * {@link com.vaadin.event.FieldEvents.FocusListener} and
  985. * {@link com.vaadin.event.FieldEvents.BlurListener} interfaces.
  986. * </p>
  987. *
  988. * @see FieldEvents
  989. */
  990. public interface Focusable extends Component {
  991. /**
  992. * Sets the focus to this component.
  993. *
  994. * <pre>
  995. * FormLayout loginBox = new FormLayout();
  996. * loginBox.setCaption(&quot;Login&quot;);
  997. * layout.addComponent(loginBox);
  998. *
  999. * // Create the first field which will be focused
  1000. * TextField username = new TextField(&quot;User name&quot;);
  1001. * loginBox.addComponent(username);
  1002. *
  1003. * // Set focus to the user name
  1004. * username.focus();
  1005. *
  1006. * TextField password = new TextField(&quot;Password&quot;);
  1007. * loginBox.addComponent(password);
  1008. *
  1009. * Button login = new Button(&quot;Login&quot;);
  1010. * loginBox.addComponent(login);
  1011. * </pre>
  1012. *
  1013. * <p>
  1014. * Notice that this interface does not provide an accessor that would
  1015. * allow finding out the currently focused component. Focus information
  1016. * can be acquired for some (but not all) components through the
  1017. * {@link com.vaadin.event.FieldEvents.FocusListener} and
  1018. * {@link com.vaadin.event.FieldEvents.BlurListener} interfaces.
  1019. * </p>
  1020. *
  1021. * @see com.vaadin.event.FieldEvents
  1022. * @see com.vaadin.event.FieldEvents.FocusEvent
  1023. * @see com.vaadin.event.FieldEvents.FocusListener
  1024. * @see com.vaadin.event.FieldEvents.BlurEvent
  1025. * @see com.vaadin.event.FieldEvents.BlurListener
  1026. */
  1027. public void focus();
  1028. /**
  1029. * Gets the <i>tabulator index</i> of the {@code Focusable} component.
  1030. *
  1031. * @return tab index set for the {@code Focusable} component
  1032. * @see #setTabIndex(int)
  1033. */
  1034. public int getTabIndex();
  1035. /**
  1036. * Sets the <i>tabulator index</i> of the {@code Focusable} component.
  1037. * The tab index property is used to specify the order in which the
  1038. * fields are focused when the user presses the Tab key. Components with
  1039. * a defined tab index are focused sequentially first, and then the
  1040. * components with no tab index.
  1041. *
  1042. * <pre>
  1043. * Form loginBox = new Form();
  1044. * loginBox.setCaption(&quot;Login&quot;);
  1045. * layout.addComponent(loginBox);
  1046. *
  1047. * // Create the first field which will be focused
  1048. * TextField username = new TextField(&quot;User name&quot;);
  1049. * loginBox.addField(&quot;username&quot;, username);
  1050. *
  1051. * // Set focus to the user name
  1052. * username.focus();
  1053. *
  1054. * TextField password = new TextField(&quot;Password&quot;);
  1055. * loginBox.addField(&quot;password&quot;, password);
  1056. *
  1057. * Button login = new Button(&quot;Login&quot;);
  1058. * loginBox.getFooter().addComponent(login);
  1059. *
  1060. * // An additional component which natural focus order would
  1061. * // be after the button.
  1062. * CheckBox remember = new CheckBox(&quot;Remember me&quot;);
  1063. * loginBox.getFooter().addComponent(remember);
  1064. *
  1065. * username.setTabIndex(1);
  1066. * password.setTabIndex(2);
  1067. * remember.setTabIndex(3); // Different than natural place
  1068. * login.setTabIndex(4);
  1069. * </pre>
  1070. *
  1071. * <p>
  1072. * After all focusable user interface components are done, the browser
  1073. * can begin again from the component with the smallest tab index, or it
  1074. * can take the focus out of the page, for example, to the location bar.
  1075. * </p>
  1076. *
  1077. * <p>
  1078. * If the tab index is not set (is set to zero), the default tab order
  1079. * is used. The order is somewhat browser-dependent, but generally
  1080. * follows the HTML structure of the page.
  1081. * </p>
  1082. *
  1083. * <p>
  1084. * A negative value means that the component is completely removed from
  1085. * the tabulation order and can not be reached by pressing the Tab key
  1086. * at all.
  1087. * </p>
  1088. *
  1089. * @param tabIndex
  1090. * the tab order of this component. Indexes usually start
  1091. * from 1. Zero means that default tab order should be used.
  1092. * A negative value means that the field should not be
  1093. * included in the tabbing sequence.
  1094. * @see #getTabIndex()
  1095. */
  1096. public void setTabIndex(int tabIndex);
  1097. }
  1098. }