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

InputControl (wip)

parent 57cddea5
Pipeline #51480286 failed with stages
in 24 minutes and 35 seconds
package com.dua3.fx.util;
import java.util.Objects;
import com.dua3.utility.options.Option;
import com.dua3.utility.options.Option.Value;
import javafx.util.StringConverter;
public class StringValueConverter extends StringConverter<Value<String>> {
private static StringValueConverter INSTANCE = new StringValueConverter();
public static StringValueConverter instance() {
return INSTANCE;
}
@Override
public Value<String> fromString(String s) {
return Option.value(s);
}
@Override
public String toString(Value<String>v) {
return Objects.toString(v.get(), "");
}
}
\ No newline at end of file
package com.dua3.fx.util.controls; package com.dua3.fx.util.controls;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Collection;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty; import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty; import javafx.beans.property.IntegerProperty;
...@@ -21,10 +23,13 @@ import javafx.beans.property.SimpleStringProperty; ...@@ -21,10 +23,13 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableStringValue; import javafx.beans.value.ObservableStringValue;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.util.StringConverter;
/** /**
* Interface for an input field. * Interface for an input field.
...@@ -143,6 +148,14 @@ public interface InputControl<R> { ...@@ -143,6 +148,14 @@ public interface InputControl<R> {
return inputControl; return inputControl;
} }
public static <T> SimpleInputControl<TextField, T> stringInput(Supplier<T> dflt, Function<T, Optional<String>> validate, StringConverter<T> converter) {
TextField control = new TextField();
ObjectProperty<T> value = new SimpleObjectProperty<>();
Bindings.bindBidirectional(control.textProperty(), value, converter);
SimpleInputControl<TextField, T> inputControl = new SimpleInputControl<>(control, value, dflt, validate);
return inputControl;
}
public static SimpleInputControl<TextField, Integer> integerInput(Supplier<Integer> dflt, Function<Integer, Optional<String>> validate) { public static SimpleInputControl<TextField, Integer> integerInput(Supplier<Integer> dflt, Function<Integer, Optional<String>> validate) {
TextField control = new TextField(); TextField control = new TextField();
StringProperty textProperty = control.textProperty(); StringProperty textProperty = control.textProperty();
...@@ -168,8 +181,8 @@ public interface InputControl<R> { ...@@ -168,8 +181,8 @@ public interface InputControl<R> {
return inputControl; return inputControl;
} }
public static <T> SimpleInputControl<ComboBox<T>, T> comboBoxInput(Supplier<T> dflt) { public static <T> SimpleInputControl<ComboBox<T>, T> comboBoxInput(Collection<T> choices, Supplier<T> dflt) {
ComboBox<T> control = new ComboBox<>(); ComboBox<T> control = new ComboBox<>(FXCollections.observableArrayList(choices));
ObjectProperty<T> value = control.valueProperty(); ObjectProperty<T> value = control.valueProperty();
SimpleInputControl<ComboBox<T>,T> inputControl = new SimpleInputControl<>(control, value, dflt, r -> Optional.empty()); SimpleInputControl<ComboBox<T>,T> inputControl = new SimpleInputControl<>(control, value, dflt, r -> Optional.empty());
return inputControl; return inputControl;
......
...@@ -156,10 +156,4 @@ public class InputPane extends InputDialogPane<Map<String,Object>> { ...@@ -156,10 +156,4 @@ public class InputPane extends InputDialogPane<Map<String,Object>> {
setContent(grid); setContent(grid);
} }
protected BooleanProperty valid = new SimpleBooleanProperty(false);
public ReadOnlyBooleanProperty validProperty() {
return valid;
}
} }
...@@ -111,18 +111,18 @@ implements InputBuilder<InputPaneBuilder> { ...@@ -111,18 +111,18 @@ implements InputBuilder<InputPaneBuilder> {
return add(id, label, Boolean.class, dflt, InputControl.checkBoxInput(dflt, text)); return add(id, label, Boolean.class, dflt, InputControl.checkBoxInput(dflt, text));
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#comboBox(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection) * @see com.dua3.fx.util.controls.InputBuilder#comboBox(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/ */
@Override @Override
public <T> InputPaneBuilder comboBox(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) { public <T> InputPaneBuilder comboBox(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
return add(id, label, cls, dflt, InputControl.comboBoxInput(dflt)); return add(id, label, cls, dflt, InputControl.comboBoxInput(items, dflt));
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#radioList(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection) * @see com.dua3.fx.util.controls.InputBuilder#radioList(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/ */
@Override @Override
public <T> InputPaneBuilder radioList(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) { public <T> InputPaneBuilder radioList(String id, String label, Supplier<T> dflt, Class<T> cls, Collection<T> items) {
return add(id, label, cls, dflt, new RadioPane<>(items, null)); return add(id, label, cls, dflt, new RadioPane<>(items, null));
} }
......
package com.dua3.fx.util.controls; package com.dua3.fx.util.controls;
import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
...@@ -14,6 +15,8 @@ import com.dua3.utility.options.Option.Value; ...@@ -14,6 +15,8 @@ import com.dua3.utility.options.Option.Value;
import com.dua3.utility.options.OptionSet; import com.dua3.utility.options.OptionSet;
import com.dua3.utility.options.OptionValues; import com.dua3.utility.options.OptionValues;
import com.dua3.fx.util.StringValueConverter;
import javafx.beans.property.Property; import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty; import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty; import javafx.beans.property.ReadOnlyStringProperty;
...@@ -67,14 +70,13 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{ ...@@ -67,14 +70,13 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
getChildren().clear(); getChildren().clear();
OptionSet optionSet = options.get(); OptionSet optionSet = options.get();
OptionValues values = new OptionValues(dflt.get()); OptionValues values = new OptionValues(dflt.get());
int row = 0; int row = 0;
for (Option<?> option: optionSet) { for (Option<?> option: optionSet) {
Label label = new Label(option.getName()); Label label = new Label(option.getName());
InputControl<?> control = createControl(values, option); var control = createControl(values, option);
items.put(option, control); items.put(option, control);
addToGrid(label, 0, row); addToGrid(label, 0, row);
...@@ -98,32 +100,39 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{ ...@@ -98,32 +100,39 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> InputControl<T> createControl(OptionValues values, Option<T> option) { private <T> InputControl<Value<T>> createControl(OptionValues values, Option<T> option) {
InputControl<T> control; InputControl<Value<T>> control;
if (option instanceof StringOption) { if (option instanceof StringOption) {
var inputControl = InputControl.stringInput(() -> (String) dflt.get().get(option).get(), r -> Optional.empty()); InputControl<Value<String>> inputControl = InputControl.stringInput(
() -> (Value<String>) dflt.get().get(option),
r -> Optional.empty(),
StringValueConverter.instance());
inputControl.valueProperty().addListener( (v,o,n) -> { inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, Option.value(n)); values.put(option, n);
}); });
values.addChangeListener( (v,o,n) -> { values.addChangeListener( (op,o,n) -> {
inputControl.valueProperty().setValue(Objects.toString(n.get(), "")); if (op.equals(option)) {
inputControl.valueProperty().setValue((Value) n);
}
}); });
return (InputControl<T>) inputControl; return (InputControl) inputControl;
} else if (option instanceof ChoiceOption<?>) { } else if (option instanceof ChoiceOption<?>) {
var choices = FXCollections.observableList(((ChoiceOption<T>)option).getChoices()); Collection<Value<T>> choices = ((ChoiceOption<T>)option).getChoices();
var inputControl = InputControl.comboBoxInput(() -> ((Value<T>)dflt.get().get(option)).get()); Supplier<Value<T>> dfltValue = () -> (Value<T>) (dflt.get().get(option));
var inputControl = InputControl.comboBoxInput(choices, dfltValue);
inputControl.valueProperty().addListener( (v,o,n) -> { inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, Option.value(n)); values.put(option, Option.value(n));
}); });
values.addChangeListener( (v,o,n) -> { values.addChangeListener( (op,o,n) -> {
inputControl.valueProperty().setValue(((Value<T>) n).get()); if (op.equals(option)) {
inputControl.valueProperty().setValue((Value) n);
}
}); });
return inputControl; return inputControl;
} }
......
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