• 0
Sign in to follow this  
Anderson.OL

Componente com lista de subcomponentes

Question

Pois bem, abandonei TCollection e TCollectionItem, criei uma classe com Array de ClientDataSets com escopo public como abaixo:

unit MultiClientDataSet;

interface

uses
  System.SysUtils, System.Classes, Fmx.Types, DataSnap.DBClient;

type
  TMultiClientDataSet = class(TComponent)
  private
    FList: TList;
    function GetClientDataSet(Index: Integer): TClientDataSet;
    procedure SetClientDataSet(Index: Integer; const Value: TClientDataSet);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    function Count: Integer;
    function AddClientDataSet(ClientDataSet: TClientDataSet): TClientDataSet; overload;
    function AddClientDataSet: TClientDataSet; overload;
    procedure RemoveAction(ClientDataSet: TClientDataSet);
    property ClientDataSets[Index: Integer] : TClientDataSet read GetClientDataSet write SetClientDataSet;
  published

    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('RCA Samples', [TMultiClientDataSet]);
end;

{ TMultiClientDataSet }

function TMultiClientDataSet.AddClientDataSet(ClientDataSet: TClientDataSet): TClientDataSet;
begin
  FList.Add(ClientDataSet);
  Result := ClientDataSet;
end;

function TMultiClientDataSet.AddClientDataSet: TClientDataSet;
var
  FClientDataSet: TClientDataSet;
begin
  FClientDataSet := TClientDataSet.Create(Self);
  FClientDataSet.Name := 'ClientDataSet' + IntToStr(Count+1);
  Result := AddClientDataSet(FClientDataSet)
end;

function TMultiClientDataSet.Count: Integer;
begin
  Result := FList.Count;
end;

constructor TMultiClientDataSet.Create(AOwner: TComponent);
begin
  inherited;
  FList := TList.Create;
end;

function TMultiClientDataSet.GetClientDataSet(Index: Integer): TClientDataSet;
begin
  Result := TClientDataSet(FList[Index]);
end;

procedure TMultiClientDataSet.RemoveAction(ClientDataSet: TClientDataSet);
begin
  if FList.Remove(ClientDataSet) >= 0 then
  begin
    ClientDataSet.Destroy;
  end;
end;

procedure TMultiClientDataSet.SetClientDataSet(Index: Integer; const Value: TClientDataSet);
begin
  TClientDataSet(FList[Index]).Assign(Value);
end;

end.
Criei um editor de componente como mostra o código abaixo:
unit MultiClientDataSetEditor;

interface

uses
  System.Classes, DesignEditors, DesignIntf, FMX.Dialogs, System.SysUtils;

type
  TMultiClientDataSetEditor = class(TComponentEditor)
  private
    procedure ShowEditor;
  public
    function GetVerbCount : integer; override;
    function GetVerb (Index : integer): string; override;
    procedure ExecuteVerb (Index : integer); override;
    procedure Edit; override;
  end;

procedure Register;

implementation

uses
  MultiClientDataSet, UnitFrmMultiClientDataSetEditor;

procedure Register;
begin
  { Declare DesignIntf e StdCtrls no uses }
  RegisterComponentEditor(TMultiClientDataSet,TMultiClientDataSetEditor);
end;

{ TMultiClientDataSetEditor }

procedure TMultiClientDataSetEditor.Edit;
begin
  inherited;

  ShowMessage('Component ' + Component.Name);

end;

procedure TMultiClientDataSetEditor.ExecuteVerb(Index: integer);
begin

  inherited;

  case Index of
    0 : ShowEditor;
    1 : ShowMessage('Editor de Actions');
    2 : TMultiClientDataSet(Component).AddClientDataSet;
  end;

end;

function TMultiClientDataSetEditor.GetVerb(Index: integer): string;
begin
  case Index of
    0 : Result := 'Editor';
    1 : Result := 'Count: ' + IntToStr(TMultiClientDataSet(Component).Count );
    2 : Result := 'Adicionar um ClientDataSet';
  end;
end;

function TMultiClientDataSetEditor.GetVerbCount: integer;
begin
  Result := 3;
end;

procedure TMultiClientDataSetEditor.ShowEditor;
var
  Frm: TFrmMultiClientDataSetEditor;
begin

  Frm := TFrmMultiClientDataSetEditor.Create(NIL);

  try
    Frm.Component := Component;
    Frm.ShowModal;
  finally
    Frm.Free;
  end;

end;

end.
Com esse editor em mãos, após incluir o componente no Form, posso adicionar ClientDataSets através do Menu Popup. componentedelphiarraycl.png Ou através do form de edição do componente componentedelphiarraycl.png O problema é que não consigo persistir isso, pois o componente inserido não fica no código do Form. componentedelphiarraycl.png Eu reparei que se eu modificar o Create do ClienteDataSet para
FClientDataSet := TClientDataSet.Create(Self.Owner);

Daí o ClientDataSet aparece código do Form, mas não fica legal.

Como faz ?

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this