1 package org.prevayler.demos.demo2.gui; 2 3 import java.awt.Container ; 4 import java.awt.event.FocusAdapter ; 5 import java.awt.event.FocusEvent ; 6 7 import javax.swing.Box ; 8 import javax.swing.JButton ; 9 import javax.swing.JList ; 10 import javax.swing.JOptionPane ; 11 import javax.swing.JPanel ; 12 import javax.swing.JScrollPane ; 13 import javax.swing.JTextField ; 14 15 import org.prevayler.Prevayler; 16 import org.prevayler.demos.demo2.business.Account; 17 import org.prevayler.demos.demo2.business.AccountListener; 18 import org.prevayler.demos.demo2.business.transactions.Deposit; 19 import org.prevayler.demos.demo2.business.transactions.HolderChange; 20 import org.prevayler.demos.demo2.business.transactions.Withdrawal; 21 22 class AccountEditFrame extends AccountFrame implements AccountListener { 23 24 private final Account account; 25 private JTextField balanceField; 26 private JList historyList; 27 28 AccountEditFrame(Account account, Prevayler prevayler, Container container) { 29 super("Account " + account.numberString(), prevayler, container); 30 31 this.account = account; 32 account.addAccountListener(this); 33 accountChanged(); 34 35 holderField.addFocusListener(new HolderListener()); 36 37 setBounds(50,50,306,300); 38 } 39 40 protected void addFields(Box fieldBox) { 41 super.addFields(fieldBox); 42 43 fieldBox.add(gap()); 44 fieldBox.add(labelContainer("Transaction History")); 45 historyList = new JList (); 46 historyList.setEnabled(false); 47 fieldBox.add(new JScrollPane (historyList)); 48 49 fieldBox.add(gap()); 50 fieldBox.add(labelContainer("Balance")); 51 balanceField = new JTextField (); 52 balanceField.setEnabled(false); 53 fieldBox.add(balanceField); 54 } 55 56 protected void addButtons(JPanel buttonPanel) { 57 buttonPanel.add(new JButton (new DepositAction())); 58 buttonPanel.add(new JButton (new WithdrawAction())); 59 buttonPanel.add(new JButton (new TransferAction())); 60 } 61 62 private class DepositAction extends RobustAction { 63 64 DepositAction() { 65 super("Deposit..."); 66 } 67 68 public void action() throws Exception { 69 Number amount = enterAmount("Deposit"); 70 if (amount == null) return; 71 _prevayler.execute(new Deposit(account, amount.longValue())); 72 } 73 } 74 75 private class WithdrawAction extends RobustAction { 76 77 WithdrawAction() { 78 super("Withdraw..."); 79 } 80 81 public void action() throws Exception { 82 Number amount = enterAmount("Withdrawal"); 83 if (amount == null) return; 84 _prevayler.execute(new Withdrawal(account, amount.longValue())); 85 } 86 } 87 88 private Number enterAmount(String operation) throws Exception { 89 String amountText = JOptionPane.showInputDialog(null,"Enter amount",operation,JOptionPane.PLAIN_MESSAGE); 90 if (amountText == null) return null; 91 return new Long (parse(amountText)); 92 } 93 94 private class TransferAction extends RobustAction { 95 96 TransferAction() { 97 super("Transfer..."); 98 } 99 100 public void action() { 101 new TransferFrame(account, _prevayler, getDesktopPane()); 102 } 103 } 104 105 public void accountChanged() { holderField.setText(account.holder()); 107 historyList.setListData(account.transactionHistory().toArray()); 108 balanceField.setText(String.valueOf(account.balance())); 109 } 110 111 private class HolderListener extends FocusAdapter { 112 public void focusLost(FocusEvent e) { 113 if (holderText().equals(account.holder())) return; 114 try { 115 _prevayler.execute(new HolderChange(account, holderText())); 116 } catch (Exception exception) { 117 RobustAction.display(exception); 118 } 119 } 120 } 121 } 122 | Popular Tags |