Classe TCollection (Filha) dentro de outra TCollection(pai)

Delphi

18/07/2005

Estou desenvolvendo um componente que utiliza a classe TCollection em uma propriedade. Isso funciona perfeitamente, porém preciso dentro dela criar uma outra TCollection (Filha). Na classe filha quando dou um click no botão [...] ele não acessa o form desta classe filha. O que eu preciso é o mesmo comportamento do componente TClientDataSet na propriedade FieldDefs (TCollection pai) e quando acessa para criar os fields tem uma propriedade ChildDefs (TCollection filha).

Abaixo tem uma cópia do fonte:
-------------------------------------

unit IDApplyUpdate;

interface

uses
Windows, Messages, SysUtils, Classes, db, DBClient;

type

TIDIteCpo = class;
TIDColCpo = class;
TTableChild = class;
TIDIteApl = class;
TIDColApl = class;
IDApplyUpdates = class;

//--------------------------------------------------------------------------
----
TIDColCpo = class(TCollection)
private
fOwner: TTableChild;
function GetItem(Index: Integer): TIDIteCpo;
procedure SetItem(Index: Integer; Value: TIDIteCpo);
protected
function GetOwner: TPersistent; override;
public
constructor Create(Owner: TTableChild);
property Items[Index: Integer]: TIDIteCpo read GetItem write SetItem;
default;
end;

TIDIteCpo = class(TCollectionItem)
private
fNameField: String;
fMain: Boolean;
protected
public
constructor Create(Collection: TCollection); override;
function GetDisplayName: string; override;
procedure Assign(Source: TPersistent); override;
published
property NameField: String read fNameField write fNameField;
property Main: Boolean read fMain write fMain;
end;

TTableChild = class(TPersistent)
private
fColCpo: TIDColCpo;
fTable: String;
fInsert: Boolean;
fUpdate: Boolean;
fDelete: Boolean;
function GetFields: TIDColCpo;
procedure SetFields(Value: TIDColCpo);
protected
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create; virtual;
destructor Destroy; override;
published
property Table: String read fTable write fTable;
property opInsert: Boolean read fInsert write fInsert;
property opUpdate: Boolean read fUpdate write fUpdate;
property opDelete: Boolean read fDelete write fDelete;
property Fields: TIDColCpo read GetFields write SetFields;
end;

TIDColApl = class(TCollection)
private
fOwner: IDApplyUpdates;
function GetItem(Index: Integer): TIDIteApl;
procedure SetItem(Index: Integer; Value: TIDIteApl);
protected
function GetOwner: TPersistent; override;
public
constructor Create(Owner: IDApplyUpdates);
property Items[Index: Integer]: TIDIteApl read GetItem write SetItem;
default;
end;


TIDIteApl = class(TCollectionItem)
private
fClientDataSet: TClientDataSet;
fNameFont: String;
fTableChild: TTableChild;
fSQL: TStrings;
function GetClientDataSet: TClientDataSet;
procedure SetTableChild(const Value: TTableChild);
procedure SetSql(Value: TStrings);
protected
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
function GetDisplayName: string; override;
published
property NameFont: String read fNameFont write fNameFont;
property ClientDataSet: TClientDataSet read GetClientDataSet write
fClientDataSet;
property TableChild: TTableChild read fTableChild write SetTableChild;
property Sql: TStrings read fSql write SetSql;
end;


IDApplyUpdates = class(TComponent)
private
fMainDataSet: TClientDataSet;
fColApl: TIDColApl;
function GetMainDataSet: TClientDataSet;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function ApplyUpdates(MaxErrors: Integer): Integer;
published
property FontData: TIDColApl read fColApl write fColApl;
property MainDataSet: TClientDataSet read GetMainDataSet write
fMainDataSet;
end;

procedure Register;

implementation

uses StrUtils, Dialogs;


procedure TTableChild.AssignTo(Dest: TPersistent);
begin
if Dest is TTableChild then
with TTableChild(Dest) do
begin
fTable:= Self.fTable;
fInsert:= Self.fInsert;
fUpdate:= Self.fUpdate;
fDelete:= Self.fDelete;
end
else
inherited AssignTo(Dest);
end;

