From d0557a2a6dccb4d60c16fa637cc40eb14c5fd9f4 Mon Sep 17 00:00:00 2001 From: teridax Date: Sat, 22 Jul 2023 14:31:00 +0200 Subject: [PATCH] logout button now brings one back to the login screen --- src/me/teridax/jcash/Main.java | 78 ++++++++++--------- .../jcash/gui/account/AccountController.java | 19 +++-- .../jcash/gui/account/AccountData.java | 10 +-- .../jcash/gui/login/LoginController.java | 8 +- src/me/teridax/jcash/gui/login/LoginData.java | 10 +-- 5 files changed, 70 insertions(+), 55 deletions(-) diff --git a/src/me/teridax/jcash/Main.java b/src/me/teridax/jcash/Main.java index 85f3685..dfef7c1 100644 --- a/src/me/teridax/jcash/Main.java +++ b/src/me/teridax/jcash/Main.java @@ -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() { diff --git a/src/me/teridax/jcash/gui/account/AccountController.java b/src/me/teridax/jcash/gui/account/AccountController.java index 330358e..2a629f3 100644 --- a/src/me/teridax/jcash/gui/account/AccountController.java +++ b/src/me/teridax/jcash/gui/account/AccountController.java @@ -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()); diff --git a/src/me/teridax/jcash/gui/account/AccountData.java b/src/me/teridax/jcash/gui/account/AccountData.java index 8a8cde1..f936082 100644 --- a/src/me/teridax/jcash/gui/account/AccountData.java +++ b/src/me/teridax/jcash/gui/account/AccountData.java @@ -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; + } } diff --git a/src/me/teridax/jcash/gui/login/LoginController.java b/src/me/teridax/jcash/gui/login/LoginController.java index 9a90a09..4112713 100644 --- a/src/me/teridax/jcash/gui/login/LoginController.java +++ b/src/me/teridax/jcash/gui/login/LoginController.java @@ -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); } diff --git a/src/me/teridax/jcash/gui/login/LoginData.java b/src/me/teridax/jcash/gui/login/LoginData.java index 3c937c8..32d02d0 100644 --- a/src/me/teridax/jcash/gui/login/LoginData.java +++ b/src/me/teridax/jcash/gui/login/LoginData.java @@ -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; + } }