Skip to content

Commit

Permalink
Fix chatview
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk committed Mar 13, 2024
1 parent b6aba0e commit bc9dd1e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 46 deletions.
26 changes: 0 additions & 26 deletions src/main/java/team/elrant/bubbles/gui/ChatPageApplication.java

This file was deleted.

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

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import team.elrant.bubbles.xmpp.ConnectedUser;

public class ChatViewApplication extends Application {
private ConnectedUser connectedUser = null;

Check warning on line 11 in src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java

View workflow job for this annotation

GitHub Actions / qodana

Unused assignment

Variable `connectedUser` initializer `null` is redundant
private String contactJid = null;

Check warning on line 12 in src/main/java/team/elrant/bubbles/gui/ChatViewApplication.java

View workflow job for this annotation

GitHub Actions / qodana

Unused assignment

Variable `contactJid` initializer `null` is redundant

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

@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ChatView.fxml"));

// Set custom controller factory
fxmlLoader.setControllerFactory(param -> {
try {
// Instantiate the controller with the required properties
return new ChatViewController(connectedUser, contactJid);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
});

AnchorPane root = fxmlLoader.load();
Scene scene = new Scene(root, 400, 800);

primaryStage.setTitle("Chat");
primaryStage.setScene(scene);
primaryStage.centerOnScreen();
primaryStage.setResizable(false);
primaryStage.show();
}

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

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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{
public class ChatViewController {
@FXML
private TextArea chatTextArea;
@FXML
Expand All @@ -18,13 +16,16 @@ public class ChatPageController{
@FXML
private Label failedLoginLabel;
private ConnectedUser connectedUser;
private String contactJid;
private String password;
private String contactJid;

public ChatPageController(ConnectedUser connectedUser,String contactJid,String password){
public ChatViewController() {
// Default constructor
}


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

@FXML
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import team.elrant.bubbles.xmpp.ConnectedUser;
import team.elrant.bubbles.xmpp.User;

Expand All @@ -30,18 +31,24 @@ public class LoginController {
protected void onSubmitButtonClick() {
try {
connectedUser = new ConnectedUser(username_field.getText(), password_field.getText(), "elrant.team");
connectedUser.initializeConnection();
} catch (Exception e) {
// e.printStackTrace(); // Only for debugging purposes
failedLoginLabel.setVisible(true);
}

if (connectedUser != null && connectedUser.isLoggedIn()) {
// Close the login window and proceed to the main application
failedLoginLabel.setVisible(false);
successfulLoginLabel.setVisible(true);
submitButton.getScene().getWindow().hide();
connectedUser.disconnect();
connectedUser.saveUserToFile("user.dat");
Stage stage = (Stage) submitButton.getScene().getWindow();
stage.close(); // Close the login window

// Open the chat window
try {
ChatViewApplication chatViewApplication = new ChatViewApplication(connectedUser, "dummy@elrant.team");
chatViewApplication.start(new Stage());
} catch (Exception e) {
e.printStackTrace(); // Replace with more robust error handling in the future
}
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/main/resources/team/elrant/bubbles/gui/ChatView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@

<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="team.elrant.bubbles.gui.ChatPageController"
fx:controller="team.elrant.bubbles.gui.ChatViewController"
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 bc9dd1e

Please sign in to comment.