Add configurable file field support

This commit is contained in:
Brendan Robert 2024-07-13 23:28:46 -05:00
parent 3a8e55d9dd
commit dafd4453eb
2 changed files with 32 additions and 5 deletions

View File

@ -31,6 +31,7 @@ import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Text; import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.util.StringConverter; import javafx.util.StringConverter;
public class ConfigurationUIController { public class ConfigurationUIController {
@ -221,9 +222,9 @@ public class ConfigurationUIController {
return Optional.of(row); return Optional.of(row);
} }
private void editKeyboardShortcut(ConfigNode node, String actionName, Text widget) { // private void editKeyboardShortcut(ConfigNode node, String actionName, Text widget) {
throw new UnsupportedOperationException("Not supported yet."); // throw new UnsupportedOperationException("Not supported yet.");
} // }
@SuppressWarnings("all") @SuppressWarnings("all")
private Node buildEditField(ConfigNode node, String settingName, Serializable value) { private Node buildEditField(ConfigNode node, String settingName, Serializable value) {
@ -247,13 +248,40 @@ public class ConfigurationUIController {
return buildTextField(node, settingName, value, null); return buildTextField(node, settingName, value, null);
} }
} else if (type.equals(File.class)) { } else if (type.equals(File.class)) {
// TODO: Add file support! return buildFileField(node, settingName, value);
} else if (ISelection.class.isAssignableFrom(type)) { } else if (ISelection.class.isAssignableFrom(type)) {
return buildDynamicSelectComponent(node, settingName, value); return buildDynamicSelectComponent(node, settingName, value);
} }
return null; return null;
} }
// NOTE: This was written but not tested/used currently. Test before using!
private Node buildFileField(ConfigNode node, String settingName, Serializable value) {
// Create a label that shows the name of the file and lets you select a file when the label is clicked.
HBox hbox = new HBox();
Label label = new Label(value == null ? "" : ((File) value).getName());
label.setMinWidth(150.0);
label.getStyleClass().add("setting-file-label");
label.setOnMouseClicked((e) -> {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(label.getScene().getWindow());
if (file != null) {
node.setFieldValue(settingName, file);
label.setText(file.getName());
}
});
hbox.getChildren().add(label);
// Add a button that lets you clear the file selection.
Label clearButton = new Label("Clear");
clearButton.getStyleClass().add("setting-file-clear");
clearButton.setOnMouseClicked((e) -> {
node.setFieldValue(settingName, null);
label.setText("");
});
return hbox;
}
private Node buildTextField(ConfigNode node, String settingName, Serializable value, String validationPattern) { private Node buildTextField(ConfigNode node, String settingName, Serializable value, String validationPattern) {
TextField widget = new TextField(String.valueOf(value)); TextField widget = new TextField(String.valueOf(value));
widget.textProperty().addListener((e) -> node.setFieldValue(settingName, widget.getText())); widget.textProperty().addListener((e) -> node.setFieldValue(settingName, widget.getText()));

View File

@ -152,7 +152,6 @@ public class DirectoryNode extends DiskNode implements FileFilter {
end = start + ENTRIES_PER_BLOCK; end = start + ENTRIES_PER_BLOCK;
} }
for (int i = start; i < end && i < directoryEntries.size(); i++, offset += FILE_ENTRY_SIZE) { for (int i = start; i < end && i < directoryEntries.size(); i++, offset += FILE_ENTRY_SIZE) {
// TODO: Add any parts that are not file entries.
// System.out.println("Entry "+i+": "+children.get(i).getName()+"; offset "+offset); // System.out.println("Entry "+i+": "+children.get(i).getName()+"; offset "+offset);
generateFileEntry(buffer, offset, i); generateFileEntry(buffer, offset, i);
} }