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

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