FileDocCategorySizeDatePackage
AccountSetPanel.javaAPI DocExample6633Sun Mar 02 22:01:02 GMT 1997bank.client

AccountSetPanel.java

/**
 * A Java panel with a CardLayout that allows the user
 * to flip through accounts.
 */
package bank.client;

import bank.server.RemoteAccount;
import bank.server.RemoteAccountSet;
import imaginary.gui.PersistentSetPanel;
import imaginary.persist.RemoteLockHolder;
import imaginary.persist.RemotePersistent;
import imaginary.persist.RemotePersistentSet;
import imaginary.util.RemoteObservable;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

public class AccountSetPanel extends PersistentSetPanel
implements ActionListener, RemotePanel {
    // index of the currently displayed account panel
    private int            current_account = -1;
    // the button to navigate to the next panel
    private Button         next_button     = null;
    // the list of account panels being displayed by this set panel
    private AccountPanel[] panels          = null;
    // the button to navigate to the previous panel
    private Button         previous_button = null;

    /********************** Constructors ************************/
    /**
     * Creates a new AccountSetPanel.  The buttons array are the next
     * and previous buttons placed on the customer panel.  Visually,
     * they fit better on the customer panel.  Process-wise, however,
     * this panel is interested in their events.
     * @param buttons the next and previous buttons in that order
     * @param h the lock holder for any modifications
     */
    public AccountSetPanel(Button[] buttons, RemoteLockHolder h)
    throws RemoteException {
        super(h);
        next_button = buttons[0];
        previous_button = buttons[1];
        createComponents();
    }

    /**
     * Constructs an AccountSetPanel observing the specified set.
     * @param buttons the next and previous buttons in that order
     * @param h the lock holder for any modifications
     * @param set the set being observed
     */
    public AccountSetPanel(Button[] buttons, RemoteLockHolder h,
                           RemotePersistentSet set)
    throws RemoteException {
        super(h, set);
        next_button = buttons[0];
        previous_button = buttons[1];
        createComponents();
    }

    /**
     * @return the account currently being viewed
     */
    public RemoteAccount getSelectedAccount() {
        AccountPanel acct_panel = panels[current_account-1];

        return (RemoteAccount)acct_panel.getObserved();
    }

    /**
     * Places the individual widgets on the panel.
     * @exception java.rmi.RemoteException if the observed set is out of reach
     */
    protected void createComponents() throws RemoteException {
        RemoteAccountSet set = (RemoteAccountSet)getObserved();
        RemotePersistent[] accounts;

        // CardLayout allows us to view one account panel at a time
        setLayout(new CardLayout(0,0));
        // Don't draw anything if no set is being observed
        if( set == null ) {
            return;
        }
        accounts = set.getPersistents();
        current_account = 1;
        panels = new AccountPanel[accounts.length];
        // For each observed account, add an account panel
        for(int i=0; i<accounts.length; i++) {
            panels[i] = new AccountPanel(getLockHolder(),
                                         (RemoteAccount)accounts[i]);
            
            panels[i].reshape(insets().left + 24,insets().top + 132,384,120);
            add("" + i, panels[i]);
        }
        previous_button.addActionListener(this);
        previous_button.enable(false);
        next_button.addActionListener(this);
        if( panels.length < 2 ) {
            next_button.enable(false);
        }
    }

    /****************** Displaying data and cleaning up *************/
    /**
     * Implementation of the ChangeObserver method for handling changes
     * in observed objects.
     */
    public void observeChange() {
        // Since this application does not support the addition
        // or deletion of accounts, it will not actually
        // observe any changes.  If it did, however, you would use
        // this method to add new panels or remove deleted ones
    }

    /**
     * When the window gets destroyed or this panel is removed,
     * cleanUp() gets called to allow the panel to remove itself
     * from the observer list of its set.
     */
    protected void cleanUp() {
        RemoteObservable o = getObserved();

        if( o != null ) {
            try {
                o.deleteObserver(this);
            }
            catch( RemoteException e ) {
            }
        }
        if( panels != null ) {
            for(int i=0; i<panels.length; i++) {
                panels[i].cleanUp();
            }
        }
    }

    /********************* Event handlers ********************/
    /**
     * This method is new to JDK 1.1.  It handles action events for
     * components this class has expressed interest in.  In the case
     * of the account set panel, we want to know when one of the buttons
     * are clicked.
     * @param event the triggered event
     */
    public void actionPerformed(ActionEvent event) {
        Button b = (Button)event.getSource();

        // the user clicked next
        if( b == next_button ) {
            // flip to the next account in the card layout
            ((CardLayout)getLayout()).next(this);
            // up the marker
            current_account++;
            // if this is the last account, disable the next button
            if( current_account == panels.length ) {
                next_button.enable(false);
            }
            // if this is not the first account, enable the previous button
            if( current_account > 1 ) {
                previous_button.enable(true);
            }
        }
        else {
            // flip to the previous account in the card layout
            ((CardLayout)getLayout()).previous(this);
            // decrement the marker
            current_account--;
            // if this is the first account, disable the previous button
            if( current_account < 2 ) {
                previous_button.enable(false);
            }
            // if this is not the last account, enable the next button
            if( current_account < panels.length ) {
                next_button.enable(true);
            }
        }           
    }
}