Bleach
Bleach

Reputation: 309

Singleton in Vaadin

Is it a good practice using Singleton pattern for the GUI(Attaching a window for my case), in Vaadin?

My use-case is: One window cannot be displayed by other users while if it is already in use by one user.


Before that use-case, I was simply adding window to the gui like below:

  this.communicationWindow = new CommunicationConfigWindow();
  this.configSettingsButton.addClickListener( e -> {
     if ( !UI.getCurrent().getWindows().contains( this.communicationWindow )
              && this.communicationWindow != null )
     {
        this.communicationWindow.init( this.config );
        this.getUI().addWindow( this.communicationWindow );
     }
  } );

Now since I want it to be shown by only one user, instead of

this.communicationWindow = new CommunicationConfigWindow();

I simply turned it into singleton like below and simply add try/catch block;

this.communicationWindow = CommunicationConfigWindow.getInstance();
this.communicationWindow = new CommunicationConfigWindow();
this.configSettingsButton.addClickListener( e -> {
    if ( !UI.getCurrent().getWindows().contains( this.communicationWindow )
                  && this.communicationWindow != null )
    {
       this.communicationWindow.init( this.config );
       try
       {
           this.getUI().addWindow( this.communicationWindow );
       }
       catch(IllegalArgumentException ex)
       {
           Notification.show( "Configuration invalid", Type.WARNING_MESSAGE);
       }
    }
});

Now, it does not allow for many users to display that window(which is what I want),yet there are 3 things:

  1. I really do feel thats a bad practice,
  2. While window is being displayed, user closes the browser it will not let the other users to display still.
  3. I am really newbie in Vaadin.

Any approaches, suggestions are welcomed.

Thank you.

Upvotes: 3

Views: 362

Answers (1)

André Schild
André Schild

Reputation: 4754

This won't work this way.

Any UI components are assigned to exactly one Vaadin session. So you can't have one window used by multiple UI instances.

The correct way to handle your use case, is to a have a window for each user, but then couple them with some kind of eventbus or broadcast, so that all windows are updated.

For this you need to enable Push in your project, since the server must send updates to "inactive" users.

https://vaadin.com/docs/v8/framework/articles/BroadcastingMessagesToOtherUsers.html

Upvotes: 2

Related Questions