Previous Next

DPM Sample - classes

DpmView.java

DpmView sets up the display of the portfolio data using a number of user interface widgets, including MultiColumnListbox and BorderPanel elements. DpmView implements listeners to handle events on relevant user interface elements, and also handles initialization of the animated ticker tape.

The view's initialize method is called after the component's controller and model are created. This method sets up all the elements needed to display p
public class DpmView extends ModelView
implements ListboxListener, WindowListener  {
public DpmView() {
}

public void initialize() {
   super.initialize();
   DpmModel model = (DpmModel)getModel();
 
   // Create a listbox to display portfolio information
   listbox = new MultiColumnListbox();

   listbox.addColumn("Symbol");
   listbox.addColumn("Shares");
   listbox.addColumn("Basis");
   listbox.addColumn("Price");
   listbox.addColumn("Change");
   listbox.addColumn("Value");

   listbox.setCaptionBarForeground( Color.pink);
   listbox.setHorizontalScrollbarVisible( false);
   listbox.setVerticalSeparatorVisible( true);
   listbox.setSelectionForeground(Color.green);
   listbox.setForeground( Color.blue);
   listbox.addListboxListener( this);

   // Change settings for status columns
   listbox.getColumnInfo(0).setWidth(100);
   listbox.getColumnInfo(0).setSorter( new SelectionSorter());
   listbox.getColumnInfo(1).setWidth(60);
   listbox.getColumnInfo(1).setSorter( new SelectionSorter());
   listbox.getColumnInfo(2).setWidth(60);
   listbox.getColumnInfo(2).setSorter( new SelectionSorter());
   listbox.getColumnInfo(3).setWidth(60);
   listbox.getColumnInfo(3).setSorter( new SelectionSorter());
   listbox.getColumnInfo(4).setWidth(60);
   listbox.getColumnInfo(4).setSorter( new SelectionSorter());
   listbox.getColumnInfo(5).setWidth(100);
   listbox.getColumnInfo(5).setSorter( new SelectionSorter());

   BorderPanel listboxPanel = new BorderPanel( BorderPanel.IN);
   listboxPanel.setThickness( 0);
   listboxPanel.setBackground( Color.lightGray);
   listboxPanel.setLayout( new BorderLayout());
   listboxPanel.add( "Center", listbox);

   BorderPanel balancePanel = new BorderPanel( BorderPanel.IN);
   balancePanel.setThickness( 0);
   balancePanel.setLayout( new GridLayout(3,3));
   balancePanel.setFont( new Font( "Bookman", Font.BOLD, 12));
   balancePanel.setBackground( Color.lightGray);
   balancePanel.setForeground( Color.blue);

   secLabel = new Label("Securities Value ", Label.RIGHT);
   secVal = new TextField("$"+ model.getSecuritiesValue());
   secVal.setEditable( false);
   secVal.setBackground( Color.black);
   secVal.setForeground( Color.green);
   cashLabel = new Label ("Cash Available ", Label.RIGHT );
   cashVal = new TextField("$"+ model.getCashAvailable());
   cashVal.setEditable( false);
   cashVal.setBackground( Color.black);
   cashVal.setForeground( Color.green);
   equityLabel = new Label("Account Equity ", Label.RIGHT );
   equityVal = new TextField("$"+ model.getEquityValue());
   equityVal.setEditable( false);
   equityVal.setBackground( Color.black);
   equityVal.setForeground( Color.green);

   balancePanel.add( secLabel);
   balancePanel.add( secVal);
   balancePanel.add( cashLabel);
   balancePanel.add( cashVal);
   balancePanel.add( equityLabel);
   balancePanel.add( equityVal);

   // Create the tickertape
   int fps = 10;	// Set Frames per minute
   tickerTape = new TickerTape(fps,"INTC 145 MSFT 97 1/8 MER 91 1/4 CSCC 35");

   BorderPanel summaryPanel = new BorderPanel( BorderPanel.IN);
   summaryPanel.setThickness( 2);
   summaryPanel.setLayout( new BorderLayout());
   summaryPanel.add( "North", balancePanel);
   summaryPanel.add( "South", tickerTape);

   BorderPanel buttonPanel = new BorderPanel( BorderPanel.OUT);
   buttonPanel.setLayout( new GridLayout(7, 1));
   buttonPanel.setThickness( 1);
   buttonPanel.setBackground( Color.lightGray);
   buttonPanel.setForeground( Color.blue);

   Button b1 = new Button("Buy");
   b1.addActionListener( getComponentController().getGUIHandler());
   Button b2 = new Button("Sell");
   b2.addActionListener( getComponentController().getGUIHandler());
   Button b3 = new Button("Set Price");
   b3.addActionListener( getComponentController().getGUIHandler());
   Button b4 = new Button("Deposit");
   b4.addActionListener( getComponentController().getGUIHandler());
   Button b5 = new Button("Withdraw");
   b5.addActionListener( getComponentController().getGUIHandler());
   Button b6 = new Button("Risk%");
   b6.addActionListener( getComponentController().getGUIHandler());
   Button b7 = new Button("Security%");
   b7.addActionListener( getComponentController().getGUIHandler());

   buttonPanel.add( b1);
   buttonPanel.add( b2);
   buttonPanel.add( b3);
   buttonPanel.add( b4);
   buttonPanel.add( b5);
   buttonPanel.add( b6);
   buttonPanel.add( b7);

   BorderPanel mainPanel = new BorderPanel( BorderPanel.SOLID );
   mainPanel.setThickness( 2);
   mainPanel.setAlignment( BorderPanel.CENTER);
   mainPanel.setText("Portfolio Market Value");
   mainPanel.setLayout( new BorderLayout());

   mainPanel.add( "Center", listboxPanel);
   mainPanel.add( "East", buttonPanel);
   mainPanel.add( "South", summaryPanel);

   // Create chart views
   riskView = new Chart( ChartType.PIECHART);
   securityView = new Chart( ChartType.PIECHART);

   riskChartFrame = new Frame( new String( " Risk Type View"));
   riskChartFrame.add( riskView);
   riskChartFrame.setVisible( false);
   riskChartFrame.addWindowListener( this);

   securityChartFrame = new Frame( new String( " Securities View"));
   securityChartFrame.add( securityView);
   securityChartFrame.setVisible( false);
   securityChartFrame.addWindowListener( this);

   setLayout(new BorderLayout());
   add( "Center", mainPanel);
   getComponentController().getComponentFrame().pack();
   tickerTape.startAnimation();
}
Set up display of the account data for the current customer
public Vector getSecurityTypeLabels() {
   Vector	aVector = new Vector();
   aVector.addElement( new String("Option"));
   aVector.addElement( new String("Stock"));
   aVector.addElement( new String("Mutual"));
   aVector.addElement( new String("Bond"));
   aVector.addElement( new String("Cash"));

   return aVector;
}

public Vector getSecurityLabels() {
   Vector	aVector = new Vector();
   DpmModel model = (DpmModel)getModel();
   Account account = model.getCustomer().getAccount();
   int index = account.getPortRecordCount();
   while( index > 0) {
      index--;
      PortRecord portRec = account.getPortRecord( index);
      aVector.addElement( new String( portRec.getSecurity().getSymbol()));
   }

   return aVector;
}

public Vector getSecurityValues() {
   Vector	aVector = new Vector();
   DpmModel model = (DpmModel)getModel();
   Account account = model.getCustomer().getAccount();
   int index = account.getPortRecordCount();
   while( index > 0) {
      index--;
      PortRecord portRec = account.getPortRecord( index);
      Float value = new Float(portRec.getShares() * portRec.getSecurity().getPrice());
      aVector.addElement( new Integer( value.intValue())); 
   }

   return aVector;
}

public Vector getSecurityTypeValues() {
   Vector	aVector = new Vector();
   DpmModel model = (DpmModel)getModel();
   Account account = model.getCustomer().getAccount();
   int index = account.getPortRecordCount();
   float options = (float)0.0;
   float stocks = (float)0.0;
   float mutuals = (float)0.0;
   float bonds = (float)0.0;
   float cash = account.getCashAvailable();
   while( index > 0) {
      index--;
      PortRecord portRec = account.getPortRecord( index);
      if( portRec.getSecurity().getType().equals(Security.kStock)) {
         float value = portRec.getShares() * portRec.getSecurity().getPrice();
         stocks += value;
      } 
      else if( portRec.getSecurity().getType().equals(Security.kOption)) {
         float value = portRec.getShares() * portRec.getSecurity().getPrice();
         options += value;
      }
      else if( portRec.getSecurity().getType().equals(Security.kMutual)) {
         float value = portRec.getShares() * portRec.getSecurity().getPrice();
         mutuals += value;
      }
      else {
         float value = portRec.getShares() * portRec.getSecurity().getPrice();
         bonds += value;
      }
   }
   aVector.addElement( new Integer( new Float(options).intValue())); 
   aVector.addElement( new Integer( new Float(stocks).intValue())); 
   aVector.addElement( new Integer( new Float(mutuals).intValue())); 
   aVector.addElement( new Integer( new Float(bonds).intValue())); 
   aVector.addElement( new Integer( new Float(cash).intValue())); 

   return aVector;
}
Create the display in the ticker tape based on the current customer's portfolio items
public String getTickerText() {
   DpmModel model = (DpmModel)getModel();
   Account account = model.getCustomer().getAccount();
   int index = account.getPortRecordCount();
   String tickText = "";
   while( index > 0) {
      index--;
      PortRecord portRec = account.getPortRecord( index);
      tickText += portRec.getSecurity().getSymbol();
      tickText += " ";
      Float price = new Float(portRec.getSecurity().getPrice());
      tickText += price.toString();
      tickText += " ";
   }
   if( tickText.length() <= 2)
      tickText = "INTC 145 MSFT 97 1/8 MER 91 1/4 CSCC 35";
   return tickText;
}
Method called when the data model changes. It creates a new display based on the updated securities information.

 

 

 

 

 

 

 

 

 

 

 

 

 

Updates any other views that are open, including risk, security type, and ticker tape

public void handleModelChange(ModelChangeEvent event) {
   super.handleModelChange(event);
   DpmModel model = (DpmModel)getModel();
   secVal.setText("$" + model.getSecuritiesValue() );
   cashVal.setText("$" + model.getCashAvailable());
   equityVal.setText("$" + model.getEquityValue());
   Account account = model.getCustomer().getAccount();

   int index = account.getPortRecordCount();
   int selectedIndex = listbox.getSelectedIndex();
   if( index != listbox.countRows())
      selectedIndex = index - 1;
   listbox.clear();
   while( index > 0) {
      index--;
      PortRecord portRec = account.getPortRecord( index);
      Object[] row = new Object[6];
      float change = portRec.getSecurity().getPrice() - portRec.getBasis();
      float value = portRec.getShares() * portRec.getSecurity().getPrice();
      row[ 0] = portRec.getSecurity().getSymbol();	
      row[ 1] = String.valueOf(portRec.getShares());
      row[ 2] = String.valueOf(portRec.getBasis());
      row[ 3] = String.valueOf(portRec.getSecurity().getPrice());
      row[ 4] = String.valueOf(change);
      row[ 5] = String.valueOf(value);

      listbox.addRow(row);
   }
   listbox.sort();
   ListboxEvent aEvt = new ListboxEvent( this, ListboxEvent.SELECTED, selectedIndex, 0, 0, 0);
   if(selectedIndex >= 0) {
      listbox.selectRow( selectedIndex);
      rowSelected( aEvt);
   }
   else {
      rowDeselected( aEvt);
   }

   // Risk type view
   if( riskChartFrame.isVisible()) {
      riskView.setChartLabels( getSecurityTypeLabels());	
      riskView.setChartData( getSecurityTypeValues());	
   }
   // Security view
   if( securityChartFrame.isVisible()) {
      securityView.setChartLabels( getSecurityLabels());	
      securityView.setChartData( getSecurityValues());	
   }

   // TickerTape
   tickerTape.setTickerText( getTickerText());
   repaint();
}
Method called when the current selection changes
public void handleModelSelectionChange(ModelChangeEvent event) {
   repaint();
}
Paints the view based on the data in the component's model
public void paint(Graphics g) {
   DpmModel model = (DpmModel)getModel();
}
Methods for determining the size of the view
public Dimension getMinimumSize() {
   return new Dimension(550, 350);
}

public Dimension getPreferredSize() {
   return getMinimumSize();
}
Access methods to the risk and security views and the ticker tape contained by this view
public Chart getSecurityView() {
   return securityView;
}

public Frame getRiskChartFrame() {
   return riskChartFrame;
}

public Frame getSecurityChartFrame() {
   return securityChartFrame;
}

public Chart getRiskView() {
   return riskView;
}

public TickerTape getTickerTape() {
   return tickerTape;
}
Methods called when an item in the portfolio is selected or deselected
public void rowSelected( ListboxEvent evt) {
   int whichRow = evt.getRow();
   Object[] row = listbox.getRow( whichRow);
   String selectSymbol = (String)row[0];

   DpmModel model = (DpmModel)getModel();
   DpmSelection curSel = (DpmSelection)getComponentController().getModelSelection();
   curSel.selectPortRecord( model.getPortRecord( selectSymbol));
}

public void rowDeselected( ListboxEvent evt) {
   int whichRow = evt.getRow();
   String selectSymbol = null;
   if( whichRow >= 0) {
      Object[] row = listbox.getRow( whichRow);
      selectSymbol = (String)row[0];
   }

   DpmModel model = (DpmModel)getModel();
   DpmSelection curSel = (DpmSelection)getComponentController().getModelSelection();
   if( curSel != null) {
      if( selectSymbol != null)
         curSel.deselectPortRecord( model.getPortRecord( selectSymbol));
      curSel.deselectAll();
   }
}
WindowListener methods
public void windowActivated( WindowEvent e) {
}

public void windowClosed( WindowEvent e) {
}

public void windowClosing( WindowEvent e) {
   Window w = e.getWindow();
   w.setVisible( false);
}

public void windowDeactivated( WindowEvent e) {
}

public void windowDeiconified( WindowEvent e) {
}

public void windowIconified( WindowEvent e) {
}

public void windowOpened( WindowEvent e) {
}
Data members
TickerTape         tickerTape;
Label              cashLabel;
TextField          cashVal;
Label              secLabel;
TextField          secVal;
Label              equityLabel;
TextField          equityVal;
MultiColumnListbox listbox;
Chart	          riskView;
Chart	          securityView;
Frame	          riskChartFrame;
Frame	          securityChartFrame;
}

Previous Next

Copyright © Taligent, Inc. 1996 - 1997.
Copyright
© IBM Corporation 1996 - 1997.
All Rights Reserved.