From 04c18fe95ea7c18187e5608127bd2133de96178a Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 19 Jul 2023 19:25:38 +0200 Subject: [PATCH] added GUI utility class --- src/me/teridax/jcash/gui/Utils.java | 59 +++++++++++++++++++ .../jcash/gui/account/AccountView.java | 43 ++++---------- src/me/teridax/jcash/gui/login/LoginView.java | 25 ++------ 3 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 src/me/teridax/jcash/gui/Utils.java diff --git a/src/me/teridax/jcash/gui/Utils.java b/src/me/teridax/jcash/gui/Utils.java new file mode 100644 index 0000000..ff21f64 --- /dev/null +++ b/src/me/teridax/jcash/gui/Utils.java @@ -0,0 +1,59 @@ +package me.teridax.jcash.gui; + +import javax.swing.*; +import java.awt.*; + +public class Utils { + + public static String addHeading(String title) { + return String.format("%s", title); + } + + /** + * Add a new row of components to the specified target component. + * This will add label to the right side of the next row and the specified component to the left. + * @param constraints the constraint to use. Must be non-null + * @param target the base component to add a row to + * @param comp the component to add to the left side + * @param row the row to add the components to + * @param name the labels text to add to the left side + */ + public static void addGridBagRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, String name) { + constraints.gridwidth = 1; + constraints.gridx = 1; + constraints.gridy = row; + constraints.weightx = 0; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(new JLabel(name, SwingConstants.RIGHT), constraints); + + constraints.gridx = 2; + constraints.gridy = row; + constraints.weightx = 1; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(comp, constraints); + } + + /** + * Add a new row of components to the specified target component. + * This will add label to the right side of the next row and the specified component to the left. + * @param constraints the constraint to use. Must be non-null + * @param target the base component to add a row to + * @param comp the component to add to the left side + * @param row the row to add the components to + * @param right the component to place on the left side + */ + public static void addGridBagRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, Component right) { + constraints.gridwidth = 1; + constraints.gridx = 1; + constraints.gridy = row; + constraints.weightx = 0; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(right, constraints); + + constraints.gridx = 2; + constraints.gridy = row; + constraints.weightx = 1; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(comp, constraints); + } +} diff --git a/src/me/teridax/jcash/gui/account/AccountView.java b/src/me/teridax/jcash/gui/account/AccountView.java index 700480d..2f3f5d7 100644 --- a/src/me/teridax/jcash/gui/account/AccountView.java +++ b/src/me/teridax/jcash/gui/account/AccountView.java @@ -10,6 +10,7 @@ import javax.swing.*; import java.awt.*; import static javax.swing.SwingConstants.RIGHT; +import static me.teridax.jcash.gui.Utils.addGridBagRow; import static me.teridax.jcash.lang.Translator.translate; public class AccountView extends JPanel { @@ -73,7 +74,7 @@ public class AccountView extends JPanel { private void createLayout() { var content = new JPanel(new GridBagLayout()); - this.setLayout(new BorderLayout(16, 16)); + this.setLayout(new BorderLayout(12, 12)); this.add(new JScrollPane(content), BorderLayout.CENTER); var constraints = new GridBagConstraints(); @@ -85,14 +86,14 @@ public class AccountView extends JPanel { accountSelectionPanel.add(iban, BorderLayout.CENTER); accountSelectionPanel.add(accountSelection, BorderLayout.EAST); - addInputRow(constraints, content, accountSelectionPanel, 1, new JLabel(translate("IBAN"), RIGHT)); - addInputRow(constraints, content, name, 2, new JLabel(translate("Name/Family-name"), RIGHT)); - addInputRow(constraints, content, address, 3, new JLabel(translate("Address"), RIGHT)); - addInputRow(constraints, content, bankName, 4, new JLabel(translate("Bank"), RIGHT)); - addInputRow(constraints, content, blz, 5, new JLabel(translate("BLZ"), RIGHT)); - addInputRow(constraints, content, type, 6, new JLabel(translate("Account"), RIGHT)); - addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel); - addInputRow(constraints, content, balance, 8, new JLabel(translate("Balance"), RIGHT)); + addGridBagRow(constraints, content, accountSelectionPanel, 1, translate("IBAN")); + addGridBagRow(constraints, content, name, 2, translate("Name/Family-name")); + addGridBagRow(constraints, content, address, 3, translate("Address")); + addGridBagRow(constraints, content, bankName, 4, translate("Bank")); + addGridBagRow(constraints, content, blz, 5, translate("BLZ")); + addGridBagRow(constraints, content, type, 6, translate("Account")); + addGridBagRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel); + addGridBagRow(constraints, content, balance, 8, translate("Balance")); var buttonPanel = Box.createHorizontalBox(); buttonPanel.add(Box.createHorizontalStrut(4)); @@ -136,30 +137,6 @@ public class AccountView extends JPanel { this.takeoff = new JButton(translate("Takeoff")); } - /** - * Add a new row of components to the specified target component. - * This will add label to the right side of the next row and the specified component to the left. - * @param constraints the constraint to use. Must be non-null - * @param target the base component to add a row to - * @param comp the component to add to the left side - * @param row the row to add the components to - * @param label the label to add to the left side - */ - private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, JLabel label) { - constraints.gridwidth = 1; - constraints.gridx = 1; - constraints.gridy = row; - constraints.weightx = 0; - constraints.fill = GridBagConstraints.HORIZONTAL; - target.add(label, constraints); - - constraints.gridx = 2; - constraints.gridy = row; - constraints.weightx = 1; - constraints.fill = GridBagConstraints.HORIZONTAL; - target.add(comp, constraints); - } - public JComboBox getAccountSelection() { return accountSelection; } diff --git a/src/me/teridax/jcash/gui/login/LoginView.java b/src/me/teridax/jcash/gui/login/LoginView.java index 5b790e3..e159336 100644 --- a/src/me/teridax/jcash/gui/login/LoginView.java +++ b/src/me/teridax/jcash/gui/login/LoginView.java @@ -7,6 +7,8 @@ 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; public class LoginView extends JPanel { @@ -33,16 +35,16 @@ public class LoginView extends JPanel { this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); this.setLayout(new BorderLayout(16, 16)); this.add(new JScrollPane(content), BorderLayout.CENTER); - this.add(new JLabel("

" + translate("Cashmachine") + "", CENTER), NORTH); + this.add(new JLabel(addHeading(translate("Cashmachine")), CENTER), NORTH); var constraints = new GridBagConstraints(); constraints.gridwidth = 4; constraints.insets = new Insets(12, 12, 12, 12); - addInputRow(constraints, content, blz, 1, translate("BLZ")); - addInputRow(constraints, content, iban, 2, translate("IBAN")); - addInputRow(constraints, content, pin, 3, translate("PIN")); + addGridBagRow(constraints, content, blz, 1, translate("BLZ")); + addGridBagRow(constraints, content, iban, 2, translate("IBAN")); + addGridBagRow(constraints, content, pin, 3, translate("PIN")); constraints.gridy = 4; constraints.anchor = GridBagConstraints.PAGE_END; @@ -92,21 +94,6 @@ public class LoginView extends JPanel { return formatter; } - private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, String name) { - constraints.gridwidth = 1; - constraints.gridx = 1; - constraints.gridy = row; - constraints.weightx = 0; - constraints.fill = GridBagConstraints.HORIZONTAL; - target.add(new JLabel(name, SwingConstants.RIGHT), constraints); - - constraints.gridx = 2; - constraints.gridy = row; - constraints.weightx = 1; - constraints.fill = GridBagConstraints.HORIZONTAL; - target.add(comp, constraints); - } - public JTextField getBlz() { return blz; }