forked from j62/ctbrec
1
0
Fork 0

Make file/dir selection boxes usable

This commit is contained in:
0xboobface 2018-12-08 16:06:17 +01:00
parent 4d55351919
commit 016c95f7f1
4 changed files with 66 additions and 19 deletions

View File

@ -39,7 +39,7 @@ public abstract class AbstractFileSelectionBox extends HBox {
return "file"; return "file";
} }
}; };
private TextField fileInput; protected TextField fileInput;
private Tooltip validationError = new Tooltip(); private Tooltip validationError = new Tooltip();
public AbstractFileSelectionBox() { public AbstractFileSelectionBox() {
@ -57,16 +57,21 @@ public abstract class AbstractFileSelectionBox extends HBox {
browse.disableProperty().bind(disableProperty()); browse.disableProperty().bind(disableProperty());
} }
public AbstractFileSelectionBox(String initialValue) {
this();
fileInput.setText(initialValue);
}
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();
File program = new File(input); File program = new File(input);
setProgram(program); setFile(program);
}; };
} }
private void setProgram(File program) { protected void setFile(File file) {
String msg = validate(program); String msg = validate(file);
if (msg != null) { if (msg != null) {
fileInput.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(2), new BorderWidths(2)))); fileInput.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(2), new BorderWidths(2))));
validationError.setText(msg); validationError.setText(msg);
@ -78,7 +83,7 @@ public abstract class AbstractFileSelectionBox extends HBox {
} else { } else {
fileInput.setBorder(Border.EMPTY); fileInput.setBorder(Border.EMPTY);
fileInput.setTooltip(null); fileInput.setTooltip(null);
fileProperty.set(program); fileProperty.set(file);
validationError.hide(); validationError.hide();
} }
} }
@ -94,21 +99,29 @@ public abstract class AbstractFileSelectionBox extends HBox {
private Button createBrowseButton() { private Button createBrowseButton() {
Button button = new Button("Select"); Button button = new Button("Select");
button.setOnAction((e) -> { button.setOnAction((e) -> {
FileChooser chooser = new FileChooser(); choose();
File program = chooser.showOpenDialog(null);
if(program != null) {
try {
fileInput.setText(program.getCanonicalPath());
} catch (IOException e1) {
LOG.error("Couldn't determine path", e1);
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR);
alert.setTitle("Whoopsie");
alert.setContentText("Couldn't determine path");
alert.showAndWait();
}
setProgram(program);
}
}); });
return button; return button;
} }
protected void choose() {
FileChooser chooser = new FileChooser();
File program = chooser.showOpenDialog(null);
if(program != null) {
try {
fileInput.setText(program.getCanonicalPath());
} catch (IOException e1) {
LOG.error("Couldn't determine path", e1);
Alert alert = new AutosizeAlert(Alert.AlertType.ERROR);
alert.setTitle("Whoopsie");
alert.setContentText("Couldn't determine path");
alert.showAndWait();
}
setFile(program);
}
}
public ObjectProperty<File> fileProperty() {
return fileProperty;
}
} }

View File

@ -2,7 +2,27 @@ package ctbrec.ui.controls;
import java.io.File; import java.io.File;
import javafx.stage.DirectoryChooser;
public class DirectorySelectionBox extends AbstractFileSelectionBox { public class DirectorySelectionBox extends AbstractFileSelectionBox {
public DirectorySelectionBox(String dir) {
super(dir);
}
@Override
protected void choose() {
DirectoryChooser chooser = new DirectoryChooser();
File currentDir = fileProperty().get();
if (currentDir.exists() && currentDir.isDirectory()) {
chooser.setInitialDirectory(currentDir);
}
File selectedDir = chooser.showDialog(null);
if(selectedDir != null) {
fileInput.setText(selectedDir.getAbsolutePath());
setFile(selectedDir);
}
}
@Override @Override
protected String validate(File file) { protected String validate(File file) {
String msg = super.validate(file); String msg = super.validate(file);

View File

@ -3,6 +3,13 @@ package ctbrec.ui.controls;
import java.io.File; import java.io.File;
public class FileSelectionBox extends AbstractFileSelectionBox { public class FileSelectionBox extends AbstractFileSelectionBox {
public FileSelectionBox() {
}
public FileSelectionBox(String initialValue) {
super(initialValue);
}
@Override @Override
protected String validate(File file) { protected String validate(File file) {
String msg = super.validate(file); String msg = super.validate(file);

View File

@ -3,6 +3,13 @@ package ctbrec.ui.controls;
import java.io.File; import java.io.File;
public class ProgramSelectionBox extends FileSelectionBox { public class ProgramSelectionBox extends FileSelectionBox {
public ProgramSelectionBox() {
}
public ProgramSelectionBox(String initialValue) {
super(initialValue);
}
@Override @Override
protected String validate(File file) { protected String validate(File file) {
String msg = super.validate(file); String msg = super.validate(file);