Advertisement
Guest User

Store in Seperate Datastructure

a guest
May 13th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.12 KB | None | 0 0
  1. // CONTACT LIST CLASS UNIT
  2.  
  3. unit ContactList;
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Messages, SysUtils, Classes, VirtualTrees, Generics.Collections;
  9.  
  10. Type
  11.    TUser = Class
  12.    Public
  13.      DisplayName,
  14.      SkypeID            : String;
  15.      UserNode           : PVirtualNode;
  16.      Constructor Create(DispName, SkID : String);
  17.    End;
  18.  
  19. Type
  20.    TCategory = Class
  21.    Private
  22.      VT                 : TVirtualStringTree;
  23.    Public
  24.      Name               : String;
  25.      ID                 : Integer;
  26.      Users              : TObjectList<TUser>;
  27.      CategoryNode       : PVirtualNode;
  28.      Constructor Create(Tree : TVirtualStringTree; FName : String; FID : Integer);
  29.      Destructor Destroy;
  30.      Function AddUser(DispName, SkID : String):TUser;
  31.    End;
  32.  
  33.  
  34. Type
  35.    TContactList = Class
  36.    Private
  37.     VT         : TVirtualStringTree;
  38.    Public
  39.     Categories : TObjectList<TCategory>;
  40.     Constructor Create(Tree : TVirtualStringTree);
  41.     Destructor Destroy;
  42.     Function AddCategoryToList(DisplayName : String; ID : Integer):TCategory;
  43.    End;
  44.  
  45. implementation
  46.  
  47.  
  48.  
  49. { TCategory }
  50.  
  51. constructor TCategory.Create(Tree : TVirtualStringTree; FName: String; FID: Integer);
  52. begin
  53.  Users  := TObjectList<TUser>.Create;
  54.  Name   := FName;
  55.  ID     := FID;
  56.  VT     := Tree;
  57. end;
  58.  
  59. destructor TCategory.Destroy;
  60. begin
  61.  Users.Free;
  62. end;
  63.  
  64.  
  65. function TCategory.AddUser(DispName, SkID: String): TUser;
  66. Var
  67.  XUser : TUser;
  68. begin
  69.  
  70.  XUser := TUser.Create(DispName, SkID);
  71.  XUser.UserNode  := Self.VT.AddChild(CategoryNode,XUser);
  72.  Self.Users.Add(XUser);
  73.  Result := XUser;
  74. end;
  75.  
  76.  
  77.  
  78. { TUser }
  79.  
  80. constructor TUser.Create(DispName, SkID: String);
  81. begin
  82.    DisplayName := DispName;
  83.    SkypeID := SkID;
  84.  
  85. end;
  86.  
  87. { TContactList }
  88.  
  89. constructor TContactList.Create(Tree : TVirtualStringTree);
  90. begin
  91.   VT            := Tree;
  92.   Categories    := TObjectList<TCategory>.Create;
  93. end;
  94.  
  95. destructor TContactList.Destroy;
  96. begin
  97.   Categories.Free;
  98. end;
  99.  
  100.  
  101. Function TContactList.AddCategoryToList(DisplayName : String; ID : Integer):TCategory;
  102. Var
  103.  Ctg : TCategory;
  104. Begin
  105.  Ctg := TCategory.Create(VT, DisplayName,ID);
  106.  Ctg.CategoryNode       := VT.AddChild(Nil, Ctg);
  107.  Categories.Add(Ctg);
  108.  Result := Ctg;
  109. End;
  110.  
  111. end.
  112.  
  113. /////////////////////////////////////////////////////////////////////////////
  114.  
  115. // Main Unit!
  116.  
  117. unit uMain;
  118.  
  119. interface
  120.  
  121. uses
  122.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  123.   Dialogs, VirtualTrees, Generics.Collections, ContactList, StdCtrls;
  124.  
  125.  
  126. type
  127.   TfrmMain = class(TForm)
  128.     VT: TVirtualStringTree;
  129.     procedure FormCreate(Sender: TObject);
  130.     procedure FormDestroy(Sender: TObject);
  131.     procedure VTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  132.       Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
  133.   private
  134.     { Private declarations }
  135.   public
  136.     { Public declarations }
  137.     ContactList : TContactList;
  138.   end;
  139.  
  140. var
  141.   frmMain: TfrmMain;
  142.  
  143. implementation
  144.  
  145. {$R *.dfm}
  146.  
  147.  
  148. procedure TfrmMain.FormCreate(Sender: TObject);
  149. Var
  150.  I, J : Integer;
  151.  Ctg : TCategory;
  152.  User : TUser;
  153. begin
  154.  VT.NodeDataSize := SizeOf(Pointer);
  155.  ContactList := TContactList.Create(VT);
  156.  
  157.  VT.BeginUpdate;
  158.  for I := 1 to 1000 do
  159.    begin
  160.      Ctg := ContactList.AddCategoryToList('Category #'+IntToStr(I),Random(100));
  161.      for J := 1 to 1000 do
  162.      begin
  163.        User := Ctg.AddUser('User #'+IntToStr(J),IntToStr(Random(3000)));
  164.      end;
  165.  
  166.    end;
  167.  VT.EndUpdate;
  168.  
  169. end;
  170.  
  171.  
  172.  
  173. procedure TfrmMain.FormDestroy(Sender: TObject);
  174. begin
  175.  ContactList.Free;
  176. end;
  177.  
  178. procedure TfrmMain.VTGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
  179.   Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
  180. Var
  181.  Obj : TObject;
  182.  Ctg : TCategory;
  183.  User : TUser;
  184. begin
  185.  
  186.  Obj := TObject(Sender.GetNodeData(Node)^);
  187.  
  188.  if Obj is TCategory then
  189.  begin
  190.    Ctg := TCategory(Obj);
  191.    case Column of
  192.     0: CellText := Ctg.Name;
  193.     1: CellText := IntToStr(Ctg.ID);
  194.    end;
  195.  end;
  196.  
  197.  if Obj is TUser then
  198.  begin
  199.    User := TUser(Obj);
  200.    case Column of
  201.     0: CellText := User.DisplayName;
  202.     1: CellText := User.SkypeID;
  203.    end;
  204.  end;
  205.  
  206. end;
  207.  
  208. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement