Commit e1ea104c authored by Axel Howind's avatar Axel Howind

default suppliers, remove overloads

parent 36ea57e6
Pipeline #50986038 passed with stages
in 17 minutes and 6 seconds
......@@ -62,12 +62,12 @@ public class DialogSample extends Application {
.title("Input")
.header("This is an input dialog.")
.text("Enter data here: ")
.string("txt", "enter text", "dflt")
.integer("integer", "enter number", 0)
.integer("integer from 4 to 7", "enter number", 0,
.string("txt", "enter text", () -> "dflt")
.integer("integer", "enter number", () -> 0)
.integer("integer from 4 to 7", "enter number", () -> 0,
i -> i >= 4 && i <= 7 ? Optional.empty() : Optional.of(i + " is not between 4 and 7"))
.comboBox("list", "choose one", "Maybe", String.class, List.of("Yes", "No", "Maybe"))
.checkBox("bool", "Yes or No:", false, "yes")
.comboBox("list", "choose one", () -> "Maybe", String.class, List.of("Yes", "No", "Maybe"))
.checkBox("bool", "Yes or No:", () -> false, "yes")
.showAndWait();
}));
......@@ -97,7 +97,7 @@ public class DialogSample extends Application {
.page("dbms",
Dialogs.inputPane()
.header("Choose your Database from the list below.")
.radioList("rdmbs", "Database", null, String.class, "H2", "PostgreSQL", "MySQL")
.radioList("rdmbs", "Database", () -> null, String.class, List.of("H2", "PostgreSQL", "MySQL"))
.resultHandler((p,b) -> true))
.showAndWait();
System.out.format("Dialog result:%n%s%n", rc);
......
package com.dua3.fx.util.controls;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import com.dua3.utility.options.OptionSet;
......@@ -13,48 +10,34 @@ import com.dua3.utility.options.OptionValues;
public interface InputBuilder<B extends InputBuilder<B>> {
<T> B add(String id, String label, Class<T> type, T dflt, InputControl<T> control);
<T> B add(String id, String label, Class<T> type, Supplier<T> dflt, InputControl<T> control);
B columns(int columns);
default B string(String id, String label, String dflt) {
default B string(String id, String label, Supplier<String> dflt) {
return string(id, label, dflt, s -> Optional.empty());
}
B string(String id, String label, String dflt, Function<String, Optional<String>> validate);
B string(String id, String label, Supplier<String> dflt, Function<String, Optional<String>> validate);
default B integer(String id, String label, Integer dflt) {
default B integer(String id, String label, Supplier<Integer> dflt) {
return integer(id, label, dflt, i -> Optional.empty());
}
B integer(String id, String label, Integer dflt, IntFunction<Optional<String>> validate);
B integer(String id, String label, Supplier<Integer> dflt, Function<Integer,Optional<String>> validate);
default B decimal(String id, String label, Double dflt) {
default B decimal(String id, String label, Supplier<Double> dflt) {
return decimal(id, label, dflt, d -> Optional.empty());
}
B decimal(String id, String label, Double dflt, DoubleFunction<Optional<String>> validate);
B decimal(String id, String label, Supplier<Double> dflt, Function<Double,Optional<String>> validate);
B checkBox(String id, String label, boolean dflt, String text);
B checkBox(String id, String label, Supplier<Boolean> dflt, String text);
<T> B comboBox(String id, String label, T dflt, Class<T> cls, Collection<T> items);
<T> B comboBox(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items);
@SuppressWarnings("unchecked")
default <T> B comboBox(String id, String label, T dflt, Class<T> cls, T... items) {
return comboBox(id, label, dflt, cls, Arrays.asList(items));
}
<T> B radioList(String id, String label, T dflt, Class<T> cls, Collection<T> items);
@SuppressWarnings("unchecked")
default <T> B radioList(String id, String label, T dflt, Class<T> cls, T... items) {
return radioList(id, label, dflt, cls, Arrays.asList(items));
}
<T> B radioList(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items);
B options(String id, String label, Supplier<OptionSet> options, Supplier<OptionValues> dflt);
default B options(String id, String label, OptionSet options, OptionValues dflt) {
return options(id, label, () -> options, () -> dflt);
}
B options(String id, String label, Supplier<OptionValues> dflt, Supplier<OptionSet> options);
}
\ No newline at end of file
......@@ -17,9 +17,7 @@ package com.dua3.fx.util.controls;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import com.dua3.utility.options.OptionSet;
......@@ -41,7 +39,7 @@ implements InputBuilder<InputDialogBuilder> {
}
@Override
public <T> InputDialogBuilder add(String id, String label, Class<T> type, T dflt, InputControl<T> control) {
public <T> InputDialogBuilder add(String id, String label, Class<T> type, Supplier<T> dflt, InputControl<T> control) {
pb.add(id, label, type, dflt, control);
return this;
}
......@@ -53,44 +51,44 @@ implements InputBuilder<InputDialogBuilder> {
}
@Override
public InputDialogBuilder string(String id, String label, String dflt, Function<String,Optional<String>> validate) {
public InputDialogBuilder string(String id, String label, Supplier<String> dflt, Function<String,Optional<String>> validate) {
pb.string(id, label, dflt, validate);
return this;
}
@Override
public InputDialogBuilder integer(String id, String label, Integer dflt, IntFunction<Optional<String>> validate) {
public InputDialogBuilder integer(String id, String label, Supplier<Integer> dflt, Function<Integer,Optional<String>> validate) {
pb.integer(id, label, dflt, validate);
return this;
}
@Override
public InputDialogBuilder decimal(String id, String label, Double dflt, DoubleFunction<Optional<String>> validate) {
public InputDialogBuilder decimal(String id, String label, Supplier<Double> dflt, Function<Double,Optional<String>> validate) {
pb.decimal(id, label, dflt, validate);
return this;
}
@Override
public InputDialogBuilder checkBox(String id, String label, boolean dflt, String text) {
public InputDialogBuilder checkBox(String id, String label, Supplier<Boolean> dflt, String text) {
pb.checkBox(id, label, dflt, text);
return this;
}
@Override
public <T> InputDialogBuilder comboBox(String id, String label, T dflt, Class<T> cls, Collection<T> items) {
public <T> InputDialogBuilder comboBox(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
pb.comboBox(id, label, dflt, cls, items);
return this;
}
@Override
public <T> InputDialogBuilder radioList(String id, String label, T dflt, Class<T> cls, Collection<T> items) {
public <T> InputDialogBuilder radioList(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
pb.radioList(id, label, dflt, cls, items);
return this;
}
@Override
public InputDialogBuilder options(String id, String label, Supplier<OptionSet> options, Supplier<OptionValues> dflt) {
pb.options(id, label, options, dflt);
public InputDialogBuilder options(String id, String label, Supplier<OptionValues> dflt, Supplier<OptionSet> options) {
pb.options(id, label, dflt, options);
return this;
}
......
......@@ -19,9 +19,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier;
import com.dua3.fx.util.controls.InputPane.Meta;
......@@ -56,7 +54,7 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @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)
*/
@Override
public <T> InputDialogPaneBuilder add(String id, String label, Class<T> type, T dflt, InputControl<T> control) {
public <T> InputDialogPaneBuilder add(String id, String label, Class<T> type, Supplier<T> dflt, InputControl<T> control) {
Objects.requireNonNull(id);
Meta<T> meta = new Meta<>(id, label, type, dflt, control);
Meta<?> prev = data.put(id, meta);
......@@ -78,7 +76,7 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#text(java.lang.String, java.lang.String, java.lang.String, java.util.function.Function)
*/
@Override
public InputDialogPaneBuilder string(String id, String label, String dflt, Function<String,Optional<String>> validate) {
public InputDialogPaneBuilder string(String id, String label, Supplier<String> dflt, Function<String,Optional<String>> validate) {
return add(id, label, String.class, dflt,
new InputControl<String>() {
final TextField control = new TextField();
......@@ -109,7 +107,7 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#integer(java.lang.String, java.lang.String, java.lang.Integer, java.util.function.Function)
*/
@Override
public InputDialogPaneBuilder integer(String id, String label, Integer dflt, IntFunction<Optional<String>> validate) {
public InputDialogPaneBuilder integer(String id, String label, Supplier<Integer> dflt, Function<Integer,Optional<String>> validate) {
return add(id, label, Integer.class, dflt,
new InputControl<Integer>() {
final TextField control = new TextField();
......@@ -146,7 +144,7 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#decimal(java.lang.String, java.lang.String, java.lang.Double, java.util.function.Function)
*/
@Override
public InputDialogPaneBuilder decimal(String id, String label, Double dflt, DoubleFunction<Optional<String>> validate) {
public InputDialogPaneBuilder decimal(String id, String label, Supplier<Double> dflt, Function<Double,Optional<String>> validate) {
return add(id, label, Double.class, dflt,
new InputControl<Double>() {
final TextField control = new TextField();
......@@ -183,13 +181,13 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#checkBox(java.lang.String, java.lang.String, boolean, java.lang.String)
*/
@Override
public InputDialogPaneBuilder checkBox(String id, String label, boolean dflt, String text) {
public InputDialogPaneBuilder checkBox(String id, String label, Supplier<Boolean> dflt, String text) {
return add(id, label, Boolean.class, dflt,
new InputControl<Boolean>() {
final CheckBox control = new CheckBox(text);
{
control.setSelected(dflt);
control.setSelected(dflt.get());
}
@Override
......@@ -218,14 +216,14 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#comboBox(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/
@Override
public <T> InputDialogPaneBuilder comboBox(String id, String label, T dflt, Class<T> cls, Collection<T> items) {
public <T> InputDialogPaneBuilder comboBox(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
return add(id, label, cls, dflt,
new InputControl<T>() {
final ComboBox<T> control = new ComboBox<>();
{
control.setItems(FXCollections.observableArrayList(items));
control.setValue(dflt);
control.setValue(dflt.get());
}
@Override
......@@ -258,13 +256,13 @@ implements InputBuilder<InputDialogPaneBuilder> {
* @see com.dua3.fx.util.controls.InputBuilder#radioList(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/
@Override
public <T> InputDialogPaneBuilder radioList(String id, String label, T dflt, Class<T> cls, Collection<T> items) {
public <T> InputDialogPaneBuilder radioList(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
return add(id, label, cls, dflt, new RadioPane<>(items, null));
}
@Override
public InputDialogPaneBuilder options(String id, String label, Supplier<OptionSet> options, Supplier<OptionValues> dflt) {
return add(id, label, OptionValues.class, dflt.get(), new OptionsPane(options, dflt));
public InputDialogPaneBuilder options(String id, String label, Supplier<OptionValues> dflt, Supplier<OptionSet> options) {
return add(id, label, OptionValues.class, dflt, new OptionsPane(options, dflt));
}
// TODO: add date and time inputs
......
......@@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Logger;
import com.dua3.fx.util.FxUtil;
......@@ -39,12 +40,12 @@ public class InputPane extends InputDialogPane<Map<String,Object>> {
static class Meta<T> {
final String id;
final Class<T> cls;
final T dflt;
final Supplier<T> dflt;
final InputControl<T> control;
Label label = new Label();
Label marker = new Label();
Meta(String id, String label, Class<T> cls, T dflt, InputControl<T> control) {
Meta(String id, String label, Class<T> cls, Supplier<T> dflt, InputControl<T> control) {
this.id = id;
this.label.setText(label);
this.cls = cls;
......@@ -58,7 +59,7 @@ public class InputPane extends InputDialogPane<Map<String,Object>> {
}
void reset() {
control.set(dflt);
control.set(dflt.get());
}
Optional<String> validate() {
......
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