diff --git a/src/main/java/jace/config/Configuration.java b/src/main/java/jace/config/Configuration.java index 8fa71c6..43ec39d 100644 --- a/src/main/java/jace/config/Configuration.java +++ b/src/main/java/jace/config/Configuration.java @@ -67,6 +67,30 @@ public class Configuration implements Reconfigurable { return null; } + static ConfigurableField getConfigurableFieldInfo(Reconfigurable subject, String settingName) { + Field f; + try { + f = subject.getClass().getField(settingName); + } catch (NoSuchFieldException | SecurityException ex) { + return null; + } + ConfigurableField annotation = f.getAnnotation(ConfigurableField.class); + return annotation; + } + + public static String getShortName(ConfigurableField f, String longName) { + return (f != null && !f.shortName().equals("")) ? f.shortName() : longName; + } + + public static InvokableAction getInvokableActionInfo(Reconfigurable subject, String actionName) { + for (Method m : subject.getClass().getMethods()) { + if (m.getName().equals(actionName) && m.isAnnotationPresent(InvokableAction.class)) { + return m.getAnnotation(InvokableAction.class); + } + } + return null; + } + @Override public String getName() { return "Configuration"; @@ -95,8 +119,8 @@ public class Configuration implements Reconfigurable { public transient ConfigNode parent; private ObservableList children; public transient Reconfigurable subject; - private Map settings; - private Map hotkeys; + public Map settings; + public Map hotkeys; private boolean changed = true; @Override @@ -514,13 +538,8 @@ public class Configuration implements Reconfigurable { boolean found = false; List shortFieldNames = new ArrayList<>(); for (String longName : n.getAllSettingNames()) { - ConfigurableField f = null; - try { - f = n.subject.getClass().getField(longName).getAnnotation(ConfigurableField.class); - } catch (NoSuchFieldException | SecurityException ex) { - Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex); - } - String shortName = (f != null && !f.shortName().equals("")) ? f.shortName() : longName; + ConfigurableField f = getConfigurableFieldInfo(n.subject, longName); + String shortName = getShortName(f, longName); shortFieldNames.add(shortName); if (fieldName.equalsIgnoreCase(longName) || fieldName.equalsIgnoreCase(shortName)) { diff --git a/src/main/java/jace/config/ConfigurationUIController.java b/src/main/java/jace/config/ConfigurationUIController.java index 5c77691..cbf077e 100644 --- a/src/main/java/jace/config/ConfigurationUIController.java +++ b/src/main/java/jace/config/ConfigurationUIController.java @@ -1,18 +1,23 @@ package jace.config; -import jace.Emulator; -import jace.JaceApplication; +import jace.config.Configuration.ConfigNode; +import java.io.Serializable; import java.net.URL; import java.util.ResourceBundle; +import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.control.Label; import javafx.scene.control.ScrollPane; import javafx.scene.control.SplitPane; +import javafx.scene.control.TextField; +import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; +import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; public class ConfigurationUIController { - @FXML private ResourceBundle resources; @@ -29,7 +34,7 @@ public class ConfigurationUIController { private ScrollPane settingsScroll; @FXML - private TreeView deviceTree; + private TreeView deviceTree; @FXML private ScrollPane treeScroll; @@ -62,5 +67,56 @@ public class ConfigurationUIController { assert deviceTree != null : "fx:id=\"deviceTree\" was not injected: check your FXML file 'Configuration.fxml'."; assert treeScroll != null : "fx:id=\"treeScroll\" was not injected: check your FXML file 'Configuration.fxml'."; deviceTree.setRoot(Configuration.BASE); + deviceTree.getSelectionModel().selectedItemProperty().addListener(this::selectionChanged); + deviceTree.maxWidthProperty().bind(treeScroll.widthProperty()); + } + + private void selectionChanged( + ObservableValue> observable, + TreeItem oldValue, + TreeItem newValue) { + clearForm(); + buildForm((ConfigNode) newValue); + } + + private void clearForm() { + settingsVbox.getChildren().clear(); + } + + private void buildForm(ConfigNode node) { + node.hotkeys.forEach((name, values) -> { + settingsVbox.getChildren().add(buildKeyShortcutRow(node, name, values)); + }); + node.settings.forEach((name, value) -> { + settingsVbox.getChildren().add(buildSettingRow(node, name, value)); + }); + } + + private Node buildSettingRow(ConfigNode node, String settingName, Serializable value) { + ConfigurableField fieldInfo = Configuration.getConfigurableFieldInfo(node.subject, settingName); + if (fieldInfo == null) return null; + HBox row = new HBox(); + Label label = new Label(fieldInfo.name()); + label.getStyleClass().add("setting-label"); + label.setMinWidth(150.0); + TextField widget = new TextField(String.valueOf(value)); + label.setLabelFor(widget); + row.getChildren().add(label); + row.getChildren().add(widget); + return row; + } + + private Node buildKeyShortcutRow(ConfigNode node, String actionName, String[] values) { + InvokableAction actionInfo = Configuration.getInvokableActionInfo(node.subject, actionName); + if (actionInfo == null) return null; + HBox row = new HBox(); + Label label = new Label(actionInfo.name()); + label.getStyleClass().add("setting-keyboard-shortcut"); + label.setMinWidth(150.0); + TextField widget = new TextField(String.valueOf(values)); + label.setLabelFor(widget); + row.getChildren().add(label); + row.getChildren().add(widget); + return row; } } diff --git a/src/main/resources/fxml/Configuration.fxml b/src/main/resources/fxml/Configuration.fxml index bac75dd..a9ea776 100644 --- a/src/main/resources/fxml/Configuration.fxml +++ b/src/main/resources/fxml/Configuration.fxml @@ -1,5 +1,6 @@ + @@ -7,12 +8,12 @@ - + - + - +