procedure TIDIteApl.SetTableChild(const Value: TTableChild);
begin
fTableChild.Assign(Value);
end;

constructor TTableChild.Create;
begin
inherited Create;
fInsert:= true;
fUpdate:= true;
fDelete:= true;
fColCpo:= TIDColCpo.Create(self);
end;

function TIDColApl.GetItem(Index: Integer): TIDIteApl;
begin
Result := TIDIteApl(inherited GetItem(Index));
end;

procedure TIDColApl.SetItem(Index: Integer; Value: TIDIteApl);
begin
inherited SetItem(Index, Value);
end;

function TIDColApl.GetOwner: TPersistent;
begin
Result:= fOwner;
end;

constructor TIDColApl.Create(Owner: IDApplyUpdates);
begin
inherited Create(TIDIteApl);
fOwner := Owner;
end;

constructor TIDIteApl.Create(Collection: TCollection);
begin
inherited create(Collection);
fTableChild:= TTableChild.Create;
fSQL := TStringList.Create;
end;

function TIDIteApl.GetClientDataSet: TClientDataSet;
begin
Result := fClientDataSet;
end;

function TIDIteApl.GetDisplayName: string;
begin
Result:= fNameFont;
if Result = ´´ then
Result:= inherited GetDisplayName;
end;

function IDApplyUpdates.ApplyUpdates(MaxErrors: Integer): Integer;
begin
// Process o Apply

end;

constructor IDApplyUpdates.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fColApl:= TIDColApl.Create(self);
end;

destructor IDApplyUpdates.Destroy;
begin
if Assigned(fColApl) then
FreeAndNil(fColApl);
inherited;
end;

function IDApplyUpdates.GetMainDataSet: TClientDataSet;
begin
Result := fMainDataSet;
end;


procedure Register;
begin
RegisterComponents(´Standard´, [IDApplyUpdates]);
end;

{ TIDIteCpo }

procedure TIDIteCpo.Assign(Source: TPersistent);
begin
if Source is TIDIteCpo then
begin
NameField := TIDIteCpo(Source).NameField;
Main := TIDIteCpo(Source).Main;
end
else
inherited Assign(Source);
end;

constructor TIDIteCpo.Create(Collection: TCollection);
begin
inherited create(Collection);
end;

function TIDIteCpo.GetDisplayName: string;
begin
Result:= fNameField;
if Result = ´´ then
Result:= inherited GetDisplayName;
end;

{ TIDColCpo }

constructor TIDColCpo.Create(Owner: TTableChild);
begin
inherited Create(TIDIteCpo);
fOwner := Owner;
end;

function TIDColCpo.GetItem(Index: Integer): TIDIteCpo;
begin
Result := TIDIteCpo(inherited GetItem(Index));
end;

function TIDColCpo.GetOwner: TPersistent;
begin
Result:= fOwner;
end;

procedure TIDColCpo.SetItem(Index: Integer; Value: TIDIteCpo);
begin
inherited SetItem(Index, Value);
end;

destructor TTableChild.Destroy;
begin
if Assigned(fColCpo) then
FreeAndNil(fColCpo);
inherited;
end;

function TTableChild.GetFields: TIDColCpo;
begin
if fColCpo = nil then
fColCpo := TIDColCpo.Create(Self);
Result := fColCpo;
end;

procedure TTableChild.SetFields(Value: TIDColCpo);
begin
Fields.Assign(Value);
end;

destructor TIDIteApl.Destroy;
begin
if Assigned(fSql) then
FreeAndNil(fSql);
inherited;
end;

procedure TIDIteApl.SetSql(Value: TStrings);
begin
if fSQL.Text <> Value.Text then
begin
fSQL.BeginUpdate;
try
fSQL.Assign(Value);
finally
fSQL.EndUpdate;
end;
end;
end;

end.


Para Testar:
--------------

1) Acessar a propriedadeFontData[...]

2) Abrir a propriedade TableChild [+]

3) Acessar a propriedade Fields [...]

Neste ponto ele não abre o form para criar os TCollection filhos.



Qualquer sugestão será muito bem vinda !


Mcd

Mcd

Curtidas 0
POSTAR