Skip to content

Commit

Permalink
ChatPage
Browse files Browse the repository at this point in the history
  • Loading branch information
davide committed Mar 12, 2024
1 parent 4555055 commit b6aba0e
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 9 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/main/java/team/elrant/bubbles/gui/ChatPageApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package team.elrant.bubbles.gui;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;

public class ChatPageApplication extends Application {

public static void main(String[] args) {
launch();
}

@Override
public void start(@org.jetbrains.annotations.NotNull Stage primaryStage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(ChatPageApplication.class.getResource("ChatView.fxml"));
Scene scene = new Scene(fxmlLoader.load(),400,800);
primaryStage.setTitle("Chat");
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
primaryStage.setResizable(false);
primaryStage.show();

}
}
49 changes: 49 additions & 0 deletions src/main/java/team/elrant/bubbles/gui/ChatPageController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package team.elrant.bubbles.gui;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import team.elrant.bubbles.xmpp.ConnectedUser;
import java.io.IOException;

public class ChatPageController{
@FXML
private TextArea chatTextArea;
@FXML
private TextArea messageTextArea;
@FXML
private Button sendButton;
@FXML
private Label failedLoginLabel;
private ConnectedUser connectedUser;
private String contactJid;
private String password;

public ChatPageController(ConnectedUser connectedUser,String contactJid,String password){
this.connectedUser = connectedUser;
this.contactJid = contactJid;
this.password = password;
}

@FXML
protected void initialize(){

connectedUser.addIncomingMessageListener();

sendButton.setOnAction(actionEvent -> sendMessage());
}
@FXML
protected void sendMessage(){
try{
String message = messageTextArea.getText();
connectedUser.getSendMessage(contactJid,message);
chatTextArea.appendText("Me: "+ message + "\n");
messageTextArea.clear();
}
catch (Exception e){
failedLoginLabel.setVisible(true);
}
}
}
43 changes: 35 additions & 8 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
public class ConnectedUser extends User {
private final String password;
private Roster roster;
private AbstractXMPPConnection connection = null; // When accessing this property, make sure it's not null
private AbstractXMPPConnection connection = null; // When accessing this property, make sure it's not null
// (or just run InitializeConnection first)
private ChatManager chatManager = null; // Same warning as connection
private ChatManager chatManager = null; // Same warning as connection

public ConnectedUser(String username, String password, String serviceName) throws SmackException, IOException, XMPPException, InterruptedException {
public ConnectedUser(String username, String password, String serviceName) {
super(username, serviceName);
this.password = password;
this.initializeConnection();
}

/**
Expand Down Expand Up @@ -115,7 +114,9 @@ private void sendMessage(String contactJid, String message) {
System.err.println("Error sending XMPP message: " + e);
}
}

public void getSendMessage(String contactJid, String message){
sendMessage(contactJid,message);
}
/**
* Accepts a subscription request from a contact.
* @param contactJid the JID of the contact to accept the subscription from (user@service.name)
Expand All @@ -137,12 +138,13 @@ public void acceptSubscription(String contactJid, String nickname) {
* @param filename The name of the file to save the user information to.
*/
public void saveUserToFile(String filename) {
try (FileOutputStream fileOut = new FileOutputStream(filename); ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
try (FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {

// Create a temporary user object without the password
User userWithoutPassword = new User(super.getUsername(), super.getServiceName());

// Write the user object (without the password) to the file
// Write the user object (without password) to the file
objectOut.writeObject(userWithoutPassword);

System.out.println("User information (excluding password) saved to " + filename);
Expand All @@ -151,6 +153,29 @@ public void saveUserToFile(String filename) {
}
}

/**
* Loads the user information from a file and initializes a ConnectedUser object.
* @param filename The name of the file containing the serialized user information.
* @return A ConnectedUser object initialized with the loaded user information.
*/
public static ConnectedUser loadUserFromFile(String filename) {
ConnectedUser connectedUser = null;
try (FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream objectIn = new ObjectInputStream(fileIn)) {

// Read the serialized user object from the file
User user = (User) objectIn.readObject();

// Initialize a new ConnectedUser object with the loaded user information
connectedUser = new ConnectedUser(user.getUsername(), "password", user.getServiceName());

System.out.println("User information loaded from " + filename);
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error loading user information from file: " + e);
}
return connectedUser;
}

public Roster getRoster() {
return roster;
}
Expand All @@ -164,6 +189,8 @@ public void disconnect() {
}

public void addIncomingMessageListener() {
chatManager.addIncomingListener((from, message, chat) -> System.out.println("Received message from " + from + ": " + message.getBody()));
chatManager.addIncomingListener((from, message, chat) -> {
System.out.println("Received message from " + from + ": " + message.getBody());
});
}
}
22 changes: 22 additions & 0 deletions src/main/resources/team/elrant/bubbles/gui/ChatView.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="team.elrant.bubbles.gui.ChatPageController"
prefHeight="400.0" prefWidth="600.0">
//Text Area for chat
<TextArea fx:id="chatTextArea" prefWidth="400" prefHeight="600" editable="false"/>

//Text Area for message
<TextArea fx:id="messageTextArea" prefWidth="300" prefHeight="100" layoutY="600"/>

//Button for message
<Button fx:id="sendButton" text="Send" onAction="#sendMessage" layoutY="620" layoutX="320"/>

//Label for error
<Label fx:id="failedLoginLabel" text="Failed to send message!" visible="false" textFill="red" layoutX="320" layoutY="650"/>
</AnchorPane>

0 comments on commit b6aba0e

Please sign in to comment.