Skip to content

Commit

Permalink
B
Browse files Browse the repository at this point in the history
  • Loading branch information
code-irisnk committed Mar 5, 2024
1 parent ab55cec commit 11dc60e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/main/java/team/elrant/bubbles/gui/LoginApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public void start(@org.jetbrains.annotations.NotNull Stage stage) throws IOExcep
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Login");
stage.setScene(scene);
scene.getStylesheets().add(BootstrapFX.bootstrapFXStylesheet());
stage.centerOnScreen();
stage.setResizable(false);
stage.show();

}

}
13 changes: 8 additions & 5 deletions src/main/java/team/elrant/bubbles/gui/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import team.elrant.bubbles.xmpp.ConnectedUser;

public class LoginController {
Expand All @@ -13,6 +15,10 @@ public class LoginController {
private PasswordField password_field;
@FXML
private Button submitButton; // Reference the button
@FXML
private AnchorPane formPane; // Reference the form pane
@FXML
private Label loginLabel; // Reference the login label

@FXML
protected void onSubmitButtonClick() {
Expand All @@ -33,10 +39,7 @@ protected void onSubmitButtonClick() {
}

@FXML
public void initialize() { // Add an initialize method
// Apply BootstrapFX styling
username_field.getStyleClass().add("form-control");
password_field.getStyleClass().add("form-control");
submitButton.getStyleClass().addAll("btn", "btn-primary");
public void initialize() {
// Styling goes here
}
}
62 changes: 57 additions & 5 deletions src/main/java/team/elrant/bubbles/xmpp/ConnectedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.impl.JidCreate;

import java.io.IOException;
import java.io.*;

/**
* The ConnectedUser class extends the User class.
* It describes how the app's current user should connect, behave, send messages, etc.
*/
public class ConnectedUser extends User {
private final String password;
private Roster roster;
private AbstractXMPPConnection connection = null;
private AbstractXMPPConnection connection = null; // When accessing this property, make sure it's not null
// (or just run InitializeConnection first)

public ConnectedUser(String username, String password, String serviceName) {
super(username, serviceName);
this.password = password;
}

/**
* The InitializeConnection method takes no parameters and connects to the server.
* After connecting, it sends a message to the test user.
*/
public void initializeConnection() {
try {
// 1. Create the connection configuration
Expand All @@ -45,10 +54,10 @@ public void initializeConnection() {

// 4. Get the roster
roster = Roster.getInstanceFor(connection);
roster.reloadAndWait(); // Ensure roster is up-to-date
roster.reloadAndWait(); // Initialize it

if (!super.getUsername().equals("dummy")) {
// 5. Add or remove contacts as needed
if (!super.getUsername().equals("dummy") && !super.getServiceName().equals("elrant.team")) {
// 5. Add the test user to the roster as needed
BareJid dummyJid = JidCreate.bareFrom("dummy@elrant.team");
if (roster.getEntry(dummyJid) == null) {
addContact(dummyJid.toString(), "Dummy");
Expand Down Expand Up @@ -113,4 +122,47 @@ public Roster getRoster() {
public boolean isLoggedIn() {
return connection.isAuthenticated();
}

/**
* Saves the user information (excluding password) to a file.
* @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)) {

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

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

System.out.println("User information (excluding password) saved to " + filename);
} catch (IOException e) {
System.err.println("Error saving user information to file: " + e);
}
}

/**
* 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;
}
}
53 changes: 28 additions & 25 deletions src/main/resources/team/elrant/bubbles/gui/LoginView.fxml
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="183.0" prefWidth="257.0" xmlns="http://javafx.com/javafx/17.0.2-ea"
fx:controller="team.elrant.bubbles.gui.LoginController">
<Label layoutX="14.0" layoutY="61.0" text="Username">
<font>
<Font name="Segoe UI Semibold" size="12.0"/>
</font>
</Label>
<TextField fx:id="username_field" layoutX="94.0" layoutY="57.0" promptText="MarioRossi"/>
<Label layoutX="15.0" layoutY="95.0" text="Password">
<font>
<Font name="Segoe UI Semibold" size="12.0"/>
</font>
</Label>
<PasswordField fx:id="password_field" layoutX="94.0" layoutY="91.0" promptText="********"/>
<Label alignment="CENTER" contentDisplay="CENTER" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="44.0"
prefWidth="257.0" text="Login" textAlignment="CENTER">
<font>
<Font name="Segoe UI Semibold" size="25.0"/>
</font>
</Label>
<Button fx:id="submitButton" layoutX="191.0" layoutY="144.0" mnemonicParsing="false" onAction="#onSubmitButtonClick"
text="Submit"/>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Pane prefHeight="203.0" prefWidth="312.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="team.elrant.bubbles.gui.LoginController">
<AnchorPane layoutY="14.0">
<children>
<AnchorPane fx:id="formPane" layoutX="22.0" layoutY="55.0" prefHeight="56.0" prefWidth="222.0">
<Label alignment="BASELINE_LEFT" layoutY="4.0" text="Username">
<font>
<Font name="Segoe UI Semibold" size="12.0" />
</font>
</Label>
<TextField fx:id="username_field" layoutX="80.0" promptText="Username" />
<Label alignment="BASELINE_LEFT" layoutX="1.0" layoutY="38.0" text="Password">
<font>
<Font name="Segoe UI Semibold" size="12.0" />
</font>
</Label>
<PasswordField fx:id="password_field" layoutX="80.0" layoutY="34.0" promptText="********" />
</AnchorPane>
<Label fx:id="loginLabel" alignment="BASELINE_CENTER" contentDisplay="CENTER" maxWidth="-Infinity" minWidth="-Infinity" prefHeight="39.0" prefWidth="293.0" text="Log-in" textAlignment="CENTER">
<font>
<Font name="Segoe UI Semibold" size="25.0" />
</font>
</Label>
</children>
</AnchorPane>
<Button fx:id="submitButton" layoutX="226.0" layoutY="150.0" mnemonicParsing="false" onAction="#onSubmitButtonClick" text="Submit" />
</Pane>

0 comments on commit 11dc60e

Please sign in to comment.