Skip to content

Commit

Permalink
Merge branch 'main' into login-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk authored Apr 21, 2024
2 parents 647e4ec + 755211c commit 6539949
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 42 deletions.
Empty file modified mvnw
100644 → 100755
Empty file.
23 changes: 20 additions & 3 deletions src/main/java/team/elrant/bubbles/gui/ChatViewController.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
*/
public class SideViewController {
private static final Logger logger = LogManager.getLogger(SideViewController.class);


// Unimplemented class
}
39 changes: 39 additions & 0 deletions src/main/java/team/elrant/bubbles/xmpp/ChatListener.java
Original file line number Diff line number Diff line change
@@ -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<String> updateChatDisplay;

public ChatListener(BareJid contactJid, Consumer<String> 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());
}
}
}
20 changes: 3 additions & 17 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> 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);
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/team/elrant/bubbles/xmpp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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.
Expand Down
39 changes: 21 additions & 18 deletions src/main/resources/team/elrant/bubbles/gui/views/ChatView.fxml
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="300.0" prefWidth="800.0"
xmlns="http://javafx.com/javafx/17.0.2-ea" fx:controller="team.elrant.bubbles.gui.ChatViewController">
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="300.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="team.elrant.bubbles.gui.ChatViewController">
<TextArea fx:id="chatTextArea" editable="false" layoutX="7.0" layoutY="10.0" prefHeight="584.0" prefWidth="630.0">
<font>
<Font name="Segoe UI" size="12.0"/>
<Font name="Segoe UI" size="12.0" />
</font>
</TextArea>
<AnchorPane layoutY="600.0" prefHeight="100.0" prefWidth="800.0">
<AnchorPane layoutX="4.0" layoutY="600.0" prefHeight="100.0" prefWidth="800.0">
<children>
<TextArea fx:id="messageTextArea" layoutX="7.0" prefHeight="94.0" prefWidth="630.0">
<font>
<Font name="Segoe UI" size="12.0"/>
<Font name="Segoe UI" size="12.0" />
</font>
</TextArea>
<Button fx:id="sendButton" layoutX="643.0" layoutY="5.0" onAction="#sendMessage" prefHeight="88.0"
prefWidth="149.0" text="Send">
<font>
<Font name="Segoe UI Semibold" size="12.0"/>
</font>
</Button>
<AnchorPane layoutX="645.0" layoutY="2.0" prefHeight="56.0" prefWidth="50.0">
<children>
<Button fx:id="sendButton" onAction="#sendMessage" prefHeight="50.0" prefWidth="50.0" text="Send">
<graphic>
<ImageView fitHeight="55" fitWidth="55.0">
<Image url="@sendicon.png" />
</ImageView>
</graphic>
</Button>
</children>
</AnchorPane>
</children>
</AnchorPane>
<Label fx:id="failedSendMessageLabel" layoutX="320" layoutY="650" text="Failed to send message!" textFill="red"
visible="false"/>
<Label fx:id="failedSendMessageLabel" layoutX="320" layoutY="650" text="Failed to send message!" textFill="red" visible="false" />
</AnchorPane>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6539949

Please sign in to comment.