Commit 960d6a6f authored by Axel Howind's avatar Axel Howind

fix button handling issues

parent 00af6236
Pipeline #52893470 failed with stages
in 17 minutes and 34 seconds
package com.dua3.fx.util.controls;
import com.dua3.utility.data.Pair;
import com.dua3.utility.lang.LangUtil;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
......@@ -11,7 +13,9 @@ import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
......@@ -19,7 +23,7 @@ public abstract class InputDialogPane<R> extends DialogPane implements Supplier<
protected final BooleanProperty valid = new SimpleBooleanProperty(true);
protected ArrayList<ButtonType> buttons = new ArrayList<>();
protected List<Pair<ButtonType, Consumer<InputDialogPane>>> buttons = new ArrayList<>();
public abstract void init();
......@@ -42,7 +46,13 @@ public abstract class InputDialogPane<R> extends DialogPane implements Supplier<
}
public void initButtons() {
getButtonTypes().setAll(buttons);
ObservableList<ButtonType> bt = getButtonTypes();
bt.clear();
for (var b: buttons) {
bt.add(b.first);
Button btn = (Button) lookupButton(b.first);
btn.setOnAction(evt -> b.second.accept(this));
}
}
protected Node createButton(ButtonType buttonType) {
......
package com.dua3.fx.util.controls;
import com.dua3.fx.util.FxUtil;
import com.dua3.utility.data.Pair;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.BooleanExpression;
......@@ -14,6 +15,7 @@ import javafx.scene.layout.GridPane;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Logger;
......@@ -79,7 +81,7 @@ public class InputPane extends InputDialogPane<Map<String,Object>> {
GridPane.setMargin(child, insets);
}
void setContent(Collection<Meta<?>> data, int columns, List<ButtonType> buttons) {
void setContent(Collection<Meta<?>> data, int columns, List<Pair<ButtonType, Consumer<InputDialogPane>>> buttons) {
this.data = Objects.requireNonNull(data);
this.columns = columns;
this.buttons = new ArrayList<>(buttons);
......
......@@ -15,12 +15,14 @@
package com.dua3.fx.util.controls;
import com.dua3.fx.util.controls.InputPane.Meta;
import com.dua3.utility.data.Pair;
import com.dua3.utility.lang.LangUtil;
import com.dua3.utility.options.OptionSet;
import com.dua3.utility.options.OptionValues;
import javafx.scene.control.ButtonType;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
......@@ -41,7 +43,7 @@ implements InputBuilder<InputPaneBuilder> {
private int columns = 1;
private LinkedHashMap<String, InputPane.Meta<?>> data = new LinkedHashMap<>();
private List<ButtonType> buttons = Collections.emptyList();
private List<Pair<ButtonType,Consumer<InputDialogPane>>> buttons = new LinkedList<>();
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#add(java.lang.String, java.lang.String, java.lang.Class, T, com.dua3.fx.util.controls.InputDialogPane.InputControl)
......@@ -136,8 +138,8 @@ implements InputBuilder<InputPaneBuilder> {
// TODO: add date and time inputs
public InputPaneBuilder buttons(ButtonType... b) {
buttons = List.of(b);
public InputPaneBuilder button(ButtonType b, Consumer<InputDialogPane> action) {
buttons.add(Pair.of(b,action));
return this;
}
......
......@@ -7,7 +7,6 @@ import javafx.beans.binding.BooleanExpression;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
......@@ -17,7 +16,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -127,21 +126,19 @@ public class WizardDialog extends Dialog<Map<String,Object>> {
// cancel button
if (isCancelable()) {
addButtonToDialogPane(page, ButtonType.CANCEL, evt -> true, evt -> true, null);
addButtonToDialogPane(page, ButtonType.CANCEL, p -> {}, null);
}
// next button
if (page.getNext()==null) {
addButtonToDialogPane(page, ButtonType.FINISH, evt -> true, evt -> true, pane.validProperty());
addButtonToDialogPane(page, ButtonType.FINISH, p -> {}, pane.validProperty());
} else {
addButtonToDialogPane(
page,
ButtonType.NEXT,
evt -> true,
evt -> {
p -> {
pageStack.add(Pair.of(name,page));
setPage(page.getNext());
return true;
},
pane.validProperty());
}
......@@ -150,11 +147,8 @@ public class WizardDialog extends Dialog<Map<String,Object>> {
if (isShowPreviousButton()) {
addButtonToDialogPane(
page,
ButtonType.PREVIOUS, evt -> {
setPage(pageStack.remove(pageStack.size()-1).first);
return false;
},
evt -> true,
ButtonType.PREVIOUS,
p -> setPage(pageStack.remove(pageStack.size()-1).first),
Bindings.isNotEmpty(pageStack)
);
}
......@@ -181,8 +175,7 @@ public class WizardDialog extends Dialog<Map<String,Object>> {
private void addButtonToDialogPane(
Page<?,?> page,
ButtonType bt,
Predicate<Event> precondition,
Predicate<Event> postcondition,
Consumer<InputDialogPane<?>> action,
BooleanExpression enabled) {
InputDialogPane<?> pane = page.pane;
List<ButtonType> buttons = pane.getButtonTypes();
......@@ -194,26 +187,13 @@ public class WizardDialog extends Dialog<Map<String,Object>> {
// when using an event handler, Dialog.close() is called before our own
// event handler.
btn.addEventFilter(ActionEvent.ACTION, evt -> {
// check precondition
if (!precondition.test(evt)) {
LOG.fine(() -> String.format("Button %s: precondition failed", bt));
evt.consume();
return;
}
// get and translate result
if (!page.apply(bt)) {
LOG.fine(() -> String.format("Button %s: result conversion failed", bt));
evt.consume();
return;
}
// check postcondition
if (!postcondition.test(evt)) {
LOG.fine(() -> String.format("Button %s: postcondition failed", bt));
evt.consume();
return;
}
action.accept(page.getPane());
});
if (enabled!=null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment