home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue51 / Observer / FObserver_Grid.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-10-08  |  3.4 KB  |  121 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   (c) TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: October 1999
  10.  
  11.   Notes: View the subject in a grid (using a TListView)
  12.  
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  14. unit FObserver_Grid;
  15.  
  16. interface
  17.  
  18. uses
  19.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  20.   ComCtrls, Grids, FObserver_Edit, Menus,
  21.   FObserver_Abstract ;
  22.  
  23. type
  24.  
  25.   //--------------------------------------------------------
  26.   TFormObserverGrid = class(TFormObserverAbstract)
  27.     LV: TListView;
  28.     MainMenu1: TMainMenu;
  29.     PopupMenu1: TPopupMenu;
  30.     Edit1: TMenuItem;
  31.     Edit2: TMenuItem;
  32.     Edit3: TMenuItem;
  33.     // When the ListView is double clicked, popup an
  34.     // edit dialog (which is also an observer)
  35.     procedure LVDblClick(Sender: TObject);
  36.     // When the form is created, assign the subject pointer
  37.     procedure FormCreate(Sender: TObject);
  38.   private
  39.   public
  40.     // Override the vitrual abstract DataToObserver method
  41.     procedure DataToObserver ; override ;
  42.   end;
  43.  
  44. var
  45.   FormObserverGrid: TFormObserverGrid;
  46.  
  47. implementation
  48. uses
  49.   Subject_Portfolio
  50.   ;
  51.  
  52. {$R *.DFM}
  53.  
  54. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  55. // *
  56. // * TFormObserver1
  57. // *
  58. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  59. procedure TFormObserverGrid.DataToObserver;
  60. var
  61.   i           : integer ;
  62.   lListItem   : TListItem ;
  63.   lStockTrans : TStockTrans ;
  64. begin
  65.   LV.Items.Clear ;
  66.   with ( Subject as TPortfolio ) do begin
  67.     // Scan the subject, reading rows of data
  68.     // into the ListView
  69.     for i := 0 to Stocks.Count - 1 do begin
  70.       lStockTrans := Stocks.Items[ i ] ;
  71.       lListItem := LV.Items.Add ;
  72.       lListItem.Caption := lStockTrans.StockCode ;
  73.       lListItem.SubItems.Add( lStockTrans.StockName ) ;
  74.       lListItem.SubItems.Add( IntToStr( lStockTrans.Qty )) ;
  75.       lListItem.SubItems.Add( Format( '%M',
  76.                                     [lStockTrans.Price] )) ;
  77.       lListItem.SubItems.Add( Format( '%M',
  78.                                     [lStockTrans.Value] )) ;
  79.       lListItem.Data := lStockTrans ;
  80.     end ;
  81.  
  82.       // Add the Total row
  83.       lListItem := LV.Items.Add ;
  84.       lListItem.Caption := 'Total' ;
  85.       lListItem.SubItems.Add( '' ) ;
  86.       lListItem.SubItems.Add( '' ) ;
  87.       lListItem.SubItems.Add( '' ) ;
  88.       lListItem.SubItems.Add( Format( '%M', [Total] )) ;
  89.  
  90.     end ;
  91. end;
  92.  
  93. // When the ListView is bouble clicked, popup an edit
  94. // dialog, which is also an observer.
  95. //----------------------------------------------------------
  96. procedure TFormObserverGrid.LVDblClick(Sender: TObject);
  97. var
  98.   lFormObserverEdit : TFormObserverEdit ;
  99. begin
  100.   inherited;
  101.   if LV.Selected = nil then
  102.     exit ; //==>
  103.  
  104.   if TObject( LV.Selected.Data ) = nil then
  105.     exit ; //==>
  106.  
  107.   lFormObserverEdit := TFormObserverEdit.Create( self ) ;
  108.   lFormObserverEdit.Data := ( TObject( LV.Selected.Data ) as TStockTrans ) ;
  109.   lFormObserverEdit.Show ;
  110. end;
  111.  
  112. // Attach to the subject when the form is created
  113. //----------------------------------------------------------
  114. procedure TFormObserverGrid.FormCreate(Sender: TObject);
  115. begin
  116.   inherited;
  117.   Subject := gPortfolio ;
  118. end;
  119.  
  120. end.
  121.