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

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