Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Component.java 40KB

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