added GUI utility class

This commit is contained in:
Sven Vogel 2023-07-19 19:25:38 +02:00
parent 7fdb408900
commit 04c18fe95e
3 changed files with 75 additions and 52 deletions

View File

@ -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("<html><title>%s</title></html>", 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);
}
}

View File

@ -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<String> getAccountSelection() {
return accountSelection;
}

View File

@ -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("<html><h1>" + translate("Cashmachine") + "</b></html>", 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;
}