/
CommonClasses.pas
144 lines (115 loc) · 3.39 KB
/
CommonClasses.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
{
Copyright (C) 2006-2008 Matteo Salvi of SalvadorSoftware
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
}
unit CommonClasses;
interface
uses
Classes, VirtualTrees;
type
TNodeList = class (TList)
private
fMaxItems: Integer;
protected
procedure RemoveMaxItems;
function GetItems(Index: Integer): PVirtualNode;
procedure SetItems(Index: Integer; Item: PVirtualNode);
public
constructor Create(MaxItems: Integer);
destructor Destroy; override;
function Add(Item: PVirtualNode): Integer;
procedure Delete(Index: integer);
function First: PVirtualNode;
function IndexOf(Item: PVirtualNode): Integer;
procedure Insert(Index: integer; Item: PVirtualNode);
function Last: PVirtualNode;
function Remove(Item: PVirtualNode): Integer;
property Items[Index: Integer]: PVirtualNode read GetItems write SetItems;
procedure SetMaxItems(Number: Integer);
function GetMaxItems: Integer;
end;
implementation
constructor TNodeList.Create(MaxItems: Integer);
begin
fMaxItems := MaxItems;
inherited Create;
end;
destructor TNodeList.destroy;
begin
clear;
inherited Destroy;
end;
function TNodeList.Add(Item: PVirtualNode): Integer;
begin
Self.Remove(Item);
Result := inherited Add(Item);
if fMaxItems > 0 then
Self.RemoveMaxItems;
end;
procedure TNodeList.Delete(Index: integer);
begin
inherited Delete(Index);
end;
function TNodeList.First: PVirtualNode;
begin
Result := inherited First;
end;
procedure TNodeList.RemoveMaxItems;
var
I : Integer;
begin
if (Self.Count - fMaxItems) > 0 then
for I := 1 to Self.Count - fMaxItems do
Self.Delete(fMaxItems);
end;
function TNodeList.IndexOf(Item: PVirtualNode): Integer;
begin
Result := inherited IndexOf(Item);
end;
procedure TNodeList.Insert(Index: integer; Item: PVirtualNode);
begin
Self.Remove(Item);
inherited insert(Index,Item);
if fMaxItems > 0 then
Self.RemoveMaxItems;
end;
function TNodeList.Last: PVirtualNode;
begin
Result := inherited Last;
end;
function TNodeList.Remove(Item: PVirtualNode): Integer;
begin
Result := IndexOf(Item);
if Result <> -1 then
Delete(Result);
end;
function TNodeList.GetItems(Index: Integer): PVirtualNode;
begin
Result := inherited Get(Index);
end;
procedure TNodeList.SetItems(Index: Integer; Item: PVirtualNode);
begin
inherited Put(Index, Item);
end;
procedure TNodeList.SetMaxItems(Number: Integer);
begin
fMaxItems := Number;
//Remove unnecessary items
RemoveMaxItems;
end;
function TNodeList.GetMaxItems: Integer;
begin
Result := fMaxItems;
end;
//------------------------------------------------------------------------------
end.