Apply Component FXML attributes after injected initialization
This commit is contained in:
@@ -145,6 +145,49 @@ class EditAddressInAComponentTest {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attributes retain FXML conversion and identity while injection precedes setters.
|
||||
*/
|
||||
@Test
|
||||
void componentAttributesApplyAfterInitialization() throws Exception {
|
||||
onFx(() -> {
|
||||
var context = new ApplicationContext("de.neitzel.fx.view.component");
|
||||
var loaded = ViewLoader.load(getClass().getResource("address-component-attributes.fxml"),
|
||||
new Person("Ada"), context);
|
||||
var first = (AddressForm) loaded.getRoot().lookup("#first");
|
||||
var second = (AddressForm) loaded.getRoot().lookup("#second");
|
||||
assertEquals("Home & work", first.getCaption());
|
||||
assertEquals(190, first.getPrefWidth());
|
||||
assertEquals(360, first.getMaxWidth());
|
||||
assertEquals(false, first.isVisible());
|
||||
assertEquals(javafx.geometry.Pos.CENTER_RIGHT, first.getAlignment());
|
||||
assertEquals(1, javafx.scene.layout.GridPane.getColumnIndex(first));
|
||||
assertSame(first.getModel(), second.getModel());
|
||||
assertEquals("Second", second.getCaption());
|
||||
assertEquals(280, second.getPrefWidth());
|
||||
assertSame(first, context.getComponent(AddressForm.class));
|
||||
TextField field = (TextField) first.lookup("#streetField");
|
||||
assertEquals("Main", field.getText());
|
||||
field.setText("Oak");
|
||||
assertEquals("Oak", ((Address) first.getModel()).getStreet());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalid names and malformed values fail rather than disappearing in the builder.
|
||||
*/
|
||||
@Test
|
||||
void invalidComponentAttributesFailTheLoad() throws Exception {
|
||||
onFx(() -> {
|
||||
for (String kind : java.util.List.of("unknown", "number")) {
|
||||
var context = new ApplicationContext("de.neitzel.fx.view.component");
|
||||
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class,
|
||||
() -> ViewLoader.load(getClass().getResource("address-component-invalid-" + kind + ".fxml"),
|
||||
new Person("Ada"), context));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs one check on the JavaFX application thread.
|
||||
*
|
||||
|
||||
@@ -67,6 +67,22 @@ public class AddressForm extends VBox implements HasViewModel {
|
||||
streetLabel.setText(captions.prefix());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the custom caption after initialization.
|
||||
* @return the caption
|
||||
*/
|
||||
public String getCaption() {
|
||||
return streetLabel.getText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom FXML attribute; the label must already be injected.
|
||||
* @param caption the caption
|
||||
*/
|
||||
public void setCaption(String caption) {
|
||||
streetLabel.setText(caption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model property a parent view sets with a direct binding.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package de.neitzel.fx.view.fixture;
|
||||
|
||||
/** Address that FXML can declare to test object-valued Component attributes. */
|
||||
public class SampleAddress extends Address {
|
||||
/** Creates an initially empty address. */
|
||||
public SampleAddress() {
|
||||
super("");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<?import de.neitzel.fx.view.component.AddressForm?>
|
||||
<?import de.neitzel.fx.view.fixture.SampleAddress?>
|
||||
<GridPane xmlns:fx="http://javafx.com/fxml/1">
|
||||
<fx:define><SampleAddress fx:id="sample" street="Main"/></fx:define>
|
||||
<AddressForm fx:id="first" caption="Home & work" prefWidth="190" maxWidth="360" visible="false" alignment="CENTER_RIGHT" model="$sample" GridPane.columnIndex="1"/>
|
||||
<AddressForm fx:id="second" caption="Second" prefWidth="280" model="$sample" GridPane.rowIndex="1"/>
|
||||
</GridPane>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?import javafx.scene.layout.StackPane?>
|
||||
<?import de.neitzel.fx.view.component.AddressForm?>
|
||||
<StackPane xmlns:fx="http://javafx.com/fxml/1"><AddressForm prefWidth="not-a-number"/></StackPane>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?import javafx.scene.layout.StackPane?>
|
||||
<?import de.neitzel.fx.view.component.AddressForm?>
|
||||
<StackPane xmlns:fx="http://javafx.com/fxml/1"><AddressForm notAProperty="bad"/></StackPane>
|
||||
Reference in New Issue
Block a user