-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When run with 0.5.0 each click on "Show form" will add another Validator/Check/TextField to the heap that will not be garbage collected since it dependsOn the "global" property allowedModulus.
- Loading branch information
Robert Lichtenberger
committed
Apr 26, 2024
1 parent
a0bc85d
commit d6e6535
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/test/java/net/synedra/validatorfx/demo/WeakListenerDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package net.synedra.validatorfx.demo; | ||
|
||
import javafx.application.Application; | ||
import javafx.beans.binding.Bindings; | ||
import javafx.beans.value.ObservableIntegerValue; | ||
import javafx.event.ActionEvent; | ||
import javafx.geometry.Insets; | ||
import javafx.geometry.Pos; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.control.RadioButton; | ||
import javafx.scene.control.ToggleGroup; | ||
import javafx.scene.layout.GridPane; | ||
import javafx.stage.Stage; | ||
|
||
/** WeakListenerDemo shows why weak listeners are required in ValidatorFX. | ||
* @author [email protected] | ||
*/ | ||
public class WeakListenerDemo extends Application { | ||
|
||
private WeakListenerDemoForm form; | ||
private GridPane grid; | ||
private RadioButton odd; | ||
private RadioButton even; | ||
private ToggleGroup togglegroup; // this contains the "global" property our check will depend on ... | ||
private ObservableIntegerValue allowedModulus; | ||
|
||
@Override | ||
public void start(Stage primaryStage) throws Exception { | ||
grid = createGrid(); | ||
|
||
Button showFormButton = new Button("Show form"); | ||
showFormButton.setOnAction(this::showNewForm); | ||
grid.add(showFormButton, 0, 0); | ||
|
||
togglegroup = new ToggleGroup(); | ||
|
||
odd = new RadioButton("Odd"); | ||
odd.setToggleGroup(togglegroup); | ||
grid.add(odd, 1, 0); | ||
|
||
even = new RadioButton("Even"); | ||
even.setToggleGroup(togglegroup); | ||
grid.add(even, 2, 0); | ||
|
||
togglegroup.selectToggle(odd); | ||
|
||
allowedModulus = Bindings.createIntegerBinding(() -> { | ||
return togglegroup.getSelectedToggle() == odd ? 1 : 0; | ||
}, togglegroup.selectedToggleProperty()); | ||
|
||
Scene scene = new Scene(grid); | ||
primaryStage.setScene(scene); | ||
primaryStage.show(); | ||
} | ||
|
||
private void showNewForm(ActionEvent e) { | ||
if (form != null) { | ||
grid.getChildren().remove(form.getPresentation()); | ||
} | ||
form = new WeakListenerDemoForm(allowedModulus); | ||
grid.add(form.getPresentation(), 0, 1, 2, 1); | ||
} | ||
|
||
private GridPane createGrid() { | ||
GridPane grid = new GridPane(); | ||
grid.setAlignment(Pos.TOP_LEFT); | ||
grid.setHgap(10); | ||
grid.setVgap(10); | ||
grid.setPadding(new Insets(25, 25, 25, 25)); | ||
grid.setMinSize(600, 400); | ||
return grid; | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
launch(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/net/synedra/validatorfx/demo/WeakListenerDemoForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.synedra.validatorfx.demo; | ||
|
||
import javafx.beans.value.ObservableIntegerValue; | ||
import javafx.geometry.Insets; | ||
import javafx.scene.Node; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.layout.VBox; | ||
import net.synedra.validatorfx.Validator; | ||
|
||
/** This class simulates a (very simple) form that contains a validator and a check. | ||
* The expectation is that if no reference to a WeakListenerDemoForm instance exists, it will be garbage collected. | ||
*/ | ||
class WeakListenerDemoForm { | ||
private Validator validator = new Validator(); | ||
private TextField textField = new TextField(); | ||
WeakListenerDemoForm(ObservableIntegerValue allowedModulus) { | ||
VBox.setMargin(textField, new Insets(5)); | ||
validator.createCheck() | ||
.dependsOn("username", textField.textProperty()) | ||
.dependsOn("allowedModulus", allowedModulus) | ||
.withMethod(c -> { | ||
String userName = c.get("username"); | ||
int allowed = c.get("allowedModulus"); | ||
if (userName.length() % 2 != allowed) { | ||
c.error("Please use " + (allowed == 0 ? "even" : "odd") + " number of letters."); | ||
} | ||
}) | ||
.decorates(textField) | ||
.immediate(); | ||
; | ||
} | ||
|
||
Node getPresentation() { | ||
return textField; | ||
} | ||
} |