Creating a UI extension

An Extension is an entity that is not a full-fledged UI component, but is instead used to enhance or extend the functionality of an existing component (or connector, more generally.) Unlike components, extensions cannot be detached and reattached once they are attached to their target.

Extensions usually consist of a pair of Connectors like components do. Hence, they can use the regular shared state and RPC mechanisms to communicate between the client and the server. Extensions may or may not have a UI. They can create and display widgets on the client side, but are not part of the regular layout hierarchy.

We will rewrite the Refresher add-on as an extension. The Refresher causes the client to “ping” the server at regular intervals, allowing the server to keep the client up-to-date if the application state is changed eg. by a background thread (because of the way Vaadin works, the server cannot itself initiate communication.)

We start by writing the barebones server-side class for our extension:

Java

  1. public class Refresher extends AbstractExtension {
  2. public Refresher(UI ui) {
  3. extend(target);
  4. }
  5. }

Two things to note:

  • If we were writing a component, we would probably want to inherit from AbstractComponent. Here, we inherit from AbstractExtension instead.

  • The connector that should be extended is passed to the constructor, which then uses the protected extend(Connector) method to attach itself to the target connector. In this case it does not make much sense attached to individual components, so the constructor only accepts UI.

Next, the Refresher needs an RPC interface to ping the server and a shared state to keep track of the interval. These are rather trivial:

Java

  1. public interface RefresherRpc extends ServerRpc {
  2. public void refresh();
  3. }

Java

  1. public class RefresherState extends SharedState {
  2. public int interval;
  3. }

The client-side connector is just like a component connector except that we inherit from AbstractExtensionConnector, not AbstractComponentConnector. We do not write a client-side widget at all, because the Refresher does not have a UI.

We create a Timer instance that calls the refresh RPC method when run. In onStateChange(), we know that either the interval, enabled state, or both have changed, so we always cancel a possible currently-running timer and schedule a new one if we’re enabled. We also remember to cancel the timer when the extension is detached.

Java

  1. @Connect(Refresher.class)
  2. public class RefresherConnector extends AbstractExtensionConnector {
  3. private Timer timer = new Timer() {
  4. @Override
  5. public void run() {
  6. getRpcProxy(RefresherRpc.class).refresh();
  7. }
  8. };
  9. @Override
  10. public void onStateChanged(StateChangeEvent event) {
  11. super.onStateChanged(event);
  12. timer.cancel();
  13. if (isEnabled()) {
  14. timer.scheduleRepeating(getState().interval);
  15. }
  16. }
  17. @Override
  18. public void onUnregister() {
  19. timer.cancel();
  20. }
  21. @Override
  22. protected void extend(ServerConnector target) {
  23. // Nothing for refresher to do here as it does not need to access the
  24. // connector it extends
  25. }
  26. @Override
  27. public RefresherState getState() {
  28. return (RefresherState) super.getState();
  29. }
  30. }

Finally, we add an event listener interface and some accessor methods to Refresher. There is nothing extension-specific in the following code:

Java

  1. public interface RefreshListener {
  2. static Method METHOD = ReflectTools.findMethod(RefreshListener.class,
  3. "refresh", RefreshEvent.class);
  4. public void refresh(RefreshEvent refreshEvent);
  5. }
  6. public class RefreshEvent extends EventObject {
  7. public RefreshEvent(Refresher refresher) {
  8. super(refresher);
  9. }
  10. public Refresher getRefresher() {
  11. return (Refresher) getSource();
  12. }
  13. }
  14. public Refresher(UI ui) {
  15. registerRpc(new RefresherRpc() {
  16. @Override
  17. public void refresh() {
  18. fireEvent(new RefreshEvent(Refresher.this));
  19. }
  20. });
  21. extend(ui);
  22. }
  23. @Override
  24. public RefresherState getState() {
  25. return (RefresherState) super.getState();
  26. }
  27. public void setInterval(int millis) {
  28. getState().interval = millis;
  29. }
  30. public int getInterval() {
  31. return getState().interval;
  32. }
  33. public void setEnabled(boolean enabled) {
  34. getState().enabled = enabled;
  35. }
  36. public boolean isEnabled() {
  37. return getState().enabled;
  38. }
  39. public void addRefreshListener(RefreshListener listener) {
  40. super.addListener(RefreshEvent.class, listener, RefreshListener.METHOD);
  41. }
  42. public void removeRefreshListener(RefreshListener listener) {
  43. super.removeListener(RefreshEvent.class, listener,
  44. RefreshListener.METHOD);
  45. }