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
|
unit basic_impl;
{$mode objfpc}{$H+}
interface
uses
Classes, basic_intf;
type
TSubject = class(TInterfacedObject, ISubject)
private
fController: Pointer;
fObservers: IInterfaceList;
fUpdateCount: integer;
function GetController: IInterface;
procedure Attach(Observer: IObserver);
procedure Detach(Observer: IObserver);
procedure Notify;
procedure BeginUpdate;
procedure EndUpdate;
public
constructor Create(const Controller: IInterface);
end;
TString = class(TInterfacedObject, IString, IVisited)
private
fString: string;
// IString
function GetAsString: string;
procedure SetAsString(const AValue: string);
// IVisited
procedure Accept(const Visitor: IVisitor);
end;
TStringSelection = class(TInterfacedObject, ISelection, IVisited)
private
fModel: IStringListModel;
fItems: IInterfaceList;
// ISelection
procedure AddItem(const Item: IInterface);
procedure Clear;
function GetCount: integer;
procedure RemoveItem(const Item: IInterface);
procedure SelectModel;
// IVisited
procedure Accept(const Visitor: IVisitor);
public
constructor Create(const Model: IStringListModel); virtual;
end;
TCommand = class(TInterfacedObject, ICommand, IVisited)
private
fEnabled: Boolean;
fSelection: ISelection;
procedure BindSelection(const Selection: ISelection);
function GetEnabled: Boolean;
protected
property Selection: ISelection read fSelection;
// IVisited
procedure Accept(const Visitor: IVisitor); virtual;
// ICommand
function Execute: Boolean; virtual; abstract;
function GetText: string; virtual; abstract;
public
constructor Create(Enabled: Boolean); virtual;
end;
TCommandSet = class(TInterfacedObject, ICommandSet, IObserver, ISubject, IVisited)
private
fItems: IInterfaceList;
fSubject: ISubject;
function GetCount: integer;
// IVisited
procedure Accept(const Visitor: IVisitor);
protected
property Count: integer read GetCount;
property Items: IInterfaceList read fItems;
// IObserver
procedure Update(const ASubject: IInterface); virtual;
// ISubject
property Subject: ISubject read fSubject implements ISubject;
public
constructor Create; virtual;
end;
TMVPModel = class(TInterfacedObject, IMVPModel, ISubject)
private
fCommandSet: ICommandSet;
fCurrentSelection: ISelection;
fSubject: ISubject;
// IMVPModel
function GetCommandSet: ICommandSet;
function GetCurrentSelection: ISelection;
protected
property CommandSet: ICommandSet read GetCommandSet;
property CurrentSelection: ISelection read GetCurrentSelection;
// 3 methods to be called by the constructor
procedure BindSelection; virtual;
procedure CreateCommandSet(var ACommandSet: ICommandSet); virtual; abstract;
procedure CreateSelection(var ASelection: ISelection); virtual; abstract;
// ISubject
property Subject: ISubject read fSubject implements ISubject;
public
constructor Create; virtual;
destructor Destroy; override;
end;
TListModel = class(TMVPModel, IListModel)
private
fItems: IInterfaceList;
protected
property Items: IInterfaceList read fItems;
// IListModel
function GetCount: Integer; virtual;
function GetItem(Idx: Integer): IInterface; virtual;
procedure Add(const Item: IInterface); virtual;
procedure Clear; virtual;
function IndexOf(const Item: IInterface): Integer; virtual;
procedure Insert(const Item, Before: IInterface); virtual;
procedure Move(const Item, Before: IInterface); virtual;
procedure Remove(const Item: IInterface); virtual;
public
destructor Destroy; override;
end;
TStringListModel = class(TListModel, IStringListModel, IVisited)
private
// IStringListModel
function IStringListModel.GetItem = StringListModelGetItem;
function StringListModelGetItem(Idx: Integer): IString;
// IVisited
procedure Accept(const Visitor: IVisitor); virtual;
protected
// IMVPModel
procedure CreateCommandSet(var ACommandSet: ICommandSet); override;
procedure CreateSelection(var ASelection: ISelection); override;
public
destructor Destroy; override;
end;
TStringModelCommandSet = class(TCommandSet)
protected
// IObserver
procedure Update(const ASubject: IInterface); override;
public
destructor Destroy; override;
end;
TStringVisitor = class(TInterfacedObject, IStringVisitor)
private
fTheString: IString;
protected
// IStringVisitor
function GetTheString: IString; virtual;
procedure VisitString(const Str: IString); virtual;
end;
TStringMoveVisitor = class(TStringVisitor, IStringMoveVisitor)
private
fCanDemote: Boolean;
fCanPromote: Boolean;
fModel: IStringListModel;
function GetCanDemote: Boolean;
function GetCanPromote: Boolean;
protected
procedure VisitString(const Str: IString); override;
public
constructor Create(const Model: IStringListModel); virtual;
end;
TPromoteStringCommand = class(TCommand)
private
fModel: Pointer;
function GetModel: IStringListModel;
protected
function Execute: Boolean; override;
function GetText: string; override;
public
constructor Create(Enabled: Boolean; const Model: IStringListModel); reintroduce; virtual;
end;
TDemoteStringCommand = class(TCommand)
private
fModel: Pointer;
function GetModel: IStringListModel;
protected
function Execute: Boolean; override;
function GetText: string; override;
public
constructor Create(Enabled: Boolean; const Model: IStringListModel); reintroduce; virtual;
end;
implementation
uses
Math;
{ TSubject }
function TSubject.GetController: IInterface;
begin
Result := IInterface(fController);
end;
procedure TSubject.Attach(Observer: IObserver);
begin
if fObservers = nil then
fObservers := TInterfaceList.Create;
if fObservers.IndexOf(Observer) < 0 then
fObservers.Add(Observer);
end;
procedure TSubject.Detach(Observer: IObserver);
begin
if fObservers <> nil then
begin
if fObservers.IndexOf(Observer) >= 0 then
fObservers.Remove(Observer);
if fObservers.Count = 0 then
fObservers := nil;
end;
end;
procedure TSubject.Notify;
var
i: integer;
begin
if fObservers <> nil then
for i := 0 to fObservers.Count-1 do
(fObservers[i] as IObserver).Update(GetController);
end;
procedure TSubject.BeginUpdate;
begin
Inc(fUpdateCount);
end;
procedure TSubject.EndUpdate;
begin
Dec(fUpdateCount);
if fUpdateCount = 0 then
Notify;
end;
constructor TSubject.Create(const Controller: IInterface);
begin
inherited Create;
fController := Pointer(Controller);
end;
{ TListModel }
function TListModel.GetCount: Integer;
begin
Result := fItems.Count;
end;
function TListModel.GetItem(Idx: Integer): IInterface;
begin
Result := fItems[Idx];
end;
procedure TListModel.Add(const Item: IInterface);
begin
Subject.BeginUpdate;
if fItems = nil then
fItems := TInterfaceList.Create;
// fItems.Add(Item as IInterface);
fItems.Add(Item);
Subject.EndUpdate;
end;
procedure TListModel.Clear;
begin
Subject.BeginUpdate;
fItems.Clear;
Subject.EndUpdate;
end;
function TListModel.IndexOf(const Item: IInterface): Integer;
begin
if fItems <> nil then
// Result := fItems.IndexOf(Item as IInterface)
Result := fItems.IndexOf(Item)
else
Result := -1;
end;
procedure TListModel.Insert(const Item, Before: IInterface);
var
InsertIdx: integer;
begin
if fItems = nil then
fItems := TInterfaceList.Create;
if fItems.IndexOf(Item) < 0 then
begin
Subject.BeginUpdate;
InsertIdx := fItems.IndexOf(Before);
if InsertIdx < 0 then
InsertIdx := 0;
fItems.Insert(InsertIdx, Item);
Subject.EndUpdate;
end;
end;
procedure TListModel.Move(const Item, Before: IInterface);
var
IdxItem: integer;
IdxBefore: integer;
MoveItem: IInterface;
begin
if fItems <> nil then
begin
IdxItem := fItems.IndexOf(Item);
if IdxItem >= 0 then
begin
Subject.BeginUpdate;
MoveItem := fItems[IdxItem];
fItems.Delete(IdxItem);
IdxBefore := fItems.IndexOf(Before);
if IdxBefore >0 then
fItems.Insert(IdxBefore, MoveItem);
Subject.EndUpdate;
end;
end; { if }
end;
procedure TListModel.Remove(const Item: IInterface);
begin
if fItems <> nil then
begin
Subject.BeginUpdate;
fItems.Remove(Item);
Subject.EndUpdate;
end;
end;
destructor TListModel.Destroy;
begin
inherited Destroy;
end;
{ TString }
function TString.GetAsString: string;
begin
end;
procedure TString.SetAsString(const AValue: string);
begin
end;
procedure TString.Accept(const Visitor: IVisitor);
begin
end;
{ TStringListModel }
function TStringListModel.StringListModelGetItem(Idx: Integer): IString;
begin
Result := Items[Idx] as IString;
end;
procedure TStringListModel.Accept(const Visitor: IVisitor);
begin
end;
procedure TStringListModel.CreateCommandSet(var ACommandSet: ICommandSet);
begin
end;
procedure TStringListModel.CreateSelection(var ASelection: ISelection);
begin
end;
destructor TStringListModel.Destroy;
begin
inherited Destroy;
end;
{ TStringSelection }
procedure TStringSelection.AddItem(const Item: IInterface);
begin
if fItems = nil then
fItems := TInterfaceList.Create;
if fItems.IndexOf(Item) < 0 then
fItems.Add(Item);
end;
procedure TStringSelection.Clear;
begin
end;
function TStringSelection.GetCount: integer;
begin
Result := fItems.Count;
end;
procedure TStringSelection.RemoveItem(const Item: IInterface);
begin
if fItems <> nil then
begin
if fItems.IndexOf(Item) >= 0 then
fItems.Remove(Item);
if fItems.Count = 0 then
fItems := nil;
end;
end;
procedure TStringSelection.SelectModel;
var
i: integer;
begin
for i := 0 to (fModel as IListModel).Count-1 do
fItems.Add(fModel.Item[i]);
end;
procedure TStringSelection.Accept(const Visitor: IVisitor);
var
i: integer;
begin
for i := 0 to fItems.Count-1 do
(fItems[i] as IVisited).Accept(Visitor);
end;
constructor TStringSelection.Create(const Model: IStringListModel);
begin
inherited Create;
fModel := Model;
end;
{ TCommand }
procedure TCommand.BindSelection(const Selection: ISelection);
begin
fSelection := Selection;
end;
function TCommand.GetEnabled: Boolean;
begin
Result := fEnabled;
end;
procedure TCommand.Accept(const Visitor: IVisitor);
begin
(Visitor as ICommandVisitor).VisitComand(self);
end;
constructor TCommand.Create(Enabled: Boolean);
begin
inherited Create;
fEnabled := Enabled;
end;
{ TCommandSet }
function TCommandSet.GetCount: integer;
begin
if fItems <> nil then
Result := fItems.Count
else
Result := 0;
end;
procedure TCommandSet.Accept(const Visitor: IVisitor);
var
i: integer;
begin
for i := 0 to fItems.Count-1 do
(fItems[i] as IVisited).Accept(Visitor);
end;
procedure TCommandSet.Update(const ASubject: IInterface);
begin
// do nothing yet
end;
constructor TCommandSet.Create;
begin
inherited Create;
fItems := TInterfaceList.Create;
end;
{ TMVPModel }
function TMVPModel.GetCommandSet: ICommandSet;
begin
Result := fCommandSet;
end;
function TMVPModel.GetCurrentSelection: ISelection;
begin
Result := fCurrentSelection;
end;
procedure TMVPModel.BindSelection;
begin
(fCurrentSelection as ISubject).Attach(fCommandSet as IObserver);
end;
constructor TMVPModel.Create;
begin
inherited Create;
fSubject := TSubject.Create(self);
CreateSelection(fCurrentSelection);
CreateCommandSet(fCommandSet);
BindSelection;
end;
destructor TMVPModel.Destroy;
begin
inherited Destroy;
end;
{ TStringModelCommandSet }
procedure TStringModelCommandSet.Update(const ASubject: IInterface);
var
ObjSelection: ISelection;
ObjVisited: IVisited;
ObjModel: IStringListModel;
Visitor: IStringMoveVisitor;
Command: ICommand;
begin
ASubject.QueryInterface(ISelection, ObjSelection);
if ObjSelection <> nil then
begin
Items.Clear;
// We are only interested in a single selection. We don't have a
// mechanism for multi-select yet.
if ObjSelection.Count = 1 then
begin
ObjSelection.QueryInterface(IVisited, ObjVisited);
if ObjVisited <> nil then
begin
ObjVisited.QueryInterface(IStringListModel, ObjModel);
if ObjModel <> nil then
begin
Visitor := TStringMoveVisitor.Create(ObjModel);
ObjVisited.Accept(Visitor);
// This will only give commands that are applicable. So in a Menu the
// available items will keep changing.
{
if Visitor.CanPromote then
begin
Command := TPromoteStringCommand.Create(True, ObjModel);
Command.BindSelection(ObjSelection);
Items.Add(Command);
end;
if Visitor.CanDemote then
begin
Command := TDemoteStringCommand.Create(True, ObjModel);
Command.BindSelection(ObjSelection);
Items.Add(Command);
end;
}
// In this case it will return all commands, but only the applicable
// ones will be Enabled. I like this idea more.
Command := TPromoteStringCommand.Create(Visitor.CanPromote, ObjModel);
Command.BindSelection(ObjSelection);
Items.Add(Command);
Command := TDemoteStringCommand.Create(Visitor.CanDemote, ObjModel);
Command.BindSelection(ObjSelection);
Items.Add(Command);
end;
end;
end;
Subject.Notify;
end;
end;
destructor TStringModelCommandSet.Destroy;
begin
inherited Destroy;
end;
{ TStringVisitor }
function TStringVisitor.GetTheString: IString;
begin
Result := fTheString;
end;
procedure TStringVisitor.VisitString(const Str: IString);
begin
fTheString := Str;
end;
{ TStringMoveVisitor }
function TStringMoveVisitor.GetCanDemote: Boolean;
begin
Result := fCanDemote;
end;
function TStringMoveVisitor.GetCanPromote: Boolean;
begin
Result := fCanPromote;
end;
procedure TStringMoveVisitor.VisitString(const Str: IString);
begin
inherited VisitString(Str);
fCanPromote := fModel.IndexOf(Str) > 0;
fCanDemote := fModel.IndexOf(Str) < (fModel.Count - 1)
end;
constructor TStringMoveVisitor.Create(const Model: IStringListModel);
begin
inherited Create;
fModel := Model;
end;
{ TPromoteStringCommand }
function TPromoteStringCommand.GetModel: IStringListModel;
begin
Result := IStringListModel(fModel);
end;
function TPromoteStringCommand.Execute: Boolean;
var
Visitor: IStringVisitor;
BeforeIdx: Integer;
begin
Visitor := TStringVisitor.Create;
try
(Selection as IVisited).Accept(Visitor);
BeforeIdx := Max(GetModel.IndexOf(Visitor.TheString) - 1, 0);
GetModel.Move(Visitor.TheString, GetModel.Item[BeforeIdx]);
Result := True;
except
Result := False;
end;
end;
function TPromoteStringCommand.GetText: string;
begin
Result := 'Promote String';
end;
constructor TPromoteStringCommand.Create(Enabled: Boolean;
const Model: IStringListModel);
begin
inherited Create(Enabled);
fModel := Pointer(Model);
end;
{ TDemoteStringCommand }
function TDemoteStringCommand.GetModel: IStringListModel;
begin
Result := IStringListModel(fModel);
end;
function TDemoteStringCommand.Execute: Boolean;
var
Visitor: IStringVisitor;
BeforeIdx: Integer;
begin
Visitor := TStringVisitor.Create;
try
(Selection as IVisited).Accept(Visitor);
BeforeIdx := GetModel.IndexOf(Visitor.TheString) + 2;
if BeforeIdx > GetModel.Count - 1 then
begin
(GetModel as ISubject).BeginUpdate;
GetModel.Remove(Visitor.TheString);
GetModel.Add(Visitor.TheString);
(GetModel as ISubject).EndUpdate;
end
else
GetModel.Move(Visitor.TheString, GetModel.Item[BeforeIdx]);
Result := True;
except
Result := False;
end;
end;
function TDemoteStringCommand.GetText: string;
begin
Result := 'Demote String';
end;
constructor TDemoteStringCommand.Create(Enabled: Boolean;
const Model: IStringListModel);
begin
inherited Create(Enabled);
fModel := Pointer(Model);
end;
end.
|