Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init #3

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package team.elrant.bubbles.gui;

import javafx.fxml.FXML;
import javafx.scene.AccessibleRole;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
Expand All @@ -11,10 +10,8 @@
import team.elrant.bubbles.xmpp.ConnectedUser;
import team.elrant.bubbles.xmpp.User;

/**
* The LoginController class controls the login functionality in the GUI.
* It handles user authentication and navigation to the main application.
*/
import java.util.Set;

public class LoginController {
private static final Logger logger = LogManager.getLogger(LoginController.class);

Expand All @@ -38,11 +35,6 @@ public class LoginController {

final String contact = "testuser";


/**
* Handles the action when submit button is clicked.
* It attempts to log in the user using the provided credentials and navigates to the main application upon successful login.
*/
@FXML
protected void onSubmitButtonClick() {
try {
Expand All @@ -58,12 +50,12 @@ protected void onSubmitButtonClick() {
}
if (connectedUser != null && connectedUser.isLoggedIn()) {
successfulLoginLabel.setVisible(true);
openChatWindow();
openSideView();
}
}

@FXML
protected void onSeePasswordCheckBoxClick (){
protected void onSeePasswordCheckBoxClick() {
if (seePasswordCheckbox.isSelected()) {
password_field_hidden.setVisible(false);
password_field_visible.setText(password_field_hidden.getText());
Expand All @@ -75,55 +67,56 @@ protected void onSeePasswordCheckBoxClick (){
}
}

/**
* Initializes the login form.
* It loads the username from a file and populates the username field, if available.
*/
@FXML
public void initialize() {
try {
ConnectedUser userFromFile = new ConnectedUser("user.dat");
if (!userFromFile.getUsername().equals("uninit")) {
username_field.setText(userFromFile.getUsername());
}
if (!userFromFile.passwordUnInit()){
if (!userFromFile.passwordUnInit()) {
userFromFile.setPasswordField(password_field_hidden);
rememberPassword.setSelected(true);
}
} catch (Exception ignored) {
logger.warn("Failed to load user information from file.");
}
password_field_hidden.setOnKeyPressed(event ->{
if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call onSubmitButton
password_field_hidden.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER)
onSubmitButtonClick();
});
password_field_visible.setOnKeyPressed(event ->{
if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call onSubmitButton
password_field_visible.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER)
onSubmitButtonClick();
});
username_field.setOnKeyPressed(event ->{
if(event.getCode() == KeyCode.ENTER) //when Enter is pressed, call onSubmitButton
username_field.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER)
onSubmitButtonClick();
});
}

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

/**
* Opens the chat window.
*/
private void openChatWindow() {
private void openSideView() {
try {
SideViewApplication sideViewApplication = new SideViewApplication(connectedUser);
sideViewApplication.start(new Stage());
closeLoginWindow();
} catch (Exception e) {
logger.debug(e);
throw new RuntimeException(e);

}
}

private void openChatWindow(User user) {
try {
if (connectedUser != null && connectedUser.isLoggedIn()) {
ChatViewApplication chatViewApplication = new ChatViewApplication(connectedUser, contact+"@bubbles.elrant.team");
ChatViewApplication chatViewApplication = new ChatViewApplication(connectedUser, user.getUsername());
chatViewApplication.start(new Stage());
closeLoginWindow();
}
} catch (Exception e) {
logger.error("Error opening chat window: {}", e.getMessage());
Expand Down
36 changes: 20 additions & 16 deletions src/main/java/team/elrant/bubbles/gui/SideViewApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import team.elrant.bubbles.xmpp.ConnectedUser;

import java.util.Objects;

public class SideViewApplication extends Application {

private static final Logger logger = LogManager.getLogger(SideViewApplication.class);
private ConnectedUser connectedUser;

public SideViewApplication(ConnectedUser connectedUser) {
this.connectedUser = connectedUser;
}

public static void main(String[] args) {
launch(args);
Expand All @@ -28,21 +34,19 @@ public static void main(String[] args) {
*/
@Override
public void start(@NotNull Stage stage) throws Exception {
try {
@NotNull FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/SideView.fxml"));
AnchorPane root = fxmlLoader.load();
@NotNull Scene scene = new Scene(root, 320, 720);
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styling/fluent-light.css")).toExternalForm());
stage.setTitle("Chat");
stage.setScene(scene);
stage.centerOnScreen();
stage.setResizable(false);
stage.show();
} catch (Exception e) {
logger.error("Error starting SideViewApplication: {}", e.getMessage());
throw e;
}
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/SideView.fxml"));
AnchorPane root = fxmlLoader.load();

// Pass the connectedUser to the controller
SideViewController controller = fxmlLoader.getController();
controller.setConnectedUser(connectedUser);

Scene scene = new Scene(root, 320, 720);
scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("styling/fluent-light.css")).toExternalForm());
stage.setTitle("Chat");
stage.setScene(scene);
stage.centerOnScreen();
stage.setResizable(false);
stage.show();
}
}


64 changes: 61 additions & 3 deletions src/main/java/team/elrant/bubbles/gui/SideViewController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,72 @@
package team.elrant.bubbles.gui;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import team.elrant.bubbles.xmpp.ConnectedUser;
import team.elrant.bubbles.xmpp.User;

import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
* The SideViewController class controls the login functionality in the GUI.
* It handles ...
*/
public class SideViewController {
private static final Logger logger = LogManager.getLogger(SideViewController.class);
// Unimplemented class
}
@FXML
private VBox userList;
private ConnectedUser connectedUser;

// Add setter for connectedUser
public void setConnectedUser(ConnectedUser connectedUser) {
this.connectedUser = connectedUser;
}

@FXML
public void initialize() {
Roster roster = ConnectedUser.getRoster();
Set <RosterEntry> rosterEntries = roster.getEntries();
for (RosterEntry entry : rosterEntries) {
String jid = entry.getJid().toString();
String[] parts = jid.split("@");
String username = parts[0];
String serviceName = parts[1];

// Create a User object
User user = new User(username, serviceName);

// Create UI components
HBox userItem = new HBox();
// ImageView profilePic = new ImageView(new Image(user.getProfilePictureUrl()));
// profilePic.setFitHeight(50);
// profilePic.setFitWidth(50);
Label userName = new Label(user.getUsername());

// Add event handler to open ChatPage
userName.setOnMouseClicked(event -> openChatPage(username));

userItem.getChildren().addAll(userName);
userList.getChildren().add(userItem);
}
}

// Method to open ChatPage
private void openChatPage(String username) {
try {
ChatViewApplication chatViewApplication = new ChatViewApplication(connectedUser, username);
chatViewApplication.start(new Stage());
} catch (Exception e) {
logger.error("Error opening chat window: {}", e.getMessage());
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class ConnectedUser extends User {
private static final Logger logger = LogManager.getLogger(ConnectedUser.class);
private final @Nullable String password;
private @Nullable Roster roster;
private static @Nullable Roster roster;
private @Nullable XMPPTCPConnection connection;
private @Nullable ChatManager chatManager;

Expand Down Expand Up @@ -198,7 +198,7 @@ public void saveUserToFile(@NotNull String filename, boolean savePassword) {
* @return The roster of the connected user.
* @throws IllegalStateException if the roster is not initialized.
*/
public @NotNull Roster getRoster() {
public static @NotNull Roster getRoster() {
if (roster != null) {
return roster;
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/team/elrant/bubbles/xmpp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public User(@NotNull String filename) throws IOException, ClassNotFoundException
public @NotNull String getServiceName() {
return serviceName;
}

public String getProfilePictureUrl() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getProfilePictureUrl'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Font name="Segoe UI" size="12.0" />
</font>
</TextField>
<Label alignment="BASELINE_LEFT" layoutX="1.0" layoutY="38.0" text="Password">
<Label alignment="BASELINE_LEFT" layoutX="1.0" layoutY="38.0" text="Mario Rossi">
<font>
<Font name="Segoe UI Semibold" size="12.0" />
</font>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
</Menu>
</menus>
</MenuBar>
<VBox fx:id="userList" layoutY="30.0" prefHeight="690.0" prefWidth="320.0" />
</children>
</AnchorPane>
Loading