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.

CreatingAUIExtension.asciidoc 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: Creating A UI Extension
  3. order: 73
  4. layout: page
  5. ---
  6. [[creating-a-ui-extension]]
  7. = Creating a UI extension
  8. An *Extension* is an entity that is not a full-fledged UI component, but
  9. is instead used to enhance or extend the functionality of an existing
  10. component (or connector, more generally.) Unlike components, extensions
  11. cannot be detached and reattached once they are attached to their
  12. target.
  13. Extensions usually consist of a pair of `Connector`{empty}s like components do.
  14. Hence, they can use the regular shared state and RPC mechanisms to
  15. communicate between the client and the server. Extensions may or may not
  16. have a UI. They can create and display widgets on the client side, but
  17. are not part of the regular layout hierarchy.
  18. We will rewrite the
  19. https://vaadin.com/directory/component/refresher[Refresher] add-on as an
  20. extension. The Refresher causes the client to "ping" the server at
  21. regular intervals, allowing the server to keep the client up-to-date if
  22. the application state is changed eg. by a background thread (because of
  23. the way Vaadin works, the server cannot itself initiate communication.)
  24. We start by writing the barebones server-side class for our extension:
  25. [source,java]
  26. ....
  27. public class Refresher extends AbstractExtension {
  28. public Refresher(UI ui) {
  29. extend(target);
  30. }
  31. }
  32. ....
  33. Two things to note:
  34. * If we were writing a component, we would probably want to inherit from
  35. `AbstractComponent`. Here, we inherit from `AbstractExtension` instead.
  36. * The connector that should be extended is passed to the constructor,
  37. which then uses the protected `extend(Connector)` method to attach
  38. itself to the target connector. In this case it does not make much sense
  39. attached to individual components, so the constructor only accepts `UI`.
  40. Next, the Refresher needs an RPC interface to ping the server and a
  41. shared state to keep track of the interval. These are rather trivial:
  42. [source,java]
  43. ....
  44. public interface RefresherRpc extends ServerRpc {
  45. public void refresh();
  46. }
  47. ....
  48. [source,java]
  49. ....
  50. public class RefresherState extends SharedState {
  51. public int interval;
  52. }
  53. ....
  54. The client-side connector is just like a component connector except that
  55. we inherit from `AbstractExtensionConnector`, not
  56. `AbstractComponentConnector`. We do not write a client-side widget at
  57. all, because the Refresher does not have a UI.
  58. We create a `Timer` instance that calls the `refresh` RPC method when
  59. run. In `onStateChange()`, we know that either the interval, enabled
  60. state, or both have changed, so we always cancel a possible
  61. currently-running timer and schedule a new one if we're enabled. We also
  62. remember to cancel the timer when the extension is detached.
  63. [source,java]
  64. ....
  65. @Connect(Refresher.class)
  66. public class RefresherConnector extends AbstractExtensionConnector {
  67. private Timer timer = new Timer() {
  68. @Override
  69. public void run() {
  70. getRpcProxy(RefresherRpc.class).refresh();
  71. }
  72. };
  73. @Override
  74. public void onStateChanged(StateChangeEvent event) {
  75. super.onStateChanged(event);
  76. timer.cancel();
  77. if (isEnabled()) {
  78. timer.scheduleRepeating(getState().interval);
  79. }
  80. }
  81. @Override
  82. public void onUnregister() {
  83. timer.cancel();
  84. }
  85. @Override
  86. protected void extend(ServerConnector target) {
  87. // Nothing for refresher to do here as it does not need to access the
  88. // connector it extends
  89. }
  90. @Override
  91. public RefresherState getState() {
  92. return (RefresherState) super.getState();
  93. }
  94. }
  95. ....
  96. Finally, we add an event listener interface and some accessor methods to
  97. `Refresher`. There is nothing extension-specific in the following code:
  98. [source,java]
  99. ....
  100. public interface RefreshListener {
  101. static Method METHOD = ReflectTools.findMethod(RefreshListener.class,
  102. "refresh", RefreshEvent.class);
  103. public void refresh(RefreshEvent refreshEvent);
  104. }
  105. public class RefreshEvent extends EventObject {
  106. public RefreshEvent(Refresher refresher) {
  107. super(refresher);
  108. }
  109. public Refresher getRefresher() {
  110. return (Refresher) getSource();
  111. }
  112. }
  113. public Refresher(UI ui) {
  114. registerRpc(new RefresherRpc() {
  115. @Override
  116. public void refresh() {
  117. fireEvent(new RefreshEvent(Refresher.this));
  118. }
  119. });
  120. extend(ui);
  121. }
  122. @Override
  123. public RefresherState getState() {
  124. return (RefresherState) super.getState();
  125. }
  126. public void setInterval(int millis) {
  127. getState().interval = millis;
  128. }
  129. public int getInterval() {
  130. return getState().interval;
  131. }
  132. public void setEnabled(boolean enabled) {
  133. getState().enabled = enabled;
  134. }
  135. public boolean isEnabled() {
  136. return getState().enabled;
  137. }
  138. public void addRefreshListener(RefreshListener listener) {
  139. super.addListener(RefreshEvent.class, listener, RefreshListener.METHOD);
  140. }
  141. public void removeRefreshListener(RefreshListener listener) {
  142. super.removeListener(RefreshEvent.class, listener,
  143. RefreshListener.METHOD);
  144. }
  145. ....