Commit 0f072a14 authored by Axel Howind's avatar Axel Howind

options

parent a90afdfc
Pipeline #50097060 failed with stages
in 16 minutes and 9 seconds
......@@ -6,6 +6,7 @@ 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.InputDialogPane.InputControl;
import com.dua3.utility.options.OptionSet;
......@@ -51,6 +52,10 @@ public interface InputBuilder<B extends InputBuilder<B>> {
return radioList(id, label, dflt, cls, Arrays.asList(items));
}
B options(String id, String label, OptionSet options, OptionValues dflt);
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);
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@ 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.InputDialogPane.InputControl;
import com.dua3.utility.options.OptionSet;
......@@ -89,7 +90,7 @@ implements InputBuilder<InputDialogBuilder> {
}
@Override
public InputDialogBuilder options(String id, String label, OptionSet options, OptionValues dflt) {
public InputDialogBuilder options(String id, String label, Supplier<OptionSet> options, Supplier<OptionValues> dflt) {
pb.options(id, label, options, dflt);
return this;
}
......
......@@ -22,6 +22,7 @@ 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.InputDialogPane.InputControl;
import com.dua3.fx.util.controls.InputDialogPane.Meta;
......@@ -263,8 +264,8 @@ implements InputBuilder<InputDialogPaneBuilder> {
}
@Override
public InputDialogPaneBuilder options(String id, String label, OptionSet options, OptionValues dflt) {
return add(id, label, OptionValues.class, dflt, new OptionsPane(options, dflt));
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));
}
// TODO: add date and time inputs
......
package com.dua3.fx.util.controls;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.dua3.fx.util.controls.InputDialogPane.InputControl;
import com.dua3.utility.options.Option;
......@@ -29,9 +27,8 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
/** Logger */
protected static final Logger LOG = Logger.getLogger(OptionsPane.class.getSimpleName());
private final OptionSet options;
private final OptionValues currentValues;
private final OptionValues newValues;
private Supplier<OptionSet> options;
private Supplier<OptionValues> currentValues;
private LinkedHashMap<Option<?>, Property<?>> items;
......@@ -44,13 +41,22 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
* @param currentValues
* the current values
*/
public OptionsPane(OptionSet optionSet, OptionValues currentValues) {
this.options = Objects.requireNonNull(optionSet);
this.currentValues = new OptionValues(currentValues);
this.newValues = new OptionValues(currentValues);
public OptionsPane(OptionSet optionSet, OptionValues currentValues) {
this(() -> optionSet, () -> currentValues);
}
public OptionsPane(Supplier<OptionSet> options, Supplier<OptionValues> dflt) {
this.options = options;
this.currentValues=dflt;
init(options.get(), dflt.get());
}
public void init(OptionSet options, OptionValues currentValues) {
getChildren().clear();
int row = 0;
for (Option<?> option: optionSet) {
for (Option<?> option: options) {
Label label = new Label(option.getName());
Property<?> property;
......@@ -59,14 +65,12 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
if (option instanceof StringOption) {
TextField c = new TextField();
c.setText(String.valueOf(value));
newValues.put(option, () -> c.textProperty().get());
control = c;
property = c.textProperty();
} else if (option instanceof ChoiceOption<?>) {
var items = FXCollections.observableList(((ChoiceOption<?>)option).getChoices());
var c = new ComboBox<>(items);
c.getSelectionModel().select(items.indexOf(value));
newValues.put(option, c.valueProperty().get());
control = c;
property = c.valueProperty();
} else {
......@@ -82,17 +86,8 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
row++;
}
}
}
/**
* Get the options for this OptionsPane.
* @return
* the options
*/
public OptionSet getOptions() {
return options;
}
private void addToGrid(Control child, int c, int r) {
if (child != null) {
add(child, c, r);
......@@ -102,8 +97,13 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
@Override
public OptionValues get() {
return new OptionValues(newValues.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().makeStatic())));
OptionValues values = new OptionValues();
for (var item: items.entrySet()) {
Option<?> option = item.getKey();
Value<?> value = Option.value(item.getValue());
values.put(option, value);
}
return values;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
......
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