forked from j62/ctbrec
Create controls for file, program and directory selection
This commit is contained in:
parent
08fa878814
commit
4d55351919
|
@ -0,0 +1,114 @@
|
|||
package ctbrec.ui.controls;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ctbrec.ui.AutosizeAlert;
|
||||
import javafx.beans.property.ObjectProperty;
|
||||
import javafx.beans.property.ObjectPropertyBase;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.geometry.Point2D;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.control.Tooltip;
|
||||
import javafx.scene.layout.Border;
|
||||
import javafx.scene.layout.BorderStroke;
|
||||
import javafx.scene.layout.BorderStrokeStyle;
|
||||
import javafx.scene.layout.BorderWidths;
|
||||
import javafx.scene.layout.CornerRadii;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.stage.FileChooser;
|
||||
|
||||
public abstract class AbstractFileSelectionBox extends HBox {
|
||||
private static final transient Logger LOG = LoggerFactory.getLogger(AbstractFileSelectionBox.class);
|
||||
|
||||
private ObjectProperty<File> fileProperty = new ObjectPropertyBase<File>() {
|
||||
@Override
|
||||
public Object getBean() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "file";
|
||||
}
|
||||
};
|
||||
private TextField fileInput;
|
||||
private Tooltip validationError = new Tooltip();
|
||||
|
||||
public AbstractFileSelectionBox() {
|
||||
super(5);
|
||||
fileInput = new TextField();
|
||||
fileInput.textProperty().addListener(textListener());
|
||||
fileInput.focusedProperty().addListener((obs, o, n) -> {
|
||||
if(!n) {
|
||||
validationError.hide();
|
||||
}
|
||||
});
|
||||
Node browse = createBrowseButton();
|
||||
getChildren().addAll(fileInput, browse);
|
||||
fileInput.disableProperty().bind(disableProperty());
|
||||
browse.disableProperty().bind(disableProperty());
|
||||
}
|
||||
|
||||
private ChangeListener<? super String> textListener() {
|
||||
return (obs, o, n) -> {
|
||||
String input = fileInput.getText();
|
||||
File program = new File(input);
|
||||
setProgram(program);
|
||||
};
|
||||
}
|
||||
|
||||
private void setProgram(File program) {
|
||||
String msg = validate(program);
|
||||
if (msg != null) {
|
||||
fileInput.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, new CornerRadii(2), new BorderWidths(2))));
|
||||
validationError.setText(msg);
|
||||
fileInput.setTooltip(validationError);
|
||||
Point2D p = fileInput.localToScreen(fileInput.getTranslateY(), fileInput.getTranslateY());
|
||||
if(!validationError.isShowing()) {
|
||||
validationError.show(getScene().getWindow(), p.getX(), p.getY() + fileInput.getHeight() + 4);
|
||||
}
|
||||
} else {
|
||||
fileInput.setBorder(Border.EMPTY);
|
||||
fileInput.setTooltip(null);
|
||||
fileProperty.set(program);
|
||||
validationError.hide();
|
||||
}
|
||||
}
|
||||
|
||||
protected String validate(File file) {
|
||||
if (file == null || !file.exists()) {
|
||||
return "File does not exist";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Button createBrowseButton() {
|
||||
Button button = new Button("Select");
|
||||
button.setOnAction((e) -> {
|
||||
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();
|
||||
}
|
||||
setProgram(program);
|
||||
}
|
||||
});
|
||||
return button;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package ctbrec.ui.controls;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class DirectorySelectionBox extends AbstractFileSelectionBox {
|
||||
@Override
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
if(msg != null) {
|
||||
return msg;
|
||||
} else if (!file.isDirectory()) {
|
||||
return "This is not a directory";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package ctbrec.ui.controls;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileSelectionBox extends AbstractFileSelectionBox {
|
||||
@Override
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
if(msg != null) {
|
||||
return msg;
|
||||
} else if (!file.isFile()) {
|
||||
return "This is not a regular file";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package ctbrec.ui.controls;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ProgramSelectionBox extends FileSelectionBox {
|
||||
@Override
|
||||
protected String validate(File file) {
|
||||
String msg = super.validate(file);
|
||||
if(msg != null) {
|
||||
return msg;
|
||||
} else if (!file.canExecute()) {
|
||||
return "This is not an executable application";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue