JCash/src/me/teridax/jcash/Main.java

117 lines
4.0 KiB
Java

package me.teridax.jcash;
import me.teridax.jcash.gui.Loader;
import me.teridax.jcash.gui.account.AccountController;
import me.teridax.jcash.gui.login.LoginController;
import me.teridax.jcash.lang.Locales;
import javax.swing.*;
import java.util.Objects;
import java.util.logging.*;
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 {
/**
* Main instance of this program. Contains the primary window.
*/
private static Main instance;
/**
* Primary window of this program
*/
private final JFrame window;
private Main() {
// create main window and set defaults
this.window = new JFrame();
this.window.setTitle(translate("Cashmachine"));
this.window.setLocationByPlatform(true);
this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
initializeSystemLogger(Level.FINE);
Locales.autodetectDefaultLocale();
// create main instance and show the login screen
instance();
getInstance().showLoginScreen();
}
/**
* Get the main singleton instance of this program
* @return the singleton instance of this class
*/
public static Main getInstance() {
return instance;
}
/**
* Attempts to create a new instance of the singleton class Main.
* This method throws an exception if the class is already instantiated.
* @throws IllegalStateException if the class is already instantiated
* @pre the static instance of this class should be null.
* @post the static instance of this class will be set
*/
public static void instance() {
if (null != instance)
throw new IllegalStateException(Main.class.getName() + " is already initialized");
LOGGER.fine("Creating singleton instance of class " + Main.class.getName());
Main.instance = new Main();
}
/**
* Shows the open dialog for selecting a database file. After selection, it then proceeds to prompt login.
* Afterward the selected account can be managed.
* 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);
}
});
}
/**
* 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);
}
}