Complete JavaFX MVVM examples, bindings and library documentation
This commit is contained in:
@@ -17,6 +17,9 @@ import java.util.Map;
|
||||
@DefaultProperty("elements")
|
||||
public class BindingList {
|
||||
|
||||
/**
|
||||
* The binding elements, in document order.
|
||||
*/
|
||||
private final List<Object> elements = new ArrayList<>();
|
||||
|
||||
/**
|
||||
|
||||
@@ -17,11 +17,33 @@ import java.util.Set;
|
||||
*/
|
||||
final class ChildMessageMirror implements MessageListener {
|
||||
|
||||
/**
|
||||
* The parent that receives copies of the child's notices.
|
||||
*/
|
||||
private final ViewModel parent;
|
||||
|
||||
/**
|
||||
* The Attribute that leads to the child. It is prepended to each copied source.
|
||||
*/
|
||||
private final String attribute;
|
||||
|
||||
/**
|
||||
* The child whose notices are carried.
|
||||
*/
|
||||
private final ViewModel child;
|
||||
|
||||
/**
|
||||
* Copies already carried, keyed by the identity of the child notice.
|
||||
*/
|
||||
private final Map<Message, Message> copies = new IdentityHashMap<>();
|
||||
|
||||
/**
|
||||
* Creates a mirror. {@link #attach} registers it and copies notices already present.
|
||||
*
|
||||
* @param parent the parent ViewModel
|
||||
* @param attribute the Attribute that leads to the child
|
||||
* @param child the child ViewModel
|
||||
*/
|
||||
private ChildMessageMirror(ViewModel parent, String attribute, ViewModel child) {
|
||||
this.parent = parent;
|
||||
this.attribute = attribute;
|
||||
@@ -97,6 +119,11 @@ final class ChildMessageMirror implements MessageListener {
|
||||
parent.noteMessageText(copy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies {@code message} onto the parent unless that copy is already there or would cycle.
|
||||
*
|
||||
* @param message the child notice
|
||||
*/
|
||||
private void carry(Message message) {
|
||||
if (copies.containsKey(message) || repeats(message)) {
|
||||
return;
|
||||
|
||||
@@ -18,6 +18,11 @@ public class Column {
|
||||
*/
|
||||
private String attribute;
|
||||
|
||||
/**
|
||||
* The starting width, or {@code null} when this Column takes an equal share of the Table.
|
||||
*/
|
||||
private Double width;
|
||||
|
||||
/**
|
||||
* Sets the header text.
|
||||
*
|
||||
@@ -53,4 +58,34 @@ public class Column {
|
||||
public String getAttribute() {
|
||||
return attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the starting width.
|
||||
* <p>
|
||||
* A Column with no starting width opens at an equal share of the Table. A drag can change the
|
||||
* width later. A width that is not positive fails the load.
|
||||
*
|
||||
* @param width the starting width
|
||||
*/
|
||||
public void setWidth(double width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the starting width.
|
||||
*
|
||||
* @return the starting width, or {@code 0} when this Column takes an equal share
|
||||
*/
|
||||
public double getWidth() {
|
||||
return width == null ? 0 : width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a starting width was set.
|
||||
*
|
||||
* @return {@code true} when {@link #setWidth(double)} has been called
|
||||
*/
|
||||
boolean hasWidth() {
|
||||
return width != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ import java.util.Map;
|
||||
*/
|
||||
final class Components {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private Components() {
|
||||
}
|
||||
|
||||
@@ -86,6 +89,13 @@ final class Components {
|
||||
modelProperty(component).setValue(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads {@code ClassName.fxml} with the Component as root and Controller.
|
||||
*
|
||||
* @param component the Component
|
||||
* @param context the container that creates nested Components
|
||||
* @return the loaded Component
|
||||
*/
|
||||
private static Object load(Object component, ApplicationContext context) {
|
||||
Class<?> type = component.getClass();
|
||||
String document = type.getSimpleName() + ".fxml";
|
||||
@@ -109,6 +119,14 @@ final class Components {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the Component's bindings once, when {@code model} first becomes non-null.
|
||||
*
|
||||
* @param component the Component
|
||||
* @param lists the binding lists captured from its document
|
||||
* @param namespace objects addressed by {@code fx:id}
|
||||
* @param context the container that creates a named Converter
|
||||
*/
|
||||
private static void watchModel(Object component, List<BindingList> lists, Map<String, Object> namespace,
|
||||
ApplicationContext context) {
|
||||
Property<Object> model = modelProperty(component);
|
||||
@@ -129,6 +147,12 @@ final class Components {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Component's {@code model} property.
|
||||
*
|
||||
* @param component the Component
|
||||
* @return the property
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Property<Object> modelProperty(Object component) {
|
||||
try {
|
||||
|
||||
@@ -10,8 +10,19 @@ package de.neitzel.fx.view;
|
||||
*/
|
||||
public class ComputedBinding {
|
||||
|
||||
/**
|
||||
* The {@code fx:id} of the control that receives the method result.
|
||||
*/
|
||||
private String control;
|
||||
|
||||
/**
|
||||
* The control property that receives the method result, such as {@code text}.
|
||||
*/
|
||||
private String property;
|
||||
|
||||
/**
|
||||
* The Controller method, written as {@code #method}.
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,9 @@ import java.util.Map;
|
||||
*/
|
||||
final class ComputedBindingApplier {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private ComputedBindingApplier() {
|
||||
}
|
||||
|
||||
@@ -52,6 +55,16 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a refresh that calls the Controller method and writes the result.
|
||||
* <p>
|
||||
* A refresh that is already running does not start again, so writing the target does not loop.
|
||||
*
|
||||
* @param controller the Controller
|
||||
* @param methodValue the method, written as {@code #method}
|
||||
* @param target the control property
|
||||
* @return the refresh
|
||||
*/
|
||||
private static Runnable refresh(Object controller, String methodValue, Property<Object> target) {
|
||||
boolean[] running = {false};
|
||||
return () -> {
|
||||
@@ -67,10 +80,24 @@ final class ComputedBindingApplier {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches JavaFX properties on controls under {@code root}, other than {@code target}.
|
||||
*
|
||||
* @param root the scene-graph root
|
||||
* @param target the property the method writes, which is not watched
|
||||
* @param refresh the refresh to run when a watched property changes
|
||||
*/
|
||||
private static void watchControls(Parent root, Property<?> target, Runnable refresh) {
|
||||
watchNode(root, target, refresh);
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches one control and walks its children.
|
||||
*
|
||||
* @param node the node
|
||||
* @param target the property the method writes, which is not watched
|
||||
* @param refresh the refresh to run when a watched property changes
|
||||
*/
|
||||
private static void watchNode(Node node, Property<?> target, Runnable refresh) {
|
||||
if (node instanceof Control control) {
|
||||
watchProperties(control, target, refresh);
|
||||
@@ -82,6 +109,13 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens to every JavaFX property on {@code bean} except {@code target}.
|
||||
*
|
||||
* @param bean the control
|
||||
* @param target the property the method writes
|
||||
* @param refresh the refresh to run when a watched property changes
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void watchProperties(Object bean, Property<?> target, Runnable refresh) {
|
||||
ChangeListener<Object> listener = (observable, oldValue, newValue) -> refresh.run();
|
||||
@@ -99,6 +133,13 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a no-argument property method.
|
||||
*
|
||||
* @param bean the object that declares the property
|
||||
* @param method the property method
|
||||
* @return the property
|
||||
*/
|
||||
private static Object readProperty(Object bean, Method method) {
|
||||
try {
|
||||
return method.invoke(bean);
|
||||
@@ -107,6 +148,13 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the writable JavaFX property named on the control.
|
||||
*
|
||||
* @param control the control
|
||||
* @param propertyName the property name, such as {@code text}
|
||||
* @return the property
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Property<Object> controlProperty(Object control, String propertyName) {
|
||||
String methodName = propertyName + "Property";
|
||||
@@ -125,6 +173,13 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the Controller method named by {@code methodValue}.
|
||||
*
|
||||
* @param controller the Controller
|
||||
* @param methodValue the method, written as {@code #method}
|
||||
* @return the method result
|
||||
*/
|
||||
private static Object invoke(Object controller, String methodValue) {
|
||||
if (controller == null) {
|
||||
throw new IllegalArgumentException("No controller for " + methodValue);
|
||||
@@ -140,6 +195,12 @@ final class ComputedBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a leading {@code #} from an FXML method value.
|
||||
*
|
||||
* @param methodValue the method value
|
||||
* @return the method name
|
||||
*/
|
||||
private static String methodName(String methodValue) {
|
||||
if (methodValue != null && methodValue.startsWith("#")) {
|
||||
return methodValue.substring(1);
|
||||
|
||||
@@ -12,10 +12,29 @@ package de.neitzel.fx.view;
|
||||
*/
|
||||
public class DirectBinding {
|
||||
|
||||
/**
|
||||
* The {@code fx:id} of the control.
|
||||
*/
|
||||
private String control;
|
||||
|
||||
/**
|
||||
* The control property, such as {@code text}.
|
||||
*/
|
||||
private String property;
|
||||
|
||||
/**
|
||||
* The Path. One name is an Attribute of this View's ViewModel.
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* The direction: {@code view}, {@code model}, or {@code both}.
|
||||
*/
|
||||
private String direction;
|
||||
|
||||
/**
|
||||
* The Converter class name, or {@code null} when values pass through.
|
||||
*/
|
||||
private String converter;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,9 @@ import java.util.Map;
|
||||
*/
|
||||
final class DirectBindingApplier {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private DirectBindingApplier() {
|
||||
}
|
||||
|
||||
@@ -68,6 +71,13 @@ final class DirectBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aligns a plain Attribute in the named direction.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the Attribute
|
||||
* @param direction {@code view}, {@code model}, or {@code both}
|
||||
*/
|
||||
private static void alignPlain(Property<?> controlProperty, Property<?> attribute, String direction) {
|
||||
switch (direction) {
|
||||
case "both" -> alignBoth(controlProperty, attribute);
|
||||
@@ -84,11 +94,35 @@ final class DirectBindingApplier {
|
||||
* name as the source, so a parent only carries the extended copy.
|
||||
*/
|
||||
private static final class Resolved {
|
||||
|
||||
/**
|
||||
* The Attribute the Path names.
|
||||
*/
|
||||
private final Property<?> attribute;
|
||||
|
||||
/**
|
||||
* Whether that Attribute is complex.
|
||||
*/
|
||||
private final boolean complex;
|
||||
|
||||
/**
|
||||
* The ViewModel that owns the Attribute. A conversion failure is recorded here.
|
||||
*/
|
||||
private final ViewModel owner;
|
||||
|
||||
/**
|
||||
* The last segment of the Path, used as the failure source.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Records one resolved Path.
|
||||
*
|
||||
* @param attribute the Attribute
|
||||
* @param complex whether the Attribute is complex
|
||||
* @param owner the ViewModel that owns it
|
||||
* @param name the last segment
|
||||
*/
|
||||
private Resolved(Property<?> attribute, boolean complex, ViewModel owner, String name) {
|
||||
this.attribute = attribute;
|
||||
this.complex = complex;
|
||||
@@ -165,6 +199,13 @@ final class DirectBindingApplier {
|
||||
ViewModels.of(instance, requested);
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows a Path through the Model instances behind the ViewModels.
|
||||
*
|
||||
* @param viewModel the ViewModel for this View
|
||||
* @param path the Path
|
||||
* @return the Model instance at the end of the Path, or {@code null} when a step is null
|
||||
*/
|
||||
private static Object instanceAt(Object viewModel, String path) {
|
||||
if (!(viewModel instanceof ViewModel root)) {
|
||||
return null;
|
||||
@@ -179,6 +220,13 @@ final class DirectBindingApplier {
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads one JavaBean attribute through {@code get} or {@code is}.
|
||||
*
|
||||
* @param bean the Model instance
|
||||
* @param name the attribute name
|
||||
* @return the current value
|
||||
*/
|
||||
private static Object readAttribute(Object bean, String name) {
|
||||
String suffix = Character.toUpperCase(name.charAt(0)) + name.substring(1);
|
||||
for (String prefix : List.of("get", "is")) {
|
||||
@@ -194,6 +242,13 @@ final class DirectBindingApplier {
|
||||
throw new IllegalArgumentException("No read-write attribute for path \"" + name + "\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JavaFX property named on a ViewModel or a replacement.
|
||||
*
|
||||
* @param target the ViewModel or replacement
|
||||
* @param name the property name
|
||||
* @return the property, or {@code null} when that name is not declared
|
||||
*/
|
||||
private static Property<?> propertyOf(Object target, String name) {
|
||||
if (target instanceof ViewModel viewModel) {
|
||||
return viewModel.property(name);
|
||||
@@ -201,10 +256,23 @@ final class DirectBindingApplier {
|
||||
return NamedProperties.get(target, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a Path that is not a read-write Attribute.
|
||||
*
|
||||
* @param path the Path
|
||||
* @return the failure
|
||||
*/
|
||||
private static IllegalArgumentException missing(String path) {
|
||||
return new IllegalArgumentException("No read-write attribute for path \"" + path + "\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks the container for the named Converter.
|
||||
*
|
||||
* @param context the container
|
||||
* @param className the Converter class name
|
||||
* @return the Converter
|
||||
*/
|
||||
private static Converter converter(ApplicationContext context, String className) {
|
||||
try {
|
||||
Class<?> type = Class.forName(className);
|
||||
@@ -245,6 +313,16 @@ final class DirectBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the converted Attribute onto the control. A failure keeps the previous control value.
|
||||
*
|
||||
* @param control the control property
|
||||
* @param attribute the Attribute
|
||||
* @param converter the Converter
|
||||
* @param owner the ViewModel that records a failure
|
||||
* @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,
|
||||
ViewModel owner, String source, Link link) {
|
||||
try {
|
||||
@@ -261,6 +339,16 @@ final class DirectBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the converted control value onto the Attribute. A failure keeps the previous Attribute.
|
||||
*
|
||||
* @param control the control property
|
||||
* @param attribute the Attribute
|
||||
* @param converter the Converter
|
||||
* @param owner the ViewModel that records a failure
|
||||
* @param source the Attribute name used as the failure source
|
||||
* @param link the guard against hearing this write
|
||||
*/
|
||||
private static void pushAttribute(Property<Object> control, Property<Object> attribute, Converter converter,
|
||||
ViewModel owner, String source, Link link) {
|
||||
try {
|
||||
@@ -277,6 +365,12 @@ final class DirectBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the exception message, or a fallback when it is missing.
|
||||
*
|
||||
* @param failure the conversion failure
|
||||
* @return the text recorded on the ViewModel
|
||||
*/
|
||||
private static String failureText(RuntimeException failure) {
|
||||
String text = failure.getMessage();
|
||||
if (text == null || text.isEmpty()) {
|
||||
@@ -289,9 +383,20 @@ final class DirectBindingApplier {
|
||||
* Guards one converted binding against hearing its own writes.
|
||||
*/
|
||||
private static final class Link {
|
||||
|
||||
/**
|
||||
* Whether this binding is currently writing one side, so the other listener stays quiet.
|
||||
*/
|
||||
private boolean applying;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the writable JavaFX property named on the control.
|
||||
*
|
||||
* @param control the control
|
||||
* @param propertyName the property name, such as {@code text}
|
||||
* @return the property
|
||||
*/
|
||||
private static Property<?> controlProperty(Object control, String propertyName) {
|
||||
String methodName = propertyName + "Property";
|
||||
try {
|
||||
@@ -309,6 +414,15 @@ final class DirectBindingApplier {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps a complex Attribute and the control aligned in both directions.
|
||||
* <p>
|
||||
* The control receives the Model instance. A later control value is stored as that instance's
|
||||
* ViewModel.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the complex Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignBothComplex(Property<?> controlProperty, Property<?> attribute) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
@@ -328,6 +442,12 @@ final class DirectBindingApplier {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a complex Attribute on the control and follows later changes. The control does not write back.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the complex Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignTowardViewComplex(Property<?> controlProperty, Property<?> attribute) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
@@ -336,6 +456,12 @@ final class DirectBindingApplier {
|
||||
source.addListener((observable, oldValue, newValue) -> control.setValue(modelBehind(newValue)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a complex Attribute once, then writes the Attribute when the control changes.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the complex Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignTowardModelComplex(Property<?> controlProperty, Property<?> attribute) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
@@ -349,6 +475,12 @@ final class DirectBindingApplier {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Model instance stored in a complex Attribute.
|
||||
*
|
||||
* @param value the Attribute value, a ViewModel or {@code null}
|
||||
* @return the Model instance, or {@code null}
|
||||
*/
|
||||
private static Object modelBehind(Object value) {
|
||||
if (value instanceof ViewModel viewModel) {
|
||||
return viewModel.model();
|
||||
@@ -356,16 +488,34 @@ final class DirectBindingApplier {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the control property to the shared list. Adds and removes are then visible on both sides.
|
||||
*
|
||||
* @param controlProperty the control property, such as {@code items}
|
||||
* @param values the list the ViewModel and the Model share
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void assignList(Property<?> controlProperty, ObservableList<?> values) {
|
||||
((Property<Object>) controlProperty).setValue(values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the control and the Attribute in both directions.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignBoth(Property<?> controlProperty, Property<?> attribute) {
|
||||
((Property<Object>) controlProperty).bindBidirectional((Property<Object>) attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the Attribute and follows later changes. Edits to the control stay on the control.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignTowardView(Property<?> controlProperty, Property<?> attribute) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
@@ -374,6 +524,12 @@ final class DirectBindingApplier {
|
||||
source.addListener((observable, oldValue, newValue) -> control.setValue(newValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the current value, then writes the Attribute when the control changes.
|
||||
*
|
||||
* @param controlProperty the control property
|
||||
* @param attribute the Attribute
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void alignTowardModel(Property<?> controlProperty, Property<?> attribute) {
|
||||
Property<Object> control = (Property<Object>) controlProperty;
|
||||
|
||||
@@ -18,8 +18,14 @@ import java.net.URL;
|
||||
*/
|
||||
final class FxmlControllers {
|
||||
|
||||
/**
|
||||
* The FXML namespace used by {@code fx:controller}.
|
||||
*/
|
||||
private static final String FXML_NAMESPACE = "http://javafx.com/fxml";
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private FxmlControllers() {
|
||||
}
|
||||
|
||||
@@ -42,6 +48,12 @@ final class FxmlControllers {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code fx:controller} value on the document root.
|
||||
*
|
||||
* @param fxml the FXML document
|
||||
* @return the class name, or {@code null} when the document names none
|
||||
*/
|
||||
private static String controllerName(URL fxml) {
|
||||
try (InputStream input = fxml.openStream()) {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
@@ -17,11 +17,24 @@ import java.util.Set;
|
||||
*/
|
||||
final class LoadSession {
|
||||
|
||||
/**
|
||||
* The sessions open on this thread. The last one is the outermost load.
|
||||
*/
|
||||
private static final ThreadLocal<Deque<LoadSession>> STACK = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* Binding lists created while this session is the current one, in document order.
|
||||
*/
|
||||
private final List<BindingList> bindingLists = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Component classes already built during the outermost load.
|
||||
*/
|
||||
private final Set<Class<?>> built = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Prevents instantiation from outside this class.
|
||||
*/
|
||||
private LoadSession() {
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,14 @@ import javafx.scene.Parent;
|
||||
*/
|
||||
public final class LoadedView {
|
||||
|
||||
/**
|
||||
* The scene-graph root of the loaded View.
|
||||
*/
|
||||
private final Parent root;
|
||||
|
||||
/**
|
||||
* The ViewModel the View is bound to, or a replacement that exposes JavaFX properties by name.
|
||||
*/
|
||||
private final Object viewModel;
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,9 +19,24 @@ public final class Message {
|
||||
FATAL
|
||||
}
|
||||
|
||||
/**
|
||||
* How serious the notice is.
|
||||
*/
|
||||
private final Severity severity;
|
||||
|
||||
/**
|
||||
* The notice text. A binding failure updates this in place.
|
||||
*/
|
||||
private String text;
|
||||
|
||||
/**
|
||||
* Where the notice came from, such as a binding Path.
|
||||
*/
|
||||
private final String source;
|
||||
|
||||
/**
|
||||
* The child notice this one copies, or {@code null} when this notice was added directly.
|
||||
*/
|
||||
private Message origin;
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,6 +12,9 @@ import javafx.scene.control.Label;
|
||||
*/
|
||||
public final class MessagePopup {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private MessagePopup() {
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@ import java.lang.reflect.Method;
|
||||
*/
|
||||
final class NamedProperties {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private NamedProperties() {
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,19 @@ package de.neitzel.fx.view;
|
||||
*/
|
||||
public class Repeat {
|
||||
|
||||
/**
|
||||
* The {@code fx:id} of the pane that holds the Component children.
|
||||
*/
|
||||
private String pane;
|
||||
|
||||
/**
|
||||
* The Component class name. Each element gets a new instance of that class.
|
||||
*/
|
||||
private String component;
|
||||
|
||||
/**
|
||||
* The collection Attribute. The list holds the Model instances.
|
||||
*/
|
||||
private String collection;
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,9 @@ import java.util.Map;
|
||||
*/
|
||||
final class RepeatApplier {
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private RepeatApplier() {
|
||||
}
|
||||
|
||||
@@ -47,6 +50,13 @@ final class RepeatApplier {
|
||||
listen(pane, type, context, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the collection Attribute named on the ViewModel.
|
||||
*
|
||||
* @param viewModel the ViewModel for this View
|
||||
* @param name the collection Attribute
|
||||
* @return the property, or {@code null} when the name is not an Attribute
|
||||
*/
|
||||
private static Property<?> collection(Object viewModel, String name) {
|
||||
if (viewModel instanceof ViewModel model) {
|
||||
return model.property(name);
|
||||
@@ -54,6 +64,17 @@ final class RepeatApplier {
|
||||
return NamedProperties.get(viewModel, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds and removes Component children as the collection changes.
|
||||
* <p>
|
||||
* A permutation or an element update is left as it is. An add or remove keeps the children
|
||||
* in list order.
|
||||
*
|
||||
* @param pane the pane named by the repeat
|
||||
* @param type the Component class
|
||||
* @param context the container that creates each Component
|
||||
* @param values the collection
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static void listen(Pane pane, Class<?> type, ApplicationContext context, ObservableList<?> values) {
|
||||
ObservableList<Object> elements = (ObservableList<Object>) values;
|
||||
@@ -74,12 +95,26 @@ final class RepeatApplier {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one Component and sets its model to {@code element}.
|
||||
*
|
||||
* @param type the Component class
|
||||
* @param context the container
|
||||
* @param element the Model instance
|
||||
* @return the loaded Component
|
||||
*/
|
||||
private static Node child(Class<?> type, ApplicationContext context, Object element) {
|
||||
Object component = Components.fresh(type, context);
|
||||
Components.setModel(component, element);
|
||||
return (Node) component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Component class named by the repeat.
|
||||
*
|
||||
* @param className the class name
|
||||
* @return the class
|
||||
*/
|
||||
private static Class<?> componentType(String className) {
|
||||
try {
|
||||
return Class.forName(className);
|
||||
|
||||
@@ -8,6 +8,9 @@ package de.neitzel.fx.view;
|
||||
*/
|
||||
final class SuppliedController implements HasViewModel {
|
||||
|
||||
/**
|
||||
* The ViewModel handed over before the bindings run.
|
||||
*/
|
||||
private ViewModel viewModel;
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,8 +47,10 @@ import java.util.Set;
|
||||
* A collection shown with one Component for each element.
|
||||
* <p>
|
||||
* The developer places the Table in the View and names the collection Attribute, the Row
|
||||
* class, and each Column. Filter and Sort change which Rows are showing and in what order.
|
||||
* class, and each Column. A Column may name a starting width; otherwise the Columns open at
|
||||
* equal widths. Filter and Sort change which Rows are showing and in what order.
|
||||
* The collection keeps its own order. The body scrolls vertically and the header stays put.
|
||||
* Controls after the Column fields stay at their own width.
|
||||
*/
|
||||
@DefaultProperty("columns")
|
||||
public class Table extends VBox {
|
||||
@@ -202,6 +204,9 @@ public class Table extends VBox {
|
||||
* One Filter per Column. A null entry means that Column has no Filter yet.
|
||||
*/
|
||||
private final List<ValueFilter> valueFilters = new ArrayList<>();
|
||||
/**
|
||||
* Index of the Column whose popup is open, or {@code -1} when no popup is open.
|
||||
*/
|
||||
private int popupIndex = -1;
|
||||
|
||||
/**
|
||||
@@ -410,6 +415,9 @@ public class Table extends VBox {
|
||||
if (!seen.add(column.getAttribute())) {
|
||||
throw new IllegalArgumentException("Attribute \"" + column.getAttribute() + "\" is used twice");
|
||||
}
|
||||
if (column.hasWidth() && !(column.getWidth() > 0 && column.getWidth() < Double.POSITIVE_INFINITY)) {
|
||||
throw new IllegalArgumentException("Column \"" + column.getHeader() + "\" needs a positive width");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1344,40 +1352,98 @@ public class Table extends VBox {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives each Column its width, and the same width to each field in that order.
|
||||
* Gives each Column its width.
|
||||
* <p>
|
||||
* Columns with no starting width share the Table width equally. A starting width is kept, and
|
||||
* a later change of the Table's width does not redistribute the Columns. A starting width
|
||||
* narrower than the header text opens at the header text.
|
||||
*/
|
||||
private void ensureWidths() {
|
||||
if (columnWidths != null && columnWidths.length == columns.size()) {
|
||||
return;
|
||||
}
|
||||
columnWidths = new double[columns.size()];
|
||||
if (!anyStartingWidth()) {
|
||||
java.util.Arrays.fill(columnWidths, tableWidth() / columns.size());
|
||||
return;
|
||||
}
|
||||
double specified = 0;
|
||||
int open = 0;
|
||||
for (int index = 0; index < columns.size(); index++) {
|
||||
Column column = columns.get(index);
|
||||
if (column.hasWidth()) {
|
||||
double chosen = Math.max(column.getWidth(), minimumWidth(index));
|
||||
columnWidths[index] = chosen;
|
||||
specified += chosen;
|
||||
} else {
|
||||
open++;
|
||||
}
|
||||
}
|
||||
if (open == 0) {
|
||||
return;
|
||||
}
|
||||
double share = (tableWidth() - specified) / open;
|
||||
for (int index = 0; index < columns.size(); index++) {
|
||||
if (!columns.get(index).hasWidth()) {
|
||||
columnWidths[index] = Math.max(share, minimumWidth(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether any Column names a starting width.
|
||||
*
|
||||
* @return {@code true} when at least one Column has a starting width
|
||||
*/
|
||||
private boolean anyStartingWidth() {
|
||||
for (Column column : columns) {
|
||||
if (column.hasWidth()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the width the equal shares are taken from.
|
||||
*
|
||||
* @return the Table width, or a fallback when the Table has not been laid out
|
||||
*/
|
||||
private double tableWidth() {
|
||||
double width = getWidth() > 0 ? getWidth() : getPrefWidth();
|
||||
if (width <= 0) {
|
||||
width = columns.size() * 80.0;
|
||||
return columns.size() * 80.0;
|
||||
}
|
||||
columnWidths = new double[columns.size()];
|
||||
java.util.Arrays.fill(columnWidths, width / columns.size());
|
||||
return width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the stored Column widths to the header and the fields.
|
||||
* <p>
|
||||
* The first fields, one per Column, take those widths. Controls after them keep their own
|
||||
* size. Spacing on an {@link HBox} row is repeated between the header cells.
|
||||
*/
|
||||
private void applyWidths() {
|
||||
if (columns.isEmpty() || columnWidths == null) {
|
||||
return;
|
||||
}
|
||||
double sum = 0;
|
||||
double columnSum = 0;
|
||||
for (double width : columnWidths) {
|
||||
sum += width;
|
||||
columnSum += width;
|
||||
}
|
||||
header.setPrefWidth(sum);
|
||||
header.setMinWidth(sum);
|
||||
double spacing = rowSpacing();
|
||||
header.setSpacing(spacing);
|
||||
double headerWidth = columnSum + spacing * Math.max(0, columns.size() - 1);
|
||||
header.setPrefWidth(headerWidth);
|
||||
header.setMinWidth(headerWidth);
|
||||
for (int index = 0; index < header.getChildren().size(); index++) {
|
||||
fixWidth((Region) header.getChildren().get(index), columnWidths[index]);
|
||||
}
|
||||
for (Node rowNode : body.getChildren()) {
|
||||
double rowWidth = rowNode instanceof HBox box ? widthOf(box, columnSum) : headerWidth;
|
||||
if (rowNode instanceof Region region) {
|
||||
region.setPrefWidth(sum);
|
||||
region.setMinWidth(sum);
|
||||
region.setPrefWidth(rowWidth);
|
||||
region.setMinWidth(rowWidth);
|
||||
}
|
||||
if (rowNode instanceof Parent parent) {
|
||||
int index = 0;
|
||||
@@ -1394,6 +1460,46 @@ public class Table extends VBox {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the spacing of a Row, so the header can use the same gaps.
|
||||
*
|
||||
* @return the spacing, or {@code 0} when no Row is an {@link HBox}
|
||||
*/
|
||||
private double rowSpacing() {
|
||||
for (Node row : body.getChildren()) {
|
||||
if (row instanceof HBox box) {
|
||||
return box.getSpacing();
|
||||
}
|
||||
}
|
||||
for (Node row : rows.values()) {
|
||||
if (row instanceof HBox box) {
|
||||
return box.getSpacing();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the width that fits the Columns and any controls after them.
|
||||
*
|
||||
* @param box the Row
|
||||
* @param columnSum the sum of the Column widths
|
||||
* @return the Row width
|
||||
*/
|
||||
private double widthOf(HBox box, double columnSum) {
|
||||
double width = columnSum + box.getInsets().getLeft() + box.getInsets().getRight();
|
||||
if (box.getChildren().size() > 1) {
|
||||
width += box.getSpacing() * (box.getChildren().size() - 1);
|
||||
}
|
||||
for (int index = columnWidths.length; index < box.getChildren().size(); index++) {
|
||||
Node child = box.getChildren().get(index);
|
||||
if (child.isManaged() && child instanceof Region region) {
|
||||
width += region.prefWidth(-1);
|
||||
}
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixes one node's width.
|
||||
*
|
||||
|
||||
@@ -77,17 +77,59 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public class ViewModel {
|
||||
|
||||
/**
|
||||
* Receives error and fatal notices. Info and warn are not logged.
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ViewModel.class);
|
||||
|
||||
/**
|
||||
* The Model instance this ViewModel was built for.
|
||||
*/
|
||||
private final Object model;
|
||||
|
||||
/**
|
||||
* JavaFX properties keyed by Attribute name.
|
||||
*/
|
||||
private final Map<String, Property<?>> properties = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Attribute names whose value is a nested ViewModel.
|
||||
*/
|
||||
private final Set<String> complexAttributes = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Notices on this ViewModel, oldest first.
|
||||
*/
|
||||
private final List<Message> messages = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* The current conversion failure for an Attribute name, so a later failure updates it in place.
|
||||
*/
|
||||
private final Map<String, Message> bindingFailures = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Listeners told when a notice is added, removed, or updated.
|
||||
*/
|
||||
private final List<MessageListener> messageListeners = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Mirrors that carry a child ViewModel's notices, keyed by the Attribute that leads to the child.
|
||||
*/
|
||||
private final Map<String, ChildMessageMirror> childMirrors = new HashMap<>();
|
||||
|
||||
/**
|
||||
* The text of {@link #latestMessage}, bound as {@code latestMessage}.
|
||||
*/
|
||||
private final StringProperty latestMessageText = new SimpleStringProperty();
|
||||
|
||||
/**
|
||||
* Callbacks registered by {@link MessagePopup#showNewMessages} and similar callers.
|
||||
*/
|
||||
private final List<Consumer<Message>> messageAdded = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* The notice most recently added, or {@code null} when there is none.
|
||||
*/
|
||||
private Message latestMessage;
|
||||
|
||||
/**
|
||||
@@ -191,6 +233,12 @@ public class ViewModel {
|
||||
return NamedProperties.get(this, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JavaBean attributes of {@code type}, excluding those declared on {@link Object}.
|
||||
*
|
||||
* @param type the Model class
|
||||
* @return the attributes
|
||||
*/
|
||||
private static List<PropertyDescriptor> descriptors(Class<?> type) {
|
||||
try {
|
||||
BeanInfo info = Introspector.getBeanInfo(type, Object.class);
|
||||
@@ -200,6 +248,12 @@ public class ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether {@code type} itself has a read-write attribute and is not a simple value.
|
||||
*
|
||||
* @param type the declared attribute type
|
||||
* @return {@code true} when the attribute is complex
|
||||
*/
|
||||
private static boolean isComplex(Class<?> type) {
|
||||
if (simpleValue(type)) {
|
||||
return false;
|
||||
@@ -214,6 +268,15 @@ public class ViewModel {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether {@code type} stays a simple value even when it has setters.
|
||||
* <p>
|
||||
* Primitives, enums, arrays, strings, numbers, booleans, collections, maps, dates, and
|
||||
* {@code java.time} types are simple.
|
||||
*
|
||||
* @param type the declared attribute type
|
||||
* @return {@code true} when the attribute is not complex
|
||||
*/
|
||||
private static boolean simpleValue(Class<?> type) {
|
||||
if (type.isPrimitive() || type.isEnum() || type.isArray()
|
||||
|| type == String.class || type == Boolean.class) {
|
||||
@@ -232,6 +295,15 @@ public class ViewModel {
|
||||
return packaged != null && packaged.getName().startsWith("java.time");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the JavaFX property for a declared attribute type.
|
||||
* <p>
|
||||
* A null number or boolean leaves the primitive property at its default.
|
||||
*
|
||||
* @param type the declared type
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static Property<?> createProperty(Class<?> type, Object value) {
|
||||
if (type == String.class) {
|
||||
return new SimpleStringProperty((String) value);
|
||||
@@ -254,6 +326,12 @@ public class ViewModel {
|
||||
return new SimpleObjectProperty<>(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an integer property. A null value leaves it at zero.
|
||||
*
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static IntegerProperty integerProperty(Object value) {
|
||||
SimpleIntegerProperty property = new SimpleIntegerProperty();
|
||||
if (value != null) {
|
||||
@@ -262,6 +340,12 @@ public class ViewModel {
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a long property. A null value leaves it at zero.
|
||||
*
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static LongProperty longProperty(Object value) {
|
||||
SimpleLongProperty property = new SimpleLongProperty();
|
||||
if (value != null) {
|
||||
@@ -270,6 +354,12 @@ public class ViewModel {
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a float property. A null value leaves it at zero.
|
||||
*
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static FloatProperty floatProperty(Object value) {
|
||||
SimpleFloatProperty property = new SimpleFloatProperty();
|
||||
if (value != null) {
|
||||
@@ -278,6 +368,12 @@ public class ViewModel {
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a double property. A null value leaves it at zero.
|
||||
*
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static DoubleProperty doubleProperty(Object value) {
|
||||
SimpleDoubleProperty property = new SimpleDoubleProperty();
|
||||
if (value != null) {
|
||||
@@ -286,6 +382,12 @@ public class ViewModel {
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a boolean property. A null value leaves it at {@code false}.
|
||||
*
|
||||
* @param value the current value, or {@code null}
|
||||
* @return the property
|
||||
*/
|
||||
private static BooleanProperty booleanProperty(Object value) {
|
||||
SimpleBooleanProperty property = new SimpleBooleanProperty();
|
||||
if (value != null) {
|
||||
@@ -294,6 +396,13 @@ public class ViewModel {
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the Attribute through its getter.
|
||||
*
|
||||
* @param getter the getter
|
||||
* @param name the Attribute name, used when the read fails
|
||||
* @return the current value
|
||||
*/
|
||||
private Object read(Method getter, String name) {
|
||||
try {
|
||||
return getter.invoke(model);
|
||||
@@ -302,6 +411,13 @@ public class ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the Attribute when the property changes.
|
||||
*
|
||||
* @param property the JavaFX property
|
||||
* @param setter the setter
|
||||
* @param name the Attribute name, used when the write fails
|
||||
*/
|
||||
private void watch(Property<?> property, Method setter, String name) {
|
||||
property.addListener((observable, oldValue, newValue) -> write(setter, name, newValue));
|
||||
}
|
||||
@@ -314,14 +430,35 @@ public class ViewModel {
|
||||
*/
|
||||
private static final class LazyComplex extends SimpleObjectProperty<Object> {
|
||||
|
||||
/**
|
||||
* The Model instance, kept until the nested ViewModel is built.
|
||||
*/
|
||||
private final Object current;
|
||||
|
||||
/**
|
||||
* Whether {@link #get()} has already built the nested ViewModel.
|
||||
*/
|
||||
private boolean realized;
|
||||
|
||||
/**
|
||||
* Whether the change listener is still being attached. Reads during that time are ignored.
|
||||
*/
|
||||
private boolean attaching = true;
|
||||
|
||||
/**
|
||||
* Holds the Model instance until the nested ViewModel is built.
|
||||
*
|
||||
* @param current the Model instance, or {@code null}
|
||||
*/
|
||||
private LazyComplex(Object current) {
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the nested ViewModel on the first read after the listener is attached.
|
||||
*
|
||||
* @return the nested ViewModel, or {@code null} when the Attribute is null
|
||||
*/
|
||||
@Override
|
||||
public Object get() {
|
||||
if (!attaching && !realized) {
|
||||
@@ -331,15 +468,31 @@ public class ViewModel {
|
||||
return super.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the next {@link #get()} to build the nested ViewModel.
|
||||
*/
|
||||
private void attached() {
|
||||
attaching = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether this attribute still holds {@code instance} and has not built its ViewModel.
|
||||
*
|
||||
* @param instance the Model instance just selected elsewhere
|
||||
* @return {@code true} when a shared read should realize this attribute too
|
||||
*/
|
||||
private boolean pending(Object instance) {
|
||||
return !realized && current == instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the Model instance behind a complex Attribute and carries that child's notices.
|
||||
*
|
||||
* @param property the JavaFX property
|
||||
* @param setter the setter
|
||||
* @param name the Attribute name
|
||||
*/
|
||||
private void watchComplex(Property<?> property, Method setter, String name) {
|
||||
property.addListener((observable, oldValue, newValue) -> {
|
||||
if (oldValue != newValue) {
|
||||
@@ -360,6 +513,10 @@ public class ViewModel {
|
||||
* @param name the Attribute that leads to the child
|
||||
* @param child the child ViewModel
|
||||
*/
|
||||
private void attachChild(String name, ViewModel child) {
|
||||
childMirrors.put(name, ChildMessageMirror.attach(this, name, child));
|
||||
}
|
||||
|
||||
/**
|
||||
* Realizes every other Complex attribute that holds the same Model instance.
|
||||
* <p>
|
||||
@@ -379,10 +536,6 @@ public class ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
private void attachChild(String name, ViewModel child) {
|
||||
childMirrors.put(name, ChildMessageMirror.attach(this, name, child));
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the copies carried for an Attribute and stops following that child.
|
||||
*
|
||||
@@ -395,6 +548,13 @@ public class ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the Attribute through its setter.
|
||||
*
|
||||
* @param setter the setter
|
||||
* @param name the Attribute name, used when the write fails
|
||||
* @param value the value
|
||||
*/
|
||||
private void write(Method setter, String name, Object value) {
|
||||
try {
|
||||
setter.invoke(model, value);
|
||||
@@ -607,6 +767,12 @@ public class ViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the getter's collection holds simple values.
|
||||
*
|
||||
* @param getter the collection getter
|
||||
* @return {@code true} when the element type is a simple value
|
||||
*/
|
||||
private static boolean isSimpleValueCollection(Method getter) {
|
||||
if (!Collection.class.isAssignableFrom(getter.getReturnType())) {
|
||||
return false;
|
||||
@@ -615,6 +781,12 @@ public class ViewModel {
|
||||
return element != null && isSimpleValue(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the getter's collection holds Model instances.
|
||||
*
|
||||
* @param getter the collection getter
|
||||
* @return {@code true} when the element type is complex
|
||||
*/
|
||||
private static boolean isModelCollection(Method getter) {
|
||||
if (!Collection.class.isAssignableFrom(getter.getReturnType())) {
|
||||
return false;
|
||||
@@ -623,6 +795,12 @@ public class ViewModel {
|
||||
return element != null && isComplex(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the single type argument of a parameterized type.
|
||||
*
|
||||
* @param type the generic return type
|
||||
* @return the element class, or {@code null} when it cannot be read
|
||||
*/
|
||||
private static Class<?> elementType(Type type) {
|
||||
if (!(type instanceof ParameterizedType parameterized) || parameterized.getActualTypeArguments().length != 1) {
|
||||
return null;
|
||||
@@ -630,6 +808,12 @@ public class ViewModel {
|
||||
return classOf(parameterized.getActualTypeArguments()[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw class of a type argument, including a wildcard's upper bound.
|
||||
*
|
||||
* @param type the type argument
|
||||
* @return the class, or {@code null} when it cannot be read
|
||||
*/
|
||||
private static Class<?> classOf(Type type) {
|
||||
if (type instanceof Class<?> raw) {
|
||||
return raw;
|
||||
@@ -643,6 +827,12 @@ public class ViewModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a collection element stays a plain value rather than a ViewModel.
|
||||
*
|
||||
* @param type the element type
|
||||
* @return {@code true} when the element is a simple value
|
||||
*/
|
||||
private static boolean isSimpleValue(Class<?> type) {
|
||||
if (type == String.class || type == Boolean.class || type == boolean.class || type.isEnum()) {
|
||||
return true;
|
||||
@@ -659,6 +849,14 @@ public class ViewModel {
|
||||
return TemporalAccessor.class.isAssignableFrom(type) || TemporalAmount.class.isAssignableFrom(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a collection in one {@link ObservableList} and stores it on the Model when the setter accepts it.
|
||||
*
|
||||
* @param setter the setter
|
||||
* @param name the Attribute name
|
||||
* @param current the current collection
|
||||
* @return the list the View watches
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object shareSimpleList(Method setter, String name, Object current) {
|
||||
if (!(current instanceof Collection<?> collection)) {
|
||||
|
||||
@@ -12,8 +12,14 @@ import java.util.Map;
|
||||
*/
|
||||
final class ViewModels {
|
||||
|
||||
/**
|
||||
* ViewModels already built, keyed by Model identity.
|
||||
*/
|
||||
private static final Map<Object, Object> BY_INSTANCE = new IdentityHashMap<>();
|
||||
|
||||
/**
|
||||
* Prevents instantiation.
|
||||
*/
|
||||
private ViewModels() {
|
||||
}
|
||||
|
||||
@@ -75,6 +81,16 @@ final class ViewModels {
|
||||
return annotation.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a ViewModel or a replacement with the Model instance.
|
||||
* <p>
|
||||
* A result that is not a {@link ViewModel} is registered here. A {@link ViewModel} registers
|
||||
* itself from its constructor, before its Attributes are walked.
|
||||
*
|
||||
* @param model the Model instance
|
||||
* @param viewModelClass the class to construct
|
||||
* @return the constructed object
|
||||
*/
|
||||
private static Object construct(Object model, Class<?> viewModelClass) {
|
||||
Constructor<?> constructor = constructor(model, viewModelClass);
|
||||
constructor.setAccessible(true);
|
||||
@@ -94,6 +110,13 @@ final class ViewModels {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the constructor that takes the Model, preferring the most specific parameter type.
|
||||
*
|
||||
* @param model the Model instance
|
||||
* @param viewModelClass the class to construct
|
||||
* @return the constructor
|
||||
*/
|
||||
private static Constructor<?> constructor(Object model, Class<?> viewModelClass) {
|
||||
Constructor<?> chosen = null;
|
||||
for (Constructor<?> candidate : viewModelClass.getDeclaredConstructors()) {
|
||||
|
||||
Reference in New Issue
Block a user