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.

AbstractComponent.java 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.Serializable;
  6. import java.lang.reflect.Method;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.LinkedList;
  12. import java.util.Locale;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. import com.vaadin.Application;
  18. import com.vaadin.event.EventRouter;
  19. import com.vaadin.event.MethodEventSource;
  20. import com.vaadin.terminal.ErrorMessage;
  21. import com.vaadin.terminal.PaintException;
  22. import com.vaadin.terminal.PaintTarget;
  23. import com.vaadin.terminal.Resource;
  24. import com.vaadin.terminal.Terminal;
  25. import com.vaadin.terminal.gwt.server.ComponentSizeValidator;
  26. import com.vaadin.tools.ReflectTools;
  27. /**
  28. * An abstract class that defines default implementation for the
  29. * {@link Component} interface. Basic UI components that are not derived from an
  30. * external component can inherit this class to easily qualify as Vaadin
  31. * components. Most components in Vaadin do just that.
  32. *
  33. * @author IT Mill Ltd.
  34. * @version
  35. * @VERSION@
  36. * @since 3.0
  37. */
  38. @SuppressWarnings("serial")
  39. public abstract class AbstractComponent implements Component, MethodEventSource {
  40. /* Private members */
  41. /**
  42. * Style names.
  43. */
  44. private ArrayList<String> styles;
  45. /**
  46. * Caption text.
  47. */
  48. private String caption;
  49. /**
  50. * Application specific data object. The component does not use or modify
  51. * this.
  52. */
  53. private Object applicationData;
  54. /**
  55. * Icon to be shown together with caption.
  56. */
  57. private Resource icon;
  58. /**
  59. * Is the component enabled (its normal usage is allowed).
  60. */
  61. private boolean enabled = true;
  62. /**
  63. * Is the component visible (it is rendered).
  64. */
  65. private boolean visible = true;
  66. /**
  67. * Is the component read-only ?
  68. */
  69. private boolean readOnly = false;
  70. /**
  71. * Description of the usage (XML).
  72. */
  73. private String description = null;
  74. /**
  75. * The container this component resides in.
  76. */
  77. private Component parent = null;
  78. /**
  79. * The EventRouter used for the event model.
  80. */
  81. private EventRouter eventRouter = null;
  82. /**
  83. * A set of event identifiers with registered listeners.
  84. */
  85. private Set<String> eventIdentifiers = null;
  86. /**
  87. * The internal error message of the component.
  88. */
  89. private ErrorMessage componentError = null;
  90. /**
  91. * Immediate mode: if true, all variable changes are required to be sent
  92. * from the terminal immediately.
  93. */
  94. private boolean immediate = false;
  95. /**
  96. * Locale of this component.
  97. */
  98. private Locale locale;
  99. /**
  100. * The component should receive focus (if {@link Focusable}) when attached.
  101. */
  102. private boolean delayedFocus;
  103. /**
  104. * List of repaint request listeners or null if not listened at all.
  105. */
  106. private LinkedList<RepaintRequestListener> repaintRequestListeners = null;
  107. /**
  108. * Are all the repaint listeners notified about recent changes ?
  109. */
  110. private boolean repaintRequestListenersNotified = false;
  111. private String testingId;
  112. /* Sizeable fields */
  113. private float width = SIZE_UNDEFINED;
  114. private float height = SIZE_UNDEFINED;
  115. private int widthUnit = UNITS_PIXELS;
  116. private int heightUnit = UNITS_PIXELS;
  117. private static final Pattern sizePattern = Pattern
  118. .compile("^(-?\\d+(\\.\\d+)?)(%|px|em|ex|in|cm|mm|pt|pc)?$");
  119. private ComponentErrorHandler errorHandler = null;
  120. /* Constructor */
  121. /**
  122. * Constructs a new Component.
  123. */
  124. public AbstractComponent() {
  125. // ComponentSizeValidator.setCreationLocation(this);
  126. }
  127. /* Get/Set component properties */
  128. /**
  129. * Gets the UIDL tag corresponding to the component.
  130. *
  131. * <p>
  132. * Note! In version 6.2 the method for mapping server side components to
  133. * their client side counterparts was enhanced. This method was made final
  134. * to intentionally "break" code where it is needed. If your code does not
  135. * compile due overriding this method, it is very likely that you need to:
  136. * <ul>
  137. * <li>remove the implementation of getTag
  138. * <li>add {@link ClientWidget} annotation to your component
  139. * </ul>
  140. *
  141. * @return the component's UIDL tag as <code>String</code>
  142. * @deprecated tags are no more required for components. Instead of tags we
  143. * are now using {@link ClientWidget} annotations to map server
  144. * side components to client side counterparts. Generating
  145. * identifier for component type is delegated to terminal.
  146. * @see ClientWidget
  147. */
  148. @Deprecated
  149. public final String getTag() {
  150. return "";
  151. }
  152. public void setDebugId(String id) {
  153. testingId = id;
  154. }
  155. public String getDebugId() {
  156. return testingId;
  157. }
  158. /**
  159. * Gets style for component. Multiple styles are joined with spaces.
  160. *
  161. * @return the component's styleValue of property style.
  162. * @deprecated Use getStyleName() instead; renamed for consistency and to
  163. * indicate that "style" should not be used to switch client
  164. * side implementation, only to style the component.
  165. */
  166. @Deprecated
  167. public String getStyle() {
  168. return getStyleName();
  169. }
  170. /**
  171. * Sets and replaces all previous style names of the component. This method
  172. * will trigger a {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  173. * RepaintRequestEvent}.
  174. *
  175. * @param style
  176. * the new style of the component.
  177. * @deprecated Use setStyleName() instead; renamed for consistency and to
  178. * indicate that "style" should not be used to switch client
  179. * side implementation, only to style the component.
  180. */
  181. @Deprecated
  182. public void setStyle(String style) {
  183. setStyleName(style);
  184. }
  185. /*
  186. * Gets the component's style. Don't add a JavaDoc comment here, we use the
  187. * default documentation from implemented interface.
  188. */
  189. public String getStyleName() {
  190. String s = "";
  191. if (styles != null) {
  192. for (final Iterator<String> it = styles.iterator(); it.hasNext();) {
  193. s += it.next();
  194. if (it.hasNext()) {
  195. s += " ";
  196. }
  197. }
  198. }
  199. return s;
  200. }
  201. /*
  202. * Sets the component's style. Don't add a JavaDoc comment here, we use the
  203. * default documentation from implemented interface.
  204. */
  205. public void setStyleName(String style) {
  206. if (style == null || "".equals(style)) {
  207. styles = null;
  208. requestRepaint();
  209. return;
  210. }
  211. if (styles == null) {
  212. styles = new ArrayList<String>();
  213. }
  214. styles.clear();
  215. styles.add(style);
  216. requestRepaint();
  217. }
  218. public void addStyleName(String style) {
  219. if (style == null || "".equals(style)) {
  220. return;
  221. }
  222. if (styles == null) {
  223. styles = new ArrayList<String>();
  224. }
  225. if (!styles.contains(style)) {
  226. styles.add(style);
  227. requestRepaint();
  228. }
  229. }
  230. public void removeStyleName(String style) {
  231. if (styles != null) {
  232. styles.remove(style);
  233. requestRepaint();
  234. }
  235. }
  236. /*
  237. * Get's the component's caption. Don't add a JavaDoc comment here, we use
  238. * the default documentation from implemented interface.
  239. */
  240. public String getCaption() {
  241. return caption;
  242. }
  243. /**
  244. * Sets the component's caption <code>String</code>. Caption is the visible
  245. * name of the component. This method will trigger a
  246. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  247. * RepaintRequestEvent}.
  248. *
  249. * @param caption
  250. * the new caption <code>String</code> for the component.
  251. */
  252. public void setCaption(String caption) {
  253. this.caption = caption;
  254. requestRepaint();
  255. }
  256. /*
  257. * Don't add a JavaDoc comment here, we use the default documentation from
  258. * implemented interface.
  259. */
  260. public Locale getLocale() {
  261. if (locale != null) {
  262. return locale;
  263. }
  264. if (parent != null) {
  265. return parent.getLocale();
  266. }
  267. final Application app = getApplication();
  268. if (app != null) {
  269. return app.getLocale();
  270. }
  271. return null;
  272. }
  273. /**
  274. * Sets the locale of this component.
  275. *
  276. * <pre>
  277. * // Component for which the locale is meaningful
  278. * InlineDateField date = new InlineDateField(&quot;Datum&quot;);
  279. *
  280. * // German language specified with ISO 639-1 language
  281. * // code and ISO 3166-1 alpha-2 country code.
  282. * date.setLocale(new Locale(&quot;de&quot;, &quot;DE&quot;));
  283. *
  284. * date.setResolution(DateField.RESOLUTION_DAY);
  285. * layout.addComponent(date);
  286. * </pre>
  287. *
  288. *
  289. * @param locale
  290. * the locale to become this component's locale.
  291. */
  292. public void setLocale(Locale locale) {
  293. this.locale = locale;
  294. requestRepaint();
  295. }
  296. /*
  297. * Gets the component's icon resource. Don't add a JavaDoc comment here, we
  298. * use the default documentation from implemented interface.
  299. */
  300. public Resource getIcon() {
  301. return icon;
  302. }
  303. /**
  304. * Sets the component's icon. This method will trigger a
  305. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  306. * RepaintRequestEvent}.
  307. *
  308. * @param icon
  309. * the icon to be shown with the component's caption.
  310. */
  311. public void setIcon(Resource icon) {
  312. this.icon = icon;
  313. requestRepaint();
  314. }
  315. /*
  316. * Tests if the component is enabled or not. Don't add a JavaDoc comment
  317. * here, we use the default documentation from implemented interface.
  318. */
  319. public boolean isEnabled() {
  320. return enabled && (parent == null || parent.isEnabled()) && isVisible();
  321. }
  322. /*
  323. * Enables or disables the component. Don't add a JavaDoc comment here, we
  324. * use the default documentation from implemented interface.
  325. */
  326. public void setEnabled(boolean enabled) {
  327. if (this.enabled != enabled) {
  328. boolean wasEnabled = this.enabled;
  329. boolean wasEnabledInContext = isEnabled();
  330. this.enabled = enabled;
  331. boolean isEnabled = enabled;
  332. boolean isEnabledInContext = isEnabled();
  333. // If the actual enabled state (as rendered, in context) has not
  334. // changed we do not need to repaint except if the parent is
  335. // invisible.
  336. // If the parent is invisible we must request a repaint so the
  337. // component is repainted with the new enabled state when the parent
  338. // is set visible again. This workaround is needed as isEnabled
  339. // checks isVisible.
  340. boolean needRepaint = (wasEnabledInContext != isEnabledInContext)
  341. || (wasEnabled != isEnabled && (getParent() == null || !getParent()
  342. .isVisible()));
  343. if (needRepaint) {
  344. requestRepaint();
  345. }
  346. }
  347. }
  348. /*
  349. * Tests if the component is in the immediate mode. Don't add a JavaDoc
  350. * comment here, we use the default documentation from implemented
  351. * interface.
  352. */
  353. public boolean isImmediate() {
  354. return immediate;
  355. }
  356. /**
  357. * Sets the component's immediate mode to the specified status. This method
  358. * will trigger a {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  359. * RepaintRequestEvent}.
  360. *
  361. * @param immediate
  362. * the boolean value specifying if the component should be in the
  363. * immediate mode after the call.
  364. * @see Component#isImmediate()
  365. */
  366. public void setImmediate(boolean immediate) {
  367. this.immediate = immediate;
  368. requestRepaint();
  369. }
  370. /*
  371. * (non-Javadoc)
  372. *
  373. * @see com.vaadin.ui.Component#isVisible()
  374. */
  375. public boolean isVisible() {
  376. return visible && (getParent() == null || getParent().isVisible());
  377. }
  378. /*
  379. * (non-Javadoc)
  380. *
  381. * @see com.vaadin.ui.Component#setVisible(boolean)
  382. */
  383. public void setVisible(boolean visible) {
  384. if (this.visible != visible) {
  385. this.visible = visible;
  386. // Instead of requesting repaint normally we
  387. // fire the event directly to assure that the
  388. // event goes through event in the component might
  389. // now be invisible
  390. fireRequestRepaintEvent(null);
  391. }
  392. }
  393. /**
  394. * <p>
  395. * Gets the component's description. The description can be used to briefly
  396. * describe the state of the component to the user. The description string
  397. * may contain certain XML tags:
  398. * </p>
  399. *
  400. * <p>
  401. * <table border=1>
  402. * <tr>
  403. * <td width=120><b>Tag</b></td>
  404. * <td width=120><b>Description</b></td>
  405. * <td width=120><b>Example</b></td>
  406. * </tr>
  407. * <tr>
  408. * <td>&lt;b></td>
  409. * <td>bold</td>
  410. * <td><b>bold text</b></td>
  411. * </tr>
  412. * <tr>
  413. * <td>&lt;i></td>
  414. * <td>italic</td>
  415. * <td><i>italic text</i></td>
  416. * </tr>
  417. * <tr>
  418. * <td>&lt;u></td>
  419. * <td>underlined</td>
  420. * <td><u>underlined text</u></td>
  421. * </tr>
  422. * <tr>
  423. * <td>&lt;br></td>
  424. * <td>linebreak</td>
  425. * <td>N/A</td>
  426. * </tr>
  427. * <tr>
  428. * <td>&lt;ul><br>
  429. * &lt;li>item1<br>
  430. * &lt;li>item1<br>
  431. * &lt;/ul></td>
  432. * <td>item list</td>
  433. * <td>
  434. * <ul>
  435. * <li>item1
  436. * <li>item2
  437. * </ul>
  438. * </td>
  439. * </tr>
  440. * </table>
  441. * </p>
  442. *
  443. * <p>
  444. * These tags may be nested.
  445. * </p>
  446. *
  447. * @return component's description <code>String</code>
  448. */
  449. public String getDescription() {
  450. return description;
  451. }
  452. /**
  453. * Sets the component's description. See {@link #getDescription()} for more
  454. * information on what the description is. This method will trigger a
  455. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  456. * RepaintRequestEvent}.
  457. *
  458. * @param description
  459. * the new description string for the component.
  460. */
  461. public void setDescription(String description) {
  462. this.description = description;
  463. requestRepaint();
  464. }
  465. /*
  466. * Gets the component's parent component. Don't add a JavaDoc comment here,
  467. * we use the default documentation from implemented interface.
  468. */
  469. public Component getParent() {
  470. return parent;
  471. }
  472. /*
  473. * Sets the parent component. Don't add a JavaDoc comment here, we use the
  474. * default documentation from implemented interface.
  475. */
  476. public void setParent(Component parent) {
  477. // If the parent is not changed, don't do anything
  478. if (parent == this.parent) {
  479. return;
  480. }
  481. if (parent != null && this.parent != null) {
  482. throw new IllegalStateException(getClass().getName()
  483. + " already has a parent.");
  484. }
  485. // Send detach event if the component have been connected to a window
  486. if (getApplication() != null) {
  487. detach();
  488. }
  489. // Connect to new parent
  490. this.parent = parent;
  491. // Send attach event if connected to a window
  492. if (getApplication() != null) {
  493. attach();
  494. }
  495. }
  496. /**
  497. * Gets the error message for this component.
  498. *
  499. * @return ErrorMessage containing the description of the error state of the
  500. * component or null, if the component contains no errors. Extending
  501. * classes should override this method if they support other error
  502. * message types such as validation errors or buffering errors. The
  503. * returned error message contains information about all the errors.
  504. */
  505. public ErrorMessage getErrorMessage() {
  506. return componentError;
  507. }
  508. /**
  509. * Gets the component's error message.
  510. *
  511. * @link Terminal.ErrorMessage#ErrorMessage(String, int)
  512. *
  513. * @return the component's error message.
  514. */
  515. public ErrorMessage getComponentError() {
  516. return componentError;
  517. }
  518. /**
  519. * Sets the component's error message. The message may contain certain XML
  520. * tags, for more information see
  521. *
  522. * @link Component.ErrorMessage#ErrorMessage(String, int)
  523. *
  524. * @param componentError
  525. * the new <code>ErrorMessage</code> of the component.
  526. */
  527. public void setComponentError(ErrorMessage componentError) {
  528. this.componentError = componentError;
  529. fireComponentErrorEvent();
  530. requestRepaint();
  531. }
  532. /*
  533. * Tests if the component is in read-only mode. Don't add a JavaDoc comment
  534. * here, we use the default documentation from implemented interface.
  535. */
  536. public boolean isReadOnly() {
  537. return readOnly;
  538. }
  539. /*
  540. * Sets the component's read-only mode. Don't add a JavaDoc comment here, we
  541. * use the default documentation from implemented interface.
  542. */
  543. public void setReadOnly(boolean readOnly) {
  544. this.readOnly = readOnly;
  545. requestRepaint();
  546. }
  547. /*
  548. * Gets the parent window of the component. Don't add a JavaDoc comment
  549. * here, we use the default documentation from implemented interface.
  550. */
  551. public Window getWindow() {
  552. if (parent == null) {
  553. return null;
  554. } else {
  555. return parent.getWindow();
  556. }
  557. }
  558. /*
  559. * Notify the component that it's attached to a window. Don't add a JavaDoc
  560. * comment here, we use the default documentation from implemented
  561. * interface.
  562. */
  563. public void attach() {
  564. requestRepaint();
  565. if (!visible) {
  566. /*
  567. * Bypass the repaint optimization in childRequestedRepaint method
  568. * when attaching. When reattaching (possibly moving) -> must
  569. * repaint
  570. */
  571. fireRequestRepaintEvent(null);
  572. }
  573. if (delayedFocus) {
  574. focus();
  575. }
  576. }
  577. /*
  578. * Detach the component from application. Don't add a JavaDoc comment here,
  579. * we use the default documentation from implemented interface.
  580. */
  581. public void detach() {
  582. }
  583. /**
  584. * Sets the focus for this component if the component is {@link Focusable}.
  585. */
  586. protected void focus() {
  587. if (this instanceof Focusable) {
  588. final Application app = getApplication();
  589. if (app != null) {
  590. getWindow().setFocusedComponent((Focusable) this);
  591. delayedFocus = false;
  592. } else {
  593. delayedFocus = true;
  594. }
  595. }
  596. }
  597. /*
  598. * Gets the parent application of the component. Don't add a JavaDoc comment
  599. * here, we use the default documentation from implemented interface.
  600. */
  601. public Application getApplication() {
  602. if (parent == null) {
  603. return null;
  604. } else {
  605. return parent.getApplication();
  606. }
  607. }
  608. /* Component painting */
  609. /* Documented in super interface */
  610. public void requestRepaintRequests() {
  611. repaintRequestListenersNotified = false;
  612. }
  613. /*
  614. * Paints the component into a UIDL stream. Don't add a JavaDoc comment
  615. * here, we use the default documentation from implemented interface.
  616. */
  617. public final void paint(PaintTarget target) throws PaintException {
  618. final String tag = target.getTag(this);
  619. if (!target.startTag(this, tag) || repaintRequestListenersNotified) {
  620. // Paint the contents of the component
  621. // Only paint content of visible components.
  622. if (isVisible()) {
  623. if (getHeight() >= 0
  624. && (getHeightUnits() != UNITS_PERCENTAGE || ComponentSizeValidator
  625. .parentCanDefineHeight(this))) {
  626. target.addAttribute("height", "" + getCSSHeight());
  627. }
  628. if (getWidth() >= 0
  629. && (getWidthUnits() != UNITS_PERCENTAGE || ComponentSizeValidator
  630. .parentCanDefineWidth(this))) {
  631. target.addAttribute("width", "" + getCSSWidth());
  632. }
  633. if (styles != null && styles.size() > 0) {
  634. target.addAttribute("style", getStyle());
  635. }
  636. if (isReadOnly()) {
  637. target.addAttribute("readonly", true);
  638. }
  639. if (isImmediate()) {
  640. target.addAttribute("immediate", true);
  641. }
  642. if (!isEnabled()) {
  643. target.addAttribute("disabled", true);
  644. }
  645. if (getCaption() != null) {
  646. target.addAttribute("caption", getCaption());
  647. }
  648. if (getIcon() != null) {
  649. target.addAttribute("icon", getIcon());
  650. }
  651. if (getDescription() != null && getDescription().length() > 0) {
  652. target.addAttribute("description", getDescription());
  653. }
  654. if (eventIdentifiers != null) {
  655. target.addAttribute("eventListeners",
  656. eventIdentifiers.toArray());
  657. }
  658. paintContent(target);
  659. final ErrorMessage error = getErrorMessage();
  660. if (error != null) {
  661. error.paint(target);
  662. }
  663. } else {
  664. target.addAttribute("invisible", true);
  665. }
  666. } else {
  667. // Contents have not changed, only cached presentation can be used
  668. target.addAttribute("cached", true);
  669. }
  670. target.endTag(tag);
  671. repaintRequestListenersNotified = false;
  672. }
  673. /**
  674. * Build CSS compatible string representation of height.
  675. *
  676. * @return CSS height
  677. */
  678. private String getCSSHeight() {
  679. if (getHeightUnits() == UNITS_PIXELS) {
  680. return ((int) getHeight()) + UNIT_SYMBOLS[getHeightUnits()];
  681. } else {
  682. return getHeight() + UNIT_SYMBOLS[getHeightUnits()];
  683. }
  684. }
  685. /**
  686. * Build CSS compatible string representation of width.
  687. *
  688. * @return CSS width
  689. */
  690. private String getCSSWidth() {
  691. if (getWidthUnits() == UNITS_PIXELS) {
  692. return ((int) getWidth()) + UNIT_SYMBOLS[getWidthUnits()];
  693. } else {
  694. return getWidth() + UNIT_SYMBOLS[getWidthUnits()];
  695. }
  696. }
  697. /**
  698. * Paints any needed component-specific things to the given UIDL stream. The
  699. * more general {@link #paint(PaintTarget)} method handles all general
  700. * attributes common to all components, and it calls this method to paint
  701. * any component-specific attributes to the UIDL stream.
  702. *
  703. * @param target
  704. * the target UIDL stream where the component should paint itself
  705. * to
  706. * @throws PaintException
  707. * if the paint operation failed.
  708. */
  709. public void paintContent(PaintTarget target) throws PaintException {
  710. }
  711. /* Documentation copied from interface */
  712. public void requestRepaint() {
  713. // The effect of the repaint request is identical to case where a
  714. // child requests repaint
  715. childRequestedRepaint(null);
  716. }
  717. /* Documentation copied from interface */
  718. public void childRequestedRepaint(
  719. Collection<RepaintRequestListener> alreadyNotified) {
  720. // Invisible components (by flag in this particular component) do not
  721. // need repaints
  722. if (!visible) {
  723. return;
  724. }
  725. fireRequestRepaintEvent(alreadyNotified);
  726. }
  727. /**
  728. * Fires the repaint request event.
  729. *
  730. * @param alreadyNotified
  731. */
  732. private void fireRequestRepaintEvent(
  733. Collection<RepaintRequestListener> alreadyNotified) {
  734. // Notify listeners only once
  735. if (!repaintRequestListenersNotified) {
  736. // Notify the listeners
  737. if (repaintRequestListeners != null
  738. && !repaintRequestListeners.isEmpty()) {
  739. final Object[] listeners = repaintRequestListeners.toArray();
  740. final RepaintRequestEvent event = new RepaintRequestEvent(this);
  741. for (int i = 0; i < listeners.length; i++) {
  742. if (alreadyNotified == null) {
  743. alreadyNotified = new LinkedList<RepaintRequestListener>();
  744. }
  745. if (!alreadyNotified.contains(listeners[i])) {
  746. ((RepaintRequestListener) listeners[i])
  747. .repaintRequested(event);
  748. alreadyNotified
  749. .add((RepaintRequestListener) listeners[i]);
  750. repaintRequestListenersNotified = true;
  751. }
  752. }
  753. }
  754. // Notify the parent
  755. final Component parent = getParent();
  756. if (parent != null) {
  757. parent.childRequestedRepaint(alreadyNotified);
  758. }
  759. }
  760. }
  761. /* Documentation copied from interface */
  762. public void addListener(RepaintRequestListener listener) {
  763. if (repaintRequestListeners == null) {
  764. repaintRequestListeners = new LinkedList<RepaintRequestListener>();
  765. }
  766. if (!repaintRequestListeners.contains(listener)) {
  767. repaintRequestListeners.add(listener);
  768. }
  769. }
  770. /* Documentation copied from interface */
  771. public void removeListener(RepaintRequestListener listener) {
  772. if (repaintRequestListeners != null) {
  773. repaintRequestListeners.remove(listener);
  774. if (repaintRequestListeners.isEmpty()) {
  775. repaintRequestListeners = null;
  776. }
  777. }
  778. }
  779. /* Component variable changes */
  780. /*
  781. * Invoked when the value of a variable has changed. Don't add a JavaDoc
  782. * comment here, we use the default documentation from implemented
  783. * interface.
  784. */
  785. public void changeVariables(Object source, Map<String, Object> variables) {
  786. }
  787. /* General event framework */
  788. private static final Method COMPONENT_EVENT_METHOD = ReflectTools
  789. .findMethod(Component.Listener.class, "componentEvent",
  790. Component.Event.class);
  791. /**
  792. * <p>
  793. * Registers a new listener with the specified activation method to listen
  794. * events generated by this component. If the activation method does not
  795. * have any arguments the event object will not be passed to it when it's
  796. * called.
  797. * </p>
  798. *
  799. * <p>
  800. * This method additionally informs the event-api to route events with the
  801. * given eventIdentifier to the components handleEvent function call.
  802. * </p>
  803. *
  804. * <p>
  805. * For more information on the inheritable event mechanism see the
  806. * {@link com.vaadin.event com.vaadin.event package documentation}.
  807. * </p>
  808. *
  809. * @param eventIdentifier
  810. * the identifier of the event to listen for
  811. * @param eventType
  812. * the type of the listened event. Events of this type or its
  813. * subclasses activate the listener.
  814. * @param target
  815. * the object instance who owns the activation method.
  816. * @param method
  817. * the activation method.
  818. *
  819. * @since 6.2
  820. */
  821. protected void addListener(String eventIdentifier, Class<?> eventType,
  822. Object target, Method method) {
  823. if (eventRouter == null) {
  824. eventRouter = new EventRouter();
  825. }
  826. if (eventIdentifiers == null) {
  827. eventIdentifiers = new HashSet<String>();
  828. }
  829. boolean needRepaint = !eventRouter.hasListeners(eventType);
  830. eventRouter.addListener(eventType, target, method);
  831. if (needRepaint) {
  832. eventIdentifiers.add(eventIdentifier);
  833. requestRepaint();
  834. }
  835. }
  836. /**
  837. * Removes all registered listeners matching the given parameters. Since
  838. * this method receives the event type and the listener object as
  839. * parameters, it will unregister all <code>object</code>'s methods that are
  840. * registered to listen to events of type <code>eventType</code> generated
  841. * by this component.
  842. *
  843. * <p>
  844. * This method additionally informs the event-api to stop routing events
  845. * with the given eventIdentifier to the components handleEvent function
  846. * call.
  847. * </p>
  848. *
  849. * <p>
  850. * For more information on the inheritable event mechanism see the
  851. * {@link com.vaadin.event com.vaadin.event package documentation}.
  852. * </p>
  853. *
  854. * @param eventIdentifier
  855. * the identifier of the event to stop listening for
  856. * @param eventType
  857. * the exact event type the <code>object</code> listens to.
  858. * @param target
  859. * the target object that has registered to listen to events of
  860. * type <code>eventType</code> with one or more methods.
  861. *
  862. * @since 6.2
  863. */
  864. protected void removeListener(String eventIdentifier, Class<?> eventType,
  865. Object target) {
  866. if (eventRouter != null) {
  867. eventRouter.removeListener(eventType, target);
  868. if (!eventRouter.hasListeners(eventType)) {
  869. eventIdentifiers.remove(eventIdentifier);
  870. requestRepaint();
  871. }
  872. }
  873. }
  874. /**
  875. * <p>
  876. * Registers a new listener with the specified activation method to listen
  877. * events generated by this component. If the activation method does not
  878. * have any arguments the event object will not be passed to it when it's
  879. * called.
  880. * </p>
  881. *
  882. * <p>
  883. * For more information on the inheritable event mechanism see the
  884. * {@link com.vaadin.event com.vaadin.event package documentation}.
  885. * </p>
  886. *
  887. * @param eventType
  888. * the type of the listened event. Events of this type or its
  889. * subclasses activate the listener.
  890. * @param target
  891. * the object instance who owns the activation method.
  892. * @param method
  893. * the activation method.
  894. */
  895. public void addListener(Class eventType, Object target, Method method) {
  896. if (eventRouter == null) {
  897. eventRouter = new EventRouter();
  898. }
  899. eventRouter.addListener(eventType, target, method);
  900. }
  901. /**
  902. * <p>
  903. * Convenience method for registering a new listener with the specified
  904. * activation method to listen events generated by this component. If the
  905. * activation method does not have any arguments the event object will not
  906. * be passed to it when it's called.
  907. * </p>
  908. *
  909. * <p>
  910. * This version of <code>addListener</code> gets the name of the activation
  911. * method as a parameter. The actual method is reflected from
  912. * <code>object</code>, and unless exactly one match is found,
  913. * <code>java.lang.IllegalArgumentException</code> is thrown.
  914. * </p>
  915. *
  916. * <p>
  917. * For more information on the inheritable event mechanism see the
  918. * {@link com.vaadin.event com.vaadin.event package documentation}.
  919. * </p>
  920. *
  921. * <p>
  922. * Note: Using this method is discouraged because it cannot be checked
  923. * during compilation. Use {@link #addListener(Class, Object, Method)} or
  924. * {@link #addListener(com.vaadin.ui.Component.Listener)} instead.
  925. * </p>
  926. *
  927. * @param eventType
  928. * the type of the listened event. Events of this type or its
  929. * subclasses activate the listener.
  930. * @param target
  931. * the object instance who owns the activation method.
  932. * @param methodName
  933. * the name of the activation method.
  934. */
  935. public void addListener(Class eventType, Object target, String methodName) {
  936. if (eventRouter == null) {
  937. eventRouter = new EventRouter();
  938. }
  939. eventRouter.addListener(eventType, target, methodName);
  940. }
  941. /**
  942. * Removes all registered listeners matching the given parameters. Since
  943. * this method receives the event type and the listener object as
  944. * parameters, it will unregister all <code>object</code>'s methods that are
  945. * registered to listen to events of type <code>eventType</code> generated
  946. * by this component.
  947. *
  948. * <p>
  949. * For more information on the inheritable event mechanism see the
  950. * {@link com.vaadin.event com.vaadin.event package documentation}.
  951. * </p>
  952. *
  953. * @param eventType
  954. * the exact event type the <code>object</code> listens to.
  955. * @param target
  956. * the target object that has registered to listen to events of
  957. * type <code>eventType</code> with one or more methods.
  958. */
  959. public void removeListener(Class eventType, Object target) {
  960. if (eventRouter != null) {
  961. eventRouter.removeListener(eventType, target);
  962. }
  963. }
  964. /**
  965. * Removes one registered listener method. The given method owned by the
  966. * given object will no longer be called when the specified events are
  967. * generated by this component.
  968. *
  969. * <p>
  970. * For more information on the inheritable event mechanism see the
  971. * {@link com.vaadin.event com.vaadin.event package documentation}.
  972. * </p>
  973. *
  974. * @param eventType
  975. * the exact event type the <code>object</code> listens to.
  976. * @param target
  977. * target object that has registered to listen to events of type
  978. * <code>eventType</code> with one or more methods.
  979. * @param method
  980. * the method owned by <code>target</code> that's registered to
  981. * listen to events of type <code>eventType</code>.
  982. */
  983. public void removeListener(Class eventType, Object target, Method method) {
  984. if (eventRouter != null) {
  985. eventRouter.removeListener(eventType, target, method);
  986. }
  987. }
  988. /**
  989. * <p>
  990. * Removes one registered listener method. The given method owned by the
  991. * given object will no longer be called when the specified events are
  992. * generated by this component.
  993. * </p>
  994. *
  995. * <p>
  996. * This version of <code>removeListener</code> gets the name of the
  997. * activation method as a parameter. The actual method is reflected from
  998. * <code>target</code>, and unless exactly one match is found,
  999. * <code>java.lang.IllegalArgumentException</code> is thrown.
  1000. * </p>
  1001. *
  1002. * <p>
  1003. * For more information on the inheritable event mechanism see the
  1004. * {@link com.vaadin.event com.vaadin.event package documentation}.
  1005. * </p>
  1006. *
  1007. * @param eventType
  1008. * the exact event type the <code>object</code> listens to.
  1009. * @param target
  1010. * the target object that has registered to listen to events of
  1011. * type <code>eventType</code> with one or more methods.
  1012. * @param methodName
  1013. * the name of the method owned by <code>target</code> that's
  1014. * registered to listen to events of type <code>eventType</code>.
  1015. */
  1016. public void removeListener(Class eventType, Object target, String methodName) {
  1017. if (eventRouter != null) {
  1018. eventRouter.removeListener(eventType, target, methodName);
  1019. }
  1020. }
  1021. /**
  1022. * Sends the event to all listeners.
  1023. *
  1024. * @param event
  1025. * the Event to be sent to all listeners.
  1026. */
  1027. protected void fireEvent(Component.Event event) {
  1028. if (eventRouter != null) {
  1029. eventRouter.fireEvent(event);
  1030. }
  1031. }
  1032. /* Component event framework */
  1033. /*
  1034. * Registers a new listener to listen events generated by this component.
  1035. * Don't add a JavaDoc comment here, we use the default documentation from
  1036. * implemented interface.
  1037. */
  1038. public void addListener(Component.Listener listener) {
  1039. addListener(Component.Event.class, listener, COMPONENT_EVENT_METHOD);
  1040. }
  1041. /*
  1042. * Removes a previously registered listener from this component. Don't add a
  1043. * JavaDoc comment here, we use the default documentation from implemented
  1044. * interface.
  1045. */
  1046. public void removeListener(Component.Listener listener) {
  1047. removeListener(Component.Event.class, listener, COMPONENT_EVENT_METHOD);
  1048. }
  1049. /**
  1050. * Emits the component event. It is transmitted to all registered listeners
  1051. * interested in such events.
  1052. */
  1053. protected void fireComponentEvent() {
  1054. fireEvent(new Component.Event(this));
  1055. }
  1056. /**
  1057. * Emits the component error event. It is transmitted to all registered
  1058. * listeners interested in such events.
  1059. */
  1060. protected void fireComponentErrorEvent() {
  1061. fireEvent(new Component.ErrorEvent(getComponentError(), this));
  1062. }
  1063. /**
  1064. * Sets the data object, that can be used for any application specific data.
  1065. * The component does not use or modify this data.
  1066. *
  1067. * @param data
  1068. * the Application specific data.
  1069. * @since 3.1
  1070. */
  1071. public void setData(Object data) {
  1072. applicationData = data;
  1073. }
  1074. /**
  1075. * Gets the application specific data. See {@link #setData(Object)}.
  1076. *
  1077. * @return the Application specific data set with setData function.
  1078. * @since 3.1
  1079. */
  1080. public Object getData() {
  1081. return applicationData;
  1082. }
  1083. /* Sizeable and other size related methods */
  1084. /*
  1085. * (non-Javadoc)
  1086. *
  1087. * @see com.vaadin.terminal.Sizeable#getHeight()
  1088. */
  1089. public float getHeight() {
  1090. return height;
  1091. }
  1092. /*
  1093. * (non-Javadoc)
  1094. *
  1095. * @see com.vaadin.terminal.Sizeable#getHeightUnits()
  1096. */
  1097. public int getHeightUnits() {
  1098. return heightUnit;
  1099. }
  1100. /*
  1101. * (non-Javadoc)
  1102. *
  1103. * @see com.vaadin.terminal.Sizeable#getWidth()
  1104. */
  1105. public float getWidth() {
  1106. return width;
  1107. }
  1108. /*
  1109. * (non-Javadoc)
  1110. *
  1111. * @see com.vaadin.terminal.Sizeable#getWidthUnits()
  1112. */
  1113. public int getWidthUnits() {
  1114. return widthUnit;
  1115. }
  1116. /*
  1117. * (non-Javadoc)
  1118. *
  1119. * @see com.vaadin.terminal.Sizeable#setHeight(float)
  1120. */
  1121. @Deprecated
  1122. public void setHeight(float height) {
  1123. setHeight(height, getHeightUnits());
  1124. }
  1125. /*
  1126. * (non-Javadoc)
  1127. *
  1128. * @see com.vaadin.terminal.Sizeable#setHeightUnits(int)
  1129. */
  1130. @Deprecated
  1131. public void setHeightUnits(int unit) {
  1132. setHeight(getHeight(), unit);
  1133. }
  1134. /*
  1135. * (non-Javadoc)
  1136. *
  1137. * @see com.vaadin.terminal.Sizeable#setHeight(float, int)
  1138. */
  1139. public void setHeight(float height, int unit) {
  1140. this.height = height;
  1141. heightUnit = unit;
  1142. requestRepaint();
  1143. // ComponentSizeValidator.setHeightLocation(this);
  1144. }
  1145. /*
  1146. * (non-Javadoc)
  1147. *
  1148. * @see com.vaadin.terminal.Sizeable#setSizeFull()
  1149. */
  1150. public void setSizeFull() {
  1151. setWidth(100, UNITS_PERCENTAGE);
  1152. setHeight(100, UNITS_PERCENTAGE);
  1153. }
  1154. /*
  1155. * (non-Javadoc)
  1156. *
  1157. * @see com.vaadin.terminal.Sizeable#setSizeUndefined()
  1158. */
  1159. public void setSizeUndefined() {
  1160. setWidth(-1, UNITS_PIXELS);
  1161. setHeight(-1, UNITS_PIXELS);
  1162. }
  1163. /*
  1164. * (non-Javadoc)
  1165. *
  1166. * @see com.vaadin.terminal.Sizeable#setWidth(float)
  1167. */
  1168. @Deprecated
  1169. public void setWidth(float width) {
  1170. setWidth(width, getWidthUnits());
  1171. }
  1172. /*
  1173. * (non-Javadoc)
  1174. *
  1175. * @see com.vaadin.terminal.Sizeable#setWidthUnits(int)
  1176. */
  1177. @Deprecated
  1178. public void setWidthUnits(int unit) {
  1179. setWidth(getWidth(), unit);
  1180. }
  1181. /*
  1182. * (non-Javadoc)
  1183. *
  1184. * @see com.vaadin.terminal.Sizeable#setWidth(float, int)
  1185. */
  1186. public void setWidth(float width, int unit) {
  1187. this.width = width;
  1188. widthUnit = unit;
  1189. requestRepaint();
  1190. // ComponentSizeValidator.setWidthLocation(this);
  1191. }
  1192. /*
  1193. * (non-Javadoc)
  1194. *
  1195. * @see com.vaadin.terminal.Sizeable#setWidth(java.lang.String)
  1196. */
  1197. public void setWidth(String width) {
  1198. float[] p = parseStringSize(width);
  1199. setWidth(p[0], (int) p[1]);
  1200. }
  1201. /*
  1202. * (non-Javadoc)
  1203. *
  1204. * @see com.vaadin.terminal.Sizeable#setHeight(java.lang.String)
  1205. */
  1206. public void setHeight(String height) {
  1207. float[] p = parseStringSize(height);
  1208. setHeight(p[0], (int) p[1]);
  1209. }
  1210. /*
  1211. * Returns array with size in index 0 unit in index 1. Null or empty string
  1212. * will produce {-1,UNITS_PIXELS}
  1213. */
  1214. private static float[] parseStringSize(String s) {
  1215. float[] values = { -1, UNITS_PIXELS };
  1216. if (s == null) {
  1217. return values;
  1218. }
  1219. s = s.trim();
  1220. if ("".equals(s)) {
  1221. return values;
  1222. }
  1223. Matcher matcher = sizePattern.matcher(s);
  1224. if (matcher.find()) {
  1225. values[0] = Float.parseFloat(matcher.group(1));
  1226. if (values[0] < 0) {
  1227. values[0] = -1;
  1228. } else {
  1229. String unit = matcher.group(3);
  1230. if (unit == null) {
  1231. values[1] = UNITS_PIXELS;
  1232. } else if (unit.equals("px")) {
  1233. values[1] = UNITS_PIXELS;
  1234. } else if (unit.equals("%")) {
  1235. values[1] = UNITS_PERCENTAGE;
  1236. } else if (unit.equals("em")) {
  1237. values[1] = UNITS_EM;
  1238. } else if (unit.equals("ex")) {
  1239. values[1] = UNITS_EX;
  1240. } else if (unit.equals("in")) {
  1241. values[1] = UNITS_INCH;
  1242. } else if (unit.equals("cm")) {
  1243. values[1] = UNITS_CM;
  1244. } else if (unit.equals("mm")) {
  1245. values[1] = UNITS_MM;
  1246. } else if (unit.equals("pt")) {
  1247. values[1] = UNITS_POINTS;
  1248. } else if (unit.equals("pc")) {
  1249. values[1] = UNITS_PICAS;
  1250. }
  1251. }
  1252. } else {
  1253. throw new IllegalArgumentException("Invalid size argument: \"" + s
  1254. + "\" (should match " + sizePattern.pattern() + ")");
  1255. }
  1256. return values;
  1257. }
  1258. public interface ComponentErrorEvent extends Terminal.ErrorEvent {
  1259. }
  1260. public interface ComponentErrorHandler extends Serializable {
  1261. /**
  1262. * Handle the component error
  1263. *
  1264. * @param event
  1265. * @return True if the error has been handled False, otherwise
  1266. */
  1267. public boolean handleComponentError(ComponentErrorEvent event);
  1268. }
  1269. /**
  1270. * Gets the error handler for the component.
  1271. *
  1272. * The error handler is dispatched whenever there is an error processing the
  1273. * data coming from the client.
  1274. *
  1275. * @return
  1276. */
  1277. public ComponentErrorHandler getErrorHandler() {
  1278. return errorHandler;
  1279. }
  1280. /**
  1281. * Sets the error handler for the component.
  1282. *
  1283. * The error handler is dispatched whenever there is an error processing the
  1284. * data coming from the client.
  1285. *
  1286. * If the error handler is not set, the application error handler is used to
  1287. * handle the exception.
  1288. *
  1289. * @param errorHandler
  1290. * AbstractField specific error handler
  1291. */
  1292. public void setErrorHandler(ComponentErrorHandler errorHandler) {
  1293. this.errorHandler = errorHandler;
  1294. }
  1295. /**
  1296. * Handle the component error event.
  1297. *
  1298. * @param error
  1299. * Error event to handle
  1300. * @return True if the error has been handled False, otherwise. If the error
  1301. * haven't been handled by this component, it will be handled in the
  1302. * application error handler.
  1303. */
  1304. public boolean handleError(ComponentErrorEvent error) {
  1305. if (errorHandler != null) {
  1306. return errorHandler.handleComponentError(error);
  1307. }
  1308. return false;
  1309. }
  1310. }