Menu

[r3204]: / branches / experimental / Src / TrunkSrc / UView.pas  Maximize  Restore  History

Download this file

872 lines (766 with data), 25.9 kB

  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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Classes that encapsulate and provide information about "views" that are
* displayed in the user interface.
}
unit UView;
interface
uses
// Delphi
Generics.Collections,
// Project
DB.UCategory, DB.USnippet, DB.USnippetKind, UBaseObjects, UInitialLetter;
type
/// <summary>
/// Interface supported by object that can compare key values associated with
/// a view for equality.
/// </summary>
/// <remarks>
/// The values stored in this object must persist even if any object stored
/// in the related IView object has been destroyed.
/// </remarks>
IViewKey = interface(IInterface)
['{E6C39F3C-63E3-4A3E-A4E7-B176DA121D9E}']
/// <summary>Checks if two view keys are equal.</summary>
function IsEqual(const Key: IViewKey): Boolean;
end;
type
/// <summary>
/// Interface supported by all view objects.
/// </summary>
IView = interface(IInterface)
['{707ED3DC-1738-4B79-B1BE-1C554572097E}']
/// <summary>Checks if two viewsare equal, i.e. they have the same "type"
/// and any properties have the the same values.</summary>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
function GetDescription: string;
/// <summary>Description of view.</summary>
property Description: string read GetDescription;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Encapsulates the data that uniquely identifies the view
/// without having have an instance of any object wrapped by the view.
/// </remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>A grouping is a view that groups views together.
/// </remarks>
function IsGrouping: Boolean;
end;
type
/// <summary>
/// Interface supported by nul view.
/// </summary>
INulView = interface(IView)
['{18D06867-2E6D-4B62-A0A5-A269C91D18D6}']
end;
type
/// <summary>
/// Interface supported by new tab page view.
/// </summary>
INewTabView = interface(IView)
['{EF760E9C-2DEE-4CCA-AB85-E6C59AA100E7}']
end;
type
/// <summary>
/// Interface supported by start page view.
/// </summary>
IStartPageView = interface(IView)
['{9D2208C4-8FFC-4532-8C3F-07514EDFAB9D}']
end;
type
/// <summary>
/// Interface supported by view that provides info about a database update.
/// </summary>
IDBUpdateInfoView = interface(IView)
['{0E5220F3-33EE-4832-9533-B97B3F3E519B}']
end;
type
/// <summary>
/// Interface supported by snippet views.
/// </summary>
ISnippetView = interface(IView)
['{E0BD3AB7-2CB8-4B07-84D9-D7625DD020B0}']
/// <summary>Gets reference to snippet associated with view.</summary>
function GetSnippet: TSnippet;
/// <summary>Snippet associated with view.</summary>
property Snippet: TSnippet read GetSnippet;
end;
type
/// <summary>
/// Interface supported by category views.
/// </summary>
ICategoryView = interface(IView)
['{C8BB04FD-B0B7-42F9-84AA-2F7FDD3A6FE8}']
/// <summary>Gets reference to category associated with view.</summary>
function GetCategory: TCategory;
/// <summary>Category associated with view.</summary>
property Category: TCategory read GetCategory;
end;
type
/// <summary>
/// Interface supported by snippet kind views.
/// </summary>
ISnippetKindView = interface(IView)
['{FE074E72-857F-4D43-A17D-A951830B78E4}']
/// <summary>Gets info about snippet kind associated with view.</summary>
function GetKindInfo: TSnippetKindInfo;
/// <summary>Info about snippet kind associated with view.</summary>
property KindInfo: TSnippetKindInfo read GetKindInfo;
end;
type
/// <summary>
/// Interface supported by initial letter views.
/// </summary>
IInitialLetterView = interface(IView)
['{E589E3E9-7178-4FF8-BDEF-D6F7DC9FEB85}']
/// <summary>Gets info about initial letter associated with view.</summary>
function GetInitialLetter: TInitialLetter;
/// <summary>Info about initial letter associated with view.</summary>
property InitialLetter: TInitialLetter read GetInitialLetter;
end;
type
/// <summary>
/// List of views.
/// </summary>
TViewList = class sealed(TList<IView>);
type
/// <summary>
/// Static factory class used to create view instances of different types.
/// </summary>
TViewFactory = class sealed(TNoConstructObject)
public
/// <summary>Creates a copy of the given view. Copy has same type and
/// properties.</summary>
class function Clone(View: IView): IView;
/// <summary>Creates a nul view instance.</summary>
class function CreateNulView: IView;
/// <summary>Creates a new tab view instance.</summary>
class function CreateNewTabView: IView;
/// <summary>Creates a start page view instance.</summary>
class function CreateStartPageView: IView;
/// <summary>Creates a database update information view instance.
/// </summary>
class function CreateDBUpdateInfoView: IView;
/// <summary>Creates a snippet view instance associated with a given
/// snippet.</summary>
class function CreateSnippetView(const Snippet: TSnippet): IView;
/// <summary>Creates a category view instance associated with a given
/// category.</summary>
class function CreateCategoryView(const Category: TCategory): IView;
/// <summary>Creates a snippet kind view instance associated with a given
/// snippet kind.</summary>
class function CreateSnippetKindView(const KindInfo: TSnippetKindInfo):
IView;
/// <summary>Creates an initial letter view instance associated with a
/// given letter.</summary>
class function CreateInitialLetterView(const Letter: TInitialLetter): IView;
/// <summary>Creates a view of given database object. View type depends on
/// the object's type.</summary>
/// <remarks>Database object type must be valid, i.e. either TSnippet or
/// TCategory, or nil.</remarks>
class function CreateDBItemView(const DBObj: TObject): IView;
end;
implementation
uses
// Delphi
SysUtils,
// Project
DB.UMain, UExceptions, USnippetIDs, UStrUtils;
type
/// <summary>
/// Base class for views that contain no other object references and are used
/// simply for display in the details pane.
/// </summary>
TSimpleView = class abstract(TInterfacedObject)
strict private
type
/// <summary>Implementation of IViewKey for descendants.</summary>
TKey = class(TInterfacedObject, IViewKey)
strict private
/// <summary>Class of object that owns this key.</summary>
fOwnerClass: TClass;
public
/// <summary>Constructs object with given owner class.</summary>
constructor Create(OwnerClass: TClass);
/// <summary>Checks if this key is equal to one passed as a parameter.
/// </summary>
/// <remarks>Method of IViewKey.</remarks>
function IsEqual(const Key: IViewKey): Boolean;
end;
public
/// <summary>Checks if this view is equal to the one passed as a parameter.
/// </summary>
/// <remarks>Method of IView.</remarks>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string; virtual; abstract;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Method of IView.</remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
/// <remarks>Method of IView.</remarks>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>Method of IView.</remarks>
function IsGrouping: Boolean;
end;
type
/// <summary>Nul view.</summary>
TNulView = class sealed(TSimpleView,
IView, INulView
)
public
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string; override;
end;
type
/// <summary>View associated with blank new tab pages.</summary>
TNewTabView = class sealed(TSimpleView,
IView, INewTabView
)
public
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string; override;
end;
type
/// <summary>View associated with start page.</summary>
TStartPageView = class sealed(TSimpleView,
IView, IStartPageView
)
public
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string; override;
end;
type
/// <summary>View associated with information about a database update.
/// </summary>
TDBUpdateInfoView = class sealed(TSimpleView,
IView, IDBUpdateInfoView
)
public
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string; override;
end;
type
/// <summary>View associated with a snippet.</summary>
TSnippetView = class sealed(TInterfacedObject,
IView, ISnippetView
)
strict private
var
/// <summary>ID of snippet associated with view.</summary>
fSnippetID: TSnippetID;
type
/// <summary>Implementation of IViewKey for snippet view.</summary>
TKey = class(TInterfacedObject, IViewKey)
strict private
var
/// <summary>Snippet id used as key.</summary>
fID: TSnippetID;
public
/// <summary>Constructs object with given snippet id as key.</summary>
constructor Create(const ID: TSnippetID);
/// <summary>Checks if this key is equal to one passed as a parameter.
/// </summary>
/// <remarks>Method of IViewKey.</remarks>
function IsEqual(const Key: IViewKey): Boolean;
end;
public
/// <summary>Constructs view for a specified snippet.</summary>
constructor Create(const Snippet: TSnippet);
/// <summary>Checks if this view is equal to the one passed as a parameter.
/// </summary>
/// <remarks>Method of IView.</remarks>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Method of IView.</remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>Method of IView.</remarks>
function IsGrouping: Boolean;
/// <summary>Gets reference to snippet associated with view.</summary>
/// <remarks>Method of ISnippetView.</remarks>
function GetSnippet: TSnippet;
end;
type
/// <summary>View associated with a category.</summary>
TCategoryView = class sealed(TInterfacedObject,
IView, ICategoryView
)
strict private
var
/// <summary>ID of category associated with view.</summary>
fCategoryID: string;
type
/// <summary>Implementation of IViewKey for category view.</summary>
TKey = class(TInterfacedObject, IViewKey)
strict private
var
/// <summary>Category id used as key.</summary>
fID: string;
public
/// <summary>Constructs object with given category id as key.</summary>
constructor Create(const ID: string);
/// <summary>Checks if this key is equal to one passed as a parameter.
/// </summary>
/// <remarks>Method of IViewKey.</remarks>
function IsEqual(const Key: IViewKey): Boolean;
end;
public
/// <summary>Constructs view for a specified category.</summary>
constructor Create(const Category: TCategory);
/// <summary>Checks if this view is equal to the one passed as a parameter.
/// </summary>
/// <remarks>Method of IView.</remarks>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Method of IView.</remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
/// <remarks>Method of IView.</remarks>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>Method of IView.</remarks>
function IsGrouping: Boolean;
/// <summary>Gets reference to category associated with view.</summary>
/// <remarks>Method of ICategoryView.</remarks>
function GetCategory: TCategory;
end;
type
/// <summary>View associated with a snippet kind grouping.</summary>
TSnippetKindView = class sealed(TInterfacedObject,
IView, ISnippetKindView
)
strict private
var
/// <summary>Snippet kind associated with view.</summary>
fKindInfo: TSnippetKindInfo;
type
/// <summary>Implementation of IViewKey for snippet kind view.
/// </summary>
TKey = class(TInterfacedObject, IViewKey)
strict private
var
/// <summary>Snippet kind used as key.</summary>
fID: TSnippetKind;
public
/// <summary>Constructs object with given snippet kind as key.
/// </summary>
constructor Create(const ID: TSnippetKind);
/// <summary>Checks if this key is equal to one passed as a parameter.
/// </summary>
/// <remarks>Method of IViewKey.</remarks>
function IsEqual(const Key: IViewKey): Boolean;
end;
public
/// <summary>Constructs view for a specified snippet kind.</summary>
constructor Create(const KindInfo: TSnippetKindInfo);
/// <summary>Checks if this view is equal to the one passed as a parameter.
/// </summary>
/// <remarks>Method of IView.</remarks>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Method of IView.</remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
/// <remarks>Method of IView.</remarks>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>Method of IView.</remarks>
function IsGrouping: Boolean;
/// <summary>Gets info about snippet kind associated with view.</summary>
/// <remarks>Method of ISnippetKindView.</remarks>
function GetKindInfo: TSnippetKindInfo;
end;
type
/// <summary>View associated with an initial letter grouping.</summary>
TInitialLetterView = class sealed(TInterfacedObject,
IView, IInitialLetterView
)
strict private
var
/// <summary>Initial letter associated with view.</summary>
fInitialLetter: TInitialLetter;
type
/// <summary>Implementation of IViewKey for initial letter view.
/// </summary>
TKey = class(TInterfacedObject, IViewKey)
strict private
var
/// <summary>Initial letter used as key.</summary>
fID: TInitialLetter;
public
/// <summary>Constructs object with given initial letter as key.
/// </summary>
constructor Create(const ID: TInitialLetter);
/// <summary>Checks if this key is equal to one passed as a parameter.
/// </summary>
/// <remarks>Method of IViewKey.</remarks>
function IsEqual(const Key: IViewKey): Boolean;
end;
public
/// <summary>Constructs view for a specified initial letter.</summary>
constructor Create(const Letter: TInitialLetter);
/// <summary>Checks if this view is equal to the one passed as a parameter.
/// </summary>
/// <remarks>Method of IView.</remarks>
function IsEqual(View: IView): Boolean;
/// <summary>Gets description of view.</summary>
/// <remarks>Method of IView.</remarks>
function GetDescription: string;
/// <summary>Gets object containing view's unique key.</summary>
/// <remarks>Method of IView.</remarks>
function GetKey: IViewKey;
/// <summary>Checks if view is user-defined.</summary>
/// <remarks>Method of IView.</remarks>
function IsUserDefined: Boolean;
/// <summary>Checks if view is a grouping.</summary>
/// <remarks>Method of IView.</remarks>
function IsGrouping: Boolean;
/// <summary>Gets unfo about initial letter associated with view.</summary>
/// <remarks>Method of IInitialLetterView.</remarks>
function GetInitialLetter: TInitialLetter;
end;
{ TSimpleView }
function TSimpleView.GetKey: IViewKey;
begin
Result := TKey.Create(Self.ClassType);
end;
function TSimpleView.IsEqual(View: IView): Boolean;
begin
Result := View.GetKey.IsEqual(Self.GetKey);
end;
function TSimpleView.IsGrouping: Boolean;
begin
Result := False;
end;
function TSimpleView.IsUserDefined: Boolean;
begin
Result := False;
end;
{ TSimpleView.TKey }
constructor TSimpleView.TKey.Create(OwnerClass: TClass);
begin
inherited Create;
fOwnerClass := OwnerClass;
end;
function TSimpleView.TKey.IsEqual(const Key: IViewKey): Boolean;
begin
Result := (Key is TKey) and ((Key as TKey).fOwnerClass = Self.fOwnerClass);
end;
{ TNulView }
function TNulView.GetDescription: string;
begin
Result := '';
end;
{ TNewTabView }
function TNewTabView.GetDescription: string;
resourcestring
sDescription = 'Empty tab';
begin
Result := sDescription;
end;
{ TStartPageView }
function TStartPageView.GetDescription: string;
resourcestring
sDesc = 'Welcome';
begin
Result := sDesc;
end;
{ TDBUpdateInfoView }
function TDBUpdateInfoView.GetDescription: string;
resourcestring
sDesc = 'Database Updated';
begin
Result := sDesc;
end;
{ TSnippetView }
constructor TSnippetView.Create(const Snippet: TSnippet);
begin
inherited Create;
fSnippetID := Snippet.ID;
end;
function TSnippetView.GetDescription: string;
begin
Result := GetSnippet.DisplayName;
end;
function TSnippetView.GetKey: IViewKey;
begin
Result := TKey.Create(fSnippetID);
end;
function TSnippetView.GetSnippet: TSnippet;
begin
Result := Database.Snippets.Find(fSnippetID);
Assert(Assigned(Result), ClassName + '.GetSnippet: Snippet not found');
end;
function TSnippetView.IsEqual(View: IView): Boolean;
var
SnippetView: ISnippetView;
begin
if not Supports(View, ISnippetView, SnippetView) then
Exit(False);
// don't compare snippet IDs directly in case snippet equality test changes
Result := GetSnippet.IsEqual(SnippetView.Snippet);
end;
function TSnippetView.IsGrouping: Boolean;
begin
Result := False;
end;
function TSnippetView.IsUserDefined: Boolean;
begin
Result := GetSnippet.UserDefined;
end;
{ TSnippetView.TKey }
constructor TSnippetView.TKey.Create(const ID: TSnippetID);
begin
inherited Create;
fID := ID;
end;
function TSnippetView.TKey.IsEqual(const Key: IViewKey): Boolean;
begin
if not (Key is TSnippetView.TKey) then
Exit(False);
Result := (Key as TSnippetView.TKey).fID = fID;
end;
{ TCategoryView }
constructor TCategoryView.Create(const Category: TCategory);
begin
inherited Create;
fCategoryID := Category.ID;
end;
function TCategoryView.GetCategory: TCategory;
begin
Result := Database.Categories.Find(fCategoryID);
Assert(Assigned(Result), ClassName + '.GetCategory: Category not found');
end;
function TCategoryView.GetDescription: string;
begin
Result := GetCategory.Description;
end;
function TCategoryView.GetKey: IViewKey;
begin
Result := TKey.Create(GetCategory.ID);
end;
function TCategoryView.IsEqual(View: IView): Boolean;
var
CatView: ICategoryView;
begin
if not Supports(View, ICategoryView, CatView) then
Exit(False);
// don't compare category IDs directly in case equality test changes
Result := GetCategory.IsEqual(CatView.Category);
end;
function TCategoryView.IsGrouping: Boolean;
begin
Result := True;
end;
function TCategoryView.IsUserDefined: Boolean;
begin
Result := GetCategory.UserDefined;
end;
{ TCategoryView.TKey }
constructor TCategoryView.TKey.Create(const ID: string);
begin
inherited Create;
fID := ID;
end;
function TCategoryView.TKey.IsEqual(const Key: IViewKey): Boolean;
begin
if not (Key is TKey) then
Exit(False);
Result := StrSameText((Key as TKey).fID, fID);
end;
{ TSnippetKindView }
constructor TSnippetKindView.Create(const KindInfo: TSnippetKindInfo);
begin
inherited Create;
fKindInfo := KindInfo;
end;
function TSnippetKindView.GetDescription: string;
begin
Result := GetKindInfo.DisplayName;
end;
function TSnippetKindView.GetKey: IViewKey;
begin
Result := TKey.Create(GetKindInfo.Kind);
end;
function TSnippetKindView.GetKindInfo: TSnippetKindInfo;
begin
Result := fKindInfo;
end;
function TSnippetKindView.IsEqual(View: IView): Boolean;
var
SnipKindView: ISnippetKindView;
begin
if not Supports(View, ISnippetKindView, SnipKindView) then
Exit(False);
Result := GetKindInfo.Kind = SnipKindView.KindInfo.Kind;
end;
function TSnippetKindView.IsGrouping: Boolean;
begin
Result := True;
end;
function TSnippetKindView.IsUserDefined: Boolean;
begin
Result := False;
end;
{ TSnippetKindView.TKey }
constructor TSnippetKindView.TKey.Create(const ID: TSnippetKind);
begin
inherited Create;
fID := ID;
end;
function TSnippetKindView.TKey.IsEqual(const Key: IViewKey): Boolean;
begin
if not (Key is TKey) then
Exit(False);
Result := (Key as TKey).fID = fID;
end;
{ TInitialLetterView }
constructor TInitialLetterView.Create(const Letter: TInitialLetter);
begin
inherited Create;
fInitialLetter := Letter;
end;
function TInitialLetterView.GetDescription: string;
begin
Result := GetInitialLetter.Letter;
end;
function TInitialLetterView.GetInitialLetter: TInitialLetter;
begin
Result := fInitialLetter;
end;
function TInitialLetterView.GetKey: IViewKey;
begin
Result := TKey.Create(GetInitialLetter);
end;
function TInitialLetterView.IsEqual(View: IView): Boolean;
var
LetterView: IInitialLetterView;
begin
if not Supports(View, IInitialLetterView, LetterView) then
Exit(False);
Result := GetInitialLetter = LetterView.InitialLetter;
end;
function TInitialLetterView.IsGrouping: Boolean;
begin
Result := True;
end;
function TInitialLetterView.IsUserDefined: Boolean;
begin
Result := False;
end;
{ TInitialLetterView.TKey }
constructor TInitialLetterView.TKey.Create(const ID: TInitialLetter);
begin
inherited Create;
fID := ID;
end;
function TInitialLetterView.TKey.IsEqual(const Key: IViewKey): Boolean;
begin
if not (Key is TKey) then
Exit(False);
Result := (Key as TKey).fID = fID;
end;
{ TViewFactory }
class function TViewFactory.Clone(View: IView): IView;
begin
if not Assigned(View) or Supports(View, INulView) then
Result := CreateNulView // also created if View = nil
else if Supports(View, IStartPageView) then
Result := CreateStartPageView
else if Supports(View, ISnippetView) then
Result := CreateSnippetView((View as ISnippetView).Snippet)
else if Supports(View, ICategoryView) then
Result := CreateCategoryView((View as ICategoryView).Category)
else if Supports(View, ISnippetKindView) then
Result := CreateSnippetKindView((View as ISnippetKindView).KindInfo)
else if Supports(View, IInitialLetterView) then
Result := CreateInitialLetterView(
(View as IInitialLetterView).InitialLetter
)
else if Supports(View, INewTabView) then
Result := CreateNewTabView
else if Supports(View, IDBUpdateInfoView) then
Result := CreateDBUpdateInfoView
else
raise EBug.CreateFmt(
'%s.CreateCopy: View does not support a valid interface', [ClassName]
);
end;
class function TViewFactory.CreateCategoryView(const Category: TCategory):
IView;
begin
Result := TCategoryView.Create(Category);
end;
class function TViewFactory.CreateDBItemView(const DBObj: TObject): IView;
begin
Result := nil;
if not Assigned(DBObj) then
Result := TViewFactory.CreateNulView
else if DBObj is TSnippet then
Result := TViewFactory.CreateSnippetView(DBObj as TSnippet)
else if DBObj is TCategory then
Result := TViewFactory.CreateCategoryView(DBObj as TCategory);
Assert(Assigned(Result),
ClassName + '.CreateDBItemView: DBObj is not valid type');
end;
class function TViewFactory.CreateDBUpdateInfoView: IView;
begin
Result := TDBUpdateInfoView.Create;
end;
class function TViewFactory.CreateInitialLetterView(
const Letter: TInitialLetter): IView;
begin
Result := TInitialLetterView.Create(Letter);
end;
class function TViewFactory.CreateNewTabView: IView;
begin
Result := TNewTabView.Create;
end;
class function TViewFactory.CreateNulView: IView;
begin
Result := TNulView.Create;
end;
class function TViewFactory.CreateSnippetKindView(
const KindInfo: TSnippetKindInfo): IView;
begin
Result := TSnippetKindView.Create(KindInfo);
end;
class function TViewFactory.CreateSnippetView(
const Snippet: TSnippet): IView;
begin
Result := TSnippetView.Create(Snippet);
end;
class function TViewFactory.CreateStartPageView: IView;
begin
Result := TStartPageView.Create;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.