summaryrefslogtreecommitdiff
path: root/extras/tiopf/gui/tiGenericListMediators.pas
blob: 52aa60ed161d426c06ddba8b46b2329d2209a2b6 (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
(*

Revision history:

  2005-09-01: First release by Graeme Geldenhuys (graemeg@gmail.com)
  2007-08-27: Ported the code to the fpGUI toolkit.  [Graeme]

Purpose:
  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.

ToDo:
  * Unit tests
  * More refactoring
  * Implement a View Manager class, so we can remove the View Lists
    created in each Form using mediating views.
  * TfpgListView mediator implementation - I first need to double check how
    far the TfpgListView control itself has been implemented.

*)

unit tiGenericListMediators;

{$mode objfpc}{$H+}

interface
uses
  tiObject
  ,tiGenericEditMediators
  ,gfx_widget
  ,gui_listbox
  ,gui_combobox
  ,gui_listview
  ,gui_menu
  ,Classes
  ;


type
  { Used so we know what needs updating, the Internal List, or just the
    Selected Object. }
  TUpdateMode = (umSelectedObject, umObjectList);

  { Abstract class that observes a list object }

  { TListMediator }

  TListMediator = class(TtiObject)
  private
    FObjectList: TtiObjectList;
    FControl: TfpgWidget;
    FSelectedObject: TtiObject;
    FShowDeleted: Boolean;
    procedure   SetShowDeleted(const Value: Boolean);
  protected
    FObserversInTransit: TList;
    FUpdateMode: TUpdateMode;
    FPopupMenu: TfpgPopupMenu;
    procedure   SetSelectedObject(const Value: TtiObject); virtual;
    function    GetModel: TtiObjectList; virtual;
    procedure   SetModel(const Value: TtiObjectList); virtual;
    function    GetView: TfpgWidget; virtual;
    procedure   SetView(const Value: TfpgWidget); virtual;
    procedure   RebuildList; virtual; abstract;
    { Used to setup things like the MaxLength of a edit box, etc. }
    procedure   SetupGUIandObject; virtual;
    procedure   BuildPopupMenu; virtual;
  public
    constructor Create; override;
    constructor CreateCustom(pObjectList: TtiObjectList; pView: TfpgWidget); virtual;
    destructor  Destroy; override;
    procedure   Update(pSubject: TtiObject); override;

    { Called from GUI to trigger events }
    procedure   HandleDeleteItem; virtual;
    procedure   HandleListChanged; virtual;
    procedure   HandleSelectionChanged; virtual; abstract;
    procedure   MenuItemAddClick(Sender: TObject); virtual;
    procedure   MenuItemEditClick(Sender: TObject); virtual;
    procedure   MenuItemDeleteClick(Sender: TObject); virtual;

    property    SelectedObject: TtiObject read FSelectedObject write SetSelectedObject;
    property    ShowDeleted: Boolean read FShowDeleted write SetShowDeleted;
    property    Model: TtiObjectList read GetModel write SetModel;
    property    View: TfpgWidget read GetView;
  end;


  { Observes a list object - TfpgListBox }

  TListBoxMediator = class(TListMediator)
  private
    OldPos: Integer;
    NewPos: Integer;
  protected
    procedure   SetSelectedObject(const Value: TtiObject); override;
    function    GetView: TfpgListBox; reintroduce;
    procedure   RebuildList; override;
    procedure   SaveBookmark;
    procedure   RestoreBookmark;
  public
    procedure   HandleSelectionChanged; override;
  published
    property    View: TfpgListBox read GetView;
  end;


  { Observes a list object - TfpgComboBox }

  TComboBoxMediator = class(TListMediator)
  protected
    procedure   SetSelectedObject(const Value: TtiObject); override;
    function    GetView: TfpgComboBox; reintroduce;
    procedure   RebuildList; override;
  public
    procedure   HandleSelectionChanged; override;
  published
    property    View: TfpgComboBox read GetView;
  end;


  { Observes a list object - TListView }
(*
  TListViewMediator = class(TListMediator)
  protected
    function    GetView: TfpgListView; reintroduce;
    procedure   RebuildList; override;
  public
    procedure   HandleSelectionChanged; override;
  published
    property    View: TfpgListView read GetView;
  end;
*)

implementation
uses
  SysUtils
  ;


{ TListBoxMediator }

procedure TListBoxMediator.SetSelectedObject(const Value: TtiObject);
var
  i: integer;
begin
  inherited SetSelectedObject(Value);
  
  if Value = nil then
  begin
    View.FocusItem := 0;
    exit; //==>
  end;

  for i := 0 to Pred(Model.Count) do
  begin
    if Value.OID.AsString = Model.Items[i].OID.AsString then
    begin
      View.FocusItem := i+1;  // fpGUI is 1-based
      exit; //==>
    end;
  end;
end;

function TListBoxMediator.GetView: TfpgListBox;
begin
  result := TfpgListBox(inherited GetView);
end;

procedure TListBoxMediator.HandleSelectionChanged;
var
  i: integer;
begin
  if View.FocusItem = 0 then
    FSelectedObject := nil
  else
  begin
    { 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(FSelectedObject) then
      FObserversInTransit.Assign(FSelectedObject.ObserverList);

    // Assign Newly selected item to SelectedObject Obj.
    FSelectedObject := TtiObject(View.Items.Objects[View.FocusItem-1]);

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

    { set the observers's Subject property to the selected object }
    for i := 0 to FSelectedObject.ObserverList.Count - 1 do
    begin
      TMediatorView(FSelectedObject.ObserverList.Items[i]).Subject :=
          FSelectedObject;
    end;

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


procedure TListBoxMediator.RebuildList;
var
  i: Integer;
  ptr: TNotifyEvent;
  selected: integer;
begin
  selected := 0;
  if (Model.CountNotDeleted) >= View.FocusItem then
  begin
    selected := View.FocusItem;
  end;

  ptr := View.OnSelect;
  View.OnSelect := nil;
  View.Items.BeginUpdate;
  try
    View.Items.Clear;
    for i := 0 to Pred(Model.Count) do
    begin
      if (not Model.Items[i].Deleted) or
         (ShowDeleted and Model.Items[i].Deleted) then
      begin
        View.Items.AddObject(Model.Items[i].Caption, Model.Items[i]);
      end;
    end;
    if Model.CountNotDeleted > 0 then
    begin
      if selected = 0 then
        selected := 1;
      View.FocusItem := selected;
    end;
  finally
    View.Items.EndUpdate;
    view.Update;
    View.OnSelect := ptr;
    HandleSelectionChanged;
  end;
end;


procedure TListBoxMediator.RestoreBookmark;
begin
  if OldPos > View.Items.Count then
    NewPos := View.Items.Count
  else if OldPos = 0 then
    NewPos := 0
  else
    NewPos := OldPos;
  View.FocusItem := NewPos;
  HandleSelectionChanged;
end;

procedure TListBoxMediator.SaveBookmark;
begin
  OldPos := View.FocusItem;
end;


{ TComboBoxMediator }

procedure TComboBoxMediator.SetSelectedObject(const Value: TtiObject);
var
  i: integer;
begin
  inherited SetSelectedObject(Value);
  
  if Value = nil then
  begin
    View.FocusItem := 0;
    exit; //==>
  end;
  
  for i := 0 to Pred(Model.Count) do
  begin
    if Value = Model.Items[i] then
    begin
      View.FocusItem := i+1;  // fpGUI is 1-based
      exit; //==>
    end;
  end;
end;

function TComboBoxMediator.GetView: TfpgComboBox;
begin
  result := TfpgComboBox(inherited GetView);
end;


procedure TComboBoxMediator.HandleSelectionChanged;
var
  i: integer;
begin
  if View.FocusItem = 0 then
    SelectedObject := nil
  else
  begin
    if Assigned(SelectedObject) then
      FObserversInTransit.Assign(SelectedObject.ObserverList);

    SelectedObject := TtiObject(View.Items.Objects[View.FocusItem-1]);

    if FObserversInTransit.Count > 0 then
      SelectedObject.ObserverList.Assign(FObserversInTransit);

    for i := 0 to SelectedObject.ObserverList.Count - 1 do
    begin
      TMediatorView(SelectedObject.ObserverList.Items[i]).Subject :=
          SelectedObject;
    end;

    SelectedObject.NotifyObservers;
  end;
end;


procedure TComboBoxMediator.RebuildList;
var
  i: Integer;
  ptr: TNotifyEvent;
  selected: integer;
begin
  selected := 0;
  if (Model.CountNotDeleted-1) >= View.FocusItem then
    selected := View.FocusItem;

  ptr := View.OnChange;
  View.OnChange := nil;
  View.Items.BeginUpdate;
  try
    View.Items.Clear;
    for i := 0 to Pred(Model.Count) do
    begin
      if (not Model.Items[i].Deleted) or
         (ShowDeleted and Model.Items[i].Deleted) then
      begin
        View.Items.AddObject( Model.Items[i].Caption, Model.Items[i] );
      end;
    end;
    if Model.CountNotDeleted > 0 then
    begin
      if selected = 0 then
        selected := 1;
      View.FocusItem := selected;
    end;
  finally
    View.Items.EndUpdate;
    View.OnChange := ptr;
    HandleSelectionChanged;
  end;
end;


{ TListViewMediator }

(*
function TListViewMediator.GetView: TfpgListView;
begin
  result := TfpgListView(inherited GetView);
end;


procedure TListViewMediator.HandleSelectionChanged;
var
  i: integer;
begin
  if not Assigned(View.Selected) then
    SelectedObject := nil
  else
  begin
    if Assigned(SelectedObject) then  // and Assigned(SelectedObject.ObserverList)
      FObserversInTransit.Assign( SelectedObject.ObserverList);

    SelectedObject := TtiObject(View.Selected.Data);

    if FObserversInTransit.Count > 0 then
      SelectedObject.ObserverList.Assign(FObserversInTransit);

    for i := 0 to SelectedObject.ObserverList.Count - 1 do
    begin
      TMediatorView(SelectedObject.ObserverList.Items[i]).Subject :=
          SelectedObject;
    end;

    SelectedObject.NotifyObservers;
  end;
end;


procedure TListViewMediator.RebuildList;
var
  i: Integer;
  lItem: TListItem;
  ptr: TLVChangeEvent;
begin
  ptr := View.OnChange;
  View.OnChange := nil;
  {$IFDEF FPC}
    View.BeginUpdate;
  {$ELSE}
    View.Items.BeginUpdate;
  {$ENDIF}
  try
    View.Items.Clear;
    for i := 0 to Pred(Model.Count) do
    begin
      if (not Model.Items[i].Deleted) or
         (ShowDeleted and Model.Items[i].Deleted) then
      begin
        lItem := View.Items.Add;
        lItem.Caption := Model.Items[i].Caption;
        lItem.Data := Model.Items[i];
      end;
    end;
    if Model.CountNotDeleted > 0 then
    begin
      SelectedObject := Model.Items[0];
      View.Selected := View.Items[0];
    end;
  finally
    {$IFDEF FPC}
      View.EndUpdate;
    {$ELSE}
      View.Items.EndUpdate;
    {$ENDIF}
    View.OnChange := ptr;
    HandleSelectionChanged;
  end;
end;
*)

{ TListMediator }

procedure TListMediator.BuildPopupMenu;
begin
  FPopupMenu := TfpgPopupMenu.Create(View);
  FPopupMenu.AddMenuItem('Add', '', @MenuItemAddClick);
  FPopupMenu.AddMenuItem('Edit', '', @MenuItemEditClick);
  FPopupMenu.AddMenuItem('Delete', '', @MenuItemDeleteClick);
end;


constructor TListMediator.Create;
begin
  inherited;
  FObserversInTransit := TList.Create;
  FShowDeleted  := False;
  { This is under construction. }
  FUpdateMode   := umObjectList;
end;


constructor TListMediator.CreateCustom(pObjectList: TtiObjectList; pView: TfpgWidget);
begin
  Create;
  Model := pObjectList;
  FControl := pView;
  BuildPopupMenu;
  Model.AttachObserver(self);
  SetupGUIandObject;
  
  // I prefer to do this once in the form after all mediator are created.
  Model.NotifyObservers;
end;


destructor TListMediator.Destroy;
begin
  FObserversInTransit.Free;
  Model.DetachObserver(self);
  inherited;
end;


function TListMediator.GetModel: TtiObjectList;
begin
  Result := FObjectList;
end;


function TListMediator.GetView: TfpgWidget;
begin
  Result := FControl;
end;


procedure TListMediator.HandleDeleteItem;
begin
  if not Assigned(SelectedObject) then
    Exit; //==>

  BeginUpdate;
  try
    SelectedObject.Deleted := True;
    RebuildList;
  finally
    EndUpdate;
  end;
end;


procedure TListMediator.HandleListChanged;
begin
  BeginUpdate;
  try
    RebuildList;
  finally
    EndUpdate;
  end;
end;


procedure TListMediator.MenuItemAddClick(Sender: TObject);
begin
  { do nothing here }
end;


procedure TListMediator.MenuItemDeleteClick(Sender: TObject);
begin
  { do nothing here }
end;


procedure TListMediator.MenuItemEditClick(Sender: TObject);
begin
  { do nothing here }
end;


procedure TListMediator.SetModel(const Value: TtiObjectList);
begin
  FObjectList := Value;
//  if FObjectList.Count > 0 then
//    FSelectedObject := FObjectList.Items[0];
end;


procedure TListMediator.SetSelectedObject(const Value: TtiObject);
begin
  FSelectedObject := Value;
end;


procedure TListMediator.SetShowDeleted(const Value: Boolean);
begin
  BeginUpdate;
  try
    FShowDeleted := Value;
    RebuildList;
  finally
    EndUpdate;
  end;
end;


procedure TListMediator.SetView(const Value: TfpgWidget);
begin
  FControl := Value;
end;


procedure TListMediator.SetupGUIandObject;
begin
  { Do nothing. Can be implemented in decendant classes. }
end;


procedure TListMediator.Update(pSubject: TtiObject);
begin
  BeginUpdate;
  try
//    inherited Update(pSubject);
    RebuildList;
  finally
    EndUpdate
  end;
end;


end.