Random as - not sure what's in this submit
This commit is contained in:
@@ -2,6 +2,7 @@ package com.example.bricklog.view;
|
||||
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.CheckBox;
|
||||
import javafx.scene.control.Label;
|
||||
@@ -9,17 +10,22 @@ import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import static javafx.scene.paint.Color.WHITE;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.text.Font;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class SettingsWindow extends BorderPane {
|
||||
|
||||
private final StackPane contentPane;
|
||||
private final VBox generalSettings;
|
||||
private final VBox serverSettings;
|
||||
private final Label connectionStatus;
|
||||
private final Scene currentScene; // Reference to the main scene for applying styles dynamically
|
||||
|
||||
public SettingsWindow(Scene scene) {
|
||||
this.currentScene = scene; // Pass the main scene
|
||||
|
||||
public SettingsWindow() {
|
||||
// Menu on the left side for navigation
|
||||
VBox menu = new VBox();
|
||||
menu.setSpacing(10);
|
||||
@@ -48,6 +54,7 @@ public class SettingsWindow extends BorderPane {
|
||||
generalSettings.setAlignment(Pos.TOP_LEFT);
|
||||
Label themeLabel = new Label("Theme:");
|
||||
CheckBox darkTheme = new CheckBox("Dark Theme");
|
||||
darkTheme.setOnAction(event -> toggleTheme(darkTheme.isSelected()));
|
||||
|
||||
Label layoutLabel = new Label("Layout:");
|
||||
CheckBox compactLayout = new CheckBox("Compact Layout");
|
||||
@@ -62,7 +69,7 @@ public class SettingsWindow extends BorderPane {
|
||||
// Connection Status Label
|
||||
connectionStatus = new Label("Server Status: Not Connected");
|
||||
connectionStatus.setFont(new Font("Arial", 14));
|
||||
connectionStatus.setTextFill(WHITE);
|
||||
connectionStatus.setTextFill(Color.WHITE);
|
||||
connectionStatus.setStyle("-fx-background-color: lightpink; -fx-padding: 5;");
|
||||
connectionStatus.setAlignment(Pos.CENTER);
|
||||
connectionStatus.setMaxWidth(Double.MAX_VALUE);
|
||||
@@ -77,7 +84,6 @@ public class SettingsWindow extends BorderPane {
|
||||
|
||||
serverSettings.getChildren().addAll(connectionStatus, ipLabel, ipField, connectButton);
|
||||
|
||||
|
||||
// Add panels to contentPane and show the default panel
|
||||
contentPane.getChildren().addAll(generalSettings, serverSettings);
|
||||
showGeneralSettings(); // Default to showing the general settings
|
||||
@@ -97,56 +103,41 @@ public class SettingsWindow extends BorderPane {
|
||||
generalSettings.setVisible(false);
|
||||
}
|
||||
|
||||
// Method to connect to the server
|
||||
private void connectToServer(String ipAddress) {
|
||||
if (!isValidIPAddress(ipAddress)) {
|
||||
connectionStatus.setText("Invalid IP Address");
|
||||
connectionStatus.setStyle("-fx-background-color: orange; -fx-padding: 5;");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isConnected = simulateServerConnection(ipAddress);
|
||||
|
||||
if (isConnected) {
|
||||
connectionStatus.setText("Connected");
|
||||
connectionStatus.setStyle("-fx-background-color: lightgreen; -fx-padding: 5;");
|
||||
} else {
|
||||
connectionStatus.setText("Not Connected");
|
||||
connectionStatus.setStyle("-fx-background-color: lightpink; -fx-padding: 5;");
|
||||
}
|
||||
// Method to toggle themes
|
||||
private void toggleTheme(boolean isDarkTheme) {
|
||||
String theme = isDarkTheme ? "/css/dark-theme.css" : "/css/light-theme.css";
|
||||
currentScene.getStylesheets().clear(); // Clear existing stylesheets
|
||||
currentScene.getStylesheets().add(Objects.requireNonNull(getClass().getResource(theme)).toExternalForm());
|
||||
}
|
||||
|
||||
// Method to connect to the server
|
||||
private void connectToServer(String ipAddress) {
|
||||
if (!isValidIPAddress(ipAddress)) {
|
||||
connectionStatus.setText("Invalid IP Address");
|
||||
connectionStatus.setStyle("-fx-background-color: orange; -fx-padding: 5;");
|
||||
return;
|
||||
}
|
||||
|
||||
// Utility method to validate IP addresses
|
||||
private boolean isValidIPAddress(String ipAddress) {
|
||||
return ipAddress.matches(
|
||||
|
||||
boolean isConnected = simulateServerConnection(ipAddress);
|
||||
|
||||
if (isConnected) {
|
||||
connectionStatus.setText("Connected");
|
||||
connectionStatus.setStyle("-fx-background-color: lightgreen; -fx-padding: 5;");
|
||||
} else {
|
||||
connectionStatus.setText("Not Connected");
|
||||
connectionStatus.setStyle("-fx-background-color: lightpink; -fx-padding: 5;");
|
||||
}
|
||||
}
|
||||
|
||||
// Utility method to validate IP addresses
|
||||
private boolean isValidIPAddress(String ipAddress) {
|
||||
return ipAddress.matches(
|
||||
"^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$"
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// DEMO PURPOSES ONLY [Commented out when in production]
|
||||
// Simulated connection logic for demonstration purposes
|
||||
private boolean simulateServerConnection(String ipAddress) {
|
||||
if (ipAddress == null || ipAddress.isEmpty()) {
|
||||
return false; // IP address is invalid
|
||||
}
|
||||
|
||||
|
||||
// Simulate connection by checking for a specific IP pattern (e.g., localhost)
|
||||
return ipAddress.equals("127.0.0.1") || ipAddress.startsWith("192.168.");
|
||||
}
|
||||
|
||||
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
//PRODUCTION _ UNCOMmENT When Needed for true Server Connectivity
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
private boolean simulateServerConnection(String ipAddress) {
|
||||
int port = 12345; // Replace with your server's port
|
||||
try (Socket socket = new Socket()) {
|
||||
socket.connect(new InetSocketAddress(ipAddress, port), 3000); // 3-second timeout
|
||||
return true; // Connection successful
|
||||
} catch (IOException e) {
|
||||
return false; // Connection failed
|
||||
}
|
||||
}
|
||||
====================================================================================================== */
|
||||
|
||||
// Simulated connection logic for demonstration purposes
|
||||
private boolean simulateServerConnection(String ipAddress) {
|
||||
return ipAddress.equals("127.0.0.1") || ipAddress.startsWith("192.168.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user