Commit a90afdfc authored by Axel Howind's avatar Axel Howind

InputBuilder.options()

parent a5c8472d
Pipeline #50090785 failed with stages
in 17 minutes and 58 seconds
......@@ -51,6 +51,6 @@ public interface InputBuilder<B extends InputBuilder<B>> {
return radioList(id, label, dflt, cls, Arrays.asList(items));
}
B options(String id, String label, OptionValues dflt, Class<OptionValues> cls, OptionSet options);
B options(String id, String label, OptionSet options, OptionValues dflt);
}
\ No newline at end of file
......@@ -87,11 +87,10 @@ implements InputBuilder<InputDialogBuilder> {
pb.radioList(id, label, dflt, cls, items);
return this;
}
@Override
public InputDialogBuilder options(String id, String label, OptionValues dflt, Class<OptionValues> cls,
OptionSet options) {
pb.options(id, label, dflt, cls, options);
public InputDialogBuilder options(String id, String label, OptionSet options, OptionValues dflt) {
pb.options(id, label, options, dflt);
return this;
}
......
......@@ -263,8 +263,8 @@ implements InputBuilder<InputDialogPaneBuilder> {
}
@Override
public InputDialogPaneBuilder options(String id, String label, OptionValues dflt, Class<OptionValues> cls, OptionSet options) {
return null;
public InputDialogPaneBuilder options(String id, String label, OptionSet options, OptionValues dflt) {
return add(id, label, OptionValues.class, dflt, new OptionsPane(options, dflt));
}
// TODO: add date and time inputs
......
......@@ -43,7 +43,7 @@ public class OptionsDialog extends Dialog<OptionValues> {
return null;
}
return optionPane.getValues();
return optionPane.get();
});
}
......
package com.dua3.fx.util.controls;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.dua3.fx.util.controls.InputDialogPane.InputControl;
import com.dua3.utility.options.Option;
import com.dua3.utility.options.Option.ChoiceOption;
import com.dua3.utility.options.Option.StringOption;
......@@ -12,15 +14,17 @@ import com.dua3.utility.options.Option.Value;
import com.dua3.utility.options.OptionSet;
import com.dua3.utility.options.OptionValues;
import javafx.beans.property.Property;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class OptionsPane extends GridPane {
public class OptionsPane extends GridPane implements InputControl<OptionValues>{
/** Logger */
protected static final Logger LOG = Logger.getLogger(OptionsPane.class.getSimpleName());
......@@ -29,6 +33,8 @@ public class OptionsPane extends GridPane {
private final OptionValues currentValues;
private final OptionValues newValues;
private LinkedHashMap<Option<?>, Property<?>> items;
private static final Insets INSETS = new Insets(2);
/**
......@@ -47,6 +53,7 @@ public class OptionsPane extends GridPane {
for (Option<?> option: optionSet) {
Label label = new Label(option.getName());
Property<?> property;
Control control;
Value<?> value = currentValues.get(option);
if (option instanceof StringOption) {
......@@ -54,17 +61,22 @@ public class OptionsPane extends GridPane {
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 {
LOG.warning("unknown option type: "+option.getClass().getName());
control = null;
property = null;
}
items.put(option, property);
addToGrid(label, 0, row);
addToGrid(control, 1, row);
......@@ -88,9 +100,26 @@ public class OptionsPane extends GridPane {
}
}
public OptionValues getValues() {
@Override
public OptionValues get() {
return new OptionValues(newValues.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().makeStatic())));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void set(OptionValues arg) {
for (var item: items.entrySet()) {
Option option = item.getKey();
Property property = item.getValue();
Value value = arg.get(option);
property.setValue(value.get());
}
}
@Override
public Node node() {
return this;
}
}
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