1 | unit ItemList;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, DOM, XML, Common, Graphics, Math;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TItemList = class;
|
---|
10 | TUndefinedEnum = (eeNone);
|
---|
11 |
|
---|
12 | TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor,
|
---|
13 | dtTime, dtDate, dtDateTime, dtEnumeration, dtReference);
|
---|
14 |
|
---|
15 | { TItemField }
|
---|
16 |
|
---|
17 | TItemField = class
|
---|
18 | SysName: string;
|
---|
19 | Name: string;
|
---|
20 | Index: Integer;
|
---|
21 | DataType: TDataType;
|
---|
22 | Position: TPoint;
|
---|
23 | Size: TPoint;
|
---|
24 | EnumStates: TStringList;
|
---|
25 | VisibleIfIndex: Integer;
|
---|
26 | constructor Create;
|
---|
27 | destructor Destroy; override;
|
---|
28 | procedure Assign(Source: TItemField);
|
---|
29 | end;
|
---|
30 |
|
---|
31 | { TItemFields }
|
---|
32 |
|
---|
33 | TItemFields = class(TObjectList<TItemField>)
|
---|
34 | function AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField;
|
---|
35 | function SearchByIndex(Index: Integer): TItemField;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TItem }
|
---|
39 |
|
---|
40 | TItem = class
|
---|
41 | private
|
---|
42 | procedure AssignValue(Source: TItem; Field: TItemField);
|
---|
43 | function CompareValue(Item: TItem; Field: TItemField): Boolean;
|
---|
44 | procedure LoadValueFromNode(Node: TDOMNode; Field: TItemField); virtual;
|
---|
45 | procedure SaveValueToNode(Node: TDOMNode; Field: TItemField); virtual;
|
---|
46 | public
|
---|
47 | Id: Integer;
|
---|
48 | Name: string;
|
---|
49 | class function GetFields: TItemFields; virtual;
|
---|
50 | function GetField(Index: Integer): TItemField;
|
---|
51 | procedure GetValue(Index: Integer; out Value); virtual;
|
---|
52 | function GetValueInteger(Index: Integer): Integer;
|
---|
53 | function GetValueString(Index: Integer): string;
|
---|
54 | function GetValueColor(Index: Integer): TColor;
|
---|
55 | function GetValueBoolean(Index: Integer): Boolean;
|
---|
56 | function GetValueEnumeration(Index: Integer): TUndefinedEnum;
|
---|
57 | function GetValueReference(Index: Integer): TItem;
|
---|
58 | function GetValueAsText(Index: Integer): string;
|
---|
59 | procedure SetValue(Index: Integer; var Value); virtual;
|
---|
60 | procedure SetValueInteger(Index: Integer; Value: Integer);
|
---|
61 | procedure SetValueString(Index: Integer; Value: string);
|
---|
62 | procedure SetValueColor(Index: Integer; Value: TColor);
|
---|
63 | procedure SetValueBoolean(Index: Integer; Value: Boolean);
|
---|
64 | procedure SetValueEnumeration(Index: Integer; Value: TUndefinedEnum);
|
---|
65 | procedure SetValueReference(Index: Integer; Value: TItem);
|
---|
66 | procedure Assign(Source: TItem); virtual;
|
---|
67 | function Compare(Item: TItem): Boolean; virtual;
|
---|
68 | function ToString: string; override;
|
---|
69 | procedure LoadFromNode(Node: TDOMNode); virtual;
|
---|
70 | procedure SaveToNode(Node: TDOMNode); virtual;
|
---|
71 | class function GetClassSysName: string; virtual;
|
---|
72 | class function GetClassName: string; virtual;
|
---|
73 | function GetReferenceList(Index: Integer): TItemList; virtual;
|
---|
74 | constructor Create; virtual;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | TItemClass = class of TItem;
|
---|
78 |
|
---|
79 | { TItemList }
|
---|
80 |
|
---|
81 | TItemList = class(TObjectList<TItem>)
|
---|
82 | private
|
---|
83 | procedure RecalculateNewId(Reset: Boolean);
|
---|
84 | procedure RecalculateItemsId;
|
---|
85 | public
|
---|
86 | NewId: Integer;
|
---|
87 | class function GetItemClass: TItemClass; virtual;
|
---|
88 | function IncrementName(Name: string): string;
|
---|
89 | function GetNextAvailableName(Name: string): string;
|
---|
90 | function FindById(Id: Integer): TItem;
|
---|
91 | function FindByName(Name: string): TItem;
|
---|
92 | function GetNewId: Integer;
|
---|
93 | function ToString: string; override;
|
---|
94 | procedure Assign(Source: TItemList); virtual;
|
---|
95 | function Compare(ItemList: TItemList): Boolean; virtual;
|
---|
96 | function AddItem(Name: string = ''): TItem; virtual;
|
---|
97 | function CreateItem(Name: string = ''): TItem; virtual;
|
---|
98 | procedure LoadFromNode(Node: TDOMNode); virtual;
|
---|
99 | procedure SaveToNode(Node: TDOMNode); virtual;
|
---|
100 | constructor Create(FreeObjects: Boolean = True);
|
---|
101 | end;
|
---|
102 |
|
---|
103 | const
|
---|
104 | DataTypeStr: array[TDataType] of string = ('None', 'String', 'Boolean',
|
---|
105 | 'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration',
|
---|
106 | 'Reference');
|
---|
107 |
|
---|
108 | resourcestring
|
---|
109 | SUnsupportedDataType = 'Unsupported field value data type %s';
|
---|
110 | SUnsupportedValueIndex = 'Unsupported value index %d';
|
---|
111 |
|
---|
112 |
|
---|
113 | implementation
|
---|
114 |
|
---|
115 | resourcestring
|
---|
116 | SYes = 'Yes';
|
---|
117 | SNo = 'No';
|
---|
118 | SItem = 'Item';
|
---|
119 | SName = 'Name';
|
---|
120 |
|
---|
121 | { TItemField }
|
---|
122 |
|
---|
123 | constructor TItemField.Create;
|
---|
124 | begin
|
---|
125 | EnumStates := TStringList.Create;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | destructor TItemField.Destroy;
|
---|
129 | begin
|
---|
130 | FreeAndNil(EnumStates);
|
---|
131 | inherited;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TItemField.Assign(Source: TItemField);
|
---|
135 | begin
|
---|
136 | SysName := Source.SysName;
|
---|
137 | Name := Source.Name;
|
---|
138 | Index := Source.Index;
|
---|
139 | DataType := Source.DataType;
|
---|
140 | Position := Source.Position;
|
---|
141 | Size := Source.Size;
|
---|
142 | EnumStates.Assign(Source.EnumStates);
|
---|
143 | end;
|
---|
144 |
|
---|
145 | { TItemList }
|
---|
146 |
|
---|
147 | procedure TItemList.Assign(Source: TItemList);
|
---|
148 | var
|
---|
149 | I: Integer;
|
---|
150 | begin
|
---|
151 | while Count > Source.Count do Delete(Count - 1);
|
---|
152 | while Count < Source.Count do AddItem('');
|
---|
153 | for I := 0 to Count - 1 do
|
---|
154 | TItem(Items[I]).Assign(Source.Items[I]);
|
---|
155 | end;
|
---|
156 |
|
---|
157 | function TItemList.Compare(ItemList: TItemList): Boolean;
|
---|
158 | var
|
---|
159 | I: Integer;
|
---|
160 | begin
|
---|
161 | Result := Count = ItemList.Count;
|
---|
162 | if not Result then Exit;
|
---|
163 | for I := 0 to Count - 1 do begin
|
---|
164 | Result := Result and TItem(Items[I]).Compare(ItemList.Items[I]);
|
---|
165 | if not Result then Break;
|
---|
166 | end;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | function TItemList.AddItem(Name: string): TItem;
|
---|
170 | begin
|
---|
171 | Result := CreateItem(Name);
|
---|
172 | Result.Id := GetNewId;
|
---|
173 | Add(Result);
|
---|
174 | end;
|
---|
175 |
|
---|
176 | function TItemList.CreateItem(Name: string): TItem;
|
---|
177 | begin
|
---|
178 | Result := GetItemClass.Create;
|
---|
179 | Result.Name := Name;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | procedure TItemList.LoadFromNode(Node: TDOMNode);
|
---|
183 | var
|
---|
184 | Node2: TDOMNode;
|
---|
185 | NewItem: TItem;
|
---|
186 | begin
|
---|
187 | Count := 0;
|
---|
188 | Node2 := Node.FirstChild;
|
---|
189 | while Assigned(Node2) and (Node2.NodeName = UnicodeString(GetItemClass.GetClassSysName)) do begin
|
---|
190 | NewItem := CreateItem;
|
---|
191 | NewItem.LoadFromNode(Node2);
|
---|
192 | Add(NewItem);
|
---|
193 | Node2 := Node2.NextSibling;
|
---|
194 | end;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TItemList.SaveToNode(Node: TDOMNode);
|
---|
198 | var
|
---|
199 | I: Integer;
|
---|
200 | NewNode2: TDOMNode;
|
---|
201 | begin
|
---|
202 | RecalculateItemsId;
|
---|
203 | for I := 0 to Count - 1 do
|
---|
204 | with TItem(Items[I]) do begin
|
---|
205 | NewNode2 := Node.OwnerDocument.CreateElement(UnicodeString(GetItemClass.GetClassSysName));
|
---|
206 | Node.AppendChild(NewNode2);
|
---|
207 | SaveToNode(NewNode2);
|
---|
208 | end;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | constructor TItemList.Create(FreeObjects: Boolean);
|
---|
212 | begin
|
---|
213 | inherited;
|
---|
214 | NewId := 1;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | procedure TItemList.RecalculateNewId(Reset: Boolean);
|
---|
218 | var
|
---|
219 | I: Integer;
|
---|
220 | begin
|
---|
221 | NewId := 1;
|
---|
222 | for I := 0 to Count - 1 do
|
---|
223 | with TItem(Items[I]) do begin
|
---|
224 | NewId := Max(NewId, Id + 1);
|
---|
225 | end;
|
---|
226 | end;
|
---|
227 |
|
---|
228 | procedure TItemList.RecalculateItemsId;
|
---|
229 | var
|
---|
230 | I: Integer;
|
---|
231 | begin
|
---|
232 | for I := 0 to Count - 1 do
|
---|
233 | Items[I].Id := I + 1;
|
---|
234 | NewId := Count + 1;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | class function TItemList.GetItemClass: TItemClass;
|
---|
238 | begin
|
---|
239 | Result := TItem;
|
---|
240 | end;
|
---|
241 |
|
---|
242 | function TItemList.IncrementName(Name: string): string;
|
---|
243 | var
|
---|
244 | I: Integer;
|
---|
245 | Num: Integer;
|
---|
246 | begin
|
---|
247 | I := LastPos(' ', Name);
|
---|
248 | if I > 0 then begin
|
---|
249 | if TryStrToInt(Copy(Name, I + 1, Length(Name)), Num) then
|
---|
250 | Result := Trim(Copy(Name, 1, I - 1)) + ' ' + IntToStr(Num + 1)
|
---|
251 | else Result := Name + ' 2';
|
---|
252 | end else Result := Name + ' 2';
|
---|
253 | end;
|
---|
254 |
|
---|
255 | function TItemList.GetNextAvailableName(Name: string): string;
|
---|
256 | begin
|
---|
257 | Result := Name;
|
---|
258 | while Assigned(FindByName(Result)) do
|
---|
259 | Result := IncrementName(Result);
|
---|
260 | end;
|
---|
261 |
|
---|
262 | function TItemList.FindById(Id: Integer): TItem;
|
---|
263 | var
|
---|
264 | I: Integer;
|
---|
265 | begin
|
---|
266 | I := 0;
|
---|
267 | while (I < Count) and (Items[I].Id <> Id) do Inc(I);
|
---|
268 | if I < Count then Result := Items[I]
|
---|
269 | else Result := nil;
|
---|
270 | end;
|
---|
271 |
|
---|
272 | function TItemList.FindByName(Name: string): TItem;
|
---|
273 | var
|
---|
274 | I: Integer;
|
---|
275 | begin
|
---|
276 | I := 0;
|
---|
277 | while (I < Count) and (Items[I].Name <> Name) do Inc(I);
|
---|
278 | if I < Count then Result := Items[I]
|
---|
279 | else Result := nil;
|
---|
280 | end;
|
---|
281 |
|
---|
282 | function TItemList.GetNewId: Integer;
|
---|
283 | begin
|
---|
284 | Result := NewId;
|
---|
285 | Inc(NewId);
|
---|
286 | end;
|
---|
287 |
|
---|
288 | function TItemList.ToString: string;
|
---|
289 | var
|
---|
290 | I: Integer;
|
---|
291 | begin
|
---|
292 | Result := '';
|
---|
293 | for I := 0 to Count - 1 do
|
---|
294 | with TItem(Items[I]) do begin
|
---|
295 | Result := Result + ToString + LineEnding;
|
---|
296 | end;
|
---|
297 | end;
|
---|
298 |
|
---|
299 | { TItemFields }
|
---|
300 |
|
---|
301 | function TItemFields.AddField(Index: Integer; SysName, Name: string; DataType: TDataType): TItemField;
|
---|
302 | begin
|
---|
303 | Result := TItemField.Create;
|
---|
304 | Result.Index := Index;
|
---|
305 | Result.Name := Name;
|
---|
306 | Result.SysName := SysName;
|
---|
307 | Result.DataType := DataType;
|
---|
308 | Add(Result);
|
---|
309 | end;
|
---|
310 |
|
---|
311 | function TItemFields.SearchByIndex(Index: Integer): TItemField;
|
---|
312 | var
|
---|
313 | I: Integer;
|
---|
314 | begin
|
---|
315 | I := 0;
|
---|
316 | while (I < Count) and (Items[I].Index <> Index) do Inc(I);
|
---|
317 | if I < Count then Result := Items[I]
|
---|
318 | else Result := nil;
|
---|
319 | end;
|
---|
320 |
|
---|
321 | { TItem }
|
---|
322 |
|
---|
323 | procedure TItem.AssignValue(Source: TItem; Field: TItemField);
|
---|
324 | begin
|
---|
325 | if Field.DataType = dtString then begin
|
---|
326 | SetValueString(Field.Index, Source.GetValueString(Field.Index));
|
---|
327 | end else
|
---|
328 | if Field.DataType = dtColor then begin
|
---|
329 | SetValueColor(Field.Index, Source.GetValueColor(Field.Index));
|
---|
330 | end else
|
---|
331 | if Field.DataType = dtInteger then begin
|
---|
332 | SetValueInteger(Field.Index, Source.GetValueInteger(Field.Index));
|
---|
333 | end else
|
---|
334 | if Field.DataType = dtBoolean then begin
|
---|
335 | SetValueBoolean(Field.Index, Source.GetValueBoolean(Field.Index));
|
---|
336 | end else
|
---|
337 | if Field.DataType = dtEnumeration then begin
|
---|
338 | SetValueEnumeration(Field.Index, Source.GetValueEnumeration(Field.Index));
|
---|
339 | end else
|
---|
340 | if Field.DataType = dtReference then begin
|
---|
341 | SetValueReference(Field.Index, Source.GetValueReference(Field.Index));
|
---|
342 | end else
|
---|
343 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
344 | end;
|
---|
345 |
|
---|
346 | function TItem.CompareValue(Item: TItem; Field: TItemField): Boolean;
|
---|
347 | begin
|
---|
348 | if Field.DataType = dtString then begin
|
---|
349 | Result := GetValueString(Field.Index) = Item.GetValueString(Field.Index);
|
---|
350 | end else
|
---|
351 | if Field.DataType = dtColor then begin
|
---|
352 | Result := GetValueColor(Field.Index) = Item.GetValueColor(Field.Index);
|
---|
353 | end else
|
---|
354 | if Field.DataType = dtInteger then begin
|
---|
355 | Result := GetValueInteger(Field.Index) = Item.GetValueInteger(Field.Index);
|
---|
356 | end else
|
---|
357 | if Field.DataType = dtBoolean then begin
|
---|
358 | Result := GetValueBoolean(Field.Index) = Item.GetValueBoolean(Field.Index);
|
---|
359 | end else
|
---|
360 | if Field.DataType = dtEnumeration then begin
|
---|
361 | Result := GetValueEnumeration(Field.Index) = Item.GetValueEnumeration(Field.Index);
|
---|
362 | end else
|
---|
363 | if Field.DataType = dtReference then begin
|
---|
364 | Result := GetValueReference(Field.Index) = Item.GetValueReference(Field.Index);
|
---|
365 | end else
|
---|
366 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
367 | end;
|
---|
368 |
|
---|
369 | procedure TItem.LoadValueFromNode(Node: TDOMNode; Field: TItemField);
|
---|
370 | var
|
---|
371 | ReadId: Integer;
|
---|
372 | ReferenceList: TItemList;
|
---|
373 | begin
|
---|
374 | if Field.DataType = dtString then begin
|
---|
375 | SetValueString(Field.Index, ReadString(Node, Field.SysName, ''));
|
---|
376 | end else
|
---|
377 | if Field.DataType = dtColor then begin
|
---|
378 | SetValueColor(Field.Index, ReadInteger(Node, Field.SysName, 0));
|
---|
379 | end else
|
---|
380 | if Field.DataType = dtInteger then begin
|
---|
381 | SetValueInteger(Field.Index, ReadInteger(Node, Field.SysName, 0));
|
---|
382 | end else
|
---|
383 | if Field.DataType = dtBoolean then begin
|
---|
384 | SetValueBoolean(Field.Index, ReadBoolean(Node, Field.SysName, False));
|
---|
385 | end else
|
---|
386 | if Field.DataType = dtEnumeration then begin
|
---|
387 | SetValueEnumeration(Field.Index, TUndefinedEnum(ReadInteger(Node, Field.SysName, 0)));
|
---|
388 | end else
|
---|
389 | if Field.DataType = dtReference then begin
|
---|
390 | ReadId := ReadInteger(Node, Field.SysName, 0);
|
---|
391 | ReferenceList := GetReferenceList(Field.Index);
|
---|
392 | if (ReadId > 0) and Assigned(ReferenceList) then
|
---|
393 | SetValueReference(Field.Index, TItem(ReferenceList[ReadId]));
|
---|
394 | end else
|
---|
395 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
396 | end;
|
---|
397 |
|
---|
398 | procedure TItem.SaveValueToNode(Node: TDOMNode; Field: TItemField);
|
---|
399 | var
|
---|
400 | Item: TItem;
|
---|
401 | begin
|
---|
402 | if Field.DataType = dtString then begin
|
---|
403 | WriteString(Node, Field.SysName, GetValueString(Field.Index));
|
---|
404 | end else
|
---|
405 | if Field.DataType = dtColor then begin
|
---|
406 | WriteInteger(Node, Field.SysName, GetValueColor(Field.Index));
|
---|
407 | end else
|
---|
408 | if Field.DataType = dtInteger then begin
|
---|
409 | WriteInteger(Node, Field.SysName, GetValueInteger(Field.Index));
|
---|
410 | end else
|
---|
411 | if Field.DataType = dtBoolean then begin
|
---|
412 | WriteBoolean(Node, Field.SysName, GetValueBoolean(Field.Index));
|
---|
413 | end else
|
---|
414 | if Field.DataType = dtEnumeration then begin
|
---|
415 | WriteInteger(Node, Field.SysName, Integer(GetValueEnumeration(Field.Index)));
|
---|
416 | end else
|
---|
417 | if Field.DataType = dtReference then begin
|
---|
418 | Item := TItem(GetValueReference(Field.Index));
|
---|
419 | if Assigned(Item) then WriteInteger(Node, Field.SysName, Item.Id)
|
---|
420 | else WriteInteger(Node, Field.SysName, 0);
|
---|
421 | end else
|
---|
422 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
423 | end;
|
---|
424 |
|
---|
425 | class function TItem.GetFields: TItemFields;
|
---|
426 | begin
|
---|
427 | Result := TItemFields.Create;
|
---|
428 | Result.AddField(1, 'Name', SName, dtString);
|
---|
429 | end;
|
---|
430 |
|
---|
431 | function TItem.GetField(Index: Integer): TItemField;
|
---|
432 | var
|
---|
433 | Fields: TItemFields;
|
---|
434 | begin
|
---|
435 | Result := TItemField.Create;
|
---|
436 | Fields := GetFields;
|
---|
437 | try
|
---|
438 | Result.Assign(Fields.SearchByIndex(Index));
|
---|
439 | finally
|
---|
440 | Fields.Free;
|
---|
441 | end;
|
---|
442 | end;
|
---|
443 |
|
---|
444 | procedure TItem.GetValue(Index: Integer; out Value);
|
---|
445 | begin
|
---|
446 | raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
|
---|
447 | end;
|
---|
448 |
|
---|
449 | function TItem.GetValueString(Index: Integer): string;
|
---|
450 | begin
|
---|
451 | GetValue(Index, Result);
|
---|
452 | end;
|
---|
453 |
|
---|
454 | function TItem.GetValueInteger(Index: Integer): Integer;
|
---|
455 | begin
|
---|
456 | GetValue(Index, Result);
|
---|
457 | end;
|
---|
458 |
|
---|
459 | function TItem.GetValueColor(Index: Integer): TColor;
|
---|
460 | begin
|
---|
461 | GetValue(Index, Result);
|
---|
462 | end;
|
---|
463 |
|
---|
464 | function TItem.GetValueBoolean(Index: Integer): Boolean;
|
---|
465 | begin
|
---|
466 | GetValue(Index, Result);
|
---|
467 | end;
|
---|
468 |
|
---|
469 | function TItem.GetValueEnumeration(Index: Integer): TUndefinedEnum;
|
---|
470 | begin
|
---|
471 | GetValue(Index, Result);
|
---|
472 | end;
|
---|
473 |
|
---|
474 | function TItem.GetValueReference(Index: Integer): TItem;
|
---|
475 | begin
|
---|
476 | GetValue(Index, Result);
|
---|
477 | end;
|
---|
478 |
|
---|
479 | function TItem.GetValueAsText(Index: Integer): string;
|
---|
480 | var
|
---|
481 | Field: TItemField;
|
---|
482 | Item: TItem;
|
---|
483 | begin
|
---|
484 | Field := GetField(Index);
|
---|
485 | try
|
---|
486 | if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index))
|
---|
487 | else if Field.DataType = dtString then Result := GetValueString(Index)
|
---|
488 | else if Field.DataType = dtColor then Result := ''
|
---|
489 | else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))]
|
---|
490 | else if Field.DataType = dtReference then begin
|
---|
491 | Item := TItem(GetValueReference(Index));
|
---|
492 | if Assigned(Item) then Result := Item.Name
|
---|
493 | else Result := '';
|
---|
494 | end else if Field.DataType = dtBoolean then begin
|
---|
495 | if GetValueBoolean(Index) then Result := SYes else Result := SNo;
|
---|
496 | end else
|
---|
497 | raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
|
---|
498 | finally
|
---|
499 | Field.Free;
|
---|
500 | end;
|
---|
501 | end;
|
---|
502 |
|
---|
503 | procedure TItem.SetValue(Index: Integer; var Value);
|
---|
504 | begin
|
---|
505 | raise Exception.Create(Format(SUnsupportedValueIndex, [Index]));
|
---|
506 | end;
|
---|
507 |
|
---|
508 | procedure TItem.SetValueInteger(Index: Integer; Value: Integer);
|
---|
509 | begin
|
---|
510 | SetValue(Index, Value);
|
---|
511 | end;
|
---|
512 |
|
---|
513 | procedure TItem.SetValueString(Index: Integer; Value: string);
|
---|
514 | begin
|
---|
515 | SetValue(Index, Value);
|
---|
516 | end;
|
---|
517 |
|
---|
518 | procedure TItem.SetValueColor(Index: Integer; Value: TColor);
|
---|
519 | begin
|
---|
520 | SetValue(Index, Value);
|
---|
521 | end;
|
---|
522 |
|
---|
523 | procedure TItem.SetValueBoolean(Index: Integer; Value: Boolean);
|
---|
524 | begin
|
---|
525 | SetValue(Index, Value);
|
---|
526 | end;
|
---|
527 |
|
---|
528 | procedure TItem.SetValueEnumeration(Index: Integer;
|
---|
529 | Value: TUndefinedEnum);
|
---|
530 | begin
|
---|
531 | SetValue(Index, Value);
|
---|
532 | end;
|
---|
533 |
|
---|
534 | procedure TItem.SetValueReference(Index: Integer; Value: TItem);
|
---|
535 | begin
|
---|
536 | SetValue(Index, Value);
|
---|
537 | end;
|
---|
538 |
|
---|
539 | procedure TItem.Assign(Source: TItem);
|
---|
540 | var
|
---|
541 | I: Integer;
|
---|
542 | Fields: TItemFields;
|
---|
543 | begin
|
---|
544 | Id := Source.Id;
|
---|
545 | if Source is ClassType then begin
|
---|
546 | Fields := GetFields;
|
---|
547 | try
|
---|
548 | for I := 0 to Fields.Count - 1 do
|
---|
549 | AssignValue(Source, Fields[I]);
|
---|
550 | finally
|
---|
551 | Fields.Free;
|
---|
552 | end;
|
---|
553 | end;
|
---|
554 | end;
|
---|
555 |
|
---|
556 | function TItem.Compare(Item: TItem): Boolean;
|
---|
557 | var
|
---|
558 | I: Integer;
|
---|
559 | Fields: TItemFields;
|
---|
560 | begin
|
---|
561 | Result := True;
|
---|
562 | Result := Result and (Id = Item.Id);
|
---|
563 | if Item is ClassType then begin
|
---|
564 | Fields := GetFields;
|
---|
565 | try
|
---|
566 | for I := 0 to Fields.Count - 1 do begin
|
---|
567 | Result := Result and CompareValue(Item, Fields[I]);
|
---|
568 | if not Result then Break;
|
---|
569 | end;
|
---|
570 | finally
|
---|
571 | Fields.Free;
|
---|
572 | end;
|
---|
573 | end;
|
---|
574 | end;
|
---|
575 |
|
---|
576 | function TItem.ToString: string;
|
---|
577 | var
|
---|
578 | Fields: TItemFields;
|
---|
579 | I: Integer;
|
---|
580 | begin
|
---|
581 | Result := 'Id: ' + IntToStr(Id) + LineEnding;
|
---|
582 | Fields := GetFields;
|
---|
583 | try
|
---|
584 | for I := 0 to Fields.Count - 1 do begin
|
---|
585 | Result := Result + Fields[I].SysName + ': ' + GetValueAsText(Fields[I].Index) + LineEnding;
|
---|
586 | end;
|
---|
587 | finally
|
---|
588 | Fields.Free;
|
---|
589 | end;
|
---|
590 | end;
|
---|
591 |
|
---|
592 | procedure TItem.LoadFromNode(Node: TDOMNode);
|
---|
593 | var
|
---|
594 | Fields: TItemFields;
|
---|
595 | I: Integer;
|
---|
596 | begin
|
---|
597 | Id := ReadInteger(Node, 'Id', 0);
|
---|
598 | Fields := GetFields;
|
---|
599 | try
|
---|
600 | for I := 0 to Fields.Count - 1 do begin
|
---|
601 | LoadValueFromNode(Node, Fields[I]);
|
---|
602 | end;
|
---|
603 | finally
|
---|
604 | Fields.Free;
|
---|
605 | end;
|
---|
606 | end;
|
---|
607 |
|
---|
608 | procedure TItem.SaveToNode(Node: TDOMNode);
|
---|
609 | var
|
---|
610 | Fields: TItemFields;
|
---|
611 | I: Integer;
|
---|
612 | begin
|
---|
613 | WriteInteger(Node, 'Id', Id);
|
---|
614 | Fields := GetFields;
|
---|
615 | try
|
---|
616 | for I := 0 to Fields.Count - 1 do begin
|
---|
617 | SaveValueToNode(Node, Fields[I]);
|
---|
618 | end;
|
---|
619 | finally
|
---|
620 | Fields.Free;
|
---|
621 | end;
|
---|
622 | end;
|
---|
623 |
|
---|
624 | class function TItem.GetClassSysName: string;
|
---|
625 | begin
|
---|
626 | Result := 'Item';
|
---|
627 | end;
|
---|
628 |
|
---|
629 | class function TItem.GetClassName: string;
|
---|
630 | begin
|
---|
631 | Result := SItem;
|
---|
632 | end;
|
---|
633 |
|
---|
634 | function TItem.GetReferenceList(Index: Integer): TItemList;
|
---|
635 | begin
|
---|
636 | Result := nil;
|
---|
637 | end;
|
---|
638 |
|
---|
639 | constructor TItem.Create;
|
---|
640 | begin
|
---|
641 | end;
|
---|
642 |
|
---|
643 | end.
|
---|
644 |
|
---|