Changeset 584 for Common/ItemList.pas
- Timestamp:
- Jan 17, 2025, 8:00:49 AM (12 days ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/ItemList.pas
r583 r584 83 83 public 84 84 type 85 TAddEvent = function 85 TAddEvent = function(constref AValue: TItem): SizeInt of object; 86 86 TGetCountEvent = function: SizeInt of object; 87 87 TSetItemEvent = procedure(Index: SizeInt; AValue: TItem) of object; 88 TGetNameEvent = procedure 88 TGetNameEvent = procedure(out Name: string) of object; 89 89 TGetItemEvent = function(Index: SizeInt): TItem of object; 90 90 TGetItemFieldsEvent = function: TItemFields of object; 91 91 TRemoveEvent = function(constref AValue: TItem): SizeInt of object; 92 TGetNextAvailableNameEvent = procedure 92 TGetNextAvailableNameEvent = procedure(Name: string; out NewName: string) of object; 93 93 TCreateItemEvent = function(Name: string = ''): TItem of object; 94 TFindByIdEvent = function(Id: Integer): TItem of object; 94 95 private 95 96 FOnAdd: TAddEvent; 96 97 FOnCreateItem: TCreateItemEvent; 98 FOnFindById: TFindByIdEvent; 97 99 FOnGetCount: TGetCountEvent; 98 100 FOnGetItem: TGetItemEvent; … … 112 114 function GetNextAvailableName(Name: string): string; 113 115 function GetItemFields: TItemFields; 116 function FindById(Id: Integer): TItem; 114 117 property Count: SizeInt read GetCount; 115 118 property Items[Index: SizeInt]: TItem read GetItem write SetItem; default; … … 125 128 property OnCreateItem: TCreateItemEvent read FOnCreateItem 126 129 write FOnCreateItem; 130 property OnFindById: TFindByIdEvent read FOnFindById 131 write FOnFindById; 127 132 end; 128 133 … … 133 138 FBaseItemList: TBaseItemList; 134 139 procedure RecalculateNewId(Reset: Boolean); 135 procedure RecalculateItemsId;136 140 function BaseGetItem(Index: SizeInt): TItem; 137 141 procedure BaseSetItem(Index: SizeInt; AValue: TItem); … … 142 146 function BaseGetItemFields: TItemFields; 143 147 function BaseCreateItem(Name: string = ''): TItem; 148 function BaseFindById(Id: Integer): TItem; 144 149 procedure BaseGetNextAvailableName(Name: string; out NewName: string); 145 150 public 146 151 NewId: Integer; 152 procedure RecalculateItemsId; 147 153 function CreateItem(Name: string = ''): T; virtual; 148 154 function IncrementName(Name: string): string; … … 245 251 end; 246 252 253 function TItemList<T>.BaseFindById(Id: Integer): TItem; 254 begin 255 Result := FindById(Id); 256 end; 257 247 258 procedure TItemList<T>.LoadFromNode(Node: TDOMNode); 248 259 var … … 265 276 NewNode2: TDOMNode; 266 277 begin 267 RecalculateItemsId;268 278 for I := 0 to Count - 1 do 269 279 with TItem(Items[I]) do begin … … 287 297 FBaseItemList.OnGetNextAvailableName := BaseGetNextAvailableName; 288 298 FBaseItemList.OnGetName := BaseGetName; 299 FBaseItemList.OnFindById := BaseFindById; 289 300 NewId := 1; 290 301 end; … … 368 379 NewName: string); 369 380 begin 370 NewName := Name ;381 NewName := Name + ' 1'; 371 382 while Assigned(FindByName(NewName)) do 372 383 NewName := IncrementName(NewName); … … 484 495 ReadId: Integer; 485 496 ReferenceList: TBaseItemList; 497 RefItem: TItem; 486 498 begin 487 499 if Field.DataType = dtString then begin … … 503 515 ReadId := ReadInteger(Node, Field.SysName, 0); 504 516 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; 507 523 end else 508 524 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); … … 818 834 end; 819 835 836 function TBaseItemList.FindById(Id: Integer): TItem; 837 begin 838 if Assigned(FOnFindById) then Result := FOnFindById(Id) 839 else raise Exception.Create('Undefined FindById handler'); 840 end; 841 820 842 end. 821 843
Note:
See TracChangeset
for help on using the changeset viewer.