Make file/dir selection boxes usable
This commit is contained in:
parent
4d55351919
commit
016c95f7f1
|
@ -39,7 +39,7 @@ public abstract class AbstractFileSelectionBox extends HBox {
|
|||
return "file";
|
||||
}
|
||||
};
|
||||
private TextField fileInput;
|
||||
protected TextField fileInput;
|
||||
private Tooltip validationError = new Tooltip();
|
||||
|
||||
public AbstractFileSelectionBox() {
|
||||
|
@ -57,16 +57,21 @@ public abstract class AbstractFileSelectionBox extends HBox {
|
|||
browse.disableProperty().bind(disableProperty());
|
||||
}
|
||||
|
||||
public AbstractFileSelectionBox(String initialValue) {
|
||||
this();
|
||||
fileInput.setText(initialValue);
|
||||
}
|
||||
|
||||
private ChangeListener<? super String> textListener() {
|
||||
return (obs, o, n) -> {
|
||||
String input = fileInput.getText();
|
||||
File program = new File(input);
|
||||
setProgram(program);
|
||||
setFile(program);
|
||||
};
|
||||
}
|
||||
|
||||
private void setProgram(File program) {
|
||||
String msg = validate(program);
|
||||
protected void setFile(File file) {
|
||||
String msg = validate(file);
|
||||
if (msg != null) {
|
||||
fileInput.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(2), new BorderWidths(2))));
|
||||
validationError.setText(msg);
|
||||
|
@ -78,7 +83,7 @@ public abstract class AbstractFileSelectionBox extends HBox {
|
|||
} else {
|
||||
fileInput.setBorder(Border.EMPTY);
|
||||
fileInput.setTooltip(null);
|
||||
fileProperty.set(program);
|
||||
fileProperty.set(file);
|
||||
validationError.hide();
|
||||
}
|
||||
}
|
||||
|
@ -94,6 +99,12 @@ public abstract class AbstractFileSelectionBox extends HBox {
|
|||
private Button createBrowseButton() {
|
||||
Button button = new Button("Select");
|
||||
button.setOnAction((e) -> {
|
||||
choose();
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
protected void choose() {
|
||||
FileChooser chooser = new FileChooser();
|
||||
File program = chooser.showOpenDialog(null);
|
||||
if(program != null) {
|
||||
|
@ -106,9 +117,11 @@ public abstract class AbstractFileSelectionBox extends HBox {
|
|||
alert.setContentText("Couldn't determine path");
|
||||
alert.showAndWait();
|
||||
}
|
||||
setProgram(program);
|
||||
setFile(program);
|
||||
}
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
public ObjectProperty<File> fileProperty() {
|
||||
return fileProperty;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,27 @@ package ctbrec.ui.controls;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import javafx.stage.DirectoryChooser;
|
||||
|
||||
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
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
|
|
|
@ -3,6 +3,13 @@ package ctbrec.ui.controls;
|
|||
import java.io.File;
|
||||
|
||||
public class FileSelectionBox extends AbstractFileSelectionBox {
|
||||
public FileSelectionBox() {
|
||||
}
|
||||
|
||||
public FileSelectionBox(String initialValue) {
|
||||
super(initialValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
|
|
|
@ -3,6 +3,13 @@ package ctbrec.ui.controls;
|
|||
import java.io.File;
|
||||
|
||||
public class ProgramSelectionBox extends FileSelectionBox {
|
||||
public ProgramSelectionBox() {
|
||||
}
|
||||
|
||||
public ProgramSelectionBox(String initialValue) {
|
||||
super(initialValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
|
|
Loading…
Reference in New Issue