summaryrefslogtreecommitdiff
path: root/extras/tiopf/gui/tiListMediators.pas
blob: 89b9913501ce9490b5deaf9be8f4bddf1fe390ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
{
  Abstract mediating views for GUI list controls. This allows you to use
  standard list components and make them object-aware.  See the demo
  application for usage.
}
unit tiListMediators;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  tiBaseMediator,
  fpg_listview,
  fpg_grid,
  fpg_listbox,
  tiObject;

type
  { Composite mediator for TfpgListView }
  TtiListViewMediatorView = class(TtiCustomListMediatorView)
  private
    FObserversInTransit: TList;
  protected
    function    GetSelectedObject: TtiObject; override;
    procedure   SetSelectedObject(const AValue: TtiObject); override;
    procedure   CreateColumns; override;
    function    DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator; override;
    procedure   DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); override;
    procedure   SetupGUIandObject; override;
    procedure   ClearList; override;
    procedure   RebuildList; override;
    procedure   SetActive(const AValue: Boolean); override;
  public
    constructor CreateCustom(AModel: TtiObjectList; AView: TfpgListView; ADisplayNames: string; AIsObserving: Boolean = True); overload;
    constructor CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TtiOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean = True); overload;
    class function ComponentClass: TClass; override;
    constructor Create; override;
    destructor  Destroy; override;
    function    View: TfpgListView; reintroduce;
    procedure   HandleSelectionChanged; override;
    function    GetObjectFromItem(AItem: TfpgLVItem): TtiObject;
  end;


  { Composite mediator for TfpgStringGrid }
  TtiStringGridMediatorView = class(TtiCustomListMediatorView)
  private
    function    DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator; override;
    procedure   DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); override;
  protected
    function    GetSelectedObject: TtiObject; override;
    procedure   SetSelectedObject(const AValue: TtiObject); override;
    procedure   CreateColumns; override;
    procedure   SetupGUIandObject; override;
    procedure   ClearList; override;
    procedure   RebuildList; override;
  public
    constructor CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean = True); reintroduce; overload;
    destructor  Destroy; override;
    class function ComponentClass: TClass; override;
    function    GetObjectFromRow(ARow: Integer): TtiObject;
    function    View: TfpgStringGrid; reintroduce;
  end;


  { Used internally for sub-mediators in ListView mediator. Moved to interface
    section so it can be overridden. }
  TtiListViewListItemMediator = class(TtiListItemMediator)
  private
    FView: TfpgLVItem;
    procedure SetupFields; virtual;
  public
    constructor CreateCustom(AModel: TtiObject; AView: TfpgLVItem; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean = True); reintroduce; overload;
    constructor CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TtiOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean = True); reintroduce; overload;
    procedure BeforeDestruction; override;
    procedure Update(ASubject: TtiObject); override;
  published
    property View: TfpgLVItem read FView;
  end;


  { Used internally for sub-mediators in StringGrid mediator. Moved to interface
    section so it can be overridden. }
  TtiStringGridRowMediator = class(TtiListItemMediator)
  private
    FView: TfpgStringGrid;
    FRowIndex: integer;
  public
    constructor CreateCustom(AModel: TtiObject; AGrid: TfpgStringGrid; const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean = True);
    procedure Update(ASubject: TtiObject); override;
  published
    property View: TfpgStringGrid read FView;
    property RowIndex: integer read FRowIndex;
  end;


  { Composite mediator for TfpgListBox }
  TtiListBoxMediatorView = class(TtiCustomListMediatorView)
  private
    function    DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator; override;
    procedure   DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); override;
  protected
    function    GetSelectedObject: TtiObject; override;
    procedure   SetSelectedObject(const AValue: TtiObject); override;
    procedure   CreateColumns; override;
    procedure   ClearList; override;
    procedure   RebuildList; override;
    procedure   SetupGUIandObject; override;
  public
    constructor CreateCustom(AModel: TtiObjectList; AListBox: TfpgListBox; ADisplayNames: string; AIsObserving: Boolean = True); reintroduce; overload;
    destructor  Destroy; override;
    class function ComponentClass: TClass; override;
    function    GetObjectFromRow(ARow: Integer): TtiObject;
    function    View: TfpgListBox; reintroduce;
  end;

  { Used internally for sub-mediators in ListBox mediator. Moved to interface
    section so it can be overridden. }
  TtiListBoxItemMediator = class(TtiListItemMediator)
  private
    FView: TfpgListBox;
    FRowIndex: integer;
  public
    constructor CreateCustom(AModel: TtiObject; AListBox: TfpgListBox; const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean = True);
    procedure   Update(ASubject: TtiObject); override;
  published
    property    View: TfpgListBox read FView;
    property    RowIndex: integer read FRowIndex;
  end;


