Complete JavaFX MVVM examples, bindings and library documentation
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
package de.neitzel.fx.example;
|
||||
|
||||
import de.neitzel.fx.example.model.Person;
|
||||
import de.neitzel.fx.example.model.Role;
|
||||
import de.neitzel.fx.example.view.AddressForm;
|
||||
import de.neitzel.fx.example.view.DeskController;
|
||||
import de.neitzel.fx.example.view.PersonViewModel;
|
||||
import de.neitzel.fx.example.view.VisitForm;
|
||||
import de.neitzel.fx.example.view.VisitRow;
|
||||
import de.neitzel.fx.view.LoadedView;
|
||||
import de.neitzel.fx.view.ViewLoader;
|
||||
import de.neitzel.injection.ApplicationContext;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.CheckBox;
|
||||
import javafx.scene.control.ChoiceBox;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.ScrollPane;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.scene.layout.VBox;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -21,7 +31,12 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
@@ -29,6 +44,9 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||
*/
|
||||
class DeskLoadsTest {
|
||||
|
||||
/**
|
||||
* Starts the JavaFX toolkit once for this class.
|
||||
*/
|
||||
@BeforeAll
|
||||
static void startJavaFx() throws InterruptedException {
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
@@ -42,50 +60,342 @@ class DeskLoadsTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The desk shows the sample person, including directions, the title, and the home note.
|
||||
*/
|
||||
@Test
|
||||
void deskEditsThePersonAndKeepsTheGreetingCurrent() throws Exception {
|
||||
void deskShowsThePerson() throws Exception {
|
||||
onFx(() -> {
|
||||
Person person = DeskApp.sample();
|
||||
ApplicationContext context = new ApplicationContext(DeskApp.class);
|
||||
LoadedView loaded = ViewLoader.load(DeskController.class.getResource("Desk.fxml"), person, context);
|
||||
Parent root = loaded.getRoot();
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
AddressForm home = homeForm(root);
|
||||
|
||||
TextField nameField = (TextField) root.lookup("#nameField");
|
||||
TextField ageField = (TextField) root.lookup("#ageField");
|
||||
Label greeting = (Label) root.lookup("#greetingLabel");
|
||||
Label homeStreet = (Label) root.lookup("#homeStreetLabel");
|
||||
TextField street = (TextField) homeForm(root).lookup("#streetField");
|
||||
VBox visits = (VBox) root.lookup("#visitPane");
|
||||
ListView<?> tags = (ListView<?>) root.lookup("#tagList");
|
||||
|
||||
assertEquals("Ada Lovelace", nameField.getText());
|
||||
assertEquals("Hello, Ada Lovelace", greeting.getText());
|
||||
assertEquals("years 36", ageField.getText());
|
||||
assertEquals("Sample Street 1", street.getText());
|
||||
assertEquals("Sample Street 1", homeStreet.getText());
|
||||
assertEquals(1, visits.getChildren().size());
|
||||
assertEquals(2, tags.getItems().size());
|
||||
|
||||
nameField.setText("Grace");
|
||||
assertEquals("Grace", person.getName());
|
||||
assertEquals("Hello, Grace", greeting.getText());
|
||||
|
||||
ageField.setText("years 41");
|
||||
assertEquals(Integer.valueOf(41), person.getAge());
|
||||
ageField.setText("nope");
|
||||
assertEquals(Integer.valueOf(41), person.getAge());
|
||||
assertNotNull(loaded.getViewModel().getLatestMessage());
|
||||
assertEquals("age", loaded.getViewModel().getLatestMessage().getSource());
|
||||
|
||||
street.setText("Other Street");
|
||||
assertEquals("Other Street", person.getHome().getStreet());
|
||||
assertEquals("Other Street", homeStreet.getText());
|
||||
|
||||
person.getTags().add("fx");
|
||||
assertEquals(3, tags.getItems().size());
|
||||
assertEquals("Ada Lovelace", text(root, "nameField"));
|
||||
assertEquals("Ada Lovelace", text(root, "nameEcho"));
|
||||
assertEquals("Ada Lovelace", text(root, "nameDraft"));
|
||||
assertEquals("Hello, Ada Lovelace (36)", text(root, "greetingLabel"));
|
||||
assertEquals("years 36", text(root, "ageField"));
|
||||
assertEquals("titled-Ada Lovelace", text(root, "titleLabel"));
|
||||
assertTrue(((CheckBox) root.lookup("#activeBox")).isSelected());
|
||||
assertEquals(Role.AUTHOR, ((ChoiceBox<?>) root.lookup("#roleChoice")).getValue());
|
||||
assertEquals("Sample Street 1", text(home, "streetField"));
|
||||
assertEquals("Sample City", text(home, "cityField"));
|
||||
assertEquals("Sample Street 1", text(root, "homeStreetLabel"));
|
||||
assertEquals("Address", text(home, "headingLabel"));
|
||||
assertEquals("Opened as Sample City", text(home, "openedAsLabel"));
|
||||
assertEquals("Kept at home", text(home, "noteField"));
|
||||
assertEquals(3, ((VBox) root.lookup("#visitPane")).getChildren().size());
|
||||
assertEquals(3, tableBody(root).getChildren().size());
|
||||
assertEquals("Street", headerText(root, 0));
|
||||
assertEquals("City", headerText(root, 1));
|
||||
assertEquals("Visited", headerText(root, 2));
|
||||
HBox header = tableHeader(root);
|
||||
assertEquals(300, ((Region) header.getChildren().get(0)).getPrefWidth());
|
||||
assertEquals(76, ((Region) header.getChildren().get(1)).getPrefWidth());
|
||||
assertEquals(120, ((Region) header.getChildren().get(2)).getPrefWidth());
|
||||
assertEquals(8, header.getSpacing());
|
||||
VisitRow first = (VisitRow) tableBody(root).getChildren().get(0);
|
||||
assertSame(loaded.person().getVisits().get(0), first.getModel());
|
||||
assertEquals("Work Road 2", text(first, "streetField"));
|
||||
assertEquals("Office City", text(first, "cityField"));
|
||||
assertEquals("2020-01-02", text(first, "visitedField"));
|
||||
assertTrue(first.getStyleClass().contains("selected"));
|
||||
assertEquals(2, ((ListView<?>) root.lookup("#tagList")).getItems().size());
|
||||
assertInstanceOf(PersonViewModel.class, loaded.view().getViewModel());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A later call to the entity setter leaves the controls on the view-model value.
|
||||
*/
|
||||
@Test
|
||||
void entitySetterLeavesTheControls() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
loaded.person().setName("Ignored");
|
||||
assertEquals("Ignored", loaded.person().getName());
|
||||
assertEquals("Ada Lovelace", text(loaded.root(), "nameField"));
|
||||
assertEquals("Ada Lovelace", text(loaded.root(), "nameEcho"));
|
||||
assertEquals("Ada Lovelace", text(loaded.root(), "nameDraft"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A second view of the same person reuses the view model, so a name edit shows on both.
|
||||
* <p>
|
||||
* The second document is only a name field. Loading the desk again would rebuild the home
|
||||
* address form, and that form is the cached singleton already showing.
|
||||
*/
|
||||
@Test
|
||||
void secondViewReusesTheViewModel() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded first = load();
|
||||
LoadedView second = ViewLoader.load(
|
||||
DeskLoadsTest.class.getResource("NameLine.fxml"), first.person(), first.context());
|
||||
assertSame(first.view().getViewModel(), second.getViewModel());
|
||||
textField(first.root(), "nameField").setText("Grace");
|
||||
assertEquals("Grace", first.person().getName());
|
||||
assertEquals("Grace", text(second.getRoot(), "nameField"));
|
||||
assertEquals("titled-Grace", text(first.root(), "titleLabel"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Both, view, and model directions on the name, and the title follows the view model.
|
||||
*/
|
||||
@Test
|
||||
void nameDirectionsUpdateTheTitleAndTheGreeting() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
textField(root, "nameField").setText("Grace");
|
||||
assertEquals("Grace", loaded.person().getName());
|
||||
assertEquals("Grace", text(root, "nameEcho"));
|
||||
assertEquals("Ada Lovelace", text(root, "nameDraft"));
|
||||
assertEquals("Hello, Grace (36)", text(root, "greetingLabel"));
|
||||
assertEquals("titled-Grace", text(root, "titleLabel"));
|
||||
|
||||
textField(root, "nameEcho").setText("Hopper");
|
||||
assertEquals("Grace", loaded.person().getName());
|
||||
assertEquals("Grace", text(root, "nameField"));
|
||||
|
||||
textField(root, "nameDraft").setText("Byron");
|
||||
assertEquals("Byron", loaded.person().getName());
|
||||
assertEquals("Byron", text(root, "nameField"));
|
||||
assertEquals("Byron", text(root, "nameEcho"));
|
||||
assertEquals("titled-Byron", text(root, "titleLabel"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A bad age keeps the previous value, records one error, and a later success clears it.
|
||||
* Changing the age also refreshes the greeting.
|
||||
*/
|
||||
@Test
|
||||
void ageConverterRecordsAndClearsAFailure() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
TextField age = textField(root, "ageField");
|
||||
age.setText("years 41");
|
||||
assertEquals(Integer.valueOf(41), loaded.person().getAge());
|
||||
assertEquals("Hello, Ada Lovelace (41)", text(root, "greetingLabel"));
|
||||
|
||||
age.setText("nope");
|
||||
assertEquals(Integer.valueOf(41), loaded.person().getAge());
|
||||
assertEquals("nope", age.getText());
|
||||
assertEquals("age", loaded.view().getViewModel().getLatestMessage().getSource());
|
||||
assertEquals(de.neitzel.fx.view.Message.Severity.ERROR,
|
||||
loaded.view().getViewModel().getLatestMessage().getSeverity());
|
||||
assertEquals(loaded.view().getViewModel().getLatestMessage().getText(), text(root, "noticeLabel"));
|
||||
|
||||
age.setText("years 41");
|
||||
assertNull(loaded.view().getViewModel().getLatestMessage());
|
||||
assertNull(text(root, "noticeLabel"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* An empty home street is carried onto the person as {@code home.street}, then cleared.
|
||||
*/
|
||||
@Test
|
||||
void emptyStreetIsCarriedFromTheHomeAddress() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
AddressForm home = homeForm(root);
|
||||
TextField street = textField(home, "streetField");
|
||||
street.setText(" ");
|
||||
assertEquals("Sample Street 1", loaded.person().getHome().getStreet());
|
||||
assertEquals(" ", street.getText());
|
||||
assertEquals("Sample Street 1", text(root, "homeStreetLabel"));
|
||||
assertEquals("home.street", loaded.view().getViewModel().getLatestMessage().getSource());
|
||||
assertEquals("Street is required", text(root, "noticeLabel"));
|
||||
|
||||
street.setText("Other Street");
|
||||
assertEquals("Other Street", loaded.person().getHome().getStreet());
|
||||
assertEquals("Other Street", text(root, "homeStreetLabel"));
|
||||
assertNull(loaded.view().getViewModel().getLatestMessage());
|
||||
|
||||
textField(home, "cityField").setText("Other City");
|
||||
assertEquals("Opened as Sample City", text(home, "openedAsLabel"));
|
||||
textField(home, "noteField").setText("Moved");
|
||||
assertEquals("Moved", loaded.person().getHome().getNote().getText());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Editing a table row updates the visit and the repeated form. Removing the row drops it.
|
||||
*/
|
||||
@Test
|
||||
void tableRowEditsTheVisitAndRemovesIt() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
VBox visits = (VBox) root.lookup("#visitPane");
|
||||
VisitRow first = (VisitRow) tableBody(root).getChildren().get(0);
|
||||
textField(first, "streetField").setText("Changed Road");
|
||||
assertEquals("Changed Road", loaded.person().getVisits().get(0).getStreet());
|
||||
VisitForm repeated = (VisitForm) visits.getChildren().get(0);
|
||||
assertEquals("Changed Road", text(repeated, "streetField"));
|
||||
|
||||
ListView<?> tags = (ListView<?>) root.lookup("#tagList");
|
||||
loaded.person().getTags().add("fx");
|
||||
assertEquals(3, tags.getItems().size());
|
||||
|
||||
Button remove = (Button) first.lookup(".button");
|
||||
assertEquals(Region.USE_COMPUTED_SIZE, remove.getPrefWidth());
|
||||
remove.fire();
|
||||
assertEquals(2, loaded.person().getVisits().size());
|
||||
assertNull(loaded.person().getSelectedVisit());
|
||||
assertEquals(2, visits.getChildren().size());
|
||||
assertEquals(2, tableBody(root).getChildren().size());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Info, warning, and fatal notices show on the notice label.
|
||||
*/
|
||||
@Test
|
||||
void noticesShowInfoWarningAndFatal() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
DeskController desk = loaded.context().getComponent(DeskController.class);
|
||||
desk.leaveNote();
|
||||
assertEquals(de.neitzel.fx.view.Message.Severity.INFO,
|
||||
loaded.view().getViewModel().getLatestMessage().getSeverity());
|
||||
assertEquals("The model already has the name Ada Lovelace.", text(root, "noticeLabel"));
|
||||
desk.leaveWarning();
|
||||
assertEquals(de.neitzel.fx.view.Message.Severity.WARN,
|
||||
loaded.view().getViewModel().getLatestMessage().getSeverity());
|
||||
assertEquals("Check the age before leaving the desk.", text(root, "noticeLabel"));
|
||||
desk.leaveFatal();
|
||||
assertEquals(de.neitzel.fx.view.Message.Severity.FATAL,
|
||||
loaded.view().getViewModel().getLatestMessage().getSeverity());
|
||||
assertEquals("The desk cannot store this person.", text(root, "noticeLabel"));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The active flag and the role write back to the person.
|
||||
*/
|
||||
@Test
|
||||
void activeAndRoleWriteBack() throws Exception {
|
||||
onFx(() -> {
|
||||
Loaded loaded = load();
|
||||
Parent root = loaded.root();
|
||||
CheckBox active = (CheckBox) root.lookup("#activeBox");
|
||||
active.setSelected(false);
|
||||
assertFalse(loaded.person().isActive());
|
||||
@SuppressWarnings("unchecked")
|
||||
ChoiceBox<Role> role = (ChoiceBox<Role>) root.lookup("#roleChoice");
|
||||
role.setValue(Role.READER);
|
||||
assertEquals(Role.READER, loaded.person().getRole());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* One loaded desk: the person, the view, and the container that built it.
|
||||
*
|
||||
* @param person the model
|
||||
* @param view the loaded view
|
||||
* @param context the container
|
||||
*/
|
||||
private record Loaded(Person person, LoadedView view, ApplicationContext context) {
|
||||
|
||||
/**
|
||||
* Returns the scene-graph root.
|
||||
*
|
||||
* @return the root
|
||||
*/
|
||||
private Parent root() {
|
||||
return view.getRoot();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the desk for a fresh sample person.
|
||||
*
|
||||
* @return the loaded desk
|
||||
*/
|
||||
private static Loaded load() {
|
||||
Person person = DeskApp.sample();
|
||||
ApplicationContext context = new ApplicationContext(DeskApp.class);
|
||||
LoadedView view = ViewLoader.load(DeskController.class.getResource("Desk.fxml"), person, context);
|
||||
return new Loaded(person, view, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text of a labeled control.
|
||||
*
|
||||
* @param parent the parent to search
|
||||
* @param id the control id
|
||||
* @return the text
|
||||
*/
|
||||
private static String text(Parent parent, String id) {
|
||||
Node node = parent.lookup("#" + id);
|
||||
if (node instanceof TextField field) {
|
||||
return field.getText();
|
||||
}
|
||||
if (node instanceof Label label) {
|
||||
return label.getText();
|
||||
}
|
||||
fail("no text on " + id);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a text field.
|
||||
*
|
||||
* @param parent the parent to search
|
||||
* @param id the field id
|
||||
* @return the field
|
||||
*/
|
||||
private static TextField textField(Parent parent, String id) {
|
||||
return (TextField) parent.lookup("#" + id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the visit table body.
|
||||
*
|
||||
* @param root the loaded scene
|
||||
* @return the rows currently showing
|
||||
*/
|
||||
private static VBox tableBody(Parent root) {
|
||||
ScrollPane scroll = (ScrollPane) root.lookup("#visitTable").lookup(".table-scroll");
|
||||
return (VBox) scroll.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the header text of one visit-table column.
|
||||
*
|
||||
* @param root the loaded scene
|
||||
* @param index the column index
|
||||
* @return the header text
|
||||
*/
|
||||
private static String headerText(Parent root, int index) {
|
||||
Label label = (Label) tableHeader(root).getChildren().get(index).lookup(".column-header");
|
||||
return label.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the visit-table header.
|
||||
*
|
||||
* @param root the loaded scene
|
||||
* @return the header
|
||||
*/
|
||||
private static HBox tableHeader(Parent root) {
|
||||
ScrollPane headerScroll = (ScrollPane) root.lookup("#visitTable").lookup(".table-header-scroll");
|
||||
return (HBox) headerScroll.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the home-address component in the loaded desk.
|
||||
*
|
||||
* @param root the loaded scene
|
||||
* @return the home-address component
|
||||
*/
|
||||
private static AddressForm homeForm(Parent root) {
|
||||
for (Node child : root.getChildrenUnmodifiable()) {
|
||||
if (child instanceof AddressForm form) {
|
||||
@@ -96,6 +406,11 @@ class DeskLoadsTest {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs one check on the JavaFX application thread.
|
||||
*
|
||||
* @param check the check
|
||||
*/
|
||||
private static void onFx(Runnable check) throws InterruptedException {
|
||||
CountDownLatch done = new CountDownLatch(1);
|
||||
AtomicReference<Throwable> failure = new AtomicReference<>();
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import de.neitzel.fx.view.BindingList?>
|
||||
<?import de.neitzel.fx.view.DirectBinding?>
|
||||
|
||||
<VBox xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
|
||||
<fx:define>
|
||||
<BindingList>
|
||||
<DirectBinding control="nameField" property="text" path="name" direction="both"/>
|
||||
</BindingList>
|
||||
</fx:define>
|
||||
<TextField fx:id="nameField" id="nameField"/>
|
||||
</VBox>
|
||||
Reference in New Issue
Block a user