Menu

[r3201]: / trunk / Src / FmSWAGImportDlg.pas  Maximize  Restore  History

Download this file

614 lines (556 with data), 15.8 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
{
* 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) 2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a wizard dialogue box that lets the user select and import
* snippets from the DelphiDabbler implmentation of the SWAG database.
}
unit FmSWAGImportDlg;
interface
uses
// Delphi
SysUtils,
ComCtrls,
CheckLst,
Controls,
StdCtrls,
Forms,
ExtCtrls,
Classes,
Generics.Collections,
// Project
FmWizardDlg,
FrBrowserBase,
FrFixedHTMLDlg,
FrHTMLDlg,
FrHTMLTpltDlg,
UBaseObjects,
UContainers,
SWAG.UCommon,
SWAG.UImporter,
SWAG.UReader;
type
TSWAGImportDlg = class(TWizardDlg, INoPublicConstruct)
tsIntro: TTabSheet;
tsCategories: TTabSheet;
lblCategories: TLabel;
frmIntro: TFixedHTMLDlgFrame;
lbCategories: TListBox;
lblCategoriesDesc: TLabel;
lblSelectSnippets: TLabel;
clbSelectSnippets: TCheckListBox;
tsUpdate: TTabSheet;
lvImports: TListView;
lblUpdateDesc: TLabel;
tsFinish: TTabSheet;
frmOutro: THTMLTpltDlgFrame;
procedure lbCategoriesDblClick(Sender: TObject);
procedure lbCategoriesKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure clbSelectSnippetsClickCheck(Sender: TObject);
procedure clbSelectSnippetsDblClick(Sender: TObject);
procedure clbSelectSnippetsKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
strict private
const
// Indices of wizard pages
cIntroPage = 0;
cSelectionPage = 1;
cUpdatePage = 2;
cFinishPage = 3;
var
fSWAGReader: TSWAGReader;
fSortedCategories: TSortedList<TSWAGCategory>;
fCurrentCatSnippets: TSortedList<TSWAGSnippet>;
fSelectedSnippets: TSortedList<TSWAGSnippet>;
fImporter: TSWAGImporter;
fCurrentCatID: string;
/// <summary>Validates entries on wizard pages indentified by the page
/// index.</summary>
procedure ValidatePage(const PageIdx: Integer);
procedure PopulateImportsLV;
procedure InitSelectionPage;
procedure InitUpdatePage;
procedure DisplaySnippetsForCategory;
procedure PreviewSelectedSnippet;
procedure GetImportSnippets(const SnipList: TList<TSWAGSnippet>);
procedure UpdateDatabase;
procedure WaitWrapper(AOwner: TComponent; const CallProc: TProc;
const WaitMsg: string);
strict protected
/// <summary>Constructs and intialises a wizard instance.</summary>
constructor InternalCreate(AOwner: TComponent); override;
/// <summary>Aligns and arranges controls in each tab sheet and sizes
/// dialog box to accomodate controls.</summary>
/// <remarks>Overridden method called from ancestor class.</remarks>
procedure ArrangeForm; override;
procedure ConfigForm; override;
/// <summary>Returns text of heading on page indexed by PageIdx.</summary>
/// <remarks>Overridden method called from ancestor class.</remarks>
function HeadingText(const PageIdx: Integer): string; override;
/// <summary>Updates state and caption of buttons on page index by
/// PageIdx.</summary>
/// <remarks>Implementation of abstract method called from ancestor class.
/// </remarks>
procedure UpdateButtons(const PageIdx: Integer); override;
/// <summary>Initialises page indexed by PageIdx.</summary>
/// <remarks>Overridden method called from ancestor class.</remarks>
procedure BeginPage(const PageIdx: Integer); override;
/// <summary>Validates page specified by PageIdx then performs any action
/// required before next page is displayed.</summary>
/// <remarks>Overridden method called from ancestor class.</remarks>
procedure MoveForward(const PageIdx: Integer; var CanMove: Boolean);
override;
public
destructor Destroy; override;
class function Execute(const AOwner: TComponent): Boolean;
end;
implementation
uses
// Delphi
Generics.Defaults,
Windows,
// Project
FmPreviewDlg,
FmWaitDlg,
UCtrlArranger,
UEncodings,
UExceptions,
UHTMLTemplate,
UMessageBox,
UStrUtils,
UWaitForThreadUI;
{$R *.dfm}
{ TSWAGImportDlg }
procedure TSWAGImportDlg.ArrangeForm;
begin
TCtrlArranger.SetLabelHeights(Self);
// Arrange controls on tab sheets
// tsIntro: nothing to do
// tsCategories
lblCategoriesDesc.Width := tsCategories.ClientWidth;
lblCategoriesDesc.Top := 3;
TCtrlArranger.AlignLefts(
[lblCategoriesDesc, lblCategories, lbCategories], 0
);
TCtrlArranger.AlignTops(
[lblCategories, lblSelectSnippets],
TCtrlArranger.BottomOf(lblCategoriesDesc, 12)
);
TCtrlArranger.AlignTops(
[lbCategories, clbSelectSnippets],
TCtrlArranger.BottomOf([lblCategories, lblSelectSnippets], 6)
);
// tsUpdate
lblUpdateDesc.Width := tsUpdate.ClientWidth;
lblUpdateDesc.Top := 3;
lvImports.Width := tsUpdate.ClientWidth;
TCtrlArranger.AlignLefts([lblUpdateDesc, lvImports], 0);
TCtrlArranger.MoveBelow(lblUpdateDesc, lvImports, 12);
// tsFinish: nothing to do
inherited;
end;
procedure TSWAGImportDlg.BeginPage(const PageIdx: Integer);
begin
case PageIdx of
cSelectionPage: InitSelectionPage;
cUpdatePage: InitUpdatePage;
end;
end;
procedure TSWAGImportDlg.clbSelectSnippetsClickCheck(Sender: TObject);
var
SelIdx: Integer;
DelIdx: Integer;
begin
SelIdx := clbSelectSnippets.ItemIndex;
if SelIdx = -1 then
Exit;
if clbSelectSnippets.Checked[SelIdx] then
begin
if not fSelectedSnippets.Contains(fCurrentCatSnippets[SelIdx]) then
fSelectedSnippets.Add(fCurrentCatSnippets[SelIdx]);
end
else
begin
DelIdx := fSelectedSnippets.IndexOf(fCurrentCatSnippets[SelIdx]);
if DelIdx >= 0 then
fSelectedSnippets.Delete(DelIdx);
end;
end;
procedure TSWAGImportDlg.clbSelectSnippetsDblClick(Sender: TObject);
begin
PreviewSelectedSnippet;
end;
procedure TSWAGImportDlg.clbSelectSnippetsKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
if Key = VK_RETURN then
PreviewSelectedSnippet;
end;
procedure TSWAGImportDlg.ConfigForm;
begin
inherited;
pcWizard.ActivePage := tsFinish;
frmOutro.Initialise(
'dlg-swag-import-outro-tplt.html',
procedure (Tplt: THTMLTemplate)
begin
Tplt.ResolvePlaceholderText(
'SWAGCategory',
TSWAGImporter.SWAGCategoryDesc
);
end
);
pcWizard.ActivePage := tsIntro;
frmIntro.Initialise('dlg-swag-import-intro.html');
end;
destructor TSWAGImportDlg.Destroy;
begin
fSWAGReader.Free;
fImporter.Free;
fSelectedSnippets.Free;
fCurrentCatSnippets.Free;
fSortedCategories.Free;
inherited;
end;
procedure TSWAGImportDlg.DisplaySnippetsForCategory;
var
CatIdx: Integer;
Idx: Integer;
N: Integer;
Snippets: TList<TSWAGSnippet>;
begin
CatIdx := lbCategories.ItemIndex;
if CatIdx = -1 then
begin
fCurrentCatID := '';
Exit;
end;
if fCurrentCatID = fSortedCategories[CatIdx].ID then
// nothing to do if current category selected again
Exit;
fCurrentCatID := fSortedCategories[CatIdx].ID;
Snippets := TList<TSWAGSnippet>.Create;
try
fSWAGReader.GetPartialSnippets(fCurrentCatID, Snippets);
clbSelectSnippets.Items.BeginUpdate;
try
fCurrentCatSnippets.Clear;
clbSelectSnippets.Clear;
// We set fCurrentCatSnippets first because it is a sorted list which
// means indices of new items added are not sequential, and we must have
// displayed title at same index in clbSelectSnippets as its snippet is in
// fCurrentCatSnippets.
fCurrentCatSnippets.AddRange(Snippets);
for Idx := 0 to Pred(fCurrentCatSnippets.Count) do
begin
N := clbSelectSnippets.Items.Add(fCurrentCatSnippets[Idx].Title);
Assert(Idx = N, 'Idx <> N');
clbSelectSnippets.Checked[Idx] := fSelectedSnippets.Contains(
fCurrentCatSnippets[Idx]
);
end;
finally
clbSelectSnippets.Items.EndUpdate;
end;
finally
Snippets.Free;
end;
end;
class function TSWAGImportDlg.Execute(const AOwner: TComponent): Boolean;
begin
with InternalCreate(AOwner) do
try
Result := ShowModal = mrOK;
finally
Free;
end;
end;
procedure TSWAGImportDlg.GetImportSnippets(const SnipList: TList<TSWAGSnippet>);
var
SnipIDs: TList<Cardinal>;
PartialSnippet: TSWAGSnippet;
resourcestring
sWaitMsg = 'Downloading Snippets From SWAG...';
begin
SnipIDs := TList<Cardinal>.Create;
try
for PartialSnippet in fSelectedSnippets do
SnipIDs.Add(PartialSnippet.ID);
fSWAGReader.GetCompleteSnippets(
SnipIDs,
SnipList,
procedure (CallProc: TProc)
begin
WaitWrapper(Self, CallProc, sWaitMsg);
end
);
finally
SnipIDs.Free;
end;
end;
function TSWAGImportDlg.HeadingText(const PageIdx: Integer): string;
resourcestring
sIntroPageHeading = 'Import snippets from SWAG';
sSelectionPageHeading = 'Select required snippets';
sUpdatePage = 'Ready to import';
sFinishPage = 'Import complete';
begin
case PageIdx of
cIntroPage: Result := sIntroPageHeading;
cSelectionPage: Result := sSelectionPageHeading;
cUpdatePage: Result := sUpdatePage;
cFinishPage: Result := sFinishPage;
end;
end;
procedure TSWAGImportDlg.InitSelectionPage;
var
Cats: TList<TSWAGCategory>;
Idx: Integer;
begin
Application.ProcessMessages;
if lbCategories.Count > 0 then
Exit;
Cats := TList<TSWAGCategory>.Create;
try
fSWAGReader.GetCategories(Cats);
lbCategories.Items.BeginUpdate;
try
// We set fSortedCategories first because it is a sorted list which means
// indices of new items added are not sequential, and we must have
// displayed title at same index in lbCategories as its category is in
// fSortedCategories.
fSortedCategories.AddRange(Cats);
for Idx := 0 to Pred(fSortedCategories.Count) do
lbCategories.Items.Add(fSortedCategories[Idx].Title);
lbCategories.ItemIndex := -1;
finally
lbCategories.Items.EndUpdate;
end;
finally
Cats.Free;
end;
end;
procedure TSWAGImportDlg.InitUpdatePage;
var
FullSnippets: TList<TSWAGSnippet>;
Snippet: TSWAGSnippet;
resourcestring
sWaitMsg = 'Downloading Snippets From SWAG...';
begin
// TODO: Refactor out into GetImportSnippets
Application.ProcessMessages;
FullSnippets := TList<TSWAGSnippet>.Create;
try
GetImportSnippets(FullSnippets);
fImporter.Reset;
for Snippet in FullSnippets do
fImporter.IncludeSnippet(Snippet);
finally
FullSnippets.Free;
end;
PopulateImportsLV;
end;
constructor TSWAGImportDlg.InternalCreate(AOwner: TComponent);
resourcestring
sDefaultWaitMsg = 'Accessing SWAG...';
begin
inherited;
fSortedCategories := TSortedList<TSWAGCategory>.Create(
TDelegatedComparer<TSWAGCategory>.Create(
function (const Left, Right: TSWAGCategory): Integer
begin
Result := StrCompareStr(Left.Title, Right.Title);
end
)
);
fSortedCategories.PermitDuplicates := True;
fCurrentCatSnippets := TSortedList<TSWAGSnippet>.Create(
TDelegatedComparer<TSWAGSnippet>.Create(
function (const Left, Right: TSWAGSnippet): Integer
begin
Result := StrCompareStr(Left.Title, Right.Title);
end
)
);
fCurrentCatSnippets.PermitDuplicates := True;
fSelectedSnippets := TSortedList<TSWAGSnippet>.Create(
TDelegatedComparer<TSWAGSnippet>.Create(
function (const Left, Right: TSWAGSnippet): Integer
begin
Result := Left.ID - Right.ID;
end
)
);
fSelectedSnippets.PermitDuplicates := False;
fImporter := TSWAGImporter.Create;
fSWAGReader := TSWAGReader.Create(
procedure (CallProc: TProc)
begin
WaitWrapper(Self, CallProc, sDefaultWaitMsg);
end
);
end;
procedure TSWAGImportDlg.lbCategoriesDblClick(Sender: TObject);
begin
DisplaySnippetsForCategory;
end;
procedure TSWAGImportDlg.lbCategoriesKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
DisplaySnippetsForCategory;
end;
procedure TSWAGImportDlg.MoveForward(const PageIdx: Integer;
var CanMove: Boolean);
begin
CanMove := False;
try
ValidatePage(PageIdx);
case PageIdx of
cUpdatePage: UpdateDatabase;
end;
CanMove := True;
except
on E: EDataEntry do
begin
TMessageBox.Error(Self, E.Message);
if Assigned(E.Ctrl) then
E.Ctrl.SetFocus;
end;
end;
end;
procedure TSWAGImportDlg.PopulateImportsLV;
var
Snippet: TSWAGSnippet;
LI: TListItem;
begin
lvImports.Items.BeginUpdate;
try
for Snippet in fSelectedSnippets do
begin
LI := lvImports.Items.Add;
LI.Caption := Snippet.Title;
LI.SubItems.Add(TSWAGImporter.MakeValidSnippetName(Snippet.ID));
end;
finally
lvImports.Items.EndUpdate;
end;
end;
procedure TSWAGImportDlg.PreviewSelectedSnippet;
var
PartialSnippet: TSWAGSnippet;
FullSnippet: TSWAGSnippet;
SelIdx: Integer;
Content: string;
resourcestring
sWaitMsg = 'Downloading Snippet From SWAG...';
begin
SelIdx := clbSelectSnippets.ItemIndex;
if SelIdx = -1 then
Exit;
PartialSnippet := fCurrentCatSnippets[SelIdx];
FullSnippet := fSWAGReader.GetCompleteSnippet(
PartialSnippet.ID,
procedure (CallProc: TProc)
begin
WaitWrapper(Self, CallProc, sWaitMsg);
end
);
// TODO: Display snippet as HTML, highlighted if necessary
Content := Format(
'ID: %d'#13#10
+ 'Category: "%s"'#13#10
+ 'File Name: "%s"'#13#10
+ 'Title: "%s"'#13#10
+ 'Author: "%s"'#13#10#13#10
+ 'Source Code:'#13#10 + StringOfChar('-', 80) + #13#10'%s',
[
FullSnippet.ID,
FullSnippet.Category,
FullSnippet.FileName,
FullSnippet.Title,
FullSnippet.Author,
StrWindowsLineBreaks(FullSnippet.SourceCode)
]
);
TPreviewDlg.Execute(
Self,
TEncodedData.Create(Content, etUTF8),
dtPlainText
);
end;
procedure TSWAGImportDlg.UpdateButtons(const PageIdx: Integer);
resourcestring
// button caption for update page
sUpdateCaption = 'Import';
begin
inherited;
case PageIdx of
cUpdatePage: btnNext.Caption := sUpdateCaption;
cFinishPage: btnBack.Enabled := False;
end;
end;
procedure TSWAGImportDlg.UpdateDatabase;
procedure SetBtnVisibility(const Show: Boolean);
begin
btnNext.Visible := Show;
btnBack.Visible := Show;
btnCancel.Visible := Show;
btnHelp.Visible := Show;
Application.ProcessMessages;
end;
begin
SetBtnVisibility(False);
try
WaitWrapper(
Self,
procedure
begin
fImporter.Import(
procedure (const Snippet: TSWAGSnippet)
begin
Application.ProcessMessages;
end
);
end,
'Importing Snippets Into Database...'
);
finally
SetBtnVisibility(True);
end;
end;
procedure TSWAGImportDlg.ValidatePage(const PageIdx: Integer);
resourcestring
sEmptySelection = 'You must select one or more snippets to import.';
begin
case PageIdx of
cSelectionPage:
begin
if fSelectedSnippets.Count = 0 then
raise EDataEntry.Create(sEmptySelection);
end;
end;
end;
procedure TSWAGImportDlg.WaitWrapper(AOwner: TComponent; const CallProc: TProc;
const WaitMsg: string);
const
PauseBeforeDisplay = 0;
MinDisplayTime = 1000;
begin
// Perform the operation
TWaitForThreadUI.Run( // this blocks until thread completes
CallProc,
False,
TWaitDlg.CreateAutoFree(AOwner, WaitMsg),
PauseBeforeDisplay,
MinDisplayTime
);
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.