Bind named read-only observables toward FXML views

This commit is contained in:
2026-11-23 18:22:59 +01:00
parent e915ed392f
commit 981cd68f49
7 changed files with 275 additions and 27 deletions
@@ -11,7 +11,9 @@ import lombok.Setter;
* View's ViewModel. Further names step into the ViewModel of a Complex attribute, and the
* direction applies to the last name. When that last name is complex, the control receives the
* Model instance behind the ViewModel. Direction values are {@code view}, {@code model}, and
* {@code both}. The Converter value is the class name. The container creates that class.
* {@code both}. The last segment of a {@code view} binding may also name a public
* {@code xxxProperty()} accessor returning a read-only observable. The Converter value is the
* class name. The container creates that class.
*/
@Getter
@Setter
@@ -6,7 +6,9 @@ import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
@@ -41,7 +43,11 @@ final class DirectBindingApplier {
return;
}
Property<?> controlProperty = controlProperty(control, binding.getProperty());
Property<?> attribute = resolved.attribute;
if (!resolved.writable) {
alignReadOnly(namespace, controlProperty, resolved, binding, context, lifetime);
return;
}
Property<?> attribute = (Property<?>) resolved.attribute;
if (resolved.complex) {
switch (binding.getDirection()) {
case "both" -> alignBothComplex(controlProperty, attribute, lifetime);
@@ -72,6 +78,23 @@ final class DirectBindingApplier {
}
}
/** Connects a named observable to the View when its accessor exposes no writable property. */
private static void alignReadOnly(Map<String, Object> namespace, Property<?> control, Resolved resolved,
DirectBinding binding, ApplicationContext context, ViewLifetime lifetime) {
if (!"view".equals(binding.getDirection())) {
throw new IllegalArgumentException("Read-only source for path \"" + binding.getPath()
+ "\" requires direction=\"view\" on " + binding.getControl() + "." + binding.getProperty()
+ " in " + namespace.get(FXMLLoader.LOCATION_KEY));
}
String converterName = binding.getConverter();
if (converterName == null || converterName.isEmpty()) {
alignTowardView(control, resolved.attribute, lifetime);
} else {
alignConverted(control, resolved.attribute, converter(context, converterName),
resolved.owner, resolved.name, true, false, lifetime);
}
}
/**
* Aligns a plain Attribute in the named direction.
*
@@ -100,7 +123,10 @@ final class DirectBindingApplier {
/**
* The Attribute the Path names.
*/
private final Property<?> attribute;
private final ObservableValue<?> attribute;
/** Whether the Attribute resolves to a writable endpoint. */
private final boolean writable;
/**
* Whether that Attribute is complex.
@@ -153,13 +179,16 @@ final class DirectBindingApplier {
current = child;
}
String last = names[names.length - 1];
Property<?> attribute = propertyOf(current, last);
if (attribute == null) {
Property<?> property = propertyOf(current, last);
ViewModel owner = current instanceof ViewModel model ? model : null;
if (property != null) {
return new Resolved(property, true, owner != null && owner.isComplex(last), owner, last);
}
ObservableValue<?> source = NamedProperties.getReadOnly(current, last);
if (source == null) {
throw missing(path);
}
boolean complex = current instanceof ViewModel stepped && stepped.isComplex(last);
ViewModel owner = current instanceof ViewModel model ? model : null;
return new Resolved(attribute, complex, owner, last);
return new Resolved(source, false, false, owner, last);
}
/**
@@ -279,10 +308,10 @@ final class DirectBindingApplier {
* which is the last name of the Path.
*/
@SuppressWarnings("unchecked")
private static void alignConverted(Property<?> controlProperty, Property<?> attribute, Converter converter,
ViewModel owner, String sourceName, boolean towardView, boolean towardModel, ViewLifetime lifetime) {
private static void alignConverted(Property<?> controlProperty, ObservableValue<?> source, Converter converter,
ViewModel owner, String sourceName, boolean towardView, boolean towardModel,
ViewLifetime lifetime) {
Property<Object> control = (Property<Object>) controlProperty;
Property<Object> source = (Property<Object>) attribute;
Link link = new Link();
pushControl(control, source, converter, owner, sourceName, link);
if (towardView) {
@@ -293,9 +322,10 @@ final class DirectBindingApplier {
});
}
if (towardModel) {
Property<Object> target = (Property<Object>) source;
lifetime.listen(control, (observable, oldValue, newValue) -> {
if (!link.applying) {
pushAttribute(control, source, converter, owner, sourceName, link);
pushAttribute(control, target, converter, owner, sourceName, link);
}
});
}
@@ -311,7 +341,7 @@ final class DirectBindingApplier {
* @param source the Attribute name used as the failure source
* @param link the guard against hearing this write
*/
private static void pushControl(Property<Object> control, Property<Object> attribute, Converter converter,
private static void pushControl(Property<Object> control, ObservableValue<?> attribute, Converter converter,
ViewModel owner, String source, Link link) {
try {
Object shown = converter.toControlValue(attribute.getValue());
@@ -321,8 +351,13 @@ final class DirectBindingApplier {
} finally {
link.applying = false;
}
owner.clearBindingFailure(source);
if (owner != null) {
owner.clearBindingFailure(source);
}
} catch (RuntimeException failure) {
if (owner == null) {
throw failure;
}
owner.reportBindingFailure(source, failureText(failure));
}
}
@@ -513,11 +548,11 @@ final class DirectBindingApplier {
* @param attribute the Attribute
*/
@SuppressWarnings("unchecked")
private static void alignTowardView(Property<?> controlProperty, Property<?> attribute, ViewLifetime lifetime) {
private static void alignTowardView(Property<?> controlProperty, ObservableValue<?> attribute,
ViewLifetime lifetime) {
Property<Object> control = (Property<Object>) controlProperty;
Property<Object> source = (Property<Object>) attribute;
control.setValue(source.getValue());
lifetime.listen(source, (observable, oldValue, newValue) -> control.setValue(newValue));
control.setValue(attribute.getValue());
lifetime.listen(attribute, (observable, oldValue, newValue) -> control.setValue(newValue));
}
/**
@@ -1,6 +1,7 @@
package de.neitzel.fx.view;
import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -26,17 +27,31 @@ final class NamedProperties {
* @return the property, or {@code null} when {@code bean} declares none of that name
*/
static Property<?> get(Object bean, String name) {
Object value = read(bean, name, true);
return value instanceof Property<?> property ? property : null;
}
/**
* Returns a named observable whose accessor exposes a read-only source.
*
* @param bean the object that declares the source
* @param name the source name
* @return the source or {@code null} when there is no such accessor or it returns {@code null}
*/
static ObservableValue<?> getReadOnly(Object bean, String name) {
Object value = read(bean, name, false);
return value instanceof ObservableValue<?> source ? source : null;
}
private static Object read(Object bean, String name, boolean writable) {
String methodName = name + "Property";
try {
Method method = bean.getClass().getMethod(methodName);
if (!Property.class.isAssignableFrom(method.getReturnType())) {
Class<?> type = method.getReturnType();
if (!ObservableValue.class.isAssignableFrom(type) || writable != Property.class.isAssignableFrom(type)) {
return null;
}
Object value = method.invoke(bean);
if (value instanceof Property<?> property) {
return property;
}
return null;
return method.invoke(bean);
} catch (NoSuchMethodException notDeclared) {
return null;
} catch (IllegalAccessException | InvocationTargetException failure) {