package me.teridax.jcash.gui.account; import me.teridax.jcash.banking.accounts.CurrentAccount; import me.teridax.jcash.banking.accounts.SavingsAccount; import me.teridax.jcash.banking.management.Profile; import me.teridax.jcash.decode.StringDecoder; import javax.swing.*; import java.awt.*; import static javax.swing.SwingConstants.RIGHT; public class AccountView extends JPanel { private JTextField iban; private JTextField name; private JTextField address; private JTextField bankName; private JTextField blz; private JTextField type; private JTextField typeSpecialProperty; private JTextField balance; private JLabel typeSpecialLabel; private JComboBox accountSelection; private JButton logout; private JButton transfer; private JButton deposit; private JButton takeoff; public AccountView() { createComponents(); createLayout(); setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); } public void setProfile(Profile profile) { var bank = profile.getBank(); var account = profile.getPrimaryAccount(); var owner = profile.getOwner(); this.blz.setText(bank.getBlz()); this.bankName.setText(bank.getName()); this.iban.setText(String.valueOf(account.getIban())); this.name.setText(owner.getName() + " " + owner.getFamilyName()); this.address.setText(owner.getStreet() + " " + owner.getCity()); this.balance.setText(StringDecoder.LOCAL_NUMBER_FORMAT.format(account.getBalance()) + " €"); this.type.setText(account.getClass().getSimpleName()); if (account instanceof CurrentAccount) { this.typeSpecialLabel.setText("Overdraft"); this.typeSpecialProperty.setText( StringDecoder.LOCAL_NUMBER_FORMAT.format(((CurrentAccount) account).getOverdraft()) + " €"); } else if (account instanceof SavingsAccount) { this.typeSpecialLabel.setText("Interest rate"); this.typeSpecialProperty.setText( ((SavingsAccount) account).getInterest() + " %" ); } this.accountSelection.removeAllItems(); for (var otherAccount : profile.getAccounts()) { this.accountSelection.addItem(otherAccount.getDescription()); } this.accountSelection.setSelectedItem(account.getDescription()); } private void createLayout() { var content = new JPanel(new GridBagLayout()); this.setLayout(new BorderLayout(16, 16)); this.add(new JScrollPane(content), BorderLayout.CENTER); var constraints = new GridBagConstraints(); constraints.gridwidth = 4; constraints.insets = new Insets(12,12,12,12); var accountSelectionPanel = new JPanel(new BorderLayout(12, 0)); accountSelectionPanel.add(iban, BorderLayout.CENTER); accountSelectionPanel.add(accountSelection, BorderLayout.EAST); addInputRow(constraints, content, accountSelectionPanel, 1, new JLabel("IBAN", RIGHT)); addInputRow(constraints, content, name, 2, new JLabel("Name/Family-name", RIGHT)); addInputRow(constraints, content, address, 3, new JLabel("Address", RIGHT)); addInputRow(constraints, content, bankName, 4, new JLabel("Bank", RIGHT)); addInputRow(constraints, content, blz, 5, new JLabel("BLZ", RIGHT)); addInputRow(constraints, content, type, 6, new JLabel("Account", RIGHT)); addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel); addInputRow(constraints, content, balance, 8, new JLabel("Balance", RIGHT)); var buttonPanel = Box.createHorizontalBox(); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(logout); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(Box.createGlue()); buttonPanel.add(transfer); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(deposit); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(takeoff); add(buttonPanel, BorderLayout.SOUTH); } private void createComponents() { this.blz = new JTextField(); this.iban = new JTextField(); this.address = new JTextField(); this.bankName = new JTextField(); this.name = new JTextField(); this.type = new JTextField(); this.balance = new JTextField(); this.typeSpecialProperty = new JTextField(); this.blz.setEditable(false); this.iban.setEditable(false); this.address.setEditable(false); this.bankName.setEditable(false); this.name.setEditable(false); this.type.setEditable(false); this.balance.setEditable(false); this.typeSpecialProperty.setEditable(false); this.typeSpecialLabel = new JLabel("", RIGHT); this.accountSelection = new JComboBox<>(); this.logout = new JButton("Logout"); this.transfer = new JButton("Transfer"); this.deposit = new JButton("Deposit"); this.takeoff = new JButton("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; } public JButton getLogout() { return logout; } public JButton getTransfer() { return transfer; } public JButton getDeposit() { return deposit; } public JButton getTakeoff() { return takeoff; } }