procedure RegisterFallBackListMediators;


implementation

uses
  tiRTTI;


procedure RegisterFallBackListMediators;
begin
  gMediatorManager.RegisterMediator(TtiListViewMediatorView, TtiObjectList);
  gMediatorManager.RegisterMediator(TtiStringGridMediatorView, TtiObjectList);
  gMediatorManager.RegisterMediator(TtiListBoxMediatorView, TtiObjectList);
end;

{ TtiListViewMediatorView }

procedure TtiListViewMediatorView.SetSelectedObject(const AValue: TtiObject);
var
  i: integer;
begin
  for i := 0 to View.Items.Count - 1 do
    if TtiObject(View.Items.Item[i].UserData) = AValue then
    begin
      View.ItemIndex := i;
      HandleSelectionChanged;   {$Note Is this line required?}
      Exit; //==>
    end;
end;

function TtiListViewMediatorView.GetSelectedObject: TtiObject;
begin
  Result := GetObjectFromItem(View.Items.Item[View.ItemIndex]);
end;

function TtiListViewMediatorView.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator;
var
  li: TfpgLVItem;
begin
  DataAndPropertyValid(AData);
  { Create ListItem and Mediator }
  View.BeginUpdate;
  try
    li := TfpgLVItem.Create(View.Items);
    View.Items.Add(li);
    Result := TtiListViewListItemMediator.CreateCustom(AData, li, OnBeforeSetupField, FieldsInfo, Active);
    li.UserData := Result;
    MediatorList.Add(Result);
  finally
    View.EndUpdate;
  end;
end;

procedure TtiListViewMediatorView.DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator);
begin
  View.Items.Delete(View.Items.IndexOf(TtiListViewListItemMediator(AMediator).View));
  inherited DoDeleteItemMediator(AIndex, AMediator);
end;

procedure TtiListViewMediatorView.CreateColumns;
var
  c: integer;
  lc: TfpgLVColumn;
  lInfo: TtiMediatorFieldInfo;
begin
  if (View.Columns.Count<>FieldsInfo.Count) then
    View.Columns.Clear;
  if View.Columns.Count = 0 then
  begin
    for c := 0 to FieldsInfo.Count-1 do
    begin
      lInfo        := FieldsInfo[c];
      lc           := TfpgLVColumn.Create(View.Columns);
      lc.AutoSize  := False;
      lc.Caption   := lInfo.Caption;
      lc.Width     := lInfo.FieldWidth;
//      lc.CaptionAlignment:= lInfo.Alignment;
      lc.Alignment := lInfo.Alignment;
      lc.Resizable := True;
      View.Columns.Add(lc);
    end;
  end;
end;

procedure TtiListViewMediatorView.SetupGUIandObject;
begin
  { Setup TfpgListView defaults }
  View.Columns.Clear;
  View.Items.Clear;
  //  FView.ViewStyle         := vsReport;
  View.ShowHeaders := True;
  //  FView.RowSelect         := True;
  //  FView.AutoSize          := False;
  //  FView.ScrollBars        := ssAutoBoth;
end;

