diff --git a/mvnw b/mvnw old mode 100644 new mode 100755 diff --git a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java index 4b675df..bf58ddb 100644 --- a/src/main/java/team/elrant/bubbles/gui/ChatViewController.java +++ b/src/main/java/team/elrant/bubbles/gui/ChatViewController.java @@ -1,16 +1,20 @@ package team.elrant.bubbles.gui; -import javafx.application.Platform; +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 team.elrant.bubbles.xmpp.ConnectedUser; +import java.security.Key; + /** * The ChatViewController class controls the chat view functionality in the GUI. * It handles sending and displaying messages in the chat interface. @@ -45,8 +49,21 @@ public ChatViewController(@NotNull ConnectedUser connectedUser, @NotNull BareJid */ @FXML protected void initialize() { + chatTextArea.setStyle("-fx-background-color: Black;"); + messageTextArea.setStyle("-fx-background-color: Black;"); connectedUser.addIncomingMessageListener(bareContactJid, this::updateChatDisplay); - sendButton.setOnAction(event -> sendMessage()); + messageTextArea.setOnKeyPressed(event ->{ + if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call sendMessage + 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 + sendMessage(); + }); + messageTextArea.setWrapText(true); + chatTextArea.setEditable(false); + chatTextArea.setWrapText(true); } /** @@ -75,6 +92,6 @@ protected void sendMessage() { * @param message The incoming message to display. */ private void updateChatDisplay(@NotNull String message) { - Platform.runLater(() -> chatTextArea.appendText(bareContactJid + ": " + message + "\n")); + chatTextArea.appendText("\n" + bareContactJid + ": " + message + "\n"); } } diff --git a/src/main/java/team/elrant/bubbles/gui/SideViewController.java b/src/main/java/team/elrant/bubbles/gui/SideViewController.java index 613e338..5438628 100644 --- a/src/main/java/team/elrant/bubbles/gui/SideViewController.java +++ b/src/main/java/team/elrant/bubbles/gui/SideViewController.java @@ -10,6 +10,5 @@ */ public class SideViewController { private static final Logger logger = LogManager.getLogger(SideViewController.class); - - + // Unimplemented class } \ No newline at end of file diff --git a/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java b/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java new file mode 100644 index 0000000..6b73825 --- /dev/null +++ b/src/main/java/team/elrant/bubbles/xmpp/ChatListener.java @@ -0,0 +1,39 @@ +package team.elrant.bubbles.xmpp; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jivesoftware.smack.chat2.Chat; +import org.jivesoftware.smack.chat2.IncomingChatMessageListener; +import org.jivesoftware.smack.packet.Message; +import org.jxmpp.jid.BareJid; +import org.jxmpp.jid.EntityBareJid; + +import java.util.function.Consumer; + +public class ChatListener implements IncomingChatMessageListener { + private static final Logger logger = LogManager.getLogger(ChatListener.class); + public BareJid contactJid; + Consumer updateChatDisplay; + + public ChatListener(BareJid contactJid, Consumer updateChatDisplay){ + this.contactJid = contactJid; + this.updateChatDisplay = updateChatDisplay; + } + + /** + * @param from The JID of the sender + * @param message The message contents + * @param chat The chat channel + */ + @Override + public void newIncomingMessage(EntityBareJid from, Message message, Chat chat) { + try { + if (from != null && from.equals(contactJid) && message.getBody() != null) { + updateChatDisplay.accept(message.getBody()); + logger.info("Received message from {}: {}", from, message.getBody()); + } + } catch (Exception e) { + logger.error("Error updating chat display: {}", e.getMessage()); + } + } +} diff --git a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java index c319595..8becf64 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java +++ b/src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java @@ -103,7 +103,7 @@ public void initializeConnection() throws SmackException, InterruptedException, * @param contactJid The JID of the contact to add (user@service.name). * @param nickname The user-defined nickname of the contact, defaults to the contact's username. */ - private void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { + public void addContact(@NotNull BareJid contactJid, @Nullable String nickname) { try { if (roster != null && !roster.contains(contactJid)) { roster.createItemAndRequestSubscription(contactJid, nickname, null); @@ -236,24 +236,10 @@ public void disconnect() { /** * Adds an incoming message listener to the chat manager. */ - public void addIncomingMessageListener() { - if (chatManager != null) { - chatManager.addIncomingListener((from, message, chat) -> - logger.info("Received message from {}: {}", from, message.getBody())); - } - } - public void addIncomingMessageListener(BareJid contactJid, Consumer updateChatDisplay) { if (chatManager != null) { - chatManager.addIncomingListener((from, message, chat) -> { - try { - if (from != null && from.equals(contactJid) && message.getBody() != null) { - updateChatDisplay.accept(message.getBody()); - } - } catch (Exception e) { - logger.error("Error updating chat display: {}", e.getMessage()); - } - }); + ChatListener chatListener = new ChatListener(contactJid, updateChatDisplay); + chatManager.addIncomingListener(chatListener); } } } diff --git a/src/main/java/team/elrant/bubbles/xmpp/User.java b/src/main/java/team/elrant/bubbles/xmpp/User.java index d6479b2..657cd4c 100644 --- a/src/main/java/team/elrant/bubbles/xmpp/User.java +++ b/src/main/java/team/elrant/bubbles/xmpp/User.java @@ -3,7 +3,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.FileInputStream; import java.io.IOException; @@ -16,7 +15,6 @@ */ public class User implements Serializable { private static final Logger logger = LogManager.getLogger(User.class); - public @NotNull String username; public @NotNull String serviceName; @@ -39,6 +37,18 @@ public User(@NotNull String username, @NotNull String serviceName) { * @throws IOException If an I/O error occurs while reading the file. * @throws ClassNotFoundException If the class of a serialized object cannot be found. */ + public User(@NotNull String filename) throws IOException, ClassNotFoundException { + try (FileInputStream fileIn = new FileInputStream(filename); + ObjectInputStream objectIn = new ObjectInputStream(fileIn)) { + User serializedUser = (User) objectIn.readObject(); + logger.info("User information loaded from {}", filename); + this.username = serializedUser.getUsername(); + this.serviceName = serializedUser.getServiceName(); + } catch (IOException | ClassNotFoundException e) { + logger.error("Error loading user information from file: {}", e.getMessage()); + throw e; + } + } /** * Retrieves the username of the user. diff --git a/src/main/resources/team/elrant/bubbles/gui/views/ChatView.fxml b/src/main/resources/team/elrant/bubbles/gui/views/ChatView.fxml index 900389c..014bf58 100644 --- a/src/main/resources/team/elrant/bubbles/gui/views/ChatView.fxml +++ b/src/main/resources/team/elrant/bubbles/gui/views/ChatView.fxml @@ -1,32 +1,35 @@ - - - - - - + + + + + + - + - + + + + + - diff --git a/src/main/resources/team/elrant/bubbles/gui/views/sendicon.png b/src/main/resources/team/elrant/bubbles/gui/views/sendicon.png new file mode 100644 index 0000000..ec94035 Binary files /dev/null and b/src/main/resources/team/elrant/bubbles/gui/views/sendicon.png differ