home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue59 / Arch / Sample / UnitFormBase.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-05-18  |  5.9 KB  |  231 lines

  1. unit UnitFormBase;
  2. interface
  3.  
  4. uses
  5.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  6.   UnitObjectBase, dbtables, ImgList;
  7.  
  8. type
  9.   TFormBase = class;
  10.   TFormBaseClass = class of TFormBase;
  11.   TFormBase = class(TForm)
  12.     ImageListBase: TImageList;
  13.     procedure FormShow(Sender: TObject);
  14.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  15.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  16.   private
  17.     FKey: integer;
  18.     FBusinessObject: TObjectBase;
  19.     constructor PrivateCreate(Owner: TComponent);
  20.     procedure AncestorInitializeForm;
  21.     procedure AncestorFinalizeForm;
  22.     public function EqualsSignature(aFormClass: TFormBaseClass; const anKey: integer): boolean;
  23.  
  24.   protected
  25.     FStringsFieldForceRole: TStrings;
  26.     class function FindForm(aFormClass: TFormBaseClass; const aKey: integer): TFormBase;
  27.     function GetBusinessObject: TObjectBase;
  28.  
  29.     function BusinessObjectClass: TObjectBaseClass; virtual;
  30.     procedure InitializeForm; virtual;
  31.     procedure FinalizeForm; virtual;
  32.  
  33.   public
  34.  
  35.     constructor Create(Owner: TComponent); override;
  36.  
  37.     class function NewInteger: integer;
  38.  
  39.     class function FormMain: TFormBase;
  40.  
  41.     class function FetchForm(const aKey: integer): TFormBase; virtual;
  42.  
  43.     property Key: integer read FKey write FKey;
  44.  
  45.     property BusinessObject: TObjectBase read GetBusinessObject;
  46.  
  47.     public destructor Destroy; override;
  48.     procedure ShowForm;
  49.     class procedure CloseAll;
  50.     class function FormCount: integer;
  51.     procedure Save;
  52.   end;
  53.  
  54. implementation
  55.  
  56. uses
  57.   UnitFormMain
  58.   ;
  59.  
  60. {$R *.DFM}
  61.  
  62. var FListForms: TList;
  63.  
  64. procedure TFormBase.ShowForm;
  65. begin
  66.   if (WindowState = wsMinimized) then
  67.     WindowState := wsNormal;
  68.   Show;
  69. end;
  70.  
  71. procedure TFormBase.FormShow(Sender: TObject);
  72. begin
  73.   if (Self = FormMain) then
  74.     Self.SetBounds(0, 0, Self.Width, Self.Height)
  75.   else
  76.       Self.SetBounds(FormMain.Left, (FormMain.Top + FormMain.Height), Self.Width, Self.Height);
  77. end;
  78.  
  79. destructor TFormBase.Destroy;
  80. begin
  81.   FinalizeForm;
  82.   AncestorFinalizeForm;
  83.   FListForms.Remove(Self);
  84.   inherited;
  85. end;
  86.  
  87. function TFormBase.EqualsSignature(
  88.   aFormClass: TFormBaseClass; const anKey: integer): boolean;
  89. begin
  90.   Result := ( (Self.ClassType = aFormClass) and (Self.Key = anKey) );
  91. end;
  92.  
  93. class function TFormBase.FetchForm(const aKey: integer): TFormBase;
  94. begin
  95.   // "Self" in a class method referes to the class type
  96.  
  97.   // See if the form already exists
  98.   Result := FindForm(Self, aKey);
  99.   if (Result = NIL) then begin
  100.     Result := Self.PrivateCreate(Application);
  101.     // Add the newly created form to the list of all existing forms.
  102.     FListForms.Add(Result);
  103.     Result.Key := aKey;
  104.     // Do special ancestor-level initializations.
  105.     Result.AncestorInitializeForm;
  106.     // Now that everything is set up in the architecture run sub-classing
  107.     // form's implementation of InitializeForm
  108.     Result.InitializeForm;
  109.   end; // then begin
  110. end;
  111.  
  112. class function TFormBase.FindForm(aFormClass: TFormBaseClass;
  113.   const aKey: integer): TFormBase;
  114. var i: integer;
  115. var aForm: TFormBase;
  116. begin
  117.   Result := NIL;
  118.   for i := 0 to FListForms.Count - 1 do begin
  119.     aForm := TFormBase(FListForms.Items[i]);
  120.     if ( aForm.EqualsSignature(aFormClass, aKey) ) then begin
  121.       Result := aForm;
  122.       exit;
  123.     end; // then begin
  124.   end; // do begin
  125. end;
  126.  
  127. procedure TFormBase.AncestorInitializeForm;
  128. begin
  129. end;
  130.  
  131. procedure TFormBase.AncestorFinalizeForm;
  132. begin
  133. end;
  134.  
  135. var PrivateClassNewInteger: integer;
  136. class function TFormBase.NewInteger: integer;
  137. begin
  138.   inc(PrivateClassNewInteger);
  139.   Result := PrivateClassNewInteger;
  140. end;
  141.  
  142. function TFormBase.GetBusinessObject: TObjectBase;
  143. begin
  144.   if (FBusinessObject = NIL) and (BusinessObjectClass <> NIL) then
  145.      FBusinessObject := BusinessObjectClass.FetchReference(Self, Key);
  146.   Result := FBusinessObject;
  147. end;
  148.  
  149. procedure TFormBase.FinalizeForm;
  150. begin
  151.   FBusinessObject.FreeReference(Self);
  152.   FBusinessObject := NIL;
  153. end;
  154.  
  155. procedure TFormBase.InitializeForm;
  156. begin
  157. end;
  158.  
  159. function TFormBase.BusinessObjectClass: TObjectBaseClass;
  160. begin
  161.   Result := NIL;
  162. end;
  163.  
  164. class function TFormBase.FormMain: TFormBase;
  165. begin
  166.   Result := UnitFormMain.FormMain; // Be careful not to do a recursive call here.
  167. end;
  168.  
  169. class procedure TFormBase.CloseAll;
  170. var i: integer;
  171. begin
  172.   for i := FListForms.Count - 1 downto 0 do begin
  173.     TFormBase(FListForms.Items[i]).Close;
  174.   end;
  175. end;
  176.  
  177. class function TFormBase.FormCount: integer;
  178. begin
  179.   Result := FListForms.Count;
  180. end;
  181.  
  182. procedure TFormBase.FormClose(Sender: TObject; var Action: TCloseAction);
  183. begin
  184.   Action := caFree;
  185. end;
  186.  
  187. procedure TFormBase.Save;
  188. begin
  189.   if (BusinessObject <> NIL) then
  190.     BusinessObject.Save;
  191. end;
  192.  
  193. procedure TFormBase.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  194. begin
  195.   CanClose := TRUE;
  196.   // BusinessObject will be NIL for the main form and list forms
  197.   if (BusinessObject <> NIL) and (BusinessObject.UpdatesPending) then begin
  198.     case MessageDlg('Save changes before closing?', mtConfirmation, [mbYes, mbNo, mbCancel], 0) of
  199.       mrYes: BusinessObject.Save;
  200.       mrNo: BusinessObject.Cancel;
  201.       else CanClose := FALSE;
  202.     end; // case
  203.   end; // then begin
  204.  
  205. end;
  206.  
  207. constructor TFormBase.Create(Owner: TComponent);
  208. begin
  209.   inherited;
  210.   // FormMain is the only form that should be auto-created.
  211.   if (Self.ClassType <> TFormMain) then
  212.     MessageDlg('TFormBase.Create is being run. Check that the form ' + Self.Name + ' is not being auto-created.', mtError, [mbOK], 0);
  213. end;
  214.  
  215. constructor TFormBase.PrivateCreate(Owner: TComponent);
  216. begin
  217.   inherited Create(Owner);
  218. end;
  219.  
  220. initialization
  221.  
  222.   FListForms := TList.Create;
  223. {******************************************************************************}
  224.  
  225. finalization
  226.  
  227.   FListForms.Free;
  228. {******************************************************************************}
  229.  
  230. end.
  231.