procedure TtiListViewMediatorView.ClearList;
begin
  MediatorList.Clear;
  if View <> nil then
    View.Items.Clear;
end;

procedure TtiListViewMediatorView.RebuildList;
begin
  View.BeginUpdate;
  try
    CreateColumns;
    CreateSubMediators;
  finally
    View.EndUpdate;
  end;
end;

procedure TtiListViewMediatorView.SetActive(const AValue: Boolean);
begin
  if not AValue then
    ClearList;
  inherited SetActive(AValue);
end;

constructor TtiListViewMediatorView.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TtiOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean);
begin
  Create; // don't forget this
  OnBeforeSetupField := AOnBeforeSetupField;
  DisplayNames := ADisplayNames;  // Will call ParseDisplaynames.
  Subject := AModel;
  SetView(AView);                 // Will call SetupGUIandObject;
  CreateSubMediators;
  Active := AIsObserving;         // Will attach/Detach
end;

class function TtiListViewMediatorView.ComponentClass: TClass;
begin
  Result := TfpgListView;
end;

constructor TtiListViewMediatorView.Create;
begin
  inherited Create;
  FObserversInTransit := TList.Create;
end;

constructor TtiListViewMediatorView.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; ADisplayNames: string; AIsObserving: Boolean);
begin
  CreateCustom(AModel, AView, nil, ADisplayNames, AIsObserving);
end;

destructor TtiListViewMediatorView.Destroy;
begin
  IsObserving := False;
  FObserversInTransit.Free;
  inherited Destroy;
end;

function TtiListViewMediatorView.View: TfpgListView;
begin
  Result := TfpgListView(inherited View);
end;

procedure TtiListViewMediatorView.HandleSelectionChanged;
var
  i: integer;
begin
  if View.ItemIndex = -1 then
    SelectedObject := nil
  else
  begin
    FObserversInTransit.Clear;
    { If an item is already selected, assign the item's List of observers to a
      temporary container. This is done so that the same observers can be
      assigned to the new item. }
    if Assigned(SelectedObject) then
      FObserversInTransit.Assign(SelectedObject.ObserverList);

    // Assign Newly selected item to SelectedObject Obj.
    SelectedObject := TtiObject(View.Items.Item[View.ItemIndex].UserData);

    { If an object was selected, copy the old item's observer List
      to the new item's observer List. }
    if FObserversInTransit.Count > 0 then
      SelectedObject.ObserverList.Assign(FObserversInTransit);

    { Set the Observers Subject property to the selected object }
    for i := 0 to SelectedObject.ObserverList.Count - 1 do
      TtiMediatorView(SelectedObject.ObserverList.Items[i]).Subject :=
        SelectedObject;

    // execute the NotifyObservers event to update the observers.
    SelectedObject.NotifyObservers;
  end;
end;

function TtiListViewMediatorView.GetObjectFromItem(AItem: TfpgLVItem): TtiObject;
begin
  if (AItem = nil) or (AItem.UserData = nil) then
    Result := nil
  else
    Result := TtiListItemMediator(AItem.UserData).Model;
end;


{ TtiListViewListItemMediator }

procedure TtiListViewListItemMediator.SetupFields;
var
  c: integer;
  lMemberName: string;
  lValue: string;
begin
  lMemberName := FFieldsInfo[0].PropName;
  lValue      := tiGetProperty(Model, lMemberName);
  if Assigned(OnBeforeSetupField) then
    OnBeforeSetupField(Model, lMemberName, lValue);
  FView.Caption := lValue;
  for c := 1 to FFieldsInfo.Count - 1 do
  begin
    lMemberName := FFieldsInfo[c].PropName;
    lValue      := tiGetProperty(Model, lMemberName);
    if Assigned(OnBeforeSetupField) then
      OnBeforeSetupField(Model, lMemberName, lValue);
    FView.SubItems.Add(lValue);
  end;
end;

constructor TtiListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean);
begin
  CreateCustom(AModel, AView, nil, AFieldsInfo, IsObserving);
