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 42KB

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