Add Open Log button
This commit is contained in:
parent
c53ce5c866
commit
43b5742d1c
|
@ -4,46 +4,91 @@ import ctbrec.docs.DocServer;
|
||||||
import ctbrec.ui.DesktopIntegration;
|
import ctbrec.ui.DesktopIntegration;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Insets;
|
import javafx.geometry.Insets;
|
||||||
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.Tab;
|
import javafx.scene.control.Tab;
|
||||||
|
import javafx.scene.control.Label;
|
||||||
import javafx.scene.layout.BorderPane;
|
import javafx.scene.layout.BorderPane;
|
||||||
|
import javafx.scene.layout.VBox;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.awt.Desktop;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HelpTab extends Tab {
|
public class HelpTab extends Tab {
|
||||||
|
|
||||||
|
private boolean serverStarted = false;
|
||||||
|
|
||||||
public HelpTab() {
|
public HelpTab() {
|
||||||
setClosable(true);
|
setClosable(false);
|
||||||
setText("Help");
|
setText("Help");
|
||||||
|
|
||||||
var openHelp = new Button("Open Help");
|
Button openHelp = new Button("Open Help");
|
||||||
|
Button openLog = new Button("Open Log File");
|
||||||
openHelp.setPadding(new Insets(20));
|
openHelp.setPadding(new Insets(20));
|
||||||
var layout = new BorderPane(openHelp);
|
openLog.setPadding(new Insets(20));
|
||||||
BorderPane.setMargin(openHelp, new Insets(20));
|
Label logFilePathLabel = new Label(new File("ctbrec.log").getAbsolutePath());
|
||||||
|
|
||||||
|
VBox vbox = new VBox();
|
||||||
|
vbox.setAlignment(Pos.CENTER); // Center align the buttons
|
||||||
|
vbox.setSpacing(20); // Set a 20 pixel gap between the buttons
|
||||||
|
vbox.getChildren().addAll(openHelp, openLog, logFilePathLabel);
|
||||||
|
|
||||||
|
BorderPane layout = new BorderPane();
|
||||||
|
|
||||||
|
// Add the VBox to the center region of the BorderPane
|
||||||
|
layout.setCenter(vbox);
|
||||||
|
|
||||||
setContent(layout);
|
setContent(layout);
|
||||||
AtomicBoolean started = new AtomicBoolean(false);
|
|
||||||
openHelp.setOnAction(e -> {
|
openHelp.setOnAction(e -> {
|
||||||
synchronized (started) {
|
if (!serverStarted) {
|
||||||
if (!started.get()) {
|
startDocumentationServer();
|
||||||
|
} else {
|
||||||
|
openDocumentationPage();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
openLog.setOnAction(e -> {
|
||||||
|
File logFile = new File("ctbrec.log");
|
||||||
|
if (logFile.exists()) {
|
||||||
|
try {
|
||||||
|
// Use Runtime.getRuntime().exec() to open the file in a separate process
|
||||||
|
String osName = System.getProperty("os.name").toLowerCase();
|
||||||
|
ProcessBuilder pb = null;
|
||||||
|
if (osName.contains("mac")) {
|
||||||
|
pb = new ProcessBuilder("open", logFile.getAbsolutePath());
|
||||||
|
} else if (osName.contains("win")) {
|
||||||
|
pb = new ProcessBuilder("explorer.exe", logFile.getAbsolutePath());
|
||||||
|
} else { // Assume it's a Unix/Linux system
|
||||||
|
pb = new ProcessBuilder("xdg-open", logFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
pb.start();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
log.error("Couldn't open log file", ex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warn("Log file doesn't exist: {}", logFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startDocumentationServer() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
DocServer.start(() ->
|
DocServer.start(() -> Platform.runLater(() -> {
|
||||||
Platform.runLater(() -> {
|
serverStarted = true;
|
||||||
started.set(true);
|
openDocumentationPage();
|
||||||
DesktopIntegration.open("http://localhost:5689/docs/index.md");
|
}));
|
||||||
})
|
|
||||||
);
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
log.error("Couldn't start documentation server", ex);
|
log.error("Couldn't start documentation server", ex);
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
private void openDocumentationPage() {
|
||||||
DesktopIntegration.open("http://localhost:5689/docs/index.md");
|
DesktopIntegration.open("http://localhost:5689/docs/index.md");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue