forked from idursun/delphipi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelphiVersionTreeViewModel.pas
176 lines (159 loc) · 4.79 KB
/
DelphiVersionTreeViewModel.pas
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
{**
DelphiPI (Delphi Package Installer)
Author : ibrahim dursun (ibrahimdursun gmail)
License : GNU General Public License 2.0
**}
unit DelphiVersionTreeViewModel;
interface
uses Classes, SysUtils, TreeModel, Utils, Generics.Collections;
type
TDelphiVersionTreeViewModel<T: INode> = class(TTreeModelBase<T>)
private
fNodes: TList<T>;
function FindDelphiVersionIndexByName(const delphiVersionName: string):Integer;
protected
function GetChildren(const parent: T): TList<T>;
public
constructor Create(const nodes: TList<T>); virtual;
function GetChild(const parent: T; index: Integer): T; override;
function GetChildCount(const parent: T): Integer; override;
end;
TCachedDelphiVersionTreeViewModel<T:INode> = class(TDelphiVersionTreeViewModel<T>)
private
fLastNodeCount: Integer;
fCache: array[DELPHI_VERSION_5..DELPHI_LAST_VERSION] of TList<T>;
procedure RefillCache;
public
function GetChild(const parent: T; index: Integer): T; override;
function GetChildCount(const parent: T): Integer; override;
end;
implementation
uses JclStrings, JclFileUtils, RegularExpressions;
{ TDelphiVersionTreeViewModel<T> }
constructor TDelphiVersionTreeViewModel<T>.Create(const nodes: TList<T>);
begin
fNodes := nodes;
end;
function TDelphiVersionTreeViewModel<T>.FindDelphiVersionIndexByName(
const delphiVersionName: string): Integer;
var
I: Integer;
begin
Result := DELPHI_VERSION_UNKNOWN;
for I := DELPHI_VERSION_5 to DELPHI_LAST_VERSION do
if StrCompare(VersionNames[i], delphiVersionName) = 0 then
Result := i;
end;
function TDelphiVersionTreeViewModel<T>.GetChild(const parent: T;
index: Integer): T;
var
ret: TList<T>;
begin
Result := default(T);
ret := GetChildren(parent);
if ret.Count > index then
Result := ret[index];
ret.Free;
end;
function TDelphiVersionTreeViewModel<T>.GetChildCount(const parent: T): Integer;
var
ret: TList<T>;
begin
ret := GetChildren(Parent);
Result := ret.Count;
ret.Free;
end;
function TDelphiVersionTreeViewModel<T>.GetChildren(const parent: T): TList<T>;
var
I: Integer;
path : string;
matches: array[DELPHI_VERSION_5..DELPHI_LAST_VERSION] of integer;
node: T;
ver, delphiVersionIndex: integer;
begin
FillChar(matches, Length(matches)*sizeof(Integer), 0);
Result := TList<T>.Create;
if parent = nil then
begin
for node in fNodes do begin
path := node.GetNodePath;
ver := Utils.GuessDelphiVersion(path);
if ver <> -1 then
Inc(matches[ver]);
end;
for I := DELPHI_VERSION_5 to DELPHI_LAST_VERSION do
if matches[i] > 0 then
Result.Add(DoCreateLogicalNode(VersionNames[i],VersionNames[i]));
end else begin
delphiVersionIndex := FindDelphiVersionIndexByName(parent.GetNodePath);
for node in fNodes do begin
path := node.GetNodePath;
ver := Utils.GuessDelphiVersion(path);
if ver = delphiVersionIndex then
Result.Add(node);
end;
end;
end;
procedure TCachedDelphiVersionTreeViewModel<T>.RefillCache;
var
I: Integer;
versionIndex: Integer;
parents, children: TList<T>;
parentNode: T;
begin
for I := DELPHI_VERSION_5 to DELPHI_LAST_VERSION do
begin
if Assigned(fCache[i]) then
FreeAndNil(fCache[i]);
fCache[i] := TList<T>.Create;
end;
parents := inherited GetChildren(default(T));
try
for parentNode in parents do
begin
children := inherited GetChildren(parentNode);
try
versionIndex := FindDelphiVersionIndexByName(parentNode.GetNodePath);
fCache[versionIndex].AddRange(children);
finally
children.Free;
end;
end;
finally
parents.Free;
end;
fLastNodeCount := fNodes.Count;
end;
function TCachedDelphiVersionTreeViewModel<T>.GetChild(const parent: T;
index: Integer): T;
var
versionIndex: Integer;
begin
if fLastNodeCount <> fNodes.Count then
RefillCache;
Result := default(T);
if parent = nil then begin
Result := inherited GetChild(parent, index);
end else begin
versionIndex := FindDelphiVersionIndexByName(parent.GetNodePath);
if versionIndex in [DELPHI_VERSION_5..DELPHI_LAST_VERSION] then
if index < fCache[versionIndex].Count then
Result := fCache[versionIndex][index];
end;
end;
function TCachedDelphiVersionTreeViewModel<T>.GetChildCount(
const parent: T): Integer;
var
versionIndex : Integer;
begin
if fLastNodeCount <> fNodes.Count then
RefillCache;
if parent <> nil then begin
versionIndex := FindDelphiVersionIndexByName(parent.GetNodePath);
if versionIndex <> -1 then
Result := fCache[versionIndex].Count;
end else begin
Result := inherited GetChildCount(default(T));
end;
end;
end.