KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > demos > demo2 > gui > AccountEditFrame


1 package org.prevayler.demos.demo2.gui;
2
3 import java.awt.Container JavaDoc;
4 import java.awt.event.FocusAdapter JavaDoc;
5 import java.awt.event.FocusEvent JavaDoc;
6
7 import javax.swing.Box JavaDoc;
8 import javax.swing.JButton JavaDoc;
9 import javax.swing.JList JavaDoc;
10 import javax.swing.JOptionPane JavaDoc;
11 import javax.swing.JPanel JavaDoc;
12 import javax.swing.JScrollPane JavaDoc;
13 import javax.swing.JTextField JavaDoc;
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 JavaDoc balanceField;
26     private JList JavaDoc historyList;
27     
28     AccountEditFrame(Account account, Prevayler prevayler, Container JavaDoc 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 JavaDoc fieldBox) {
41         super.addFields(fieldBox);
42         
43         fieldBox.add(gap());
44         fieldBox.add(labelContainer("Transaction History"));
45         historyList = new JList JavaDoc();
46         historyList.setEnabled(false);
47         fieldBox.add(new JScrollPane JavaDoc(historyList));
48         
49         fieldBox.add(gap());
50         fieldBox.add(labelContainer("Balance"));
51         balanceField = new JTextField JavaDoc();
52         balanceField.setEnabled(false);
53         fieldBox.add(balanceField);
54     }
55
56     protected void addButtons(JPanel JavaDoc buttonPanel) {
57         buttonPanel.add(new JButton JavaDoc(new DepositAction()));
58         buttonPanel.add(new JButton JavaDoc(new WithdrawAction()));
59         buttonPanel.add(new JButton JavaDoc(new TransferAction()));
60     }
61     
62     private class DepositAction extends RobustAction {
63         
64         DepositAction() {
65             super("Deposit...");
66         }
67
68         public void action() throws Exception JavaDoc {
69             Number JavaDoc 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 JavaDoc {
82             Number JavaDoc amount = enterAmount("Withdrawal");
83             if (amount == null) return;
84             _prevayler.execute(new Withdrawal(account, amount.longValue()));
85         }
86     }
87
88     private Number JavaDoc enterAmount(String JavaDoc operation) throws Exception JavaDoc {
89         String JavaDoc amountText = JOptionPane.showInputDialog(null,"Enter amount",operation,JOptionPane.PLAIN_MESSAGE);
90         if (amountText == null) return null;
91         return new Long JavaDoc(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() { //Implements AccountListener.
106
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 JavaDoc {
112         public void focusLost(FocusEvent JavaDoc e) {
113             if (holderText().equals(account.holder())) return;
114             try {
115                 _prevayler.execute(new HolderChange(account, holderText()));
116             } catch (Exception JavaDoc exception) {
117                 RobustAction.display(exception);
118             }
119         }
120     }
121 }
122
Popular Tags