com.dua3.utility.fx.controls.InputGridBuilder Maven / Gradle / Ivy
// Copyright 2019 Axel Howind
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.dua3.utility.fx.controls;
import com.dua3.cabe.annotations.Nullable;
import com.dua3.utility.fx.controls.InputGrid.Meta;
import com.dua3.utility.lang.LangUtil;
import com.dua3.utility.options.Arguments;
import com.dua3.utility.options.Option;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.Property;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.stage.FileChooser;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
/**
* Builder for Alert Dialogs.
*
* Provides a fluent interface to create Alerts.
*/
public class InputGridBuilder
implements InputBuilder {
private final LinkedHashMap> data = new LinkedHashMap<>();
private int columns = 1;
InputGridBuilder() {
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#build()
*/
public InputGrid build() {
InputGrid grid = new InputGrid();
grid.setContent(data.values(), columns);
return grid;
} /* (non-Javadoc)
* @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 InputGridBuilder add(String id, String label, Class type, Supplier dflt, InputControl control) {
return doAdd(id, label, type, dflt, control);
}
static class ControlWrapper implements InputControl {
private final Node node;
private final Property value = new SimpleObjectProperty<>(null);
private final BooleanProperty valid = new SimpleBooleanProperty(true);
private final ReadOnlyStringProperty error = new SimpleStringProperty("");
ControlWrapper(Node node) {
this.node = node;
}
@Override
public Node node() {
return node;
}
@Override
public Property valueProperty() {
return value;
}
@Override
public void reset() { /* nop */ }
@Override
public ReadOnlyBooleanProperty validProperty() {
return valid;
}
@Override
public ReadOnlyStringProperty errorProperty() {
return error;
}
} /* (non-Javadoc)
* @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 InputGridBuilder add(String id, Class type, Supplier dflt, InputControl control) {
return doAdd(id, null, type, dflt, control);
}
private InputGridBuilder doAdd(String id, @Nullable String label, Class type, Supplier dflt, InputControl control) {
Meta meta = new Meta<>(id, label, type, dflt, control);
Meta> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
}
@Override
public InputGridBuilder addNode(String id, @Nullable String label, Node node) {
Meta meta = new Meta<>(id, label, Void.class, null, new ControlWrapper(node));
Meta> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
}
@Override
public InputGridBuilder addNode(String id, Node node) {
Meta meta = new Meta<>(id, null, Void.class, null, new ControlWrapper(node));
Meta> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#columns(int)
*/
@Override
public InputGridBuilder columns(int columns) {
this.columns = LangUtil.requirePositive(columns);
return this;
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#text(java.lang.String, java.lang.String, java.lang.String, java.util.function.Function)
*/
@Override
public InputGridBuilder string(String id, String label, Supplier dflt, Function> validate) {
return add(id, label, String.class, dflt, InputControl.stringInput(dflt, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#integer(java.lang.String, java.lang.String, java.lang.Integer, java.util.function.Function)
*/
@Override
public InputGridBuilder integer(String id, String label, Supplier dflt, Function> validate) {
return add(id, label, Integer.class, dflt, InputControl.integerInput(dflt, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#decimal(java.lang.String, java.lang.String, java.lang.Double, java.util.function.Function)
*/
@Override
public InputGridBuilder decimal(String id, String label, Supplier dflt, Function> validate) {
return add(id, label, Double.class, dflt, InputControl.decimalInput(dflt, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#checkBox(java.lang.String, java.lang.String, boolean, java.lang.String)
*/
@Override
public InputGridBuilder checkBox(String id, String label, Supplier dflt, String text, Function> validate) {
return add(id, label, Boolean.class, dflt, InputControl.checkBoxInput(dflt, text, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#comboBox(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/
@Override
public InputGridBuilder comboBox(String id, String label, Supplier dflt, Class cls, Collection items, Function> validate) {
return add(id, label, cls, dflt, InputControl.comboBoxInput(items, dflt, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#comboBoxEx(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/
@Override
public InputGridBuilder comboBoxEx(
String id,
String label,
@Nullable UnaryOperator edit,
@Nullable Supplier add,
@Nullable BiPredicate, T> remove,
Function format,
Supplier dflt,
Class cls,
Collection items,
Function> validate) {
return add(id, label, cls, dflt, InputControl.comboBoxExInput(items, dflt, edit, add, remove, format, validate));
}
/* (non-Javadoc)
* @see com.dua3.fx.util.controls.InputBuilder#radioList(java.lang.String, java.lang.String, T, java.lang.Class, java.util.Collection)
*/
@Override
public InputGridBuilder radioList(String id, String label, Supplier dflt, Class cls, Collection items,
Function> validate) {
return add(id, label, cls, dflt, new RadioPane<>(items, null, validate));
}
@Override
public InputGridBuilder options(String id, String label, Supplier dflt, Supplier>> options) {
return add(id, label, Arguments.class, dflt, new OptionsPane(options, dflt));
}
@Override
public InputGridBuilder options(String id, Supplier dflt, Supplier>> options) {
return add(id, Arguments.class, dflt, new OptionsPane(options, dflt));
}
@Override
public InputGridBuilder chooseFile(String id, String label, Supplier dflt, FileDialogMode mode, boolean existingOnly, Collection filter, Function> validate) {
return add(id, label, Path.class, dflt, new FileInput(mode, existingOnly, dflt, filter, validate));
}
}