Commit ccfe4690 authored by Axel Howind's avatar Axel Howind

fixes to OptionsPane/FileInput

parent 982ae807
Pipeline #56064350 failed with stages
in 16 minutes and 51 seconds
package com.dua3.fx.util;
import com.dua3.utility.options.Option;
import com.dua3.utility.options.Option.Value;
public class ValueConverter<T> {
public Value<T> toValue(T t) {
return Option.value(t);
}
public T fromValue(Value<T>v) {
return v.get();
}
}
......@@ -12,6 +12,7 @@ import javafx.stage.FileChooser;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;
......@@ -20,7 +21,7 @@ public class FileInput extends HBox implements InputControl<File> {
private final ObjectProperty<File> value = new SimpleObjectProperty<>();
private final InputBuilder.FileDialogMode mode;
private final FileChooser.ExtensionFilter filter;
private final FileChooser.ExtensionFilter[] filters;
private final Supplier<File> dflt;
private final TextField tfFilename;
......@@ -29,9 +30,9 @@ public class FileInput extends HBox implements InputControl<File> {
private final StringProperty error = new SimpleStringProperty("");
private final BooleanProperty valid = new SimpleBooleanProperty(true);
public FileInput(InputBuilder.FileDialogMode mode, Supplier<File> dflt, FileChooser.ExtensionFilter filter) {
public FileInput(InputBuilder.FileDialogMode mode, Supplier<File> dflt, FileChooser.ExtensionFilter... filters) {
this.mode = Objects.requireNonNull(mode);
this.filter = Objects.requireNonNull(filter);
this.filters = Arrays.copyOf(Objects.requireNonNull(filters),filters.length);
this.dflt = Objects.requireNonNull(dflt);
this.tfFilename = new TextField();
......@@ -49,13 +50,13 @@ public class FileInput extends HBox implements InputControl<File> {
if (mode== InputBuilder.FileDialogMode.OPEN) {
Dialogs.chooseFile()
.initialDir(initialDir)
.filter(filter)
.filter(filters)
.showOpenDialog(null)
.ifPresent(f -> value.setValue(f));
} else {
Dialogs.chooseFile()
.initialDir(initialDir)
.filter(filter)
.filter(filters)
.showSaveDialog(null)
.ifPresent(f -> value.setValue(f));
}
......
......@@ -8,8 +8,10 @@ import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.util.StringConverter;
import java.io.File;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Locale;
......@@ -176,4 +178,7 @@ public interface InputControl<R> {
return inputControl;
}
static InputControl<File> chooseFile(Supplier<File> dflt, Function<File, Optional<String>> validate, InputBuilder.FileDialogMode mode, FileChooser.ExtensionFilter... filters) {
return new FileInput(mode, dflt, filters);
}
}
\ No newline at end of file
package com.dua3.fx.util.controls;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Logger;
import com.dua3.fx.util.StringValueConverter;
import com.dua3.utility.io.OpenMode;
import com.dua3.utility.options.Option;
import com.dua3.utility.options.Option.ChoiceOption;
import com.dua3.utility.options.Option.StringOption;
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.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
......@@ -22,6 +16,14 @@ import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import java.io.File;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Logger;
public class OptionsPane extends GridPane implements InputControl<OptionValues>{
......@@ -107,10 +109,28 @@ public class OptionsPane extends GridPane implements InputControl<OptionValues>{
inputControl.valueProperty().setValue((Value) n);
}
});
return (InputControl) inputControl;
} else if (option instanceof Option.FileOption) {
throw new UnsupportedOperationException();
Option.FileOption fop = (Option.FileOption) option;
Supplier<File> dfltValue = () -> fop.getDefault().get();
InputBuilder.FileDialogMode mode = fop.getMode().includes(OpenMode.READ) ? InputBuilder.FileDialogMode.OPEN : InputBuilder.FileDialogMode.SAVE;
FileChooser.ExtensionFilter filters = new FileChooser.ExtensionFilter("supported Files", fop.getExtensions());
InputControl<File> fileInputControl = InputControl.chooseFile(
dfltValue,
file -> file == null ? Optional.of("no file selected") : Optional.empty(),
mode,
filters);
InputControl<Value<File>> inputControl = new ValueInputControl<>(fileInputControl);
inputControl.valueProperty().addListener( (v,o,n) -> {
values.put(option, n);
});
values.addChangeListener( (op,o,n) -> {
if (op.equals(option)) {
inputControl.valueProperty().setValue((Value) n);
}
});
return (InputControl) inputControl;
} else if (option instanceof Option.SimpleOption<?>) {
throw new UnsupportedOperationException();
} else if (option instanceof ChoiceOption<?>) {
......
package com.dua3.fx.util.controls;
import com.dua3.utility.options.Option;
import com.dua3.utility.options.Option.Value;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import java.util.Objects;
/**
* Interface for an input field.
*
* @param <T> the input result type
*/
public class ValueInputControl<T> implements InputControl<Value<T>> {
private final InputControl<T> wrapped;
private Property<Value<T>> value;
public ValueInputControl(InputControl<T> ic) {
this.wrapped = Objects.requireNonNull(ic);
T current = wrapped.get();
this.value = new SimpleObjectProperty<>(current == null ? null : Option.value(current));
// bind value to wrapped value
value.addListener((v,o,n) -> {
wrapped.set(n.get());
});
wrapped.valueProperty().addListener((v,o,n) -> {
value.setValue((Option.value(n)));
});
}
/**
* Get the Node for this input element.
*
* @return the node
*/
public Node node() {
return wrapped.node();
}
/**
* Set/update control state.
*/
public void init() {
wrapped.init();
}
/**
* Reset value to default
*/
public void reset() {
wrapped.reset();
}
@Override
public Property<Value<T>> valueProperty() {
return value;
}
@Override
public ReadOnlyBooleanProperty validProperty() {
return wrapped.validProperty();
}
@Override
public ReadOnlyStringProperty errorProperty() {
return wrapped.errorProperty();
}
}
\ No newline at end of file
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