Menu

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

Download this file

285 lines (248 with data), 8.7 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
{
* 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) 2009-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a set of classes that populate the overview treeview with a list
* of snippets. Each class groups the snippets in different ways.
}
unit UOverviewTreeBuilder;
interface
uses
// Delphu
ComCtrls,
// Project
DB.USnippet, UGroups, UView, UViewItemTreeNode;
type
{
TOverviewTreeBuilder:
Abstract base class for classes that populate the overview treeview with a
list of snippets. Each subclass groups the snippets in different ways.
}
TOverviewTreeBuilder = class abstract(TObject)
strict private
var
fTreeView: TTreeView; // Value of TreeView property
fSnippetList: TSnippetList; // Value of SnippetList property
strict protected
property TreeView: TTreeView read fTreeView;
{Reference to treeview populated by class}
property SnippetList: TSnippetList read fSnippetList;
{List of snippets to be displayed in treeview}
function AddViewItemNode(const ParentNode: TViewItemTreeNode;
ViewItem: IView): TViewItemTreeNode;
{Adds a new node to the tree view that represents a view item.
@param ParentNode [in] Node that is parent of new node.
@param ViewItem [in] View item for which we are adding node.
@return New tree node.
}
function CreateGrouping: TGrouping; virtual; abstract;
{Creates a grouping object of the required type.
@return Required grouping object.
}
function CreateViewItemForGroup(const Group: TGroupItem): IView;
virtual; abstract;
{Creates a view item of a type that matches the type of a group item.
@param Group [in] Group item for which view item is required.
@return Required view item object.
}
public
constructor Create(const TV: TTreeView; const SnippetList: TSnippetList);
{Class constructor. Sets up object to populate a treeview with a list of
snippets.
@param TV [in] Treeview control to be populated.
@param SnippetList [in] List of snippets to be added to TV.
}
procedure Build;
{Populates the treeview.
}
end;
{
TOverviewTreeBuilderClass:
Class reference for TOverviewTreeBuilder sub-classes.
}
TOverviewTreeBuilderClass = class of TOverviewTreeBuilder;
{
TOverviewCategorisedTreeBuilder:
Class that populates the overview treeview with a list of snippets grouped
by category.
}
TOverviewCategorisedTreeBuilder = class sealed(TOverviewTreeBuilder)
strict protected
function CreateGrouping: TGrouping; override;
{Creates a categorised grouping object.
@return Required grouping object.
}
function CreateViewItemForGroup(const Group: TGroupItem): IView;
override;
{Creates a category view item from group item.
@param Group [in] Group item containing category data.
@return Required category view item for group.
}
end;
{
TOverviewAlphabeticTreeBuilder:
Class that populates the overview treeview with a list of snippets grouped
by initial letter of the snippet name.
}
TOverviewAlphabeticTreeBuilder = class sealed(TOverviewTreeBuilder)
strict protected
function CreateGrouping: TGrouping; override;
{Creates an alphabetic grouping object.
@return Required grouping object.
}
function CreateViewItemForGroup(const Group: TGroupItem): IView;
override;
{Creates an alpha view item from group item.
@param Group [in] Group item containing alpha data.
@return Required alpha view item for group.
}
end;
{
TOverviewSnipKindTreeBuilder:
Class that populates the overview treeview with a list of snippets grouped
by snippet kind.
}
TOverviewSnipKindTreeBuilder = class sealed(TOverviewTreeBuilder)
strict protected
function CreateGrouping: TGrouping; override;
{Creates a snippet kind grouping object.
@return Required grouping object.
}
function CreateViewItemForGroup(const Group: TGroupItem): IView;
override;
{Creates a snippet kind view item from group item.
@param Group [in] Group item containing snippet kind data.
@return Required snippet kind view item for group.
}
end;
implementation
uses
// Project
UPreferences;
{
NOTE:
Early version of this code, that were contained in the FrOverview unit used
to add a section head tree node for every category and then delete empty ones.
However, attempts to delete the first node from a tree causes an endless loop,
freezing the program. This could be a bug in the Delphi treeview component.
Therefore the code in this unit now builds or uses a list of snippets for
each section and does not create a section header node for sections that
contain no snippets.
}
{ TOverviewTreeBuilder }
function TOverviewTreeBuilder.AddViewItemNode(
const ParentNode: TViewItemTreeNode; ViewItem: IView): TViewItemTreeNode;
{Adds a new node to the tree view that represents a view item.
@param ParentNode [in] Node that is parent of new node.
@param ViewItem [in] View item for which we are adding node.
@return New tree node.
}
begin
Result := TreeView.Items.AddChild(ParentNode, ViewItem.Description)
as TViewItemTreeNode;
Result.ViewItem := ViewItem;
end;
procedure TOverviewTreeBuilder.Build;
{Populates the treeview.
}
var
Snippet: TSnippet; // each snippet in a list
ParentNode: TViewItemTreeNode; // each section node in tree
Grouping: TGrouping; // groups snippets
Group: TGroupItem; // each group of snippets
begin
// Create required grouping of snippets
Grouping := CreateGrouping;
try
// Create tree
for Group in Grouping do
begin
if not Group.IsEmpty or Preferences.ShowEmptySections then
begin
ParentNode := AddViewItemNode(nil, CreateViewItemForGroup(Group));
for Snippet in Group.SnippetList do
AddViewItemNode(
ParentNode, TViewFactory.CreateSnippetView(Snippet)
);
end;
end;
finally
Grouping.Free;
end;
end;
constructor TOverviewTreeBuilder.Create(const TV: TTreeView;
const SnippetList: TSnippetList);
{Class constructor. Sets up object to populate a treeview with a list of
snippets.
@param TV [in] Treeview control to be populated.
@param SnippetList [in] List of snippets to be added to TV.
}
begin
inherited Create;
fTreeView := TV;
fSnippetList := SnippetList;
end;
{ TOverviewCategorisedTreeBuilder }
function TOverviewCategorisedTreeBuilder.CreateGrouping: TGrouping;
{Creates a categorised grouping object.
@return Required grouping object.
}
begin
Result := TCategoryGrouping.Create(SnippetList);
end;
function TOverviewCategorisedTreeBuilder.CreateViewItemForGroup(
const Group: TGroupItem): IView;
{Creates a category view item from group item.
@param Group [in] Group item containing category data.
@return Required category view item for group.
}
begin
Result := TViewFactory.CreateCategoryView(
(Group as TCategoryGroupItem).Category
);
end;
{ TOverviewAlphabeticTreeBuilder }
function TOverviewAlphabeticTreeBuilder.CreateGrouping: TGrouping;
{Creates an alphabetic grouping object.
@return Required grouping object.
}
begin
Result := TAlphaGrouping.Create(SnippetList);
end;
function TOverviewAlphabeticTreeBuilder.CreateViewItemForGroup(
const Group: TGroupItem): IView;
{Creates an alpha view item from group item.
@param Group [in] Group item containing alpha data.
@return Required alpha view item for group.
}
begin
Result := TViewFactory.CreateInitialLetterView(
(Group as TAlphaGroupItem).Letter
);
end;
{ TOverviewSnipKindTreeBuilder }
function TOverviewSnipKindTreeBuilder.CreateGrouping: TGrouping;
{Creates a snippet kind grouping object.
@return Required grouping object.
}
begin
Result := TSnipKindGrouping.Create(SnippetList);
end;
function TOverviewSnipKindTreeBuilder.CreateViewItemForGroup(
const Group: TGroupItem): IView;
{Creates a snippet kind view item from group item.
@param Group [in] Group item containing snippet kind data.
@return Required snippet kind view item for group.
}
begin
Result := TViewFactory.CreateSnippetKindView(
(Group as TSnipKindGroupItem).SnipKindInfo
);
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.