FileDocCategorySizeDatePackage
BankClient.javaAPI DocExample9651Wed Feb 05 02:30:36 GMT 1997bank.client

BankClient

public class BankClient extends Frame implements WindowListener, ActionListener, Runnable, imaginary.persist.RemoteLockHolder
BankClient is the main class for the client application. It is invoked by the command line:
java bank.client.BankClient APP_SERVER_ADDR
For example:
java bank.client.BankClient byzantium.imaginary.com
This class is a subclass of Frame. At the start of the application, it creates an instance of itself and shows itself, thus starting the application.

Fields Summary
private static String
host
private CustomerPanel
customer_panel
private Menu
account_menu
private Menu
help_menu
private imaginary.persist.RemoteLock
lock
private MenuBar
menu_bar
private bank.server.RemoteAppServer
server
Constructors Summary
public BankClient()
Creates a new instance of this frame object. Its first task is to export itself as a remote object. Before it starts to paint itself, it creates a thread in which it connects to the application server. This prevents any network lag from slowing the painting of the screen.

exception
java.rmi.RemoteException thrown if the export fails


                                                                   
        
        super();
        UnicastRemoteObject.exportObject(this);
        { // Connect to the server in another thread
            Thread t = new Thread(this);

            t.start();
        }
        setTitle("First Imaginary Bank of Java");
        addNotify();
        resize(insets().left + insets().right + 450,insets().top +
               insets().bottom + 256);
        setBackground(new Color(12632256));
        { // Menus
            MenuItem item;
            
            menu_bar = new MenuBar();
            account_menu = new Menu("Account");
            item = new MenuItem("Open...");
            item.addActionListener(this);
            account_menu.add(item);
            item = new MenuItem("Transfer...");
            item.addActionListener(this);
            account_menu.add(item);
            account_menu.addSeparator();
            item = new MenuItem("Exit");
            item.addActionListener(this);
            account_menu.add(item);
            menu_bar.add(account_menu);
            
            help_menu = new Menu("Help");
            menu_bar.setHelpMenu(help_menu);
            item = new MenuItem("About");
            item.addActionListener(this);
            help_menu.add(item);
            menu_bar.add(help_menu);
            setMenuBar(menu_bar);
        }
        customer_panel = new CustomerPanel(this);
        add(customer_panel);
        addWindowListener(this);
        customer_panel.show();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent event)
JDK 1.1 AWT method for performing ActionListener related functionality. Specifically, this means responding to menu clicks and button clicks.

param
event the triggered event

        MenuItem m = (MenuItem)event.getSource();
        String l = m.getLabel();
        
        if( l.equalsIgnoreCase("Open...") ) {
            // Opens a dialog box that prompts the user for a
            // customer ID to open
            OpenAccountDialog d = new OpenAccountDialog(this, true);
            
            d.show();
        }
        else if( l.equalsIgnoreCase("Transfer...") ) {
            // Opens a dialog box that allowed a user to
            // specify an amount and target account for
            // money transfers
            RemoteAccount acct;
            TransferDialog d;
            
            acct = customer_panel.getSelectedAccount();
            d = new TransferDialog(this, true, acct);
            d.show();
        }
        else if( l.equalsIgnoreCase("About") ) {
            // Shows a simple about dialog
            AboutDialog d = new AboutDialog(this, true);
            
            d.show();
        }
        else if( l.equalsIgnoreCase("Exit") ) {
            // Confirms the quit for the user
            QuitDialog d = new QuitDialog(this, true);
            
            d.show();
        }
    
public voidloadCustomer(int cust_id)
The OpenAccountDialog calls this method once the user has entered a customer ID and clicked OK. This method then uses that ID to load a customer object from the application server.

param
cust_id the customer ID to be loaded

        RemoteCustomer c;

        try {
            c = server.getCustomer(this, cust_id);
            customer_panel.cleanUp();
            remove(customer_panel);
            customer_panel = new CustomerPanel(this, c);
            add(customer_panel);
            validateTree();
            customer_panel.show();
        }
        catch( PersistenceException e ) {
            System.err.println("Error loading remote customer.");
            e.printStackTrace();
        }
        catch( RemoteException e ) {
            System.err.println("Failed to load remote customer.");
            e.printStackTrace();
        }
    
public static voidmain(java.lang.String[] args)
Sets the static host variable to the first command line argument. It then creates a new BankClient frame and shows it.

param
args the command line arguments to the application


                                        
         
        try {
            BankClient frame;

            if( args.length != 1 ) {
                System.out.println("Syntax: <java bank.client.BankClient " +
                                   "APP_SERVER_HOST>");
                return;
            }
            host = args[0];
            frame =  new BankClient();
            frame.show();
        }
        catch( RemoteException e ) {
            e.printStackTrace();
            System.exit(-1);
        }
    
public voidmonitorLock(imaginary.persist.RemotePersistent p)
This is called by the remote lock object from time to time just to check if the network is still up.

param
p the Persistent this object holds a lock on

    
public voidrun()
This method runs in a separate thread at the time the frame is created so that it can load the application server.

        try {
            System.out.println("Connecting to app server at " + host + "...");
            server = (RemoteAppServer)Naming.lookup("rmi://" + host +
                                                    "/AppServer");
            System.out.println("Connected to server...");
        }
        catch( Exception e ) {
            System.err.println("Failed to connect to application server.");
            e.printStackTrace();
            server = null;
        }
    
public voidsetLock(imaginary.persist.RemoteLock l)
This is called by the RemoteLock object when it first gets created.

param
l the remote lock object for thie RemoteLockHolder

        lock = l;
    
public synchronized voidshow()
Move the frame to the proper place when it is shown.

        move(50, 50);
        super.show();
    
public synchronized voidtransfer(float amount, bank.server.RemoteAccount source, bank.server.RemoteAccount target)
The TransferDialog box calls this method with the desired transfer amount, source account, and target account. The method in turn tells the source account to tranfer the specified amount to the specified target account.

param
amount the amount of money to transfer
param
source the source bank account from which to transfer the funds
param
target the bank account to which the money to be transferred

        try {
            source.transfer(this, amount, target);
            lock.save();
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    
public voidwindowActivated(java.awt.event.WindowEvent event)

    
public voidwindowClosed(java.awt.event.WindowEvent event)

    
public voidwindowClosing(java.awt.event.WindowEvent event)

        customer_panel.cleanUp();
        dispose();
        System.exit(0);
    
public voidwindowDeactivated(java.awt.event.WindowEvent event)

    
public voidwindowDeiconified(java.awt.event.WindowEvent event)

    
public voidwindowIconified(java.awt.event.WindowEvent event)

    
public voidwindowOpened(java.awt.event.WindowEvent event)