Changeset 298 for trunk/UItemList.pas
- Timestamp:
- Jul 10, 2019, 5:35:15 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UItemList.pas
r296 r298 9 9 10 10 type 11 TItemList = class; 11 12 TUndefinedEnum = (eeNone); 12 13 13 14 TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor, 14 dtTime, dtDate, dtDateTime, dtEnumeration );15 dtTime, dtDate, dtDateTime, dtEnumeration, dtReference); 15 16 16 17 { TItemField } … … 54 55 function GetValueBoolean(Index: Integer): Boolean; 55 56 function GetValueEnumeration(Index: Integer): TUndefinedEnum; 57 function GetValueReference(Index: Integer): TItem; 56 58 function GetValueAsText(Index: Integer): string; 57 59 procedure SetValue(Index: Integer; var Value); virtual; … … 61 63 procedure SetValueBoolean(Index: Integer; Value: Boolean); 62 64 procedure SetValueEnumeration(Index: Integer; Value: TUndefinedEnum); 65 procedure SetValueReference(Index: Integer; Value: TItem); 63 66 procedure Assign(Source: TItem); virtual; 64 67 procedure LoadFromNode(Node: TDOMNode); virtual; … … 66 69 class function GetClassSysName: string; virtual; 67 70 class function GetClassName: string; virtual; 71 function GetReferenceList(Index: Integer): TItemList; virtual; 72 constructor Create; virtual; 68 73 end; 69 74 … … 81 86 function GetNewId: Integer; 82 87 procedure Assign(Source: TItemList); virtual; 83 function AddItem(Name: string): TItem; virtual; 88 function AddItem(Name: string = ''): TItem; virtual; 89 function CreateItem(Name: string = ''): TItem; virtual; 84 90 procedure LoadFromNode(Node: TDOMNode); virtual; 85 91 procedure SaveToNode(Node: TDOMNode); virtual; … … 89 95 const 90 96 DataTypeStr: array[TDataType] of string = ('None', 'String', 'Boolean', 91 'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration'); 97 'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration', 98 'Reference'); 92 99 93 100 resourcestring … … 136 143 while Count < Source.Count do AddItem(''); 137 144 for I := 0 to Count - 1 do 138 Items[I].Assign(Source.Items[I]);145 TItem(Items[I]).Assign(Source.Items[I]); 139 146 end; 140 147 141 148 function TItemList.AddItem(Name: string): TItem; 149 begin 150 Result := CreateItem(Name); 151 Result.Id := GetNewId; 152 Add(Result); 153 end; 154 155 function TItemList.CreateItem(Name: string): TItem; 142 156 begin 143 157 Result := GetItemClass.Create; 144 158 Result.Name := Name; 145 Result.Id := GetNewId;146 Add(Result);147 159 end; 148 160 … … 155 167 Node2 := Node.FirstChild; 156 168 while Assigned(Node2) and (Node2.NodeName = UnicodeString(GetItemClass.GetClassSysName)) do begin 157 NewItem := GetItemClass.Create;169 NewItem := CreateItem; 158 170 NewItem.LoadFromNode(Node2); 159 171 Add(NewItem); … … 177 189 constructor TItemList.Create(FreeObjects: Boolean); 178 190 begin 179 inherited Create(FreeObjects);191 inherited; 180 192 NewId := 1; 181 193 end; … … 273 285 SetValueEnumeration(Field.Index, Source.GetValueEnumeration(Field.Index)); 274 286 end else 287 if Field.DataType = dtReference then begin 288 SetValueReference(Field.Index, Source.GetValueReference(Field.Index)); 289 end else 275 290 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); 276 291 end; 277 292 278 293 procedure TItem.LoadValueFromNode(Node: TDOMNode; Field: TItemField); 294 var 295 ReadId: Integer; 296 ReferenceList: TItemList; 279 297 begin 280 298 if Field.DataType = dtString then begin … … 293 311 SetValueEnumeration(Field.Index, TUndefinedEnum(ReadInteger(Node, Field.SysName, 0))); 294 312 end else 313 if Field.DataType = dtReference then begin 314 ReadId := ReadInteger(Node, Field.SysName, 0); 315 ReferenceList := GetReferenceList(Field.Index); 316 if (ReadId > 0) and Assigned(ReferenceList) then 317 SetValueReference(Field.Index, TItem(ReferenceList.List[ReadId])); 318 end else 295 319 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); 296 320 end; 297 321 298 322 procedure TItem.SaveValueToNode(Node: TDOMNode; Field: TItemField); 323 var 324 Item: TItem; 299 325 begin 300 326 if Field.DataType = dtString then begin … … 312 338 if Field.DataType = dtEnumeration then begin 313 339 WriteInteger(Node, Field.SysName, Integer(GetValueEnumeration(Field.Index))); 340 end else 341 if Field.DataType = dtReference then begin 342 Item := TItem(GetValueReference(Field.Index)); 343 if Assigned(Item) then WriteInteger(Node, Field.SysName, Item.Id) 344 else WriteInteger(Node, Field.SysName, 0); 314 345 end else 315 346 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); … … 329 360 Fields := GetFields; 330 361 try 331 Result.Assign(Fields [Index]);362 Result.Assign(Fields.SearchByIndex(Index)); 332 363 finally 333 364 Fields.Free; … … 365 396 end; 366 397 398 function TItem.GetValueReference(Index: Integer): TItem; 399 begin 400 GetValue(Index, Result); 401 end; 402 367 403 function TItem.GetValueAsText(Index: Integer): string; 368 404 var 369 Fields: TItemFields;370 405 Field: TItemField; 371 begin 372 Fields := GetFields; 373 Field := GetField(Fields.IndexOf(Fields.SearchByIndex(Index))); 374 if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index)) 375 else if Field.DataType = dtString then Result := GetValueString(Index) 376 else if Field.DataType = dtColor then Result := '' 377 else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))] 378 else if Field.DataType = dtBoolean then begin 379 if GetValueBoolean(Index) then Result := SYes else Result := SNo; 380 end else 381 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); 382 Field.Free; 383 Fields.Free; 406 Item: TItem; 407 begin 408 Field := GetField(Index); 409 try 410 if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index)) 411 else if Field.DataType = dtString then Result := GetValueString(Index) 412 else if Field.DataType = dtColor then Result := '' 413 else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))] 414 else if Field.DataType = dtReference then begin 415 Item := TItem(GetValueReference(Index)); 416 if Assigned(Item) then Result := Item.Name 417 else Result := ''; 418 end else if Field.DataType = dtBoolean then begin 419 if GetValueBoolean(Index) then Result := SYes else Result := SNo; 420 end else 421 raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]])); 422 finally 423 Field.Free; 424 end; 384 425 end; 385 426 … … 411 452 procedure TItem.SetValueEnumeration(Index: Integer; 412 453 Value: TUndefinedEnum); 454 begin 455 SetValue(Index, Value); 456 end; 457 458 procedure TItem.SetValueReference(Index: Integer; Value: TItem); 413 459 begin 414 460 SetValue(Index, Value); … … 474 520 end; 475 521 522 function TItem.GetReferenceList(Index: Integer): TItemList; 523 begin 524 Result := nil; 525 end; 526 527 constructor TItem.Create; 528 begin 529 end; 530 476 531 end. 477 532
Note:
See TracChangeset
for help on using the changeset viewer.