Added SettingsToggleButton

This commit is contained in:
Kai S. K. Engelbart 2020-04-18 16:04:47 +02:00
parent 0e75bdd057
commit 621d07a300
4 changed files with 39 additions and 13 deletions

View File

@ -1,8 +1,6 @@
package envoy.client.data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
@ -10,11 +8,13 @@ import javax.swing.JComponent;
import javafx.scene.Node;
import envoy.client.ui.SettingsToggleButton;
/**
* Encapsulates a persistent value that is directly or indirectly mutable by the
* user.<br>
* <br>
* Project: <strong>envoy-clientChess</strong><br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsItem.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
@ -27,17 +27,11 @@ public class SettingsItem<T> implements Serializable {
private T value;
private String userFriendlyName, description;
private transient Consumer<T> changeHandler;
private transient Function<SettingsItem<?>, Node> nodeCreator;
private static final Map<Class<?>, Function<SettingsItem<?>, Node>> nodeCreators = new HashMap<>();
private transient Consumer<T> changeHandler;
private transient Function<SettingsItem<?>, ? extends Node> nodeCreator;
private static final long serialVersionUID = 1L;
static {
}
/**
* Initializes a {@link SettingsItem}. The default value's class will be mapped
* to a {@link JComponent} that can be used to display this {@link SettingsItem}
@ -53,7 +47,7 @@ public class SettingsItem<T> implements Serializable {
this.userFriendlyName = userFriendlyName;
this.description = description;
if (nodeCreators.containsKey(value.getClass())) nodeCreator = nodeCreators.get(value.getClass());
if (value.getClass() == Boolean.class) nodeCreator = s -> new SettingsToggleButton((SettingsItem<Boolean>) s);
}
/**

View File

@ -1,5 +1,7 @@
package envoy.client.ui;
import javafx.scene.layout.VBox;
import envoy.client.data.Settings;
/**
@ -17,5 +19,11 @@ public class GeneralSettingsPane extends SettingsPane {
/**
* @since Envoy Client v0.1-beta
*/
public GeneralSettingsPane() { super("General"); }
public GeneralSettingsPane() {
super("General");
var vbox = new VBox();
for (var name : new String[] { "onCloseMode", "enterToSend" })
vbox.getChildren().add(settings.getItems().get(name).getNode());
getChildren().add(vbox);
}
}

View File

@ -0,0 +1,23 @@
package envoy.client.ui;
import javafx.event.ActionEvent;
import javafx.scene.control.ToggleButton;
import envoy.client.data.SettingsItem;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsToggleButton.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public final class SettingsToggleButton extends ToggleButton {
public SettingsToggleButton(SettingsItem<Boolean> settingsItem) {
super(settingsItem.getUserFriendlyName());
addEventHandler(ActionEvent.ACTION, e -> settingsItem.set(!settingsItem.get()));
}
}

View File

@ -17,6 +17,7 @@ module envoy {
requires javafx.controls;
requires javafx.fxml;
requires javafx.base;
requires javafx.graphics;
opens envoy.client.ui to javafx.graphics, javafx.fxml;
}