feat: add LoginController.java logic for handling UI events
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package com.university.chat.Client.UI;
|
||||
|
||||
import com.university.chat.Client.Net.chatClient;
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class LoginController {
|
||||
|
||||
@FXML
|
||||
public HBox titleBar;
|
||||
|
||||
private double xOffset = 0;
|
||||
private double yOffset = 0;
|
||||
|
||||
@FXML
|
||||
private TextField usernameField;
|
||||
|
||||
@FXML
|
||||
private Button nameButton;
|
||||
|
||||
private final chatClient client = new chatClient();
|
||||
|
||||
private ChatController chatController;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
titleBar.setOnMousePressed(event -> {
|
||||
xOffset = event.getSceneX();
|
||||
yOffset = event.getSceneY();
|
||||
});
|
||||
|
||||
titleBar.setOnMouseDragged(event -> {
|
||||
Stage stage = (Stage) titleBar.getScene().getWindow();
|
||||
stage.setX(event.getScreenX() - xOffset);
|
||||
stage.setY(event.getScreenY() - yOffset);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void onConnectClicked() {
|
||||
String username = usernameField.getText().trim();
|
||||
|
||||
if (username.isEmpty()) {
|
||||
showError("Username cannot be empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
nameButton.setDisable(true);
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
|
||||
client.connect("localhost", 9000);
|
||||
|
||||
client.startListening(
|
||||
this::handleServerMessage,
|
||||
this::handleConnectionError
|
||||
);
|
||||
|
||||
client.login(username);
|
||||
|
||||
} catch (Exception e) {
|
||||
Platform.runLater(() -> {
|
||||
showError("Failed to connect to server.\nMake sure the server is running.\n\n" + e.getMessage());
|
||||
nameButton.setDisable(false);
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
private void handleServerMessage(Object obj) {
|
||||
if (obj instanceof ChatMessage msg) {
|
||||
|
||||
if (msg.getType() == MessageType.LOGIN_SUCCESS) {
|
||||
Platform.runLater(() -> {
|
||||
showInfo("Login successful!");
|
||||
openChatWindow();
|
||||
});
|
||||
}
|
||||
else if (msg.getType() == MessageType.LOGIN_FAILED) {
|
||||
Platform.runLater(() -> {
|
||||
showError("Login failed: username taken");
|
||||
nameButton.setDisable(false);
|
||||
});
|
||||
}
|
||||
else if (msg.getType() == MessageType.USER_LIST) {
|
||||
Platform.runLater(() -> {
|
||||
if (chatController != null) {
|
||||
String content = msg.getContent();
|
||||
java.util.List<String> users = java.util.Arrays.asList(content.split(","));
|
||||
chatController.updateUsersList(users);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
Platform.runLater(() -> {
|
||||
if (chatController != null) {
|
||||
chatController.sendPublicMessage();
|
||||
}
|
||||
System.out.println("Server: " + msg.getContent());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleConnectionError(Exception e) {
|
||||
Platform.runLater(() -> showError("Connection lost: " + e.getMessage()));
|
||||
}
|
||||
|
||||
|
||||
private void showError(String text) {
|
||||
Alert alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.setTitle("Error");
|
||||
alert.setHeaderText(null);
|
||||
alert.setContentText(text);
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
private void showInfo(String text) {
|
||||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||||
alert.setTitle("Info");
|
||||
alert.setHeaderText(null);
|
||||
alert.setContentText(text);
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
private void openChatWindow() {
|
||||
try {
|
||||
|
||||
FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource("chat.fxml")
|
||||
);
|
||||
|
||||
Parent root = loader.load();
|
||||
|
||||
this.chatController = loader.getController();
|
||||
this.chatController.setClient(client);
|
||||
|
||||
Stage stage = (Stage) usernameField.getScene().getWindow();
|
||||
|
||||
stage.setScene(new Scene(root, 900, 600));
|
||||
|
||||
stage.getScene().getStylesheets().add(
|
||||
getClass().getResource("/chat_style.css").toExternalForm()
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void closeApp() {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void minimize() {
|
||||
Stage stage = (Stage) usernameField.getScene().getWindow();
|
||||
stage.setIconified(true);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void maximize() {
|
||||
Stage stage = (Stage) usernameField.getScene().getWindow();
|
||||
stage.setMaximized(!stage.isMaximized());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user