KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.prevayler.demos.demo2.gui;
2
3 import org.prevayler.Prevayler;
4 import org.prevayler.demos.demo2.business.*;
5 import org.prevayler.demos.demo2.business.transactions.*;
6 import javax.swing.*;
7 import javax.swing.event.*;
8 import java.awt.*;
9 import java.awt.event.*;
10
11 class AllAccountsFrame extends JInternalFrame implements BankListener, AccountListener {
12
13     private final Prevayler _prevayler;
14     private final JList accountList;
15
16     AllAccountsFrame(Prevayler prevayler, Container container) {
17         super("All Accounts", true); //true means resizable.
18
_prevayler = prevayler;
19         
20         accountList = new JList();
21         accountList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
22         bank().setBankListener(this);
23         listenToAccounts();
24         refreshAccounts();
25         
26         container.add(this);
27             getContentPane().add(new JScrollPane(accountList), BorderLayout.CENTER);
28         getContentPane().add(accountButtons(), BorderLayout.SOUTH);
29
30         setBounds(10,10,330,240);
31         show();
32     }
33   
34     private void listenToAccounts() {
35         java.util.Iterator JavaDoc it = accounts().iterator();
36         while (it.hasNext()) {
37             ((Account)it.next()).addAccountListener(this);
38         }
39     }
40
41     private void refreshAccounts() {
42         accountList.setListData(accounts().toArray());
43     }
44     
45     private java.util.List JavaDoc accounts() {
46         return bank().accounts();
47     }
48     
49     private Bank bank() {
50         return (Bank)_prevayler.prevalentSystem();
51     }
52     
53     public void accountCreated(Account a) { //Implements BankListener.
54
a.addAccountListener(this);
55         refreshAccounts();
56     }
57
58     public void accountDeleted(Account a) { //Implements BankListener.
59
a.removeAccountListener(this);
60         refreshAccounts();
61     }
62
63     public void accountChanged() { //Implements AccountListener.
64
refreshAccounts();
65     }
66   
67     private JPanel accountButtons() {
68         JPanel result = new JPanel();
69    
70         result.add(new JButton(new AccountCreation()));
71         result.add(new JButton(new AccountEditAction()));
72         result.add(new JButton(new AccountDeleteAction()));
73     
74         return result;
75     }
76
77     class AccountCreation extends AbstractAction {
78         
79         AccountCreation() {
80             super("Create");
81         }
82
83         public void actionPerformed(ActionEvent e) {
84             new NewAccountFrame(_prevayler, getDesktopPane());
85         }
86         
87     }
88     
89     abstract class SelectedAccountAction extends RobustAction implements ListSelectionListener {
90
91         SelectedAccountAction(String JavaDoc name) {
92             super(name);
93             refreshEnabled();
94             accountList.addListSelectionListener(this);
95         }
96         
97         private void refreshEnabled() {
98             this.setEnabled(accountList.getSelectedValue() != null);
99         }
100
101         public void valueChanged(ListSelectionEvent event) {
102             refreshEnabled();
103         }
104
105         protected void action() throws Exception JavaDoc {
106             action((Account)accountList.getSelectedValue());
107         }
108
109         abstract void action(Account account) throws Exception JavaDoc;
110     }
111
112     class AccountEditAction extends SelectedAccountAction {
113     
114         AccountEditAction() {
115             super("Edit");
116         }
117         
118         void action(Account account) {
119             new AccountEditFrame(account, _prevayler, getDesktopPane());
120         }
121     }
122
123     class AccountDeleteAction extends SelectedAccountAction {
124     
125         AccountDeleteAction() {
126             super("Delete");
127         }
128         
129         void action(Account account) throws Exception JavaDoc {
130             int option = JOptionPane.showConfirmDialog(null, "Delete selected account?", "Account Deletion", JOptionPane.YES_NO_OPTION);
131             if (option != JOptionPane.YES_OPTION) return;
132             
133             _prevayler.execute(new AccountDeletion(account));
134         }
135             
136     }
137
138 }
Popular Tags