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

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