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

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