Skip to content

Commit abe44ec

Browse files
committed
Remove view model argument for the default init/deinit methods of View
1 parent d0c685c commit abe44ec

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

mvvm4fx-core/src/main/java/com/techsenger/mvvm4fx/core/AbstractChildView.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,4 @@ public AbstractChildView(T viewModel) {
3131
public AbstractChildComponent<?> getComponent() {
3232
return (AbstractChildComponent<?>) super.getComponent();
3333
}
34-
35-
@Override
36-
protected void addListeners(T viewModel) {
37-
super.addListeners(viewModel);
38-
}
3934
}

mvvm4fx-core/src/main/java/com/techsenger/mvvm4fx/core/AbstractComponentView.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,68 +49,68 @@ public AbstractComponent<?> getComponent() {
4949
* Performs initialization.
5050
*/
5151
protected void initialize() {
52-
build(viewModel);
53-
bind(viewModel);
54-
addListeners(viewModel);
55-
addHandlers(viewModel);
52+
build();
53+
bind();
54+
addListeners();
55+
addHandlers();
5656
}
5757

5858
/**
5959
* Performs deinitialization.
6060
*/
6161
protected void deinitialize() {
62-
removeHandlers(viewModel);
63-
removeListeners(viewModel);
64-
unbind(viewModel);
65-
unbuild(viewModel);
62+
removeHandlers();
63+
removeListeners();
64+
unbind();
65+
unbuild();
6666
}
6767

6868
/**
6969
* Builds view.
7070
*/
71-
protected void build(T viewModel) { }
71+
protected void build() { }
7272

7373
/**
7474
* Binds view to viewModel etc.
7575
*/
76-
protected void bind(T viewModel) { }
76+
protected void bind() { }
7777

7878
/**
7979
* Initializes listeners to different properties etc.
8080
*/
81-
protected void addListeners(T viewModel) { }
81+
protected void addListeners() { }
8282

8383
/**
8484
* Initializes handlers for mouse, keyboard etc events.
8585
*/
86-
protected void addHandlers(T viewModel) { }
86+
protected void addHandlers() { }
8787

8888
/**
8989
* Removes handlers.
9090
*
9191
* @param viewModel
9292
*/
93-
protected void removeHandlers(T viewModel) { }
93+
protected void removeHandlers() { }
9494

9595
/**
9696
* Removes listeners.
9797
*
9898
* @param viewModel
9999
*/
100-
protected void removeListeners(T viewModel) { }
100+
protected void removeListeners() { }
101101

102102
/**
103103
* Unbinds view from viewModel etc.
104104
*
105105
* @param viewModel
106106
*/
107-
protected void unbind(T viewModel) { }
107+
protected void unbind() { }
108108

109109
/**
110110
* Unbuilds view.
111111
* @param viewModel
112112
*/
113-
protected void unbuild(T viewModel) { }
113+
protected void unbuild() { }
114114

115115
void setComponent(AbstractComponent<?> component) {
116116
this.component = component;

mvvm4fx-demo/src/main/java/com/techsenger/mvvm4fx/demo/PersonDialogView.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public Dialog<Person> getDialog() {
7171
}
7272

7373
@Override
74-
protected void build(PersonDialogViewModel viewModel) {
75-
super.build(viewModel);
74+
protected void build() {
75+
super.build();
7676
firstNameLabel.setMinWidth(Region.USE_PREF_SIZE);
7777
HBox.setHgrow(firstNameTextField, Priority.ALWAYS);
7878
lastNameLabel.setMinWidth(Region.USE_PREF_SIZE);
@@ -88,7 +88,7 @@ protected void build(PersonDialogViewModel viewModel) {
8888
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL, ButtonType.OK);
8989
dialog.setResultConverter(dialogButton -> {
9090
if (dialogButton == ButtonType.OK) {
91-
return viewModel.createPerson();
91+
return getViewModel().createPerson();
9292
}
9393
return null;
9494
});
@@ -97,24 +97,25 @@ protected void build(PersonDialogViewModel viewModel) {
9797
}
9898

9999
@Override
100-
protected void bind(PersonDialogViewModel viewModel) {
101-
super.bind(viewModel);
102-
dialog.titleProperty().bind(viewModel.titleProperty());
103-
104-
firstNameTextField.textProperty().bindBidirectional(viewModel.getPerson().firstNameProperty());
105-
bindValid(firstNameTextField, viewModel.firstNameValidProperty());
106-
lastNameTextField.textProperty().bindBidirectional(viewModel.getPerson().lastNameProperty());
107-
bindValid(lastNameTextField, viewModel.lastNameValidProperty());
108-
ageTextField.textProperty().bindBidirectional(viewModel.getPerson().ageProperty(),
100+
protected void bind() {
101+
super.bind();
102+
var vm = getViewModel();
103+
dialog.titleProperty().bind(vm.titleProperty());
104+
105+
firstNameTextField.textProperty().bindBidirectional(vm.getPerson().firstNameProperty());
106+
bindValid(firstNameTextField, vm.firstNameValidProperty());
107+
lastNameTextField.textProperty().bindBidirectional(vm.getPerson().lastNameProperty());
108+
bindValid(lastNameTextField, vm.lastNameValidProperty());
109+
ageTextField.textProperty().bindBidirectional(vm.getPerson().ageProperty(),
109110
new IntegerStringConverter());
110-
bindValid(ageTextField, viewModel.ageValidProperty());
111+
bindValid(ageTextField, vm.ageValidProperty());
111112
}
112113

113114
@Override
114-
protected void addHandlers(PersonDialogViewModel viewModel) {
115-
super.addHandlers(viewModel);
115+
protected void addHandlers() {
116+
super.addHandlers();
116117
okButton.addEventFilter(ActionEvent.ACTION, event -> {
117-
if (!viewModel.isPersonValid()) {
118+
if (!getViewModel().isPersonValid()) {
118119
event.consume();
119120
}
120121
});

mvvm4fx-demo/src/main/java/com/techsenger/mvvm4fx/demo/PersonRegistryView.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public PersonRegistryView(Stage stage, PersonRegistryViewModel viewModel) {
5656
}
5757

5858
@Override
59-
protected void build(PersonRegistryViewModel viewModel) {
60-
super.build(viewModel);
59+
protected void build() {
60+
super.build();
6161
VBox.setVgrow(personTable, Priority.ALWAYS);
62-
personTable.setItems(viewModel.getPersons());
62+
personTable.setItems(getViewModel().getPersons());
6363
personTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
6464
var idColumn = new TableColumn<Person, Integer>("Id");
6565
idColumn.setCellValueFactory(data -> data.getValue().idProperty());
@@ -78,31 +78,33 @@ protected void build(PersonRegistryViewModel viewModel) {
7878
}
7979

8080
@Override
81-
protected void bind(PersonRegistryViewModel viewModel) {
82-
super.bind(viewModel);
83-
stage.titleProperty().bind(viewModel.titleProperty());
84-
viewModel.selectedPersonProperty().bind(personTable.getSelectionModel().selectedItemProperty());
85-
removeButton.disableProperty().bind(viewModel.removeDisabledProperty());
81+
protected void bind() {
82+
super.bind();
83+
var vm = getViewModel();
84+
stage.titleProperty().bind(vm.titleProperty());
85+
vm.selectedPersonProperty().bind(personTable.getSelectionModel().selectedItemProperty());
86+
removeButton.disableProperty().bind(vm.removeDisabledProperty());
8687
}
8788

8889
@Override
89-
protected void addHandlers(PersonRegistryViewModel viewModel) {
90-
super.addHandlers(viewModel);
90+
protected void addHandlers() {
91+
super.addHandlers();
92+
var vm = getViewModel();
9193
addButton.setOnAction(e -> {
92-
var dialogVM = viewModel.createDialog();
94+
var dialogVM = vm.createDialog();
9395
var dialogV = new PersonDialogView(stage, dialogVM);
9496
var dialogComponent = new PersonDialog(dialogV);
9597
dialogComponent.initialize();
9698

9799
var jfxDialog = dialogV.getDialog();
98100
// javafx 19 has a bug - it shows a system notification in Ubuntu when closing
99101
var result = jfxDialog.showAndWait();
100-
viewModel.add(result);
102+
vm.add(result);
101103

102104
dialogComponent.deinitialize();
103105
});
104-
removeButton.setOnAction(e -> viewModel.remove());
105-
refreshButton.setOnAction(e -> viewModel.refresh());
106+
removeButton.setOnAction(e -> vm.remove());
107+
refreshButton.setOnAction(e -> vm.refresh());
106108
}
107109

108110
void showStage() {

0 commit comments

Comments
 (0)