From d2a4fcd176d61b2eaf11c2da02e9cfa029e9f456 Mon Sep 17 00:00:00 2001 From: Jafea7 Date: Sun, 25 May 2025 22:03:24 +1000 Subject: [PATCH] Update HelpTab.java Add WAN IP address to Help tab --- .../src/main/java/ctbrec/ui/tabs/HelpTab.java | 65 ++++++++++++++----- common/src/main/java/ctbrec/WANIPFetcher.java | 39 +++++++++++ 2 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 common/src/main/java/ctbrec/WANIPFetcher.java diff --git a/client/src/main/java/ctbrec/ui/tabs/HelpTab.java b/client/src/main/java/ctbrec/ui/tabs/HelpTab.java index abc39038..e0e4275f 100644 --- a/client/src/main/java/ctbrec/ui/tabs/HelpTab.java +++ b/client/src/main/java/ctbrec/ui/tabs/HelpTab.java @@ -4,17 +4,17 @@ import ctbrec.Config; import ctbrec.OS; import ctbrec.docs.DocServer; import ctbrec.ui.DesktopIntegration; +import ctbrec.WANIPFetcher; import javafx.application.Platform; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Button; -import javafx.scene.control.Tab; import javafx.scene.control.Label; +import javafx.scene.control.Tab; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import lombok.extern.slf4j.Slf4j; -// import java.awt.Desktop; import java.io.File; import java.io.IOException; @@ -23,33 +23,66 @@ public class HelpTab extends Tab { private boolean serverStarted = false; + private String getWanIp() { + try { + return WANIPFetcher.getWANIP(); + } catch (Exception e) { + return "Unavailable"; + } + } + public HelpTab() { setClosable(false); setText("Help"); + Label wanStaticLabel = new Label("WAN IP:"); + wanStaticLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-font-weight: bold; -fx-alignment: center;"); + Label wanValueLabel = new Label(getWanIp()); + wanValueLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-underline: true; -fx-alignment: center;"); + VBox wanLabelBox = new VBox(2); + wanLabelBox.setAlignment(Pos.CENTER); + wanLabelBox.getChildren().addAll(wanStaticLabel, wanValueLabel); + Button openHelp = new Button("Open Help"); + openHelp.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 16px; -fx-font-weight: bold; -fx-text-fill: #ffffff; -fx-background-color: #4CAF50; -fx-padding: 15 30; -fx-border-radius: 5; -fx-background-radius: 5;"); + Button openLog = new Button("Open Log File"); - Button openCfg = new Button("Open config dir"); - openHelp.setPadding(new Insets(20)); - openLog.setPadding(new Insets(20)); - openCfg.setPadding(new Insets(20)); - Label logFilePathLabel = new Label("Log file: " + new File("ctbrec.log").getAbsolutePath()); + openLog.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 16px; -fx-font-weight: bold; -fx-text-fill: #ffffff; -fx-background-color: #2196F3; -fx-padding: 15 30; -fx-border-radius: 5; -fx-background-radius: 5;"); + + Button openCfg = new Button("Open Config Dir"); + openCfg.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 16px; -fx-font-weight: bold; -fx-text-fill: #ffffff; -fx-background-color: #FF9800; -fx-padding: 15 30; -fx-border-radius: 5; -fx-background-radius: 5;"); + File cfgDir = Config.getInstance().getConfigDir(); String path = (cfgDir != null) ? cfgDir.getAbsolutePath() : "Unknown"; - Label configPathLabel = new Label("Config dir: " + path); + Label configStaticLabel = new Label("Config Dir:"); + configStaticLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-font-weight: bold; -fx-alignment: center;"); + Label configValueLabel = new Label(path); + configValueLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-underline: true; -fx-alignment: center;"); + VBox configLabelBox = new VBox(2); + configLabelBox.setAlignment(Pos.CENTER); + configLabelBox.getChildren().addAll(configStaticLabel, configValueLabel); + Label logStaticLabel = new Label("Log File:"); + logStaticLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-font-weight: bold; -fx-alignment: center;"); + Label logValueLabel = new Label(new File("ctbrec.log").getAbsolutePath()); + logValueLabel.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 14px; -fx-underline: true; -fx-alignment: center;"); + VBox logLabelBox = new VBox(2); + logLabelBox.setAlignment(Pos.CENTER); + logLabelBox.getChildren().addAll(logStaticLabel, logValueLabel); + + // Create VBox for all components 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, openCfg, configPathLabel, openLog, logFilePathLabel); + vbox.setAlignment(Pos.CENTER); + vbox.setSpacing(20); + vbox.getChildren().addAll(wanLabelBox, openHelp, configLabelBox, openCfg, logLabelBox, openLog); + // Set up the BorderPane layout BorderPane layout = new BorderPane(); - - // Add the VBox to the center region of the BorderPane layout.setCenter(vbox); setContent(layout); + // Button actions remain unchanged openHelp.setOnAction(e -> { if (!serverStarted) { startDocumentationServer(); @@ -62,14 +95,13 @@ public class HelpTab extends Tab { 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 + } else { pb = new ProcessBuilder("xdg-open", logFile.getAbsolutePath()); } pb.start(); @@ -85,14 +117,13 @@ public class HelpTab extends Tab { File configDir = Config.getInstance().getConfigDir(); if (configDir.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", configDir.getAbsolutePath()); } else if (osName.contains("win")) { pb = new ProcessBuilder("explorer.exe", configDir.getAbsolutePath()); - } else { // Assume it's a Unix/Linux system + } else { pb = new ProcessBuilder("xdg-open", configDir.getAbsolutePath()); } pb.start(); diff --git a/common/src/main/java/ctbrec/WANIPFetcher.java b/common/src/main/java/ctbrec/WANIPFetcher.java new file mode 100644 index 00000000..488bfc86 --- /dev/null +++ b/common/src/main/java/ctbrec/WANIPFetcher.java @@ -0,0 +1,39 @@ +package ctbrec; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URL; + +public class WANIPFetcher { + + public static void main(String[] args) { + try { + String ip = getWANIP(); + System.out.println("Your WAN IP address is: " + ip); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static String getWANIP() throws Exception { + URI uri = new URI("https://icanhazip.com"); + URL url = uri.toURL(); // Convert URI to URL + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuilder content = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + + in.close(); + connection.disconnect(); + + return content.toString().trim(); + } +} \ No newline at end of file