Ver Mensaje Individual
  #5  
Antiguo 31-10-2007
Avatar de ariefez
ariefez ariefez is offline
Miembro
 
Registrado: sep 2005
Ubicación: Perú - Lima
Posts: 63
Reputación: 19
ariefez Va por buen camino
Guiate de esta unidad tiene lo basico pa crear las colecciones

Código Delphi [-]
unit Prueba;

interface

uses
  Classes;

type

  { TMiItem }

  TMiComponente = class;

  TMiItem = class(TCollectionItem)
  private
    FAlignment: TAlignment;
  protected
    function  GetMiComponente: TMiComponente;
    function GetDisplayName: string; override;
  public
    procedure Assign(Source: TPersistent); override;
    property  MiComponente: TMiComponente read GetMiComponente;
  published
    property Alignment: TAlignment read FAlignment write FAlignment default taLeftJustify;
  end;

  { TMiLista }

  TMiItemClass = class of TMiItem;

  TMiLista = class(TCollection)
  private
    FMiComponente: TMiComponente;
    function GetMiItem(Index: Integer): TMiItem;
    procedure SetMiItem(Index: Integer; Value: TMiItem);
  protected
    function GetOwner: TPersistent; override;
    procedure Update(Item: TCollectionItem); override;
  public
    constructor Create(MiComponente: TMiComponente;
      InfoListClass: TMiItemClass);
    function Add: TMiItem;
    property MiComponente: TMiComponente read FMiComponente;
    property Items[Index: Integer]: TMiItem read GetMiItem write SetMiItem; default;
  end;

  TMiComponente = class(TComponent)
  private
    FMiLista: TMiLista;
    procedure SetMiLista(const Value: TMiLista);
  protected
    function CreateMiLista: TMiLista; dynamic;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property MiLista: TMiLista read FMiLista write SetMiLista;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Ejemplos', [TMiComponente]);
end;

{ TMiComponente }

constructor TMiComponente.Create(AOwner: TComponent);
begin
  inherited;

  FMiLista := CreateMiLista;
end;

function TMiComponente.CreateMiLista: TMiLista;
begin
  Result := TMiLista.Create(Self, TMiItem);
end;

destructor TMiComponente.Destroy;
begin
  FMiLista.Free;

  inherited;
end;

procedure TMiComponente.SetMiLista(const Value: TMiLista);
begin
  FMiLista := Value;
end;

{ TMiItem }

procedure TMiItem.Assign(Source: TPersistent);
begin
  if Source is TMiItem then
  begin
    if Assigned(Collection) then
      Collection.BeginUpdate;
    try
      FAlignment := TMiItem(Source).Alignment;
    finally
      if Assigned(Collection) then
        Collection.EndUpdate;
    end;
  end else
    inherited Assign(Source);
end;

function TMiItem.GetDisplayName: string;
begin
//  Result := 'Nombre q quieras ver en el inspector';
//  if Result = '' then
    Result := inherited GetDisplayName;
end;

function TMiItem.GetMiComponente: TMiComponente;
begin
  if Assigned(Collection) and (Collection is TMiLista) then
    Result := TMiLista(Collection).MiComponente
  else
    Result := nil;
end;

{ TMiLista }

function TMiLista.Add: TMiItem;
begin
  Result := TMiItem(inherited Add);
end;

constructor TMiLista.Create(MiComponente: TMiComponente;
  InfoListClass: TMiItemClass);
begin
  inherited Create(InfoListClass);
  FMiComponente := MiComponente;
end;

function TMiLista.GetMiItem(Index: Integer): TMiItem;
begin
  Result := TMiItem(inherited Items[Index]);
end;

function TMiLista.GetOwner: TPersistent;
begin
  Result := FMiComponente;
end;

procedure TMiLista.SetMiItem(Index: Integer; Value: TMiItem);
begin
  Items[Index].Assign(Value);
end;

procedure TMiLista.Update(Item: TCollectionItem);
begin
  inherited;

  // Actualizacion forma global o por item segun sea "Item"
end;

end.
Responder Con Cita