How do I addExtension() in Cuba to use a Vaadin add-on

Hi,

i’m using a Vaadin Add-on that extends from AbstractExtension like this

public class PostMessageReceiverExtension extends AbstractExtension {

In a plain Vaadin application it would be used like this from the main UI.

public class PostmessagereceivertestUI extends UI {

And in the init method normally there would be this

final PostMessageReceiverExtension receiver = new PostMessageReceiverExtension(); addExtension(receiver);

How can I achieve the same thing with CUBA?

Do I need to extend something or unwrap something to get to the addExtension method?

I was also wondering if it is possible to get to addExtension from a screen declared like this or is it only possible from the main UI?

public class CustomerEdit extends AbstractEditor<Customer> {

Thanks.

Hi,

You can use PostMessageReceiverExtension with AppUI instance in the following way:

  1. Extend AppUI class with your own in web module:
public class CustomAppUI extends AppUI {

    private PostMessageReceiverExtension postMessageReceiverExtension;

    @Override
    protected void init(VaadinRequest request) {
        super.init(request);

        this.postMessageReceiverExtension = new PostMessageReceiverExtension();

        addExtension(postMessageReceiverExtension);
    }

    public PostMessageReceiverExtension getPostMessageReceiverExtension() {
        return postMessageReceiverExtension;
    }
}
  1. Register it in web-spring.xml:
<bean id="cuba_AppUI" class="com.company.demo.web.CustomAppUI" scope="prototype"/>
  1. You can get it from your code using static UI.getCurrent() method:
CustomAppUI ui = (CustomAppUI) AppUI.getCurrent();
PostMessageReceiverExtension extension = ui.getPostMessageReceiverExtension();

Usually, extensions provide constructor that receives UI component to extend, but this extensions for some reason doesn’t. That’s why we have to extend AppUI class.

Hi Yuriy,

I also have similar problem when using vaadin add-on. I noticed that you have bean scope configured to ‘vaadin’, but when I configure the same, it works as the same as ‘singleton’. Question is, do we really have ‘vaadin’ bean scope, is there any other special bean scopes in CUBA?

Additionally, singleton scope won’t allow me to open another tab in browser for the same app, only prototype scope could do. I think for AppUI, we should use prototype. I’m using CUBA 6.9.5.

Thanks!

Hi,

Custom UI class should always be declared in prototype scope, because default AppUI is defined there. It is our internal scope for beans bound to VaadinSession.

Hi Yuriy,

Could you please explain the code in com.haulmont.cuba.web.AppUI.java in v6.9.5, it is prototype bean, am i messing something?

/**
 * Single window / page of web application. Root component of Vaadin layout.
 */
@org.springframework.stereotype.Component(AppUI.NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Push(transport = Transport.WEBSOCKET_XHR)
@PreserveOnRefresh
public class AppUI extends UI implements ErrorHandler, CubaHistoryControl.HistoryBackHandler {

    public static final String NAME = "cuba_AppUI";

    public static final String LAST_REQUEST_ACTION_ATTR = "lastRequestAction";
    public static final String LAST_REQUEST_PARAMS_ATTR = "lastRequestParams";

Oh, sorry for my mistake. I’ve mixed up AppUI and App classes. AppUI definitely should be in prototype scope.

I’ll fix the example above.

OK, understand, thank you!