Changeset 584 for Common/ItemList.pas


Ignore:
Timestamp:
Jan 17, 2025, 8:00:49 AM (12 days ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/ItemList.pas

    r583 r584  
    8383  public
    8484  type
    85     TAddEvent = function (constref AValue: TItem): SizeInt of object;
     85    TAddEvent = function(constref AValue: TItem): SizeInt of object;
    8686    TGetCountEvent = function: SizeInt of object;
    8787    TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object;
    88     TGetNameEvent = procedure (out Name: string) of object;
     88    TGetNameEvent = procedure(out Name: string) of object;
    8989    TGetItemEvent = function(Index: SizeInt): TItem of object;
    9090    TGetItemFieldsEvent = function: TItemFields of object;
    9191    TRemoveEvent = function(constref AValue: TItem): SizeInt of object;
    92     TGetNextAvailableNameEvent = procedure (Name: string; out NewName: string) of object;
     92    TGetNextAvailableNameEvent = procedure(Name: string; out NewName: string) of object;
    9393    TCreateItemEvent = function(Name: string = ''): TItem of object;
     94    TFindByIdEvent = function(Id: Integer): TItem of object;
    9495  private
    9596    FOnAdd: TAddEvent;
    9697    FOnCreateItem: TCreateItemEvent;
     98    FOnFindById: TFindByIdEvent;
    9799    FOnGetCount: TGetCountEvent;
    98100    FOnGetItem: TGetItemEvent;
     
    112114    function GetNextAvailableName(Name: string): string;
    113115    function GetItemFields: TItemFields;
     116    function FindById(Id: Integer): TItem;
    114117    property Count: SizeInt read GetCount;
    115118    property Items[Index: SizeInt]: TItem read GetItem write SetItem; default;
     
    125128    property OnCreateItem: TCreateItemEvent read FOnCreateItem
    126129      write FOnCreateItem;
     130    property OnFindById: TFindByIdEvent read FOnFindById
     131      write FOnFindById;
    127132  end;
    128133
     
    133138    FBaseItemList: TBaseItemList;
    134139    procedure RecalculateNewId(Reset: Boolean);
    135     procedure RecalculateItemsId;
    136140    function BaseGetItem(Index: SizeInt): TItem;
    137141    procedure BaseSetItem(Index: SizeInt; AValue: TItem);
     
    142146    function BaseGetItemFields: TItemFields;
    143147    function BaseCreateItem(Name: string = ''): TItem;
     148    function BaseFindById(Id: Integer): TItem;
    144149    procedure BaseGetNextAvailableName(Name: string; out NewName: string);
    145150  public
    146151    NewId: Integer;
     152    procedure RecalculateItemsId;
    147153    function CreateItem(Name: string = ''): T; virtual;
    148154    function IncrementName(Name: string): string;
     
    245251end;
    246252
     253function TItemList<T>.BaseFindById(Id: Integer): TItem;
     254begin
     255  Result := FindById(Id);
     256end;
     257
    247258procedure TItemList<T>.LoadFromNode(Node: TDOMNode);
    248259var
     
    265276  NewNode2: TDOMNode;
    266277begin
    267   RecalculateItemsId;
    268278  for I := 0 to Count - 1 do
    269279  with TItem(Items[I]) do begin
     
    287297  FBaseItemList.OnGetNextAvailableName := BaseGetNextAvailableName;
    288298  FBaseItemList.OnGetName := BaseGetName;
     299  FBaseItemList.OnFindById := BaseFindById;
    289300  NewId := 1;
    290301end;
     
    368379  NewName: string);
    369380begin
    370   NewName := Name;
     381  NewName := Name + ' 1';
    371382  while Assigned(FindByName(NewName)) do
    372383    NewName := IncrementName(NewName);
     
    484495  ReadId: Integer;
    485496  ReferenceList: TBaseItemList;
     497  RefItem: TItem;
    486498begin
    487499  if Field.DataType = dtString then begin
     
    503515    ReadId := ReadInteger(Node, Field.SysName, 0);
    504516    ReferenceList := GetReferenceList(Field.Index);
    505     if (ReadId > 0) and Assigned(ReferenceList) then
    506       SetValueReference(Field.Index, TItem(ReferenceList[ReadId]));
     517    if (ReadId > 0) and Assigned(ReferenceList) then begin
     518      RefItem := ReferenceList.FindById(ReadId);
     519      if Assigned(RefItem) then
     520        SetValueReference(Field.Index, RefItem)
     521        else raise Exception.Create('Reference id ' + IntToStr(ReadId) + ' not found.');
     522    end;
    507523  end else
    508524    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     
    818834end;
    819835
     836function TBaseItemList.FindById(Id: Integer): TItem;
     837begin
     838  if Assigned(FOnFindById) then Result := FOnFindById(Id)
     839    else raise Exception.Create('Undefined FindById handler');
     840end;
     841
    820842end.
    821843
Note: See TracChangeset for help on using the changeset viewer.