diff --git a/src/me/teridax/jcash/Main.java b/src/me/teridax/jcash/Main.java index 82358c1..850e4a1 100644 --- a/src/me/teridax/jcash/Main.java +++ b/src/me/teridax/jcash/Main.java @@ -53,6 +53,8 @@ public final class Main { } private void initialize() { + // create the login mask + this.loginMask = new LoginController(); // when we have logged in set the account viewer as window content @@ -64,6 +66,8 @@ public final class Main { this.window.getContentPane().add(loginMask.getView(), LOGIN_SCREEN_STRING_IDENT); + // create the account viewer + this.accountController = new AccountController(); this.window.getContentPane().add(accountController.getView(), PROFILE_SCREEN_STRING_IDENT); } @@ -72,7 +76,9 @@ public final class Main { this.bms = Loader.load(); this.loginMask.setBankingManagementSystem(bms); showLoginScreen(); - this.window.setSize(800, 600); + this.window.pack(); + this.window.setResizable(false); + this.window.setLocationRelativeTo(null); this.window.setVisible(true); } @@ -90,13 +96,16 @@ public final class Main { getInstance().loadDatabase(); } + /** + * Loads the extra font file used on the login button + */ private static void loadExtraFont() { try { - var font = Font.createFont(Font.TRUETYPE_FONT, IconProvider.class.getResourceAsStream("res/Circus.ttf")); + var font = Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(IconProvider.class.getResourceAsStream("res/Circus.ttf"))); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(font); - } catch (IOException | FontFormatException e) { - e.printStackTrace(); + } catch (IOException | FontFormatException | NullPointerException e) { + LOGGER.warning("Could not load font file: " + e.getMessage()); } } @@ -173,6 +182,7 @@ public final class Main { * Logs the user out of the database, hiding the main window. */ public void logout() { + this.loginMask.logout(); this.layout.show(this.window.getContentPane(), LOGIN_SCREEN_STRING_IDENT); } diff --git a/src/me/teridax/jcash/decode/StringDecoder.java b/src/me/teridax/jcash/decode/StringDecoder.java index dd53aeb..c3f83ff 100644 --- a/src/me/teridax/jcash/decode/StringDecoder.java +++ b/src/me/teridax/jcash/decode/StringDecoder.java @@ -29,6 +29,16 @@ public class StringDecoder { return formatter; } + public static Object getIntegerNumberFormatter() { + var formatter = new NumberFormatter(); + formatter.setValueClass(Integer.class); + formatter.setMinimum(0d); + formatter.setAllowsInvalid(true); + formatter.setCommitsOnValidEdit(true); + + return formatter; + } + /** * Attempts to convert the given string into a double value representing a percentage. * The output value will be in the range [0, 100]. Strings formatted without a percentage @@ -129,7 +139,7 @@ public class StringDecoder { var trimmed = name.trim(); - var pattern = Pattern.compile("[^\\s]+", Pattern.CASE_INSENSITIVE); + var pattern = Pattern.compile("[^\\d]+", Pattern.CASE_INSENSITIVE); var matcher = pattern.matcher(trimmed); if (matcher.find()) { return matcher.group(); diff --git a/src/me/teridax/jcash/gui/IconProvider.java b/src/me/teridax/jcash/gui/IconProvider.java index abece0e..7a99021 100644 --- a/src/me/teridax/jcash/gui/IconProvider.java +++ b/src/me/teridax/jcash/gui/IconProvider.java @@ -26,7 +26,7 @@ public class IconProvider { return DEFAULT_IMAGE; } - public static Image getSetupIcon() { - return loadIcon("res/login.jpg"); + public static Image getBackground() { + return loadIcon("res/background.png"); } } diff --git a/src/me/teridax/jcash/gui/NumberDocumentFilter.java b/src/me/teridax/jcash/gui/NumberDocumentFilter.java deleted file mode 100644 index 8a5188f..0000000 --- a/src/me/teridax/jcash/gui/NumberDocumentFilter.java +++ /dev/null @@ -1,17 +0,0 @@ -package me.teridax.jcash.gui; - -import javax.swing.text.AttributeSet; -import javax.swing.text.BadLocationException; -import javax.swing.text.DocumentFilter; -import javax.swing.text.NumberFormatter; - -public class NumberDocumentFilter extends DocumentFilter { - @Override - public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { - super.insertString(fb, offset, string, attr); - } - - private String filterString(String text) { - return ""; - } -} diff --git a/src/me/teridax/jcash/gui/Utils.java b/src/me/teridax/jcash/gui/Utils.java index 9e57fd9..ebe2e2e 100644 --- a/src/me/teridax/jcash/gui/Utils.java +++ b/src/me/teridax/jcash/gui/Utils.java @@ -32,6 +32,9 @@ public class Utils { constraints.fill = GridBagConstraints.HORIZONTAL; target.add(new JLabel(name, SwingConstants.RIGHT), constraints); + if (comp == null) + return; + constraints.gridx = 2; constraints.gridy = row; constraints.weightx = 1; diff --git a/src/me/teridax/jcash/gui/login/LoginController.java b/src/me/teridax/jcash/gui/login/LoginController.java index 4112713..dfa7c61 100644 --- a/src/me/teridax/jcash/gui/login/LoginController.java +++ b/src/me/teridax/jcash/gui/login/LoginController.java @@ -57,13 +57,13 @@ public class LoginController { var blz = this.view.getBlz().getText(); var iban = this.getIban(); if (iban.isEmpty()) { - Utils.error("no IBAN entered"); + Utils.error("invalid IBAN entered"); return; } var pin = this.getPin(); if (pin.isEmpty()) { - Utils.error("no PIN entered"); + Utils.error("invalid PIN entered"); return; } @@ -86,4 +86,8 @@ public class LoginController { public LoginData getData() { return data; } + + public void logout() { + this.view.getPin().setText(""); + } } diff --git a/src/me/teridax/jcash/gui/login/LoginView.java b/src/me/teridax/jcash/gui/login/LoginView.java index c8cfb90..0dcf11c 100644 --- a/src/me/teridax/jcash/gui/login/LoginView.java +++ b/src/me/teridax/jcash/gui/login/LoginView.java @@ -1,15 +1,12 @@ package me.teridax.jcash.gui.login; +import me.teridax.jcash.decode.StringDecoder; import me.teridax.jcash.gui.IconProvider; import javax.swing.*; import javax.swing.text.*; import java.awt.*; -import java.io.IOException; -import java.text.NumberFormat; -import static java.awt.BorderLayout.NORTH; -import static javax.swing.SwingConstants.CENTER; import static me.teridax.jcash.gui.Utils.addGridBagRow; import static me.teridax.jcash.gui.Utils.addHeading; import static me.teridax.jcash.lang.Translator.translate; @@ -33,41 +30,47 @@ public class LoginView extends JPanel { } private void layoutComponents() { - var content = new JPanel(new GridBagLayout()); + var content = new JLabel(); + content.setIcon(new ImageIcon(IconProvider.getBackground())); + content.setLayout(new BorderLayout()); - this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); - this.setLayout(new BorderLayout(16, 16)); + var loginPane = new JPanel(new GridBagLayout()); + loginPane.setOpaque(true); + content.add(loginPane, BorderLayout.CENTER); + content.add(Box.createHorizontalStrut(400), BorderLayout.WEST); + + this.setLayout(new BorderLayout(32, 32)); this.add(new JScrollPane(content), BorderLayout.CENTER); - this.add(new JLabel(addHeading(translate("Cashmachine")), CENTER), NORTH); var constraints = new GridBagConstraints(); constraints.gridwidth = 4; constraints.insets = new Insets(12, 12, 12, 12); - addGridBagRow(constraints, content, blz, 1, translate("BLZ")); - addGridBagRow(constraints, content, iban, 2, translate("IBAN")); - addGridBagRow(constraints, content, pin, 3, translate("PIN")); + addGridBagRow(constraints, loginPane, new JLabel(addHeading(translate("Cashmachine"))), 0, ""); + addGridBagRow(constraints, loginPane, blz, 1, translate("BLZ")); + addGridBagRow(constraints, loginPane, iban, 2, translate("IBAN")); + addGridBagRow(constraints, loginPane, pin, 3, translate("PIN")); constraints.gridy = 4; constraints.anchor = GridBagConstraints.PAGE_END; constraints.weightx = 0; constraints.fill = GridBagConstraints.NONE; - constraints.insets = new Insets(12, 12, 12, 12); - content.add(login, constraints); + constraints.insets = new Insets(0, 0, 0, 12); + loginPane.add(login, constraints); } private void createComponents() { this.blz = new JFormattedTextField(); - this.iban = new JFormattedTextField(getNumberFormat()); + this.iban = new JFormattedTextField(); this.pin = new JPasswordField(); this.login = new JButton(translate("Login")); - this.login.setFont(new Font("Circus", Font.PLAIN, 72)); - this.login.setBackground(Color.RED); - this.login.setForeground(Color.CYAN); + this.login.setFont(new Font("Circus", Font.PLAIN, 28)); + this.login.setBackground(Color.CYAN); restrictPasswordToDigits(); + restrictIbanInput(); } /** @@ -88,17 +91,22 @@ public class LoginView extends JPanel { }); } - private NumberFormatter getNumberFormat() { - var format = NumberFormat.getIntegerInstance(); - format.setGroupingUsed(false); + /** + * Adds a document filter onto {@link #iban} that filters out everything that is not a digit. + * The filter also restricts the amount of digits that can be entered to {@link #MAX_PIN_DECIMAL_DIGITS} + */ + private void restrictIbanInput() { + ((AbstractDocument) this.iban.getDocument()).setDocumentFilter(new DocumentFilter() { + @Override + public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) + throws BadLocationException { + String newText = fb.getDocument().getText(0, fb.getDocument().getLength()) + text; - var formatter = new NumberFormatter(format); - formatter.setValueClass(Integer.class); - formatter.setMinimum(0); - formatter.setMaximum(Integer.MAX_VALUE); - formatter.setAllowsInvalid(false); - - return formatter; + if (newText.matches(String.format("\\d{1,%s}", MAX_PIN_DECIMAL_DIGITS))) { + super.replace(fb, offset, length, text, attrs); + } + } + }); } public JTextField getBlz() { diff --git a/src/me/teridax/jcash/gui/res/background.png b/src/me/teridax/jcash/gui/res/background.png new file mode 100644 index 0000000..bae8c57 Binary files /dev/null and b/src/me/teridax/jcash/gui/res/background.png differ diff --git a/src/me/teridax/jcash/gui/res/image-sources.md b/src/me/teridax/jcash/gui/res/image-sources.md index 62148ed..ea1d7ba 100644 --- a/src/me/teridax/jcash/gui/res/image-sources.md +++ b/src/me/teridax/jcash/gui/res/image-sources.md @@ -1,2 +1,4 @@ https://pixabay.com/vectors/register-cash-register-modern-23666/ -![register](https://cdn.pixabay.com/photo/2012/04/01/17/34/register-23666_960_720.png) \ No newline at end of file +![register](https://cdn.pixabay.com/photo/2012/04/01/17/34/register-23666_960_720.png) +Font file +https://www.dafont.com/de/circus.font?text=Login \ No newline at end of file diff --git a/src/me/teridax/jcash/gui/res/login.jpg b/src/me/teridax/jcash/gui/res/login.jpg deleted file mode 100644 index 3f2bec1..0000000 Binary files a/src/me/teridax/jcash/gui/res/login.jpg and /dev/null differ