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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.Serializable;
  6. import java.lang.reflect.InvocationHandler;
  7. import java.lang.reflect.Method;
  8. import java.lang.reflect.Proxy;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.Collections;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Locale;
  17. import java.util.Map;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import com.vaadin.Application;
  21. import com.vaadin.event.ActionManager;
  22. import com.vaadin.event.EventRouter;
  23. import com.vaadin.event.MethodEventSource;
  24. import com.vaadin.event.ShortcutListener;
  25. import com.vaadin.terminal.ErrorMessage;
  26. import com.vaadin.terminal.PaintException;
  27. import com.vaadin.terminal.PaintTarget;
  28. import com.vaadin.terminal.PaintTarget.PaintStatus;
  29. import com.vaadin.terminal.Resource;
  30. import com.vaadin.terminal.Terminal;
  31. import com.vaadin.terminal.gwt.client.ComponentState;
  32. import com.vaadin.terminal.gwt.client.communication.ClientRpc;
  33. import com.vaadin.terminal.gwt.server.ClientMethodInvocation;
  34. import com.vaadin.terminal.gwt.server.ComponentSizeValidator;
  35. import com.vaadin.terminal.gwt.server.ResourceReference;
  36. import com.vaadin.terminal.gwt.server.RpcManager;
  37. import com.vaadin.terminal.gwt.server.RpcTarget;
  38. import com.vaadin.terminal.gwt.server.ServerRpcManager;
  39. import com.vaadin.tools.ReflectTools;
  40. /**
  41. * An abstract class that defines default implementation for the
  42. * {@link Component} interface. Basic UI components that are not derived from an
  43. * external component can inherit this class to easily qualify as Vaadin
  44. * components. Most components in Vaadin do just that.
  45. *
  46. * @author Vaadin Ltd.
  47. * @version
  48. * @VERSION@
  49. * @since 3.0
  50. */
  51. @SuppressWarnings("serial")
  52. public abstract class AbstractComponent implements Component, MethodEventSource {
  53. /* Private members */
  54. /**
  55. * Application specific data object. The component does not use or modify
  56. * this.
  57. */
  58. private Object applicationData;
  59. /**
  60. * The container this component resides in.
  61. */
  62. private HasComponents parent = null;
  63. /**
  64. * The EventRouter used for the event model.
  65. */
  66. private EventRouter eventRouter = null;
  67. /**
  68. * The internal error message of the component.
  69. */
  70. private ErrorMessage componentError = null;
  71. /**
  72. * Locale of this component.
  73. */
  74. private Locale locale;
  75. /**
  76. * The component should receive focus (if {@link Focusable}) when attached.
  77. */
  78. private boolean delayedFocus;
  79. /**
  80. * List of repaint request listeners or null if not listened at all.
  81. */
  82. private LinkedList<RepaintRequestListener> repaintRequestListeners = null;
  83. /* Sizeable fields */
  84. private float width = SIZE_UNDEFINED;
  85. private float height = SIZE_UNDEFINED;
  86. private Unit widthUnit = Unit.PIXELS;
  87. private Unit heightUnit = Unit.PIXELS;
  88. private static final Pattern sizePattern = Pattern
  89. .compile("^(-?\\d+(\\.\\d+)?)(%|px|em|ex|in|cm|mm|pt|pc)?$");
  90. private ComponentErrorHandler errorHandler = null;
  91. /**
  92. * Keeps track of the Actions added to this component; the actual
  93. * handling/notifying is delegated, usually to the containing window.
  94. */
  95. private ActionManager actionManager;
  96. /**
  97. * A map from client to server RPC interface class to the RPC call manager
  98. * that handles incoming RPC calls for that interface.
  99. */
  100. private Map<Class<?>, RpcManager> rpcManagerMap = new HashMap<Class<?>, RpcManager>();
  101. /**
  102. * A map from server to client RPC interface class to the RPC proxy that
  103. * sends ourgoing RPC calls for that interface.
  104. */
  105. private Map<Class<?>, ClientRpc> rpcProxyMap = new HashMap<Class<?>, ClientRpc>();
  106. /**
  107. * Shared state object to be communicated from the server to the client when
  108. * modified.
  109. */
  110. private ComponentState sharedState;
  111. /**
  112. * Pending RPC method invocations to be sent.
  113. */
  114. private ArrayList<ClientMethodInvocation> pendingInvocations = new ArrayList<ClientMethodInvocation>();
  115. private String connectorId;
  116. /* Constructor */
  117. /**
  118. * Constructs a new Component.
  119. */
  120. public AbstractComponent() {
  121. // ComponentSizeValidator.setCreationLocation(this);
  122. }
  123. /* Get/Set component properties */
  124. public void setDebugId(String id) {
  125. getState().setDebugId(id);
  126. }
  127. public String getDebugId() {
  128. return getState().getDebugId();
  129. }
  130. /**
  131. * Gets style for component. Multiple styles are joined with spaces.
  132. *
  133. * @return the component's styleValue of property style.
  134. * @deprecated Use getStyleName() instead; renamed for consistency and to
  135. * indicate that "style" should not be used to switch client
  136. * side implementation, only to style the component.
  137. */
  138. @Deprecated
  139. public String getStyle() {
  140. return getStyleName();
  141. }
  142. /**
  143. * Sets and replaces all previous style names of the component. This method
  144. * will trigger a {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  145. * RepaintRequestEvent}.
  146. *
  147. * @param style
  148. * the new style of the component.
  149. * @deprecated Use setStyleName() instead; renamed for consistency and to
  150. * indicate that "style" should not be used to switch client
  151. * side implementation, only to style the component.
  152. */
  153. @Deprecated
  154. public void setStyle(String style) {
  155. setStyleName(style);
  156. }
  157. /*
  158. * Gets the component's style. Don't add a JavaDoc comment here, we use the
  159. * default documentation from implemented interface.
  160. */
  161. public String getStyleName() {
  162. String s = "";
  163. if (getState().getStyles() != null) {
  164. for (final Iterator<String> it = getState().getStyles().iterator(); it
  165. .hasNext();) {
  166. s += it.next();
  167. if (it.hasNext()) {
  168. s += " ";
  169. }
  170. }
  171. }
  172. return s;
  173. }
  174. /*
  175. * Sets the component's style. Don't add a JavaDoc comment here, we use the
  176. * default documentation from implemented interface.
  177. */
  178. public void setStyleName(String style) {
  179. if (style == null || "".equals(style)) {
  180. getState().setStyles(null);
  181. requestRepaint();
  182. return;
  183. }
  184. if (getState().getStyles() == null) {
  185. getState().setStyles(new ArrayList<String>());
  186. }
  187. List<String> styles = getState().getStyles();
  188. styles.clear();
  189. String[] styleParts = style.split(" +");
  190. for (String part : styleParts) {
  191. if (part.length() > 0) {
  192. styles.add(part);
  193. }
  194. }
  195. requestRepaint();
  196. }
  197. public void addStyleName(String style) {
  198. if (style == null || "".equals(style)) {
  199. return;
  200. }
  201. if (style.contains(" ")) {
  202. // Split space separated style names and add them one by one.
  203. for (String realStyle : style.split(" ")) {
  204. addStyleName(realStyle);
  205. }
  206. return;
  207. }
  208. if (getState().getStyles() == null) {
  209. getState().setStyles(new ArrayList<String>());
  210. }
  211. List<String> styles = getState().getStyles();
  212. if (!styles.contains(style)) {
  213. styles.add(style);
  214. requestRepaint();
  215. }
  216. }
  217. public void removeStyleName(String style) {
  218. if (getState().getStyles() != null) {
  219. String[] styleParts = style.split(" +");
  220. for (String part : styleParts) {
  221. if (part.length() > 0) {
  222. getState().getStyles().remove(part);
  223. }
  224. }
  225. requestRepaint();
  226. }
  227. }
  228. /*
  229. * Get's the component's caption. Don't add a JavaDoc comment here, we use
  230. * the default documentation from implemented interface.
  231. */
  232. public String getCaption() {
  233. return getState().getCaption();
  234. }
  235. /**
  236. * Sets the component's caption <code>String</code>. Caption is the visible
  237. * name of the component. This method will trigger a
  238. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  239. * RepaintRequestEvent}.
  240. *
  241. * @param caption
  242. * the new caption <code>String</code> for the component.
  243. */
  244. public void setCaption(String caption) {
  245. getState().setCaption(caption);
  246. requestRepaint();
  247. }
  248. /*
  249. * Don't add a JavaDoc comment here, we use the default documentation from
  250. * implemented interface.
  251. */
  252. public Locale getLocale() {
  253. if (locale != null) {
  254. return locale;
  255. }
  256. if (parent != null) {
  257. return parent.getLocale();
  258. }
  259. final Application app = getApplication();
  260. if (app != null) {
  261. return app.getLocale();
  262. }
  263. return null;
  264. }
  265. /**
  266. * Sets the locale of this component.
  267. *
  268. * <pre>
  269. * // Component for which the locale is meaningful
  270. * InlineDateField date = new InlineDateField(&quot;Datum&quot;);
  271. *
  272. * // German language specified with ISO 639-1 language
  273. * // code and ISO 3166-1 alpha-2 country code.
  274. * date.setLocale(new Locale(&quot;de&quot;, &quot;DE&quot;));
  275. *
  276. * date.setResolution(DateField.RESOLUTION_DAY);
  277. * layout.addComponent(date);
  278. * </pre>
  279. *
  280. *
  281. * @param locale
  282. * the locale to become this component's locale.
  283. */
  284. public void setLocale(Locale locale) {
  285. this.locale = locale;
  286. // FIXME: Reload value if there is a converter
  287. requestRepaint();
  288. }
  289. /*
  290. * Gets the component's icon resource. Don't add a JavaDoc comment here, we
  291. * use the default documentation from implemented interface.
  292. */
  293. public Resource getIcon() {
  294. ResourceReference ref = ((ResourceReference) getState().getIcon());
  295. if (ref == null) {
  296. return null;
  297. } else {
  298. return ref.getResource();
  299. }
  300. }
  301. /**
  302. * Sets the component's icon. This method will trigger a
  303. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  304. * RepaintRequestEvent}.
  305. *
  306. * @param icon
  307. * the icon to be shown with the component's caption.
  308. */
  309. public void setIcon(Resource icon) {
  310. if (icon == null) {
  311. getState().setIcon(null);
  312. } else {
  313. getState().setIcon(new ResourceReference(icon));
  314. }
  315. requestRepaint();
  316. }
  317. /*
  318. * (non-Javadoc)
  319. *
  320. * @see com.vaadin.ui.Component#isEnabled()
  321. */
  322. public boolean isEnabled() {
  323. return getState().isEnabled();
  324. }
  325. /*
  326. * (non-Javadoc)
  327. *
  328. * @see com.vaadin.ui.Component#setEnabled(boolean)
  329. */
  330. public void setEnabled(boolean enabled) {
  331. if (getState().isEnabled() != enabled) {
  332. getState().setEnabled(enabled);
  333. requestRepaint();
  334. }
  335. }
  336. /*
  337. * (non-Javadoc)
  338. *
  339. * @see com.vaadin.terminal.gwt.client.Connector#isConnectorEnabled()
  340. */
  341. public boolean isConnectorEnabled() {
  342. if (getParent() == null) {
  343. // No parent -> the component cannot receive updates from the client
  344. return false;
  345. } else {
  346. boolean thisEnabledAndVisible = isEnabled() && isVisible();
  347. if (!thisEnabledAndVisible) {
  348. return false;
  349. }
  350. if (!getParent().isConnectorEnabled()) {
  351. return false;
  352. }
  353. return getParent().isComponentVisible(this);
  354. }
  355. }
  356. /*
  357. * Tests if the component is in the immediate mode. Don't add a JavaDoc
  358. * comment here, we use the default documentation from implemented
  359. * interface.
  360. */
  361. public boolean isImmediate() {
  362. return getState().isImmediate();
  363. }
  364. /**
  365. * Sets the component's immediate mode to the specified status. This method
  366. * will trigger a {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  367. * RepaintRequestEvent}.
  368. *
  369. * @param immediate
  370. * the boolean value specifying if the component should be in the
  371. * immediate mode after the call.
  372. * @see Component#isImmediate()
  373. */
  374. public void setImmediate(boolean immediate) {
  375. getState().setImmediate(immediate);
  376. requestRepaint();
  377. }
  378. /*
  379. * (non-Javadoc)
  380. *
  381. * @see com.vaadin.ui.Component#isVisible()
  382. */
  383. public boolean isVisible() {
  384. return getState().isVisible();
  385. }
  386. /*
  387. * (non-Javadoc)
  388. *
  389. * @see com.vaadin.ui.Component#setVisible(boolean)
  390. */
  391. public void setVisible(boolean visible) {
  392. if (getState().isVisible() == visible) {
  393. return;
  394. }
  395. getState().setVisible(visible);
  396. requestRepaint();
  397. if (getParent() != null) {
  398. // Must always repaint the parent (at least the hierarchy) when
  399. // visibility of a child component changes.
  400. getParent().requestRepaint();
  401. }
  402. }
  403. /**
  404. * <p>
  405. * Gets the component's description, used in tooltips and can be displayed
  406. * directly in certain other components such as forms. The description can
  407. * be used to briefly describe the state of the component to the user. The
  408. * description string may contain certain XML tags:
  409. * </p>
  410. *
  411. * <p>
  412. * <table border=1>
  413. * <tr>
  414. * <td width=120><b>Tag</b></td>
  415. * <td width=120><b>Description</b></td>
  416. * <td width=120><b>Example</b></td>
  417. * </tr>
  418. * <tr>
  419. * <td>&lt;b></td>
  420. * <td>bold</td>
  421. * <td><b>bold text</b></td>
  422. * </tr>
  423. * <tr>
  424. * <td>&lt;i></td>
  425. * <td>italic</td>
  426. * <td><i>italic text</i></td>
  427. * </tr>
  428. * <tr>
  429. * <td>&lt;u></td>
  430. * <td>underlined</td>
  431. * <td><u>underlined text</u></td>
  432. * </tr>
  433. * <tr>
  434. * <td>&lt;br></td>
  435. * <td>linebreak</td>
  436. * <td>N/A</td>
  437. * </tr>
  438. * <tr>
  439. * <td>&lt;ul><br>
  440. * &lt;li>item1<br>
  441. * &lt;li>item1<br>
  442. * &lt;/ul></td>
  443. * <td>item list</td>
  444. * <td>
  445. * <ul>
  446. * <li>item1
  447. * <li>item2
  448. * </ul>
  449. * </td>
  450. * </tr>
  451. * </table>
  452. * </p>
  453. *
  454. * <p>
  455. * These tags may be nested.
  456. * </p>
  457. *
  458. * @return component's description <code>String</code>
  459. */
  460. public String getDescription() {
  461. return getState().getDescription();
  462. }
  463. /**
  464. * Sets the component's description. See {@link #getDescription()} for more
  465. * information on what the description is. This method will trigger a
  466. * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent
  467. * RepaintRequestEvent}.
  468. *
  469. * The description is displayed as HTML/XHTML in tooltips or directly in
  470. * certain components so care should be taken to avoid creating the
  471. * possibility for HTML injection and possibly XSS vulnerabilities.
  472. *
  473. * @param description
  474. * the new description string for the component.
  475. */
  476. public void setDescription(String description) {
  477. getState().setDescription(description);
  478. requestRepaint();
  479. }
  480. /*
  481. * Gets the component's parent component. Don't add a JavaDoc comment here,
  482. * we use the default documentation from implemented interface.
  483. */
  484. public HasComponents getParent() {
  485. return parent;
  486. }
  487. /*
  488. * Sets the parent component. Don't add a JavaDoc comment here, we use the
  489. * default documentation from implemented interface.
  490. */
  491. public void setParent(HasComponents parent) {
  492. // If the parent is not changed, don't do anything
  493. if (parent == this.parent) {
  494. return;
  495. }
  496. if (parent != null && this.parent != null) {
  497. throw new IllegalStateException(getClass().getName()
  498. + " already has a parent.");
  499. }
  500. // Send detach event if the component have been connected to a window
  501. if (getApplication() != null) {
  502. detach();
  503. }
  504. // Connect to new parent
  505. this.parent = parent;
  506. // Send attach event if connected to a window
  507. if (getApplication() != null) {
  508. attach();
  509. }
  510. }
  511. /**
  512. * Gets the error message for this component.
  513. *
  514. * @return ErrorMessage containing the description of the error state of the
  515. * component or null, if the component contains no errors. Extending
  516. * classes should override this method if they support other error
  517. * message types such as validation errors or buffering errors. The
  518. * returned error message contains information about all the errors.
  519. */
  520. public ErrorMessage getErrorMessage() {
  521. return componentError;
  522. }
  523. /**
  524. * Gets the component's error message.
  525. *
  526. * @link Terminal.ErrorMessage#ErrorMessage(String, int)
  527. *
  528. * @return the component's error message.
  529. */
  530. public ErrorMessage getComponentError() {
  531. return componentError;
  532. }
  533. /**
  534. * Sets the component's error message. The message may contain certain XML
  535. * tags, for more information see
  536. *
  537. * @link Component.ErrorMessage#ErrorMessage(String, int)
  538. *
  539. * @param componentError
  540. * the new <code>ErrorMessage</code> of the component.
  541. */
  542. public void setComponentError(ErrorMessage componentError) {
  543. this.componentError = componentError;
  544. fireComponentErrorEvent();
  545. requestRepaint();
  546. }
  547. /*
  548. * Tests if the component is in read-only mode. Don't add a JavaDoc comment
  549. * here, we use the default documentation from implemented interface.
  550. */
  551. public boolean isReadOnly() {
  552. return getState().isReadOnly();
  553. }
  554. /*
  555. * Sets the component's read-only mode. Don't add a JavaDoc comment here, we
  556. * use the default documentation from implemented interface.
  557. */
  558. public void setReadOnly(boolean readOnly) {
  559. getState().setReadOnly(readOnly);
  560. requestRepaint();
  561. }
  562. /*
  563. * Gets the parent window of the component. Don't add a JavaDoc comment
  564. * here, we use the default documentation from implemented interface.
  565. */
  566. public Root getRoot() {
  567. if (parent == null) {
  568. return null;
  569. } else {
  570. return parent.getRoot();
  571. }
  572. }
  573. /*
  574. * Notify the component that it's attached to a window. Don't add a JavaDoc
  575. * comment here, we use the default documentation from implemented
  576. * interface.
  577. */
  578. public void attach() {
  579. getRoot().componentAttached(this);
  580. requestRepaint();
  581. if (delayedFocus) {
  582. focus();
  583. }
  584. setActionManagerViewer();
  585. }
  586. /*
  587. * Detach the component from application. Don't add a JavaDoc comment here,
  588. * we use the default documentation from implemented interface.
  589. */
  590. public void detach() {
  591. if (actionManager != null) {
  592. // Remove any existing viewer. Root cast is just to make the
  593. // compiler happy
  594. actionManager.setViewer((Root) null);
  595. }
  596. getRoot().componentDetached(this);
  597. }
  598. /**
  599. * Sets the focus for this component if the component is {@link Focusable}.
  600. */
  601. protected void focus() {
  602. if (this instanceof Focusable) {
  603. final Application app = getApplication();
  604. if (app != null) {
  605. getRoot().setFocusedComponent((Focusable) this);
  606. delayedFocus = false;
  607. } else {
  608. delayedFocus = true;
  609. }
  610. }
  611. }
  612. /**
  613. * Gets the application object to which the component is attached.
  614. *
  615. * <p>
  616. * The method will return {@code null} if the component is not currently
  617. * attached to an application. This is often a problem in constructors of
  618. * regular components and in the initializers of custom composite
  619. * components. A standard workaround is to move the problematic
  620. * initialization to {@link #attach()}, as described in the documentation of
  621. * the method.
  622. * </p>
  623. * <p>
  624. * <b>This method is not meant to be overridden. Due to CDI requirements we
  625. * cannot declare it as final even though it should be final.</b>
  626. * </p>
  627. *
  628. * @return the parent application of the component or <code>null</code>.
  629. * @see #attach()
  630. */
  631. public Application getApplication() {
  632. if (parent == null) {
  633. return null;
  634. } else {
  635. return parent.getApplication();
  636. }
  637. }
  638. /* Component painting */
  639. @Deprecated
  640. public void requestRepaintRequests() {
  641. // This is no longer needed. Remove when Component no longer extends
  642. // Paintable
  643. }
  644. /**
  645. *
  646. * <p>
  647. * Paints the Paintable into a UIDL stream. This method creates the UIDL
  648. * sequence describing it and outputs it to the given UIDL stream.
  649. * </p>
  650. *
  651. * <p>
  652. * It is called when the contents of the component should be painted in
  653. * response to the component first being shown or having been altered so
  654. * that its visual representation is changed.
  655. * </p>
  656. *
  657. * <p>
  658. * <b>Do not override this to paint your component.</b> Override
  659. * {@link #paintContent(PaintTarget)} instead.
  660. * </p>
  661. *
  662. *
  663. * @param target
  664. * the target UIDL stream where the component should paint itself
  665. * to.
  666. * @throws PaintException
  667. * if the paint operation failed.
  668. */
  669. public void paint(PaintTarget target) throws PaintException {
  670. // Only paint content of visible components.
  671. if (!isVisibleInContext()) {
  672. return;
  673. }
  674. final String tag = target.getTag(this);
  675. final PaintStatus status = target.startPaintable(this, tag);
  676. if (PaintStatus.DEFER == status) {
  677. // nothing to do but flag as deferred and close the paintable tag
  678. // paint() will be called again later to paint the contents
  679. target.addAttribute("deferred", true);
  680. } else {
  681. // Paint the contents of the component
  682. paintContent(target);
  683. }
  684. target.endPaintable(this);
  685. }
  686. /**
  687. * Checks if the component is visible and its parent is visible,
  688. * recursively.
  689. * <p>
  690. * This is only a helper until paint is moved away from this class.
  691. *
  692. * @return
  693. */
  694. @Deprecated
  695. protected boolean isVisibleInContext() {
  696. HasComponents p = getParent();
  697. while (p != null) {
  698. if (!p.isVisible()) {
  699. return false;
  700. }
  701. p = p.getParent();
  702. }
  703. if (getParent() != null && !getParent().isComponentVisible(this)) {
  704. return false;
  705. }
  706. // All parents visible, return this state
  707. return isVisible();
  708. }
  709. /**
  710. * Build CSS compatible string representation of height.
  711. *
  712. * @return CSS height
  713. */
  714. private String getCSSHeight() {
  715. if (getHeightUnits() == Unit.PIXELS) {
  716. return ((int) getHeight()) + getHeightUnits().getSymbol();
  717. } else {
  718. return getHeight() + getHeightUnits().getSymbol();
  719. }
  720. }
  721. /**
  722. * Build CSS compatible string representation of width.
  723. *
  724. * @return CSS width
  725. */
  726. private String getCSSWidth() {
  727. if (getWidthUnits() == Unit.PIXELS) {
  728. return ((int) getWidth()) + getWidthUnits().getSymbol();
  729. } else {
  730. return getWidth() + getWidthUnits().getSymbol();
  731. }
  732. }
  733. /**
  734. * Paints any needed component-specific things to the given UIDL stream. The
  735. * more general {@link #paint(PaintTarget)} method handles all general
  736. * attributes common to all components, and it calls this method to paint
  737. * any component-specific attributes to the UIDL stream.
  738. *
  739. * @param target
  740. * the target UIDL stream where the component should paint itself
  741. * to
  742. * @throws PaintException
  743. * if the paint operation failed.
  744. */
  745. public void paintContent(PaintTarget target) throws PaintException {
  746. }
  747. /**
  748. * Returns the shared state bean with information to be sent from the server
  749. * to the client.
  750. *
  751. * Subclasses should override this method and set any relevant fields of the
  752. * state returned by super.getState().
  753. *
  754. * @since 7.0
  755. *
  756. * @return updated component shared state
  757. */
  758. public ComponentState getState() {
  759. if (null == sharedState) {
  760. sharedState = createState();
  761. }
  762. // TODO This logic should be on the client side and the state should
  763. // simply be a data object with "width" and "height".
  764. if (getHeight() >= 0
  765. && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator
  766. .parentCanDefineHeight(this))) {
  767. sharedState.setHeight("" + getCSSHeight());
  768. } else {
  769. sharedState.setHeight("");
  770. }
  771. if (getWidth() >= 0
  772. && (getWidthUnits() != Unit.PERCENTAGE || ComponentSizeValidator
  773. .parentCanDefineWidth(this))) {
  774. sharedState.setWidth("" + getCSSWidth());
  775. } else {
  776. sharedState.setWidth("");
  777. }
  778. ErrorMessage error = getErrorMessage();
  779. if (null != error) {
  780. sharedState.setErrorMessage(error.getFormattedHtmlMessage());
  781. }
  782. return sharedState;
  783. }
  784. /**
  785. * Creates the shared state bean to be used in server to client
  786. * communication.
  787. *
  788. * Subclasses should implement this method and return a new instance of the
  789. * correct state class.
  790. *
  791. * All configuration of the values of the state should be performed in
  792. * {@link #getState()}, not in {@link #createState()}.
  793. *
  794. * @since 7.0
  795. *
  796. * @return new shared state object
  797. */
  798. protected ComponentState createState() {
  799. return new ComponentState();
  800. }
  801. /* Documentation copied from interface */
  802. public void requestRepaint() {
  803. // Invisible components (by flag in this particular component) do not
  804. // need repaints
  805. if (!getState().isVisible()) {
  806. return;
  807. }
  808. fireRequestRepaintEvent();
  809. }
  810. /**
  811. * Fires the repaint request event.
  812. *
  813. * @param alreadyNotified
  814. */
  815. // Notify listeners only once
  816. private void fireRequestRepaintEvent() {
  817. // Notify the listeners
  818. if (repaintRequestListeners != null
  819. && !repaintRequestListeners.isEmpty()) {
  820. final Object[] listeners = repaintRequestListeners.toArray();
  821. final RepaintRequestEvent event = new RepaintRequestEvent(this);
  822. for (int i = 0; i < listeners.length; i++) {
  823. ((RepaintRequestListener) listeners[i]).repaintRequested(event);
  824. }
  825. }
  826. }
  827. /* Documentation copied from interface */
  828. public void addListener(RepaintRequestListener listener) {
  829. if (repaintRequestListeners == null) {
  830. repaintRequestListeners = new LinkedList<RepaintRequestListener>();
  831. }
  832. if (!repaintRequestListeners.contains(listener)) {
  833. repaintRequestListeners.add(listener);
  834. }
  835. }
  836. /* Documentation copied from interface */
  837. public void removeListener(RepaintRequestListener listener) {
  838. if (repaintRequestListeners != null) {
  839. repaintRequestListeners.remove(listener);
  840. if (repaintRequestListeners.isEmpty()) {
  841. repaintRequestListeners = null;
  842. }
  843. }
  844. }
  845. /* Component variable changes */
  846. /*
  847. * Invoked when the value of a variable has changed. Don't add a JavaDoc
  848. * comment here, we use the default documentation from implemented
  849. * interface.
  850. */
  851. public void changeVariables(Object source, Map<String, Object> variables) {
  852. }
  853. /* General event framework */
  854. private static final Method COMPONENT_EVENT_METHOD = ReflectTools
  855. .findMethod(Component.Listener.class, "componentEvent",
  856. Component.Event.class);
  857. /**
  858. * <p>
  859. * Registers a new listener with the specified activation method to listen
  860. * events generated by this component. If the activation method does not
  861. * have any arguments the event object will not be passed to it when it's
  862. * called.
  863. * </p>
  864. *
  865. * <p>
  866. * This method additionally informs the event-api to route events with the
  867. * given eventIdentifier to the components handleEvent function call.
  868. * </p>
  869. *
  870. * <p>
  871. * For more information on the inheritable event mechanism see the
  872. * {@link com.vaadin.event com.vaadin.event package documentation}.
  873. * </p>
  874. *
  875. * @param eventIdentifier
  876. * the identifier of the event to listen for
  877. * @param eventType
  878. * the type of the listened event. Events of this type or its
  879. * subclasses activate the listener.
  880. * @param target
  881. * the object instance who owns the activation method.
  882. * @param method
  883. * the activation method.
  884. *
  885. * @since 6.2
  886. */
  887. protected void addListener(String eventIdentifier, Class<?> eventType,
  888. Object target, Method method) {
  889. if (eventRouter == null) {
  890. eventRouter = new EventRouter();
  891. }
  892. boolean needRepaint = !eventRouter.hasListeners(eventType);
  893. eventRouter.addListener(eventType, target, method);
  894. if (needRepaint) {
  895. getState().addRegisteredEventListener(eventIdentifier);
  896. requestRepaint();
  897. }
  898. }
  899. /**
  900. * Checks if the given {@link Event} type is listened for this component.
  901. *
  902. * @param eventType
  903. * the event type to be checked
  904. * @return true if a listener is registered for the given event type
  905. */
  906. protected boolean hasListeners(Class<?> eventType) {
  907. return eventRouter != null && eventRouter.hasListeners(eventType);
  908. }
  909. /**
  910. * Removes all registered listeners matching the given parameters. Since
  911. * this method receives the event type and the listener object as
  912. * parameters, it will unregister all <code>object</code>'s methods that are
  913. * registered to listen to events of type <code>eventType</code> generated
  914. * by this component.
  915. *
  916. * <p>
  917. * This method additionally informs the event-api to stop routing events
  918. * with the given eventIdentifier to the components handleEvent function
  919. * call.
  920. * </p>
  921. *
  922. * <p>
  923. * For more information on the inheritable event mechanism see the
  924. * {@link com.vaadin.event com.vaadin.event package documentation}.
  925. * </p>
  926. *
  927. * @param eventIdentifier
  928. * the identifier of the event to stop listening for
  929. * @param eventType
  930. * the exact event type the <code>object</code> listens to.
  931. * @param target
  932. * the target object that has registered to listen to events of
  933. * type <code>eventType</code> with one or more methods.
  934. *
  935. * @since 6.2
  936. */
  937. protected void removeListener(String eventIdentifier, Class<?> eventType,
  938. Object target) {
  939. if (eventRouter != null) {
  940. eventRouter.removeListener(eventType, target);
  941. if (!eventRouter.hasListeners(eventType)) {
  942. getState().removeRegisteredEventListener(eventIdentifier);
  943. requestRepaint();
  944. }
  945. }
  946. }
  947. /**
  948. * <p>
  949. * Registers a new listener with the specified activation method to listen
  950. * events generated by this component. If the activation method does not
  951. * have any arguments the event object will not be passed to it when it's
  952. * called.
  953. * </p>
  954. *
  955. * <p>
  956. * For more information on the inheritable event mechanism see the
  957. * {@link com.vaadin.event com.vaadin.event package documentation}.
  958. * </p>
  959. *
  960. * @param eventType
  961. * the type of the listened event. Events of this type or its
  962. * subclasses activate the listener.
  963. * @param target
  964. * the object instance who owns the activation method.
  965. * @param method
  966. * the activation method.
  967. */
  968. public void addListener(Class<?> eventType, Object target, Method method) {
  969. if (eventRouter == null) {
  970. eventRouter = new EventRouter();
  971. }
  972. eventRouter.addListener(eventType, target, method);
  973. }
  974. /**
  975. * <p>
  976. * Convenience method for registering a new listener with the specified
  977. * activation method to listen events generated by this component. If the
  978. * activation method does not have any arguments the event object will not
  979. * be passed to it when it's called.
  980. * </p>
  981. *
  982. * <p>
  983. * This version of <code>addListener</code> gets the name of the activation
  984. * method as a parameter. The actual method is reflected from
  985. * <code>object</code>, and unless exactly one match is found,
  986. * <code>java.lang.IllegalArgumentException</code> is thrown.
  987. * </p>
  988. *
  989. * <p>
  990. * For more information on the inheritable event mechanism see the
  991. * {@link com.vaadin.event com.vaadin.event package documentation}.
  992. * </p>
  993. *
  994. * <p>
  995. * Note: Using this method is discouraged because it cannot be checked
  996. * during compilation. Use {@link #addListener(Class, Object, Method)} or
  997. * {@link #addListener(com.vaadin.ui.Component.Listener)} instead.
  998. * </p>
  999. *
  1000. * @param eventType
  1001. * the type of the listened event. Events of this type or its
  1002. * subclasses activate the listener.
  1003. * @param target
  1004. * the object instance who owns the activation method.
  1005. * @param methodName
  1006. * the name of the activation method.
  1007. */
  1008. public void addListener(Class<?> eventType, Object target, String methodName) {
  1009. if (eventRouter == null) {
  1010. eventRouter = new EventRouter();
  1011. }
  1012. eventRouter.addListener(eventType, target, methodName);
  1013. }
  1014. /**
  1015. * Removes all registered listeners matching the given parameters. Since
  1016. * this method receives the event type and the listener object as
  1017. * parameters, it will unregister all <code>object</code>'s methods that are
  1018. * registered to listen to events of type <code>eventType</code> generated
  1019. * by this component.
  1020. *
  1021. * <p>
  1022. * For more information on the inheritable event mechanism see the
  1023. * {@link com.vaadin.event com.vaadin.event package documentation}.
  1024. * </p>
  1025. *
  1026. * @param eventType
  1027. * the exact event type the <code>object</code> listens to.
  1028. * @param target
  1029. * the target object that has registered to listen to events of
  1030. * type <code>eventType</code> with one or more methods.
  1031. */
  1032. public void removeListener(Class<?> eventType, Object target) {
  1033. if (eventRouter != null) {
  1034. eventRouter.removeListener(eventType, target);
  1035. }
  1036. }
  1037. /**
  1038. * Removes one registered listener method. The given method owned by the
  1039. * given object will no longer be called when the specified events are
  1040. * generated by this component.
  1041. *
  1042. * <p>
  1043. * For more information on the inheritable event mechanism see the
  1044. * {@link com.vaadin.event com.vaadin.event package documentation}.
  1045. * </p>
  1046. *
  1047. * @param eventType
  1048. * the exact event type the <code>object</code> listens to.
  1049. * @param target
  1050. * target object that has registered to listen to events of type
  1051. * <code>eventType</code> with one or more methods.
  1052. * @param method
  1053. * the method owned by <code>target</code> that's registered to
  1054. * listen to events of type <code>eventType</code>.
  1055. */
  1056. public void removeListener(Class<?> eventType, Object target, Method method) {
  1057. if (eventRouter != null) {
  1058. eventRouter.removeListener(eventType, target, method);
  1059. }
  1060. }
  1061. /**
  1062. * <p>
  1063. * Removes one registered listener method. The given method owned by the
  1064. * given object will no longer be called when the specified events are
  1065. * generated by this component.
  1066. * </p>
  1067. *
  1068. * <p>
  1069. * This version of <code>removeListener</code> gets the name of the
  1070. * activation method as a parameter. The actual method is reflected from
  1071. * <code>target</code>, and unless exactly one match is found,
  1072. * <code>java.lang.IllegalArgumentException</code> is thrown.
  1073. * </p>
  1074. *
  1075. * <p>
  1076. * For more information on the inheritable event mechanism see the
  1077. * {@link com.vaadin.event com.vaadin.event package documentation}.
  1078. * </p>
  1079. *
  1080. * @param eventType
  1081. * the exact event type the <code>object</code> listens to.
  1082. * @param target
  1083. * the target object that has registered to listen to events of
  1084. * type <code>eventType</code> with one or more methods.
  1085. * @param methodName
  1086. * the name of the method owned by <code>target</code> that's
  1087. * registered to listen to events of type <code>eventType</code>.
  1088. */
  1089. public void removeListener(Class<?> eventType, Object target,
  1090. String methodName) {
  1091. if (eventRouter != null) {
  1092. eventRouter.removeListener(eventType, target, methodName);
  1093. }
  1094. }
  1095. /**
  1096. * Returns all listeners that are registered for the given event type or one
  1097. * of its subclasses.
  1098. *
  1099. * @param eventType
  1100. * The type of event to return listeners for.
  1101. * @return A collection with all registered listeners. Empty if no listeners
  1102. * are found.
  1103. */
  1104. public Collection<?> getListeners(Class<?> eventType) {
  1105. if (eventType.isAssignableFrom(RepaintRequestEvent.class)) {
  1106. // RepaintRequestListeners are not stored in eventRouter
  1107. if (repaintRequestListeners == null) {
  1108. return Collections.EMPTY_LIST;
  1109. } else {
  1110. return Collections
  1111. .unmodifiableCollection(repaintRequestListeners);
  1112. }
  1113. }
  1114. if (eventRouter == null) {
  1115. return Collections.EMPTY_LIST;
  1116. }
  1117. return eventRouter.getListeners(eventType);
  1118. }
  1119. /**
  1120. * Sends the event to all listeners.
  1121. *
  1122. * @param event
  1123. * the Event to be sent to all listeners.
  1124. */
  1125. protected void fireEvent(Component.Event event) {
  1126. if (eventRouter != null) {
  1127. eventRouter.fireEvent(event);
  1128. }
  1129. }
  1130. /* Component event framework */
  1131. /*
  1132. * Registers a new listener to listen events generated by this component.
  1133. * Don't add a JavaDoc comment here, we use the default documentation from
  1134. * implemented interface.
  1135. */
  1136. public void addListener(Component.Listener listener) {
  1137. addListener(Component.Event.class, listener, COMPONENT_EVENT_METHOD);
  1138. }
  1139. /*
  1140. * Removes a previously registered listener from this component. Don't add a
  1141. * JavaDoc comment here, we use the default documentation from implemented
  1142. * interface.
  1143. */
  1144. public void removeListener(Component.Listener listener) {
  1145. removeListener(Component.Event.class, listener, COMPONENT_EVENT_METHOD);
  1146. }
  1147. /**
  1148. * Emits the component event. It is transmitted to all registered listeners
  1149. * interested in such events.
  1150. */
  1151. protected void fireComponentEvent() {
  1152. fireEvent(new Component.Event(this));
  1153. }
  1154. /**
  1155. * Emits the component error event. It is transmitted to all registered
  1156. * listeners interested in such events.
  1157. */
  1158. protected void fireComponentErrorEvent() {
  1159. fireEvent(new Component.ErrorEvent(getComponentError(), this));
  1160. }
  1161. /**
  1162. * Sets the data object, that can be used for any application specific data.
  1163. * The component does not use or modify this data.
  1164. *
  1165. * @param data
  1166. * the Application specific data.
  1167. * @since 3.1
  1168. */
  1169. public void setData(Object data) {
  1170. applicationData = data;
  1171. }
  1172. /**
  1173. * Gets the application specific data. See {@link #setData(Object)}.
  1174. *
  1175. * @return the Application specific data set with setData function.
  1176. * @since 3.1
  1177. */
  1178. public Object getData() {
  1179. return applicationData;
  1180. }
  1181. /* Sizeable and other size related methods */
  1182. /*
  1183. * (non-Javadoc)
  1184. *
  1185. * @see com.vaadin.terminal.Sizeable#getHeight()
  1186. */
  1187. public float getHeight() {
  1188. return height;
  1189. }
  1190. /*
  1191. * (non-Javadoc)
  1192. *
  1193. * @see com.vaadin.terminal.Sizeable#getHeightUnits()
  1194. */
  1195. public Unit getHeightUnits() {
  1196. return heightUnit;
  1197. }
  1198. /*
  1199. * (non-Javadoc)
  1200. *
  1201. * @see com.vaadin.terminal.Sizeable#getWidth()
  1202. */
  1203. public float getWidth() {
  1204. return width;
  1205. }
  1206. /*
  1207. * (non-Javadoc)
  1208. *
  1209. * @see com.vaadin.terminal.Sizeable#getWidthUnits()
  1210. */
  1211. public Unit getWidthUnits() {
  1212. return widthUnit;
  1213. }
  1214. /*
  1215. * (non-Javadoc)
  1216. *
  1217. * @see com.vaadin.terminal.Sizeable#setHeight(float, Unit)
  1218. */
  1219. public void setHeight(float height, Unit unit) {
  1220. if (unit == null) {
  1221. throw new IllegalArgumentException("Unit can not be null");
  1222. }
  1223. this.height = height;
  1224. heightUnit = unit;
  1225. requestRepaint();
  1226. // ComponentSizeValidator.setHeightLocation(this);
  1227. }
  1228. /*
  1229. * (non-Javadoc)
  1230. *
  1231. * @see com.vaadin.terminal.Sizeable#setSizeFull()
  1232. */
  1233. public void setSizeFull() {
  1234. setWidth(100, Unit.PERCENTAGE);
  1235. setHeight(100, Unit.PERCENTAGE);
  1236. }
  1237. /*
  1238. * (non-Javadoc)
  1239. *
  1240. * @see com.vaadin.terminal.Sizeable#setSizeUndefined()
  1241. */
  1242. public void setSizeUndefined() {
  1243. setWidth(-1, Unit.PIXELS);
  1244. setHeight(-1, Unit.PIXELS);
  1245. }
  1246. /*
  1247. * (non-Javadoc)
  1248. *
  1249. * @see com.vaadin.terminal.Sizeable#setWidth(float, Unit)
  1250. */
  1251. public void setWidth(float width, Unit unit) {
  1252. if (unit == null) {
  1253. throw new IllegalArgumentException("Unit can not be null");
  1254. }
  1255. this.width = width;
  1256. widthUnit = unit;
  1257. requestRepaint();
  1258. // ComponentSizeValidator.setWidthLocation(this);
  1259. }
  1260. /*
  1261. * (non-Javadoc)
  1262. *
  1263. * @see com.vaadin.terminal.Sizeable#setWidth(java.lang.String)
  1264. */
  1265. public void setWidth(String width) {
  1266. Size size = parseStringSize(width);
  1267. if (size != null) {
  1268. setWidth(size.getSize(), size.getUnit());
  1269. } else {
  1270. setWidth(-1, Unit.PIXELS);
  1271. }
  1272. }
  1273. /*
  1274. * (non-Javadoc)
  1275. *
  1276. * @see com.vaadin.terminal.Sizeable#setHeight(java.lang.String)
  1277. */
  1278. public void setHeight(String height) {
  1279. Size size = parseStringSize(height);
  1280. if (size != null) {
  1281. setHeight(size.getSize(), size.getUnit());
  1282. } else {
  1283. setHeight(-1, Unit.PIXELS);
  1284. }
  1285. }
  1286. /*
  1287. * Returns array with size in index 0 unit in index 1. Null or empty string
  1288. * will produce {-1,Unit#PIXELS}
  1289. */
  1290. private static Size parseStringSize(String s) {
  1291. if (s == null) {
  1292. return null;
  1293. }
  1294. s = s.trim();
  1295. if ("".equals(s)) {
  1296. return null;
  1297. }
  1298. float size = 0;
  1299. Unit unit = null;
  1300. Matcher matcher = sizePattern.matcher(s);
  1301. if (matcher.find()) {
  1302. size = Float.parseFloat(matcher.group(1));
  1303. if (size < 0) {
  1304. size = -1;
  1305. unit = Unit.PIXELS;
  1306. } else {
  1307. String symbol = matcher.group(3);
  1308. unit = Unit.getUnitFromSymbol(symbol);
  1309. }
  1310. } else {
  1311. throw new IllegalArgumentException("Invalid size argument: \"" + s
  1312. + "\" (should match " + sizePattern.pattern() + ")");
  1313. }
  1314. return new Size(size, unit);
  1315. }
  1316. private static class Size implements Serializable {
  1317. float size;
  1318. Unit unit;
  1319. public Size(float size, Unit unit) {
  1320. this.size = size;
  1321. this.unit = unit;
  1322. }
  1323. public float getSize() {
  1324. return size;
  1325. }
  1326. public Unit getUnit() {
  1327. return unit;
  1328. }
  1329. }
  1330. public interface ComponentErrorEvent extends Terminal.ErrorEvent {
  1331. }
  1332. public interface ComponentErrorHandler extends Serializable {
  1333. /**
  1334. * Handle the component error
  1335. *
  1336. * @param event
  1337. * @return True if the error has been handled False, otherwise
  1338. */
  1339. public boolean handleComponentError(ComponentErrorEvent event);
  1340. }
  1341. /**
  1342. * Gets the error handler for the component.
  1343. *
  1344. * The error handler is dispatched whenever there is an error processing the
  1345. * data coming from the client.
  1346. *
  1347. * @return
  1348. */
  1349. public ComponentErrorHandler getErrorHandler() {
  1350. return errorHandler;
  1351. }
  1352. /**
  1353. * Sets the error handler for the component.
  1354. *
  1355. * The error handler is dispatched whenever there is an error processing the
  1356. * data coming from the client.
  1357. *
  1358. * If the error handler is not set, the application error handler is used to
  1359. * handle the exception.
  1360. *
  1361. * @param errorHandler
  1362. * AbstractField specific error handler
  1363. */
  1364. public void setErrorHandler(ComponentErrorHandler errorHandler) {
  1365. this.errorHandler = errorHandler;
  1366. }
  1367. /**
  1368. * Handle the component error event.
  1369. *
  1370. * @param error
  1371. * Error event to handle
  1372. * @return True if the error has been handled False, otherwise. If the error
  1373. * haven't been handled by this component, it will be handled in the
  1374. * application error handler.
  1375. */
  1376. public boolean handleError(ComponentErrorEvent error) {
  1377. if (errorHandler != null) {
  1378. return errorHandler.handleComponentError(error);
  1379. }
  1380. return false;
  1381. }
  1382. /*
  1383. * Actions
  1384. */
  1385. /**
  1386. * Gets the {@link ActionManager} used to manage the
  1387. * {@link ShortcutListener}s added to this {@link Field}.
  1388. *
  1389. * @return the ActionManager in use
  1390. */
  1391. protected ActionManager getActionManager() {
  1392. if (actionManager == null) {
  1393. actionManager = new ActionManager();
  1394. setActionManagerViewer();
  1395. }
  1396. return actionManager;
  1397. }
  1398. /**
  1399. * Set a viewer for the action manager to be the parent sub window (if the
  1400. * component is in a window) or the root (otherwise). This is still a
  1401. * simplification of the real case as this should be handled by the parent
  1402. * VOverlay (on the client side) if the component is inside an VOverlay
  1403. * component.
  1404. */
  1405. private void setActionManagerViewer() {
  1406. if (actionManager != null && getRoot() != null) {
  1407. // Attached and has action manager
  1408. Window w = findParentOfType(Window.class, this);
  1409. if (w != null) {
  1410. actionManager.setViewer(w);
  1411. } else {
  1412. actionManager.setViewer(getRoot());
  1413. }
  1414. }
  1415. }
  1416. /**
  1417. * Helper method for finding the first parent component of a given type.
  1418. * Useful e.g. for finding the Window the component is inside.
  1419. *
  1420. * @param <T>
  1421. * @param parentType
  1422. * The type to look for
  1423. * @param c
  1424. * The target component
  1425. * @return A parent component of type {@literal parentType} or null if no
  1426. * parent component in the hierarchy can be assigned to the given
  1427. * type.
  1428. */
  1429. private static <T extends Component> T findParentOfType(
  1430. Class<T> parentType, Component c) {
  1431. Component p = c.getParent();
  1432. if (p == null) {
  1433. return null;
  1434. }
  1435. if (parentType.isAssignableFrom(p.getClass())) {
  1436. return (T) p;
  1437. }
  1438. return findParentOfType(parentType, p);
  1439. }
  1440. public void addShortcutListener(ShortcutListener shortcut) {
  1441. getActionManager().addAction(shortcut);
  1442. }
  1443. public void removeShortcutListener(ShortcutListener shortcut) {
  1444. if (actionManager != null) {
  1445. actionManager.removeAction(shortcut);
  1446. }
  1447. }
  1448. /**
  1449. * Registers an RPC interface implementation for this component.
  1450. *
  1451. * A component can listen to multiple RPC interfaces, and subclasses can
  1452. * register additional implementations.
  1453. *
  1454. * @since 7.0
  1455. *
  1456. * @param implementation
  1457. * RPC interface implementation
  1458. * @param rpcInterfaceType
  1459. * RPC interface class for which the implementation should be
  1460. * registered
  1461. */
  1462. protected <T> void registerRpcImplementation(T implementation,
  1463. Class<T> rpcInterfaceType) {
  1464. rpcManagerMap.put(rpcInterfaceType, new ServerRpcManager<T>(this,
  1465. implementation, rpcInterfaceType));
  1466. }
  1467. /**
  1468. * Returns an RPC proxy for a given server to client RPC interface for this
  1469. * component.
  1470. *
  1471. * TODO more javadoc, subclasses, ...
  1472. *
  1473. * @param rpcInterface
  1474. * RPC interface type
  1475. *
  1476. * @since 7.0
  1477. */
  1478. public <T extends ClientRpc> T getRpcProxy(final Class<T> rpcInterface) {
  1479. // create, initialize and return a dynamic proxy for RPC
  1480. try {
  1481. if (!rpcProxyMap.containsKey(rpcInterface)) {
  1482. InvocationHandler handler = new InvocationHandler() {
  1483. public Object invoke(Object proxy, Method method,
  1484. Object[] args) throws Throwable {
  1485. addMethodInvocationToQueue(rpcInterface.getName()
  1486. .replaceAll("\\$", "."), method.getName(), args);
  1487. // TODO no need to do full repaint if only RPC calls
  1488. requestRepaint();
  1489. return null;
  1490. }
  1491. };
  1492. Class<?> proxyClass = Proxy.getProxyClass(
  1493. rpcInterface.getClassLoader(),
  1494. new Class[] { rpcInterface });
  1495. T rpcProxy = (T) proxyClass.getConstructor(
  1496. new Class[] { InvocationHandler.class }).newInstance(
  1497. new Object[] { handler });
  1498. // cache the proxy
  1499. rpcProxyMap.put(rpcInterface, rpcProxy);
  1500. }
  1501. return (T) rpcProxyMap.get(rpcInterface);
  1502. } catch (Exception e) {
  1503. // TODO exception handling?
  1504. throw new RuntimeException(e);
  1505. }
  1506. }
  1507. /**
  1508. * For internal use: adds a method invocation to the pending RPC call queue.
  1509. *
  1510. * @param interfaceName
  1511. * RPC interface name
  1512. * @param methodName
  1513. * RPC method name
  1514. * @param parameters
  1515. * RPC vall parameters
  1516. *
  1517. * @since 7.0
  1518. */
  1519. protected void addMethodInvocationToQueue(String interfaceName,
  1520. String methodName, Object[] parameters) {
  1521. // add to queue
  1522. pendingInvocations.add(new ClientMethodInvocation(this, interfaceName,
  1523. methodName, parameters));
  1524. }
  1525. /**
  1526. * @see RpcTarget#getRpcManager(Class)
  1527. *
  1528. * @param rpcInterface
  1529. * RPC interface for which a call was made
  1530. * @return RPC Manager handling calls for the interface
  1531. *
  1532. * @since 7.0
  1533. */
  1534. public RpcManager getRpcManager(Class<?> rpcInterface) {
  1535. return rpcManagerMap.get(rpcInterface);
  1536. }
  1537. public List<ClientMethodInvocation> retrievePendingRpcCalls() {
  1538. if (pendingInvocations.isEmpty()) {
  1539. return Collections.emptyList();
  1540. } else {
  1541. List<ClientMethodInvocation> result = pendingInvocations;
  1542. pendingInvocations = new ArrayList<ClientMethodInvocation>();
  1543. return Collections.unmodifiableList(result);
  1544. }
  1545. }
  1546. public String getConnectorId() {
  1547. if (connectorId == null) {
  1548. if (getApplication() == null) {
  1549. throw new RuntimeException(
  1550. "Component must be attached to an application when getConnectorId() is called for the first time");
  1551. }
  1552. connectorId = getApplication().createConnectorId(this);
  1553. }
  1554. return connectorId;
  1555. }
  1556. }