Menu

[r1143]: / branches / v4-dev / Src / UView.pas  Maximize  Restore  History

Download this file

445 lines (378 with data), 12.0 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
{
* UView.pas
*
* Classes that encapsulate and provide information about "view items" that are
* displayed in the user interface, e.g.. routines, categories and the welcome
* page.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UView.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UView;
interface
uses
// Delphi
Generics.Collections,
// Project
UBaseObjects, UInitialLetter, USnippetKindInfo, USnippets;
type
/// <summary>
/// Interface supported by all view objects.
/// </summary>
IView = interface(IInterface)
['{707ED3DC-1738-4B79-B1BE-1C554572097E}']
/// Checks if two view items are equal, i.e. they have the same "type" and
/// any properties have the the same values.
function IsEqual(View: IView): Boolean;
/// View description.
function GetDescription: string;
property Description: string read GetDescription;
end;
/// <summary>
/// Interface supported by nul view.
/// </summary>
INulView = interface(IView)
['{18D06867-2E6D-4B62-A0A5-A269C91D18D6}']
end;
/// <summary>
/// Interface supported by start page view.
/// </summary>
IStartPageView = interface(IView)
['{9D2208C4-8FFC-4532-8C3F-07514EDFAB9D}']
end;
/// <summary>
/// Interface supported by snippet views.
/// </summary>
ISnippetView = interface(IView)
['{E0BD3AB7-2CB8-4B07-84D9-D7625DD020B0}']
/// Snippet associated with view.
function GetSnippet: TRoutine;
property Snippet: TRoutine read GetSnippet;
end;
/// <summary>
/// Interface supported by category views.
/// </summary>
ICategoryView = interface(IView)
['{C8BB04FD-B0B7-42F9-84AA-2F7FDD3A6FE8}']
/// Category associated with view.
function GetCategory: TCategory;
property Category: TCategory read GetCategory;
end;
/// <summary>
/// Interface supported by snippet kind views.
/// </summary>
ISnippetKindView = interface(IView)
['{FE074E72-857F-4D43-A17D-A951830B78E4}']
/// Info about snippet kind associated with view
function GetKindInfo: TSnippetKindInfo;
property KindInfo: TSnippetKindInfo read GetKindInfo;
end;
/// <summary>
/// Interface supported by initial letter views.
/// </summary>
IInitialLetterView = interface(IView)
['{E589E3E9-7178-4FF8-BDEF-D6F7DC9FEB85}']
/// Info about initial letter associated with view
function GetInitialLetter: TInitialLetter;
property InitialLetter: TInitialLetter read GetInitialLetter;
end;
/// <summary>
/// List of view items.
/// </summary>
TViewItemList = class sealed(TList<IView>);
/// <summary>
/// Static factory class used to create view instances of different types.
/// </summary>
TViewItemFactory = class sealed(TNoConstructObject)
public
/// Creates a copy of the given view. Copy has same type and properties.
class function Clone(View: IView): IView;
/// Creates a nul view instance.
class function CreateNulView: IView;
/// Creates a start page view instance.
class function CreateStartPageView: IView;
/// Creates a snippet view instance associated with a given snippet.
class function CreateSnippetView(const Snippet: TRoutine): IView;
/// Creates a category view instance associated with a given category
class function CreateCategoryView(const Category: TCategory): IView;
/// Creates a snippet kind view instance associated with a given snippet
/// kind.
class function CreateSnippetKindView(const KindInfo: TSnippetKindInfo):
IView;
/// Creates an initial letter view instance associated with a given letter.
class function CreateInitialLetterView(const Letter: TInitialLetter): IView;
end;
implementation
uses
// Delphi
SysUtils,
// Project
UExceptions;
type
/// Nul view
TNulViewItem = class sealed(TInterfacedObject,
IView, INulView
)
public
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
end;
/// View associated with start page
TStartPageViewItem = class sealed(TInterfacedObject,
IView, IStartPageView
)
public
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
end;
/// View associated with a snippet.
TSnippetViewItem = class sealed(TInterfacedObject,
IView, ISnippetView
)
strict private
/// Associated snippet.
var fSnippet: TRoutine;
public
/// Constructs view for a specified snippet.
constructor Create(const Snippet: TRoutine);
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
{ ISnippetView methods }
function GetSnippet: TRoutine;
end;
/// View associated with a category.
TCategoryViewItem = class sealed(TInterfacedObject,
IView, ICategoryView
)
strict private
/// Associated category.
var fCategory: TCategory;
public
// Constructs view for a specified category.
constructor Create(const Category: TCategory);
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
{ ICategoryView methods }
function GetCategory: TCategory;
end;
/// View associated with a snippet kind grouping.
TSnippetKindViewItem = class sealed(TInterfacedObject,
IView, ISnippetKindView
)
strict private
/// Associated snippet kind.
var fKindInfo: TSnippetKindInfo;
public
/// Constructs view for a specified snippet kind.
constructor Create(const KindInfo: TSnippetKindInfo);
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
{ ISnippetKindView methods }
function GetKindInfo: TSnippetKindInfo;
end;
/// View associated with an initial letter grouping.
TInitialLetterViewItem = class sealed(TInterfacedObject,
IView, IInitialLetterView
)
strict private
/// Associated initial letter.
var fLetter: TInitialLetter;
public
/// Constructs view for a specified initial letter.
constructor Create(const Letter: TInitialLetter);
{ IView methods }
function GetDescription: string;
function IsEqual(View: IView): Boolean;
{ IInitialLetterView methods }
function GetInitialLetter: TInitialLetter;
end;
{ TNulViewItem }
function TNulViewItem.GetDescription: string;
begin
Result := '';
end;
function TNulViewItem.IsEqual(View: IView): Boolean;
begin
Result := Supports(View, INulView);
end;
{ TStartPageViewItem }
function TStartPageViewItem.GetDescription: string;
resourcestring
sDesc = 'Welcome';
begin
Result := sDesc;
end;
function TStartPageViewItem.IsEqual(View: IView): Boolean;
begin
Result := Supports(View, IStartPageView);
end;
{ TSnippetViewItem }
constructor TSnippetViewItem.Create(const Snippet: TRoutine);
begin
inherited Create;
fSnippet := Snippet;
end;
function TSnippetViewItem.GetDescription: string;
begin
Result := fSnippet.Name;
end;
function TSnippetViewItem.GetSnippet: TRoutine;
begin
Result := fSnippet;
end;
function TSnippetViewItem.IsEqual(View: IView): Boolean;
var
SnippetView: ISnippetView;
begin
if not Supports(View, ISnippetView, SnippetView) then
Exit(False);
Result := fSnippet.IsEqual(SnippetView.Snippet);
end;
{ TCategoryViewItem }
constructor TCategoryViewItem.Create(const Category: TCategory);
begin
inherited Create;
fCategory := Category;
end;
function TCategoryViewItem.GetCategory: TCategory;
begin
Result := fCategory;
end;
function TCategoryViewItem.GetDescription: string;
begin
Result := fCategory.Description;
end;
function TCategoryViewItem.IsEqual(View: IView): Boolean;
var
CatView: ICategoryView;
begin
if not Supports(View, ICategoryView, CatView) then
Exit(False);
Result := fCategory.IsEqual(CatView.Category);
end;
{ TSnippetKindViewItem }
constructor TSnippetKindViewItem.Create(const KindInfo: TSnippetKindInfo);
begin
inherited Create;
fKindInfo := KindInfo;
end;
function TSnippetKindViewItem.GetDescription: string;
begin
Result := fKindInfo.DisplayName;
end;
function TSnippetKindViewItem.GetKindInfo: TSnippetKindInfo;
begin
Result := fKindInfo;
end;
function TSnippetKindViewItem.IsEqual(View: IView): Boolean;
var
SnipKindView: ISnippetKindView;
begin
if not Supports(View, ISnippetKindView, SnipKindView) then
Exit(False);
Result := fKindInfo.Kind = SnipKindView.KindInfo.Kind;
end;
{ TInitialLetterViewItem }
constructor TInitialLetterViewItem.Create(const Letter: TInitialLetter);
begin
inherited Create;
fLetter := Letter;
end;
function TInitialLetterViewItem.GetDescription: string;
begin
Result := fLetter.Letter;
end;
function TInitialLetterViewItem.GetInitialLetter: TInitialLetter;
begin
Result := fLetter;
end;
function TInitialLetterViewItem.IsEqual(View: IView): Boolean;
var
LetterView: IInitialLetterView;
begin
if not Supports(View, IInitialLetterView, LetterView) then
Exit(False);
Result := fLetter.Letter =
LetterView.InitialLetter.Letter;
end;
{ TViewItemFactory }
class function TViewItemFactory.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
raise EBug.CreateFmt(
'%s.CreateCopy: View does not support a valid interface', [ClassName]
);
end;
class function TViewItemFactory.CreateCategoryView(const Category: TCategory):
IView;
begin
Result := TCategoryViewItem.Create(Category);
end;
class function TViewItemFactory.CreateInitialLetterView(
const Letter: TInitialLetter): IView;
begin
Result := TInitialLetterViewItem.Create(Letter);
end;
class function TViewItemFactory.CreateNulView: IView;
begin
Result := TNulViewItem.Create;
end;
class function TViewItemFactory.CreateSnippetKindView(
const KindInfo: TSnippetKindInfo): IView;
begin
Result := TSnippetKindViewItem.Create(KindInfo);
end;
class function TViewItemFactory.CreateSnippetView(
const Snippet: TRoutine): IView;
begin
Result := TSnippetViewItem.Create(Snippet);
end;
class function TViewItemFactory.CreateStartPageView: IView;
begin
Result := TStartPageViewItem.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.