Chapter “Creating a Java API for a Web Component” in Vaadin document described:
private static final PropertyDescriptor<Boolean, Boolean> pinProperty = PropertyDescriptors.propertyWithDefault("pin", false);
public void setPin(boolean pin) {
pinProperty.set(this, pin);
}
public boolean isPin() {
return pinProperty.get(this);
}
PropertyDescriptors.propertyWithDefault can support data types like Boolean, String, Integer or Double. If data type of the property changes to Object, what should be modified?
I noticed JsonObject config.toJson() is used as a parameter invoked by javascript function initLazy().
public class SortableLayout extends Div {
...
private void initConnector(Element layout, SortableConfig config) {
runBeforeClientResponse(ui -> ui.getPage().executeJs(
"window.Vaadin.Flow.sortableConnector.initLazy($0, $1, $2)", config.toJson(),
getElement(), layout));
}
...
}
If object captureMode in sortablejs.js works as attribute/property of SortableLayout, how to code setCaptureMode() and getCaptureMode() in SortableLayout.java?
In my case, the configuration (captureMode) was done only to set the object from the server to the client. (in one way)
If you need to synchronize the client value (captureMode) on the server side, it’s probably better to listen an javascript event on the server side (like on update capture mode).
public CompletableFuture<JsonValue> getAnimation() {
return getOption("animation");
}
public CompletableFuture<JsonValue> getOption(String option) {
return getElement()
.callJsFunction("$connector.getOption", option).toCompletableFuture();
}
If you want to have a synchronous getter, it’s probably better to synchronize the state (captureMode) from the javascript side to the Java side with an event (“on Capture Mode changed”).
public CompletableFuture<JsonValue> getAnimation() {
return getOption("animation");
}
public CompletableFuture<JsonValue> getOption(String option) {
return getElement()
.callJsFunction("$connector.getOption", option).toCompletableFuture();
}
If you want to have a synchronous getter, it’s probably better to synchronize the state (captureMode) from the javascript side to the Java side with an event (“on Capture Mode changed”).
Hi Jean Christophe, I had the similar problem, how did you manage to work around the session lock when you use the CompletableFuture.
I got this error
java.lang.IllegalStateException: Cannot access state in VaadinSession or UI without locking the session.
I tried to put it inside a thread, I got the result but it gave the same exception
As completablefuture is asynchronous you don’t use a thread but need to lock the vaadin session if you are updating the view.
For example:
sortableLayout.getAnimation().thenAccept(value -> {
System.out.println("animation"+value); // you don't need to lock the vaadin session because it's not a ui command
sortableLayout.getUI().ifPresent(ui -> ui.access(() -> {
// you can update the UI
myTextfield.setValue(value);
} ))
});
I hope this helps (I didn’t run this code). Try to not do long operation in the ui.access, like save something in the database, but only things relate to the ui.