Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk committed Apr 23, 2024
1 parent fee7a1f commit 74ea40e
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 244 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ build/
.DS_Store!/user.dat

### Bubbles ###
user.dat
user.dat
messageLogs*.txt
7 changes: 1 addition & 6 deletions saveMessage.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
Me: 1
Me: 2
Me: 3
Me: 4
Me: 5
;
Me: 1Me: 2Me: 3Me: 4Me: 5;;;
39 changes: 20 additions & 19 deletions src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;
import team.elrant.bubbles.xmpp.ConnectedUser;

import java.io.File;
import java.util.Objects;

/**
Expand Down Expand Up @@ -55,21 +54,23 @@ public static void main(String[] args) {
@Override
public void start(@NotNull Stage stage) throws Exception {
try {
@NotNull FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/ChatView.fxml"));
if (contactJid.isEmpty()) {
throw new IllegalArgumentException("Contact JID is empty.");
}

logger.debug("Contact JID before parsing: {}", contactJid);

EntityBareJid bareContactJid = JidCreate.entityBareFrom(contactJid);

logger.debug("Parsed bare contact JID: {}", bareContactJid);

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/ChatView.fxml"));

// Set custom controller factory
fxmlLoader.setControllerFactory(param -> {
try {
// Instantiate the controller with the required properties
BareJid bareContactJid = JidCreate.bareFrom(contactJid);
return new ChatViewController(connectedUser, bareContactJid);
} catch (Exception e) {
logger.error("Error creating ChatViewController: {}", e.getMessage());
throw new RuntimeException(e);
}
});
fxmlLoader.setControllerFactory(param -> new ChatViewController(connectedUser, bareContactJid));

AnchorPane root = fxmlLoader.load();
@NotNull Scene scene = new Scene(root, 800, 700);
Scene scene = new Scene(root, 800, 700);
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styling/fluent-light.css")).toExternalForm());
stage.setTitle("Chat");
stage.setScene(scene);
Expand All @@ -79,15 +80,15 @@ public void start(@NotNull Stage stage) throws Exception {
stage.setOnCloseRequest(windowEvent -> {
try {
ChatViewController chatViewController = fxmlLoader.getController();
chatViewController.saveMessage();
}
catch (Exception e){
logger.error("Error: {}",e.getMessage());
chatViewController.saveToFile();
} catch (Exception e) {
logger.error("Error: {}", e.getMessage());
}
});
} catch (Exception e) {
logger.error("Error starting ChatViewApplication: {}", e.getMessage());
throw e;
}
}
}

}
109 changes: 54 additions & 55 deletions src/main/java/team/elrant/bubbles/gui/ChatViewController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package team.elrant.bubbles.gui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
import team.elrant.bubbles.xmpp.ConnectedUser;

import java.io.*;
import java.security.Key;
import java.util.StringTokenizer;

/**
* The ChatViewController class controls the chat view functionality in the GUI.
Expand All @@ -24,13 +20,17 @@
public class ChatViewController {
private static final Logger logger = LogManager.getLogger(ChatViewController.class);
private final @NotNull ConnectedUser connectedUser;
private final @NotNull BareJid bareContactJid;
private final @NotNull EntityBareJid bareContactJid;

@FXML
private TextArea chatTextArea;

@FXML
private TextArea messageTextArea;

@FXML
private Button sendButton;

@FXML
private Label failedSendMessageLabel;

Expand All @@ -40,7 +40,7 @@ public class ChatViewController {
* @param connectedUser The connected user instance.
* @param bareContactJid The JID of the contact with whom the user is communicating.
*/
public ChatViewController(@NotNull ConnectedUser connectedUser, @NotNull BareJid bareContactJid) {
public ChatViewController(@NotNull ConnectedUser connectedUser, @NotNull EntityBareJid bareContactJid) {
this.connectedUser = connectedUser;
this.bareContactJid = bareContactJid;
}
Expand All @@ -51,71 +51,79 @@ public ChatViewController(@NotNull ConnectedUser connectedUser, @NotNull BareJid
*/
@FXML
protected void initialize() {
chatTextArea.setStyle("-fx-background-color: Black;");
messageTextArea.setStyle("-fx-background-color: Black;");
if(verifyFile() != 0){
if (verifyFile() != 0) {
readFromFile();
}
connectedUser.addIncomingMessageListener(bareContactJid, this::updateChatDisplay);
messageTextArea.setOnKeyPressed(event ->{
if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call sendMessage
messageTextArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER)
sendMessage();
});
sendButton.setOnKeyPressed(event -> sendMessage()); //call sendMessage when button is pressed
chatTextArea.setOnKeyPressed(event ->{
if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call sendMessage
sendButton.setOnKeyPressed(event -> sendMessage());
chatTextArea.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER)
sendMessage();
});
messageTextArea.setWrapText(true);
chatTextArea.setEditable(false);
chatTextArea.setWrapText(true);

}

protected int verifyFile(){
File file = new File("saveMessage.txt");
return (int)file.length();
/**
* Verifies if the message log file exists and returns its size.
*
* @return The size of the message log file, or 0 if the file does not exist.
*/
protected int verifyFile() {
File file = new File("messageLogs" + bareContactJid + ".txt");
return (int) file.length();
}

protected void saveMessage(){
try {
FileWriter f = new FileWriter("saveMessage.txt",false);
PrintWriter file = new PrintWriter(f);
file.println(chatTextArea.getText() + ";");
f.flush();
f.close();
}
catch (IOException e){
logger.error("Error To Save Message");
/**
* Saves the current chat messages to a file.
*/
protected void saveToFile() {
try (PrintWriter file = new PrintWriter(new FileWriter("messageLogs" + bareContactJid + ".txt", false))) {
String[] lines = chatTextArea.getText().split("\\n");
for (String line : lines) {
file.println(line);
}
} catch (IOException e) {
logger.error("Error saving messages: {}", e.getMessage());
}
}
protected void readFromFile(){ //Read from file and add to chatAreaText
try {

/**
* Reads chat messages from a file and appends them to the chat area.
*/
protected void readFromFile() {
try (BufferedReader file = new BufferedReader(new FileReader("messageLogs" + bareContactJid + ".txt"))) {
String line;
FileReader f = new FileReader("saveMessage.txt");
BufferedReader file = new BufferedReader(f);
StringTokenizer tokenizer;
String string = file.readLine();
while (string != null){
tokenizer = new StringTokenizer(string,";");
line = "\n" + tokenizer.nextToken();
chatTextArea.appendText(string);
string = file.readLine();
while ((line = file.readLine()) != null) {
chatTextArea.appendText(line + "\n");
}
f.close();
}
catch (IOException e){
logger.error("Error To Read From File");
} catch (IOException e) {
logger.error("Error reading from file: {}", e.getMessage());
}
}

/**
* Updates the chat display with the incoming message.
*
* @param message The incoming message to display.
*/
private void updateChatDisplay(@NotNull String message) {
chatTextArea.appendText("\n" + bareContactJid + ": " + message + "\n");
}

/**
* Sends a message to the contact and updates the chat display.
*/
@FXML
protected void sendMessage() {
try {
String message = messageTextArea.getText().trim(); // Trim to remove leading/trailing whitespace
if (!message.isEmpty()) { // Check if the message is not empty
String message = messageTextArea.getText().trim();
if (!message.isEmpty()) {
connectedUser.sendMessage(bareContactJid, message);
chatTextArea.appendText("Me: " + message + "\n");
messageTextArea.clear();
Expand All @@ -127,13 +135,4 @@ protected void sendMessage() {
logger.error("Error sending message: {}", e.getMessage());
}
}

/**
* Updates the chat display with the incoming message.
*
* @param message The incoming message to display.
*/
private void updateChatDisplay(@NotNull String message) {
chatTextArea.appendText("\n" + bareContactJid + ": " + message + "\n");
}
}
34 changes: 18 additions & 16 deletions src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import team.elrant.bubbles.xmpp.ConnectedUser;
import team.elrant.bubbles.xmpp.User;

import java.util.Set;

public class LoginController {
private static final Logger logger = LogManager.getLogger(LoginController.class);
Expand All @@ -33,8 +30,10 @@ public class LoginController {
private CheckBox seePasswordCheckbox;
private @Nullable ConnectedUser connectedUser;

final String contact = "testuser";

/**
* Handles the submit button click event.
* Attempts to log in the user and open the main application view if successful.
*/
@FXML
protected void onSubmitButtonClick() {
try {
Expand All @@ -54,6 +53,10 @@ protected void onSubmitButtonClick() {
}
}

/**
* Handles the see password checkbox click event.
* Toggles the visibility of the password fields.
*/
@FXML
protected void onSeePasswordCheckBoxClick() {
if (seePasswordCheckbox.isSelected()) {
Expand All @@ -67,6 +70,10 @@ protected void onSeePasswordCheckBoxClick() {
}
}

/**
* Initializes the login view.
* Loads user information from file if available and sets event handlers.
*/
@FXML
public void initialize() {
try {
Expand Down Expand Up @@ -95,11 +102,17 @@ public void initialize() {
});
}

/**
* Closes the login window.
*/
private void closeLoginWindow() {
Stage stage = (Stage) submitButton.getScene().getWindow();
stage.close();
}

/**
* Opens the main application view (SideView).
*/
private void openSideView() {
try {
SideViewApplication sideViewApplication = new SideViewApplication(connectedUser);
Expand All @@ -111,15 +124,4 @@ private void openSideView() {

}
}

private void openChatWindow(User user) {
try {
if (connectedUser != null && connectedUser.isLoggedIn()) {
ChatViewApplication chatViewApplication = new ChatViewApplication(connectedUser, user.getUsername());
chatViewApplication.start(new Stage());
}
} catch (Exception e) {
logger.error("Error opening chat window: {}", e.getMessage());
}
}
}
Loading

0 comments on commit 74ea40e

Please sign in to comment.