package COM.xinit.demon.co.uk.3tier.TraderTool;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import TraderTool.*;
import database.*;
import tickertape.*;

public class TraderView extends Panel implements UserInterface,ActionListener,ItemListener {
        Choice clientchoice;
        Button editclient;
        Button addclient;
        Button sell;
        Button buy;
        CustomerRec allcustomers[];
        Frame       dummy  = new Frame();
        ClientDialogue clientdial;
        TraderController bapp;
        PortfolioList portfolio;
        MarketList    market;


    TraderView (TraderController ba) {
        GridBagLayout      gb  = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(gb);

        bapp = ba;

	DBAccess dba = bapp.getDB();

        Font labelfont  = new Font("Helvetica",Font.BOLD,15);
        Font buttonfont = new Font("Helvetica",Font.BOLD,12);

        Panel p1 = new Panel();
        Panel p2 = new Panel();
        Panel p3 = new Panel();

        Label l1 = new Label("Trader:");
        l1.setFont(labelfont);
        Label l2 = new Label("Fred Bloggs");
        l2.setFont(labelfont);
        Label l3 = new Label("Client:");
        l3.setFont(labelfont);

        clientchoice = new Choice();
        setCustomers(ba);

        editclient = new Button ( "Edit" );
        addclient  = new Button ( "Add" );
        editclient.setFont(buttonfont); 
        addclient.setFont(buttonfont); 


   
        sell = new Button("Sell->");
        sell.setFont(buttonfont); 
        buy  = new Button("<- Buy"); 
        buy.setFont(buttonfont); 
        p3.setLayout(new BorderLayout());
        p3.add("North",sell);
        p3.add("South",buy);
        
        portfolio = new PortfolioList(dba, 25);
        market    = new MarketList(dba, 25);
 
        p1.add (l1);     
        p1.add (l2);   
  
        gbc.insets = new Insets ( 5,5,5,5 );
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill   = GridBagConstraints.VERTICAL;
        gb.setConstraints(p1,gbc);
        add (p1);

        p2.add (l3);     
        p2.add (clientchoice);
        p2.add (editclient);
        p2.add (addclient);     
        gbc.fill        = GridBagConstraints.BOTH;
        gbc.weightx     = 1.0;
        gbc.gridwidth   = GridBagConstraints.REMAINDER;
        gb.setConstraints(p2,gbc);
        add (p2); 

	TickerTape tt = new TickerTape("liverpool_st", 300);

	add(tt, gbc);
    
        gbc.weighty     = 1.0;
        gbc.gridwidth   = 1;
        gb.setConstraints(portfolio,gbc);
        add (portfolio);
     
        gbc.fill        = GridBagConstraints.NONE;
        gbc.weightx     = 0.0;
        gbc.weighty     = 0.0;
        gb.setConstraints(p3,gbc);
        add(p3);

        gbc.fill        = GridBagConstraints.BOTH;
        gbc.weightx     = 1.0;
        gbc.weighty     = 1.0;
        gbc.gridwidth   = GridBagConstraints.REMAINDER;
        gb.setConstraints(market,gbc);
        add (market);
        // load the portfolio with the default SSN
        changeSSN();

    }

    public void itemStateChanged(ItemEvent ie)
    {
        changeSSN();
    }

    public void actionPerformed(ActionEvent ae)
    {
         if (ae.getSource() == addclient) {
             if (clientdial == null) {
                 clientdial = new ClientDialogue(bapp,dummy,true);
                 clientdial.pack();
             } 
             clientdial.makeAdd();
             clientdial.show();               
         } else if (ae.getSource() == editclient) {
             if (clientdial == null) {
                 clientdial = new ClientDialogue(bapp,dummy,true);
                 clientdial.pack();
             } 
             clientdial.makeEdit();
             clientdial.loadClient(allcustomers[clientchoice.getSelectedIndex()]);
             clientdial.show();               
         }
         setCustomers(bapp);
    }

    public void addItemListener(ItemListener il)
    {
        clientchoice.addItemListener(il);
    }

    public void addActionListener(ActionListener al)
    {
        editclient.addActionListener(al);
        addclient.addActionListener(al);
        sell.addActionListener(al);
        buy.addActionListener(al);
    }

    public void setCustomers(TraderController ba)
    {
        String oldSSN = getCustomerSSN();
        // call the ba method to return the client list
        allcustomers = ba.getAllCustomers();
        clientchoice.setVisible(false);
        clientchoice.removeAll();
        // loop through and update the choices
        for (int i = 0; i < allcustomers.length; i++ ) {
            clientchoice.add(allcustomers[i].getName());
        }
        if (oldSSN != null) {          
            int pos = 0;
            while (pos < allcustomers.length && !allcustomers[pos].getSSN().equals(oldSSN)) {
                pos++;
            }
            if (pos < allcustomers.length) {
                clientchoice.select(pos);
            }
        } 
        clientchoice.setVisible(true);
    }

    public String getCustomerSSN()
    {
        if (allcustomers != null) {
            return allcustomers[clientchoice.getSelectedIndex()].getSSN();
        } else {
            return null;
        }
    }

    public void changeSSN ()
    {
        portfolio.setCustomer(getCustomerSSN()); 
        market.setCustomer(getCustomerSSN()); 
    }


}



Last Updated