end;

constructor TtiListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TtiOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean);
begin
  inherited Create;
  Model      := AModel;
  FView       := AView;
  FFieldsInfo := AFieldsInfo;
  OnBeforeSetupField := AOnBeforeSetupField;
  SetupFields;
  Active      := IsObserving; // Will attach
end;

procedure TtiListViewListItemMediator.BeforeDestruction;
begin
  Model.DetachObserver(self);
  Model := nil;
  FView  := nil;
  inherited BeforeDestruction;
end;

procedure TtiListViewListItemMediator.Update(ASubject: TtiObject);
var
  c: integer;
  lMemberName: string;
  lValue: string;
begin
  Assert(Model = ASubject);

  lMemberName := FFieldsInfo[0].PropName;
  lValue      := tiGetProperty(Model, lMemberName);
  if Assigned(OnBeforeSetupField) then
    OnBeforeSetupField(Model, lMemberName, lValue);

  FView.Caption := lValue;

  for c := 1 to FFieldsInfo.Count - 1 do
  begin
    lMemberName := FFieldsInfo[c].PropName;
    lValue      := Model.PropValue[lMemberName];
    if Assigned(OnBeforeSetupField) then
      OnBeforeSetupField(Model, lMemberName, lValue);
    FView.SubItems[c - 1] := lValue;
  end;
end;


{ TtiStringGridMediatorView }

function TtiStringGridMediatorView.GetSelectedObject: TtiObject;
begin
  Result := GetObjectFromRow(View.FocusRow);
end;

procedure TtiStringGridMediatorView.SetSelectedObject(const AValue: TtiObject);
var
  i: integer;
  o: TObject;
begin
  for i := 0 to View.RowCount - 1 do
  begin
    o := View.Objects[0, i];
    if Assigned(o) and (TtiListItemMediator(o).Model = AValue) then
    begin
      View.FocusRow := i;
      Exit; //==>
    end;
  end;  { for }
end;

function TtiStringGridMediatorView.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator;
var
  i: integer;
  lFieldName: string;
begin
  View.BeginUpdate;
  try
    if ARowIdx = View.RowCount then // In case of add notification
      View.RowCount := View.RowCount+1;
    for i := 0 to FieldsInfo.Count - 1 do
    begin
      lFieldName := FieldsInfo[i].PropName;
      View.Cells[i, ARowIdx] := tiGetProperty(AData, lFieldName);  // set Cell text
    end;
    Result := TtiStringGridRowMediator.CreateCustom(AData, View, FieldsInfo, ARowIdx, Active);
    View.Objects[0, ARowIdx] := Result;   // set Object reference inside grid. It used to be AData.
    MediatorList.Add(Result);
  finally
    View.EndUpdate;
  end;
end;

procedure TtiStringGridMediatorView.DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator);
begin
  View.DeleteRow(AIndex);
  inherited DoDeleteItemMediator(AIndex, AMediator);
end;

procedure TtiStringGridMediatorView.CreateColumns;
var
  i: integer;
  lColumnTotalWidth: integer;
  lGridNonContentWidth: integer;
  lLastColumnWidth: integer;
begin
  lColumnTotalWidth := 0;
  // Grid is 2px border left + right, 1px col gridline separator, 15px vertical scrollbar width
  lGridNonContentWidth := 2 + 2 + (FieldsInfo.Count - 1) + 15;
  for i := 0 to FieldsInfo.Count - 1 do
  begin
    View.ColumnWidth[i]       := FieldsInfo[i].FieldWidth;
    View.ColumnTitle[i]       := FieldsInfo[i].Caption;
    View.Columns[i].Alignment := FieldsInfo[i].Alignment;
    //resize the last column to fill the grid.
    if i = FieldsInfo.Count - 1 then
    begin
      if View.Width > (lColumnTotalWidth + lGridNonContentWidth) then
      begin
        lLastColumnWidth := View.Width - (lColumnTotalWidth + lGridNonContentWidth);
        if lLastColumnWidth > 10 then
          View.ColumnWidth[i] := lLastColumnWidth;
      end;
    end
    else
      lColumnTotalWidth := lColumnTotalWidth + View.ColumnWidth[i];
  end;
end;

procedure TtiStringGridMediatorView.SetupGUIandObject;
begin
  //Setup default properties for the StringGrid
  View.RowSelect   := True;
  View.ColumnCount := FieldsInfo.Count;
  if ShowDeleted then
    View.RowCount := Model.Count
  else
    View.RowCount := Model.CountNotDeleted;
end;

procedure TtiStringGridMediatorView.ClearList;
begin
  MediatorList.Clear;
  if View <> nil then
    View.RowCount := 1; {$Note Double check if this is desired. Shouldn't it be 0 instead. }
end;

procedure TtiStringGridMediatorView.RebuildList;
begin
//  writeln('--- TStringGridMediator.RebuildList');
  { This rebuilds the whole list. Not very efficient. }
  View.BeginUpdate;
  try
    SetupGUIandObject;
    MediatorList.Clear;
    CreateSubMediators;
  finally
    View.EndUpdate;
  end;
end;

constructor TtiStringGridMediatorView.CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean);
begin
  inherited Create;
  DisplayNames := ADisplayNames;
  Subject := AModel;
  SetView(AGrid);
  CreateSubMediators;
  IsObserving := AIsObserving;
end;

destructor TtiStringGridMediatorView.Destroy;
begin
  IsObserving := False;
  inherited Destroy;
end;

class function TtiStringGridMediatorView.ComponentClass: TClass;
begin
  Result := TfpgStringGrid;
end;

function TtiStringGridMediatorView.GetObjectFromRow(ARow: Integer): TtiObject;
var
  O: TObject;
begin
  if View.RowCount = 0 then
  begin
    Result := nil;
    Exit;
  end;

  if ARow = -1 then
    Result := nil
  else
  begin
    O := View.Objects[0, ARow];
    if O <> nil then
      Result := TtiListItemMediator(O).Model
    else
      Result := nil;
  end;
end;

function TtiStringGridMediatorView.View: TfpgStringGrid;
begin
  Result := TfpgStringGrid(inherited View);
end;


{ TtiStringGridRowMediator }

constructor TtiStringGridRowMediator.CreateCustom(AModel: TtiObject; AGrid: TfpgStringGrid; const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean);
begin
  inherited Create;
  Model      := AModel;
  FView       := AGrid;
  FFieldsInfo := AFieldsInfo;
  FRowIndex   := ARowIndex;
  Active      := IsObserving; // Will attach
end;

procedure TtiStringGridRowMediator.Update(ASubject: TtiObject);
var
  i: integer;
  lFieldName: string;
  lValue: string;
begin
  Assert(Model = ASubject);
  for i := 0 to FFieldsInfo.Count - 1 do
  begin
    lFieldName := FFieldsInfo[I].PropName;
    lValue     := tiGetProperty(Model, lFieldName);
    if Assigned(OnBeforeSetupField) then
      OnBeforeSetupField(Model, lFieldName, lValue);
    FView.Cells[i, FRowIndex] := lValue;
  end;
end;


{ TtiListBoxMediatorView }

function TtiListBoxMediatorView.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer): TtiListItemMediator;
var
  i: integer;
  lFieldName: string;
begin
  View.BeginUpdate;
  try
    if FieldsInfo.Count > 0 then    // only take the first field it if exists
    begin
      lFieldName := FieldsInfo[0].PropName;
      View.Items.Add(tiGetProperty(AData, lFieldName));  // set Cell text
    end
    else
      View.Items.Add(AData.Caption); // the default fallback

    Result := TtiListBoxItemMediator.CreateCustom(AData, View, FieldsInfo, ARowIdx, Active);
    View.Items.Objects[ARowIdx] := Result;   // set Object reference inside grid. It used to be AData.
    MediatorList.Add(Result);
  finally
    View.EndUpdate;
  end;
end;

procedure TtiListBoxMediatorView.DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator);
begin
  View.Items.Delete(AIndex);
  inherited DoDeleteItemMediator(AIndex, AMediator);
end;

function TtiListBoxMediatorView.GetSelectedObject: TtiObject;
begin
  Result := GetObjectFromRow(View.FocusItem);
end;

procedure TtiListBoxMediatorView.SetSelectedObject(const AValue: TtiObject);
var
  i: integer;
  o: TObject;
begin
  for i := 0 to View.ItemCount - 1 do
  begin
    o := View.Items.Objects[i];
    if Assigned(o) and (TtiListItemMediator(o).Model = AValue) then
    begin
      View.FocusItem := i;
      Exit; //==>
    end;
  end;  { for }
//  inherited SetSelectedObject(AValue);
end;

procedure TtiListBoxMediatorView.CreateColumns;
begin
  // do nothing - we don't support columns
end;

procedure TtiListBoxMediatorView.ClearList;
begin
  MediatorList.Clear;
  if View <> nil then
    View.Items.Clear;
end;

procedure TtiListBoxMediatorView.RebuildList;
begin
  { This rebuilds the whole list. Not very efficient. }
  View.BeginUpdate;
  try
    SetupGUIandObject;
    MediatorList.Clear;
//    CreateColumns;
    CreateSubMediators;
  finally
    View.EndUpdate;
  end;
end;

procedure TtiListBoxMediatorView.SetupGUIandObject;
begin
  View.Items.Clear;
  inherited SetupGUIandObject;
end;

constructor TtiListBoxMediatorView.CreateCustom(AModel: TtiObjectList;
  AListBox: TfpgListBox; ADisplayNames: string; AIsObserving: Boolean);
begin
  inherited Create;
  DisplayNames := ADisplayNames;
  Subject := AModel;
  SetView(AListBox);
  CreateSubMediators;
  IsObserving := AIsObserving;
end;

destructor TtiListBoxMediatorView.Destroy;
begin
  IsObserving := False;
  inherited Destroy;
end;

class function TtiListBoxMediatorView.ComponentClass: TClass;
begin
  Result := TfpgListView;
end;

function TtiListBoxMediatorView.GetObjectFromRow(ARow: Integer): TtiObject;
var
  O: TObject;
begin
  if View.ItemCount = 0 then
  begin
    Result := nil;
    Exit;
  end;

  if ARow = -1 then
    Result := nil
  else
  begin
    O := View.Items.Objects[ARow];
    if O <> nil then
      Result := TtiListItemMediator(O).Model
    else
      Result := nil;
  end;
end;

function TtiListBoxMediatorView.View: TfpgListBox;
begin
  Result := TfpgListBox(inherited View);
end;

{ TtiListBoxItemMediator }

constructor TtiListBoxItemMediator.CreateCustom(AModel: TtiObject; AListBox: TfpgListBox;
  const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean);
begin
  inherited Create;
  Model      := AModel;
  FView       := AListBox;
  FFieldsInfo := AFieldsInfo;
  FRowIndex   := ARowIndex;
  Active      := IsObserving; // Will attach
end;

procedure TtiListBoxItemMediator.Update(ASubject: TtiObject);
var
  i: integer;
  lFieldName: string;
  lValue: string;
  s: string;
begin
  Assert(Model = ASubject);
  s := '';
  for i := 0 to FFieldsInfo.Count - 1 do
  begin
    lFieldName := FFieldsInfo[I].PropName;
    lValue := tiGetProperty(Model, lFieldName);
    if Assigned(OnBeforeSetupField) then
      OnBeforeSetupField(Model, lFieldName, lValue);
    s := s + ', ' + lValue;
  end;
  FView.Items[FRowIndex] := s;
//  inherited Update(ASubject);
end;

end.