logout button now brings one back to the login screen

This commit is contained in:
Sven Vogel 2023-07-22 14:31:00 +02:00
parent f1f743d233
commit d0557a2a6d
5 changed files with 70 additions and 55 deletions

View File

@ -1,5 +1,6 @@
package me.teridax.jcash;
import me.teridax.jcash.banking.management.BankingManagementSystem;
import me.teridax.jcash.gui.IconProvider;
import me.teridax.jcash.gui.Loader;
import me.teridax.jcash.gui.account.AccountController;
@ -7,17 +8,19 @@ import me.teridax.jcash.gui.login.LoginController;
import me.teridax.jcash.lang.Locales;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
import java.util.logging.Level;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
import static me.teridax.jcash.Logging.LOGGER;
import static me.teridax.jcash.Logging.initializeSystemLogger;
import static me.teridax.jcash.lang.Translator.translate;
public final class Main {
private static final String LOGIN_SCREEN_STRING_IDENT = "LoginScreen";
private static final String PROFILE_SCREEN_STRING_IDENT = "ProfileScreen";
/**
* Main instance of this program. Contains the primary window.
*/
@ -27,6 +30,12 @@ public final class Main {
* Primary window of this program
*/
private final JFrame window;
private final CardLayout layout;
private BankingManagementSystem bms;
private LoginController loginMask;
private AccountController accountController;
private Main() {
// create main window and set defaults
@ -35,6 +44,35 @@ public final class Main {
this.window.setLocationByPlatform(true);
this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.window.setIconImage(IconProvider.getWindowIcon());
this.layout = new CardLayout();
this.window.getContentPane().setLayout(this.layout);
initialize();
}
private void initialize() {
this.loginMask = new LoginController();
// when we have logged in set the account viewer as window content
this.loginMask.addAccountSelectionListener(account -> {
LOGGER.finer("account selected: " + Objects.toString(account, "null"));
accountController.setProfile(account, bms);
layout.show(window.getContentPane(), PROFILE_SCREEN_STRING_IDENT);
});
this.window.getContentPane().add(loginMask.getView(), LOGIN_SCREEN_STRING_IDENT);
this.accountController = new AccountController();
this.window.getContentPane().add(accountController.getView(), PROFILE_SCREEN_STRING_IDENT);
}
public void loadDatabase() {
this.bms = Loader.load();
this.loginMask.setBankingManagementSystem(bms);
showLoginScreen();
this.window.setSize(800, 600);
this.window.setVisible(true);
}
public static void main(String[] args) {
@ -46,7 +84,7 @@ public final class Main {
// create main instance and show the login screen
instance();
getInstance().showLoginScreen();
getInstance().loadDatabase();
}
/**
@ -115,44 +153,14 @@ public final class Main {
* This method is non-blocking and all work described is performed asynchronously on the AWT Event dispatcher.
*/
public void showLoginScreen() {
SwingUtilities.invokeLater(() -> {
LOGGER.finer("showing login screen");
try {
// select db file
var path = Loader.load();
// read database and login
var login = new LoginController(path);
// when we have logged in set the account viewer as window content
login.addAccountSelectionListener(account -> {
LOGGER.finer("account selected: " + Objects.toString(account, "null"));
var profileCont = new AccountController(account, login.getData().getBms());
this.window.setContentPane(profileCont.getView());
this.window.revalidate();
this.window.repaint();
});
// we are not logged in yet, so show the login prompt on the main window
this.window.setContentPane(login.getView());
this.window.setSize(800, 600);
this.window.setVisible(true);
} catch (IllegalStateException e) {
LOGGER.fine("Unable to show login mask: " + e.getMessage());
showMessageDialog(null, e.getMessage(), translate("Closing JCash"), ERROR_MESSAGE);
System.exit(0);
}
});
this.layout.show(this.window.getContentPane(), LOGIN_SCREEN_STRING_IDENT);
}
/**
* Logs the user out of the database, hiding the main window.
*/
public void logout() {
window.setContentPane(new JLabel(translate("you're logged out")));
window.setVisible(false);
this.layout.show(this.window.getContentPane(), LOGIN_SCREEN_STRING_IDENT);
}
public JFrame getWindow() {

View File

@ -18,18 +18,21 @@ public class AccountController {
*/
private final AccountView view;
private final Profile profile;
private Profile profile;
public AccountController(Profile profile, BankingManagementSystem bms) {
this.profile = profile;
public AccountController() {
this.view = new AccountView();
this.view.setProfile(profile);
this.data = new AccountData(bms);
createListeners(profile);
this.data = new AccountData();
}
private void createListeners(Profile profile) {
public void setProfile(Profile profile, BankingManagementSystem bms) {
this.profile = profile;
this.view.setProfile(profile);
this.data.setBms(bms);
this.createListeners();
}
private void createListeners() {
this.view.getAccountSelection().addActionListener(e -> changeAccount());
this.view.getLogout().addActionListener(e -> logout());
this.view.getDeposit().addActionListener(e -> depositMoney());

View File

@ -4,13 +4,13 @@ import me.teridax.jcash.banking.management.BankingManagementSystem;
public class AccountData {
private final BankingManagementSystem bms;
public AccountData(BankingManagementSystem bms) {
this.bms = bms;
}
private BankingManagementSystem bms;
public BankingManagementSystem getBms() {
return bms;
}
public void setBms(BankingManagementSystem bms) {
this.bms = bms;
}
}

View File

@ -18,13 +18,17 @@ public class LoginController {
private AccountSelectionListener listener;
public LoginController(BankingManagementSystem bms) {
public LoginController() {
this.view = new LoginView();
this.data = new LoginData(bms);
this.data = new LoginData();
addActionListeners();
}
public void setBankingManagementSystem(BankingManagementSystem bms) {
this.data.setBms(bms);
}
private void addActionListeners() {
this.view.getLogin().addActionListener(this::login);
}

View File

@ -12,11 +12,7 @@ import java.util.Optional;
*/
public class LoginData {
private final BankingManagementSystem bms;
public LoginData(BankingManagementSystem bms) {
this.bms = bms;
}
private BankingManagementSystem bms;
/**
* authenticate the specified account with the provided pin.
@ -53,4 +49,8 @@ public class LoginData {
public BankingManagementSystem getBms() {
return bms;
}
public void setBms(BankingManagementSystem bms) {
this.bms = bms;
}
}