119 lines
3.4 KiB
Java
119 lines
3.4 KiB
Java
package me.teridax.jcash.gui.takeoff;
|
|
|
|
import me.teridax.jcash.banking.Account;
|
|
import me.teridax.jcash.decode.StringUtils;
|
|
|
|
import javax.swing.*;
|
|
import javax.swing.text.NumberFormatter;
|
|
import java.awt.*;
|
|
import java.text.NumberFormat;
|
|
import java.text.ParseException;
|
|
import java.util.Locale;
|
|
|
|
import static javax.swing.JOptionPane.ERROR_MESSAGE;
|
|
import static javax.swing.JOptionPane.showMessageDialog;
|
|
|
|
public class TakeoffView {
|
|
|
|
private JDialog dialog;
|
|
private JButton cancel;
|
|
private JButton deposit;
|
|
private JFormattedTextField value;
|
|
|
|
public TakeoffView(Account account) {
|
|
createComponents(account);
|
|
layoutComponents();
|
|
}
|
|
|
|
public void showDialog() {
|
|
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
|
|
dialog.setTitle("Deposit money");
|
|
dialog.pack();
|
|
dialog.setResizable(false);
|
|
dialog.setLocationRelativeTo(null);
|
|
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
dialog.setVisible(true);
|
|
}
|
|
|
|
private void layoutComponents() {
|
|
dialog.getContentPane().setLayout(new GridBagLayout());
|
|
|
|
var c = new GridBagConstraints();
|
|
c.gridx = 0;
|
|
c.gridy = 0;
|
|
c.weightx = 1;
|
|
c.weighty = 1;
|
|
c.fill = GridBagConstraints.BOTH;
|
|
c.anchor = GridBagConstraints.CENTER;
|
|
c.insets = new Insets(10, 10, 10, 10);
|
|
dialog.getContentPane().add(new JLabel("Takeoff money"), c);
|
|
|
|
c.gridx = 0;
|
|
c.gridy = 1;
|
|
c.fill = GridBagConstraints.NONE;
|
|
c.anchor = GridBagConstraints.LAST_LINE_END;
|
|
c.insets = new Insets(10, 10, 10, 10);
|
|
c.weightx = 0;
|
|
dialog.getContentPane().add(new JLabel("Betrag", SwingConstants.RIGHT), c);
|
|
|
|
c.gridx = 1;
|
|
c.gridy = 1;
|
|
c.fill = GridBagConstraints.HORIZONTAL;
|
|
c.anchor = GridBagConstraints.LAST_LINE_END;
|
|
c.insets = new Insets(10, 10, 10, 10);
|
|
c.weightx = 1;
|
|
dialog.getContentPane().add(value, c);
|
|
|
|
c.gridx = 1;
|
|
c.gridy = 2;
|
|
c.fill = GridBagConstraints.NONE;
|
|
c.anchor = GridBagConstraints.LAST_LINE_END;
|
|
c.insets = new Insets(10, 10, 10, 10);
|
|
c.weightx = 0;
|
|
dialog.getContentPane().add(cancel, c);
|
|
|
|
c.gridx = 2;
|
|
c.gridy = 2;
|
|
c.fill = GridBagConstraints.NONE;
|
|
c.anchor = GridBagConstraints.LAST_LINE_END;
|
|
c.insets = new Insets(10, 10, 10, 10);
|
|
c.weightx = 0;
|
|
dialog.getContentPane().add(deposit, c);
|
|
}
|
|
|
|
private void createComponents(Account account) {
|
|
this.dialog = new JDialog();
|
|
|
|
this.cancel = new JButton("Abbrechen");
|
|
this.deposit = new JButton("Auszahlen");
|
|
this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT);
|
|
|
|
this.dialog.setContentPane(new JPanel(new GridBagLayout()));
|
|
}
|
|
|
|
public double getAmount() {
|
|
if (value.getText().isBlank()) {
|
|
showMessageDialog(null, "invalid amount", "currency must not be blank", ERROR_MESSAGE);
|
|
return 0;
|
|
}
|
|
|
|
try {
|
|
return NumberFormat.getNumberInstance().parse(value.getText()).doubleValue();
|
|
} catch (ParseException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public JButton getCancel() {
|
|
return cancel;
|
|
}
|
|
|
|
public JButton getDeposit() {
|
|
return deposit;
|
|
}
|
|
|
|
public void dispose() {
|
|
this.dialog.dispose();
|
|
}
|
|
}
|