forked from j62/ctbrec
1
0
Fork 0

Fix: allow empty input / deletion of post-processing script

This commit is contained in:
0xboobface 2018-12-16 20:29:48 +01:00
parent d74737113a
commit 910d21463a
4 changed files with 44 additions and 27 deletions

View File

@ -6,9 +6,10 @@ import java.io.IOException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ctbrec.StringUtil;
import ctbrec.ui.AutosizeAlert; import ctbrec.ui.AutosizeAlert;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.ObjectPropertyBase; import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.Node; import javafx.scene.Node;
@ -29,18 +30,20 @@ import javafx.stage.FileChooser;
public abstract class AbstractFileSelectionBox extends HBox { public abstract class AbstractFileSelectionBox extends HBox {
private static final transient Logger LOG = LoggerFactory.getLogger(AbstractFileSelectionBox.class); private static final transient Logger LOG = LoggerFactory.getLogger(AbstractFileSelectionBox.class);
private ObjectProperty<File> fileProperty = new ObjectPropertyBase<File>() { // private ObjectProperty<File> fileProperty = new ObjectPropertyBase<File>() {
@Override // @Override
public Object getBean() { // public Object getBean() {
return null; // return null;
} // }
//
@Override // @Override
public String getName() { // public String getName() {
return "file"; // return "file";
} // }
}; // };
private StringProperty fileProperty = new SimpleStringProperty();
protected TextField fileInput; protected TextField fileInput;
protected boolean allowEmptyValue = false;
private Tooltip validationError = new Tooltip(); private Tooltip validationError = new Tooltip();
public AbstractFileSelectionBox() { public AbstractFileSelectionBox() {
@ -67,8 +70,14 @@ public abstract class AbstractFileSelectionBox extends HBox {
private ChangeListener<? super String> textListener() { private ChangeListener<? super String> textListener() {
return (obs, o, n) -> { return (obs, o, n) -> {
String input = fileInput.getText(); String input = fileInput.getText();
if(StringUtil.isBlank(input) && allowEmptyValue) {
fileProperty.set("");
hideValidationHints();
return;
} else {
File program = new File(input); File program = new File(input);
setFile(program); setFile(program);
}
}; };
} }
@ -83,12 +92,16 @@ public abstract class AbstractFileSelectionBox extends HBox {
validationError.show(getScene().getWindow(), p.getX(), p.getY() + fileInput.getHeight() + 4); validationError.show(getScene().getWindow(), p.getX(), p.getY() + fileInput.getHeight() + 4);
} }
} else { } else {
fileProperty.set(file.getAbsolutePath());
hideValidationHints();
}
}
private void hideValidationHints() {
fileInput.setBorder(Border.EMPTY); fileInput.setBorder(Border.EMPTY);
fileInput.setTooltip(null); fileInput.setTooltip(null);
fileProperty.set(file);
validationError.hide(); validationError.hide();
} }
}
protected String validate(File file) { protected String validate(File file) {
if (file == null || !file.exists()) { if (file == null || !file.exists()) {
@ -98,6 +111,10 @@ public abstract class AbstractFileSelectionBox extends HBox {
} }
} }
public void allowEmptyValue() {
this.allowEmptyValue = true;
}
private Button createBrowseButton() { private Button createBrowseButton() {
Button button = new Button("Select"); Button button = new Button("Select");
button.setOnAction((e) -> { button.setOnAction((e) -> {
@ -123,7 +140,7 @@ public abstract class AbstractFileSelectionBox extends HBox {
} }
} }
public ObjectProperty<File> fileProperty() { public StringProperty fileProperty() {
return fileProperty; return fileProperty;
} }
} }

View File

@ -12,7 +12,7 @@ public class DirectorySelectionBox extends AbstractFileSelectionBox {
@Override @Override
protected void choose() { protected void choose() {
DirectoryChooser chooser = new DirectoryChooser(); DirectoryChooser chooser = new DirectoryChooser();
File currentDir = fileProperty().get(); File currentDir = new File(fileProperty().get());
if (currentDir.exists() && currentDir.isDirectory()) { if (currentDir.exists() && currentDir.isDirectory()) {
chooser.setInitialDirectory(currentDir); chooser.setInitialDirectory(currentDir);
} }

View File

@ -173,7 +173,7 @@ public class ActionSettingsPanel extends TitledPane {
if(playSound.isSelected()) { if(playSound.isSelected()) {
ActionConfiguration ac = new ActionConfiguration(); ActionConfiguration ac = new ActionConfiguration();
ac.setType(PlaySound.class.getName()); ac.setType(PlaySound.class.getName());
File file = sound.fileProperty().get(); File file = new File(sound.fileProperty().get());
ac.getConfiguration().put("file", file.getAbsolutePath()); ac.getConfiguration().put("file", file.getAbsolutePath());
ac.setName("play " + file.getName()); ac.setName("play " + file.getName());
config.getActions().add(ac); config.getActions().add(ac);
@ -181,7 +181,7 @@ public class ActionSettingsPanel extends TitledPane {
if(executeProgram.isSelected()) { if(executeProgram.isSelected()) {
ActionConfiguration ac = new ActionConfiguration(); ActionConfiguration ac = new ActionConfiguration();
ac.setType(ExecuteProgram.class.getName()); ac.setType(ExecuteProgram.class.getName());
File file = program.fileProperty().get(); File file = new File(program.fileProperty().get());
ac.getConfiguration().put("file", file.getAbsolutePath()); ac.getConfiguration().put("file", file.getAbsolutePath());
ac.setName("execute " + file.getName()); ac.setName("execute " + file.getName());
config.getActions().add(ac); config.getActions().add(ac);

View File

@ -238,7 +238,7 @@ public class SettingsTab extends Tab implements TabSelectionListener {
recordingsDirectory = new DirectorySelectionBox(Config.getInstance().getSettings().recordingsDir); recordingsDirectory = new DirectorySelectionBox(Config.getInstance().getSettings().recordingsDir);
recordingsDirectory.prefWidth(400); recordingsDirectory.prefWidth(400);
recordingsDirectory.fileProperty().addListener((obs, o, n) -> { recordingsDirectory.fileProperty().addListener((obs, o, n) -> {
String path = n.getAbsolutePath(); String path = n;
if(!Objects.equals(path, Config.getInstance().getSettings().recordingsDir)) { if(!Objects.equals(path, Config.getInstance().getSettings().recordingsDir)) {
Config.getInstance().getSettings().recordingsDir = path; Config.getInstance().getSettings().recordingsDir = path;
saveConfig(); saveConfig();
@ -310,10 +310,10 @@ public class SettingsTab extends Tab implements TabSelectionListener {
GridPane.setMargin(maxResolution, new Insets(0, 0, 0, CHECKBOX_MARGIN)); GridPane.setMargin(maxResolution, new Insets(0, 0, 0, CHECKBOX_MARGIN));
layout.add(new Label("Post-Processing"), 0, row); layout.add(new Label("Post-Processing"), 0, row);
// TODO allow empty strings to remove post-processing scripts
postProcessing = new ProgramSelectionBox(Config.getInstance().getSettings().postProcessing); postProcessing = new ProgramSelectionBox(Config.getInstance().getSettings().postProcessing);
postProcessing.allowEmptyValue();
postProcessing.fileProperty().addListener((obs, o, n) -> { postProcessing.fileProperty().addListener((obs, o, n) -> {
String path = n.getAbsolutePath(); String path = n;
if(!Objects.equals(path, Config.getInstance().getSettings().postProcessing)) { if(!Objects.equals(path, Config.getInstance().getSettings().postProcessing)) {
Config.getInstance().getSettings().postProcessing = path; Config.getInstance().getSettings().postProcessing = path;
saveConfig(); saveConfig();
@ -395,7 +395,7 @@ public class SettingsTab extends Tab implements TabSelectionListener {
layout.add(new Label("Player"), 0, row); layout.add(new Label("Player"), 0, row);
mediaPlayer = new ProgramSelectionBox(Config.getInstance().getSettings().mediaPlayer); mediaPlayer = new ProgramSelectionBox(Config.getInstance().getSettings().mediaPlayer);
mediaPlayer.fileProperty().addListener((obs, o, n) -> { mediaPlayer.fileProperty().addListener((obs, o, n) -> {
String path = n.getAbsolutePath(); String path = n;
if (!Objects.equals(path, Config.getInstance().getSettings().mediaPlayer)) { if (!Objects.equals(path, Config.getInstance().getSettings().mediaPlayer)) {
Config.getInstance().getSettings().mediaPlayer = path; Config.getInstance().getSettings().mediaPlayer = path;
saveConfig(); saveConfig();