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

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