vaadin / collaboration-kit

The simplest way to build real-time collaboration into web apps
https://vaadin.com/collaboration
6 stars 1 forks source link

FormManager does not work without a CollaborationBinder -- contrary to what is documented #76

Open enver-haase opened 2 months ago

enver-haase commented 2 months ago

Describe the bug See the source code. I expect the FormManager to be an abstract communications means, so that when I say I would "highlight()' on one end, this should be handled in the HighlightHandler on the other end. But the handler is not triggered. Same with value changes.

To Reproduce Setup the provided view (source code).

Expected behavior I expect the FormManager to be an abstract communications means, so that when I say I would "highlight()' on one end, this should be handled in the HighlightHandler on the other end.

Versions

Additional context https://vaadin.com/docs/latest/tools/collaboration/managers/form-manager

enver-haase commented 2 months ago
package com.example.application.views.helloworld;

import com.example.application.views.MainLayout;
import com.vaadin.collaborationengine.FormManager;
import com.vaadin.collaborationengine.UserInfo;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

@PageTitle("Hello World")
@Route(value = "", layout = MainLayout.class)
@RouteAlias(value = "", layout = MainLayout.class)
public class HelloWorldView extends HorizontalLayout {

    Logger logger = LoggerFactory.getLogger(HelloWorldView.class);

    public static class Preorder {
        private String name;

        public Preorder(String name) {
            setName(name);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    // For the FormManager, just in case we need such a property in the component mentioned in the Constructor?
    // But it does not at all work, the form manager is not bound to the component data-binding-wise.
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public HelloWorldView() {
        TextField name = new TextField("Your name");
        setMargin(true);
        setVerticalComponentAlignment(Alignment.END, name);
        add(name);

        UserInfo userInfo = new UserInfo(UUID.randomUUID().toString(), "Tapio Heino");

        // HERE IT SAYS THE COLLABORATION BINDER IS NOT NEEDED:
        // https://vaadin.com/docs/latest/tools/collaboration/managers/form-manager

        // WITHOUT THIS BINDER, THE FORM MANAGER DOES NOT WORK: NO HIGHLIGHTING, NO VALUE SETTING
        //CollaborationBinder<Preorder> cb = new CollaborationBinder<>(Preorder.class, userInfo);
        //cb.bind(name, "name");
        //cb.setTopic("22222", () -> new Preorder("Your name please"));

        // I EXPECT THIS TO WORK EVEN WITHOUT THE COLLABORATION BINDER
        // FOR EXAMPLE, I WANT TO BE ABLE TO SET THE VALUE OF THE PROPERTY
        // AND ALSO I WANT TO BE NOTIFIED WHEN THE PROPERTY IS HIGHLIGHTED
        logger.warn("Setting up form manager for user " + userInfo.getName());
        FormManager formManager = new FormManager(this, userInfo, "11111");
        formManager.setHighlightHandler(context -> {
            String propertyName = context.getPropertyName();
            UserInfo user = context.getUser();
            // Executed when a field is highlighted
            logger.warn("Field " + propertyName + " is highlighted for user " + user.getName());
            return () -> {
                // Executed when a field is no longer highlighted
                logger.warn("Field " + propertyName + " is no longer highlighted for user " + user.getName());
            };
        });

        // THIS DOES NOT WORK AT ALL
        formManager.highlight("name", true);

        // THIS DOES NOT WORK AT ALL
        formManager.setValue("name", "KUNO KNAUF");

        // NOT INVESTIGATED YET
        formManager.setPropertyChangeHandler(context -> {
            String propertyName = context.getPropertyName();
            Object value = context.getValue();
            logger.warn("Field " + propertyName + " changed to " + value);
        });
    }
}
Legioth commented 2 months ago

Seems like there's a bug in the documentation. The highlight and setValue calls in your example are run before the form manager has connected to the chosen topic. It works if you wrap those lines in a formManager.setActivationHandler callback.