FileDocCategorySizeDatePackage
AccountPanel.javaAPI DocExample5009Fri Jun 27 17:33:12 BST 1997bank.client

AccountPanel.java

/**
 * An AccountPanel displays information for a single bank account.
 */
package bank.client;

import bank.server.RemoteAccount;
import bank.server.RemoteCustomer;
import imaginary.gui.PersistentPanel;
import imaginary.persist.RemoteLockHolder;
import imaginary.util.RemoteObservable;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Label;
import java.awt.TextField;
import java.rmi.RemoteException;
import java.text.NumberFormat;
import java.util.Locale;

public class AccountPanel extends PersistentPanel
implements RemotePanel {
    private TextField     balance_field = null;
    private Label         balance_label = null;
    private CheckboxGroup cb_group      = null;
    private boolean       changed       = false;
    private Checkbox      checking_cb   = null;
    private TextField     id_field      = null;
    private Label         id_label      = null;
    private Checkbox      savings_cb    = null;

    /*********************** Constructors *************************/
    /**
     * Constructs a new AccountPanel associated with a specific
     * RemoteLockHolder.
     * @param h the RemoteLockHolder associated with this panel's edits
     */
    public AccountPanel(RemoteLockHolder h) throws RemoteException {
        super(h);
        createComponents();
    }

    /**
     * Constructs a new AccountPanel for the specified account.
     * @param h the RemoteLockHolder associated with this panel's edits
     * @param a the bank account to display
     */
    public AccountPanel(RemoteLockHolder h, RemoteAccount a)
    throws RemoteException {
        super(h, a);
        createComponents();
    }

    /**
     * Puts the panel's widgets on the panel
     */
    public void createComponents() throws RemoteException {
        setLayout(null);
        { // check boxes
            cb_group = new CheckboxGroup();
            checking_cb = new Checkbox("Checking", cb_group, false);
            checking_cb.enable(false);
            checking_cb.reshape(12,12,84,24);
            add(checking_cb);
            savings_cb = new Checkbox("Savings", cb_group, false);
            savings_cb.enable(false);
            savings_cb.reshape(12,48,84,24);
            add(savings_cb);
        }
        { // ID
            id_label = new Label("Account ID:",Label.RIGHT);
            id_label.reshape(96,12,88,24);
            add(id_label);
            id_field = new TextField();
            id_field.setEditable(false);
            id_field.reshape(192,12,124,28);
            add(id_field);
        }
        { // balance
            balance_label = new Label("Balance:",Label.RIGHT);
            balance_label.reshape(108,48,76,24);
            add(balance_label);
            balance_field = new TextField();
            balance_field.setEditable(false);
            balance_field.reshape(192,48,124,24);
            add(balance_field);
        }
        refresh();
    }

    /****************** Displaying data and cleaning up *************/
    /**
     * This implements the ChangeObserver method inherited from
     * PersistentPanel that handles responding to changes in
     * the observed account.
     */
    public void observeChange() {
        refresh();
    }

    /**
     * Displays current account data on the screen.
     */
    public void refresh() {
        RemoteObservable o = getObserved();

        if( o == null ) {
            id_field.setText("");
            balance_field.setText("");
            checking_cb.setState(true);
            savings_cb.setState(false);
        }
        else {
            RemoteAccount a = (RemoteAccount)o;

            try {
                NumberFormat f = NumberFormat.getCurrencyInstance();
                
                id_field.setText("" + a.getId());
                balance_field.setText(f.format(a.getBalance()));
                if( a.getAccountType().equals("C") ) {
                    checking_cb.setState(true);
                    savings_cb.setState(false);
                }
                else {
                    checking_cb.setState(false);
                    savings_cb.setState(true);
                }
            }
            catch( RemoteException e ) {
                e.printStackTrace();
                id_field.setText("");
                balance_field.setText("");
                checking_cb.setState(true);
                savings_cb.setState(false);
            }
        }
    }

    /**
     * When the window containing this is closed or the panel is
     * removed, it should call cleanUp() to clean up any observer
     * relationships.
     */
    protected void cleanUp() {
        RemoteObservable o = getObserved();

        if( o != null ) {
            try {
                o.deleteObserver(this);
            }
            catch( RemoteException e ) {
            }
        }
    }
}