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
| uses Borland.Vcl.classes, Borland.Vcl.Contnrs, ExpertApplications.BaseNote.Contnrs;
type
EIndexedCollectionError = class(Exception);
TIndexedCollection = class;
TIndexedCollectionItem = class;
TIndexedCollectionItemClass = class of TIndexedCollectionItem;
TIndexedBucketProc = procedure(AInfo, AItem, AData: TObject; out AContinue: Boolean);
TIndexedCollection = class(TPersistent)
private
FItemClass: TIndexedCollectionItemClass;
FItems: TIndexedBucketList;
FNotIndexed: TList;
FUpdateCount: Integer;
function GetCount: Integer;
procedure InsertItem(Item: TIndexedCollectionItem);
procedure RemoveItem(Item: TIndexedCollectionItem);
procedure UpdateIndex(Item: TIndexedCollectionItem);
protected
procedure Notify(Item: TIndexedCollectionItem; Action: TCollectionNotification); virtual;
{ Design-time editor support }
procedure Changed;
function GetItem(Index: Integer): TIndexedCollectionItem;
procedure Update(Item: TIndexedCollectionItem); virtual;
property UpdateCount: Integer read FUpdateCount;
public
constructor Create(ItemClass: TIndexedCollectionItemClass; Count:
TBucketListSizes);
destructor Destroy; override;
function Add: TIndexedCollectionItem;
procedure Assign(Source: TPersistent); override;
procedure BeginUpdate; virtual;
procedure Clear;
procedure Delete(Index: Integer);
procedure EndUpdate; virtual;
function ForEach(AProc: TIndexedBucketProc; AInfo: TObject = nil): Boolean;
function Insert(Index: Integer): TIndexedCollectionItem;
property Count: Integer read GetCount;
property ItemClass: TIndexedCollectionItemClass read FItemClass;
property IndexedItems[Index: Integer]: TIndexedCollectionItem read GetItem;
end;
TIndexedCollectionItem = class(TPersistent)
private
FCollection: TIndexedCollection;
FIndex: Integer;
function GetIndex: Integer;
protected
procedure Changed(AllItems: Boolean);
procedure SetCollection(Value: TIndexedCollection); virtual;
procedure SetIndex(Value: Integer); virtual;
public
constructor Create(Collection: TIndexedCollection); virtual;
destructor Destroy; override;
property Collection: TIndexedCollection read FCollection write SetCollection;
property Index: Integer read GetIndex;
end;
implementation
uses Borland.Vcl.RTLConsts;
{ TIndexedCollection }
function TIndexedCollection.Add: TIndexedCollectionItem;
begin
Result := FItemClass.Create(Self);
end;
procedure AssignItem(AInfo,AItem,AData:TObject;out AContinue : boolean);
begin
if Integer(AItem)=0 then
TIndexedCollection(AInfo).FNotIndexed.Add(AData)
else
TIndexedCollection(AInfo).Add.Assign(TPersistent(AData));
AContinue:=true;
end;
procedure TIndexedCollection.Assign(Source: TPersistent);
begin
if Source is TIndexedCollection then
begin
BeginUpdate;
try
Clear;
TIndexedCollection(Source).ForEach(AssignItem,self);
finally
EndUpdate;
end;
Exit;
end;
inherited Assign(Source);
end;
procedure TIndexedCollection.BeginUpdate;
begin
Inc(FUpdateCount);
end;
procedure TIndexedCollection.Changed;
begin
if FUpdateCount = 0 then
Update(nil);
end;
procedure FreeAll(AInfo,AItem,AData:TObject;out AContinue : boolean);
begin
TIndexedCollection(AInfo).FItems.Remove(Integer(AItem));
AData.Free;
AContinue:=true;
end;
// Procédure TIndexedCollection.Clear
// Supprime les éléments de la liste et libère la mémoire associée.
procedure TIndexedCollection.Clear;
var
I: Integer;
begin
BeginUpdate;
try
FItems.ForEach(FreeAll,self);
for I := FNotIndexed.Count - 1 downto 0 do
TIndexedCollectionItem(FNotIndexed.Extract(FNotIndexed.Items[I])).free;
finally
EndUpdate;
end;
end;
constructor TIndexedCollection.Create(ItemClass: TIndexedCollectionItemClass;
Count: TBucketListSizes);
begin
inherited Create;
FItemClass := ItemClass;
FItems := TIndexedBucketList.Create(Count);
FNotIndexed := TList.Create;
end;
procedure TIndexedCollection.Delete(Index: Integer);
var item : TIndexedCollectionItem;
begin
item:=TIndexedCollectionItem(FItems.Remove(Index));
Notify(item, cnDeleting);
item.Free;
end;
destructor TIndexedCollection.Destroy;
begin
FUpdateCount := 1;
Clear;
FItems.Free;
FNotIndexed.Free;
inherited Destroy;
end;
procedure TIndexedCollection.EndUpdate;
begin
Dec(FUpdateCount);
Changed;
end;
function TIndexedCollection.ForEach(AProc: TIndexedBucketProc;
AInfo: TObject): Boolean;
var
i: Integer;
begin
for i := 0 to FNotIndexed.count - 1 do
begin
AProc(AInfo,TObject(0),FNotIndexed.Items[i],result);
if not result then
exit;
end;
result:=FItems.ForEach(AProc,AInfo);
end;
function TIndexedCollection.GetCount: Integer;
begin
result:=FNotIndexed.Count+FItems.Count;
end;
function TIndexedCollection.GetItem(Index: Integer): TIndexedCollectionItem;
begin
result:=TIndexedCollectionItem(FItems.Data[Index]);
end;
function TIndexedCollection.Insert(Index: Integer): TIndexedCollectionItem;
begin
Result := Add;
Result.SetIndex(Index);
end;
procedure TIndexedCollection.InsertItem(Item: TIndexedCollectionItem);
begin
if not (Item is FItemClass) then
TList.Error(SInvalidProperty, 0);
if Item.FIndex<>0 then
FItems.Add(Item.Index,Item)
else
FNotIndexed.Add(Item);
Item.FCollection := Self;
Notify(Item, cnAdded);
Changed;
end;
procedure TIndexedCollection.Notify(Item: TIndexedCollectionItem;
Action: TCollectionNotification);
begin
end;
procedure TIndexedCollection.RemoveItem(Item: TIndexedCollectionItem);
var
AData: TObject;
begin
Notify(Item, cnExtracting);
if FItems.Find(TObject(Item.Index),AData) then
FItems.Remove(Item.Index)
else FNotIndexed.Remove(item);
Item.FCollection := nil;
Changed;
end;
procedure TIndexedCollection.Update(Item: TIndexedCollectionItem);
begin
end;
procedure TIndexedCollection.UpdateIndex(Item: TIndexedCollectionItem);
begin
FNotIndexed.Extract(Item);
FItems.Add(Item.Index,Item);
end;
{ TIndexedCollectionItem }
procedure TIndexedCollectionItem.Changed(AllItems: Boolean);
var
Item: TIndexedCollectionItem;
begin
if (FCollection <> nil) and (FCollection.FUpdateCount = 0) then
begin
if AllItems then
Item := nil
else
Item := Self;
FCollection.Update(Item);
end;
end;
constructor TIndexedCollectionItem.Create(Collection: TIndexedCollection);
begin
inherited Create;
SetCollection(Collection);
end;
destructor TIndexedCollectionItem.Destroy;
begin
SetCollection(nil);
inherited;
end;
function TIndexedCollectionItem.GetIndex: Integer;
begin
result:=FIndex;
end;
procedure TIndexedCollectionItem.SetCollection(Value: TIndexedCollection);
begin
if FCollection <> Value then
begin
if FCollection <> nil then
FCollection.RemoveItem(Self);
if Value <> nil then
Value.InsertItem(Self);
end;
end;
procedure TIndexedCollectionItem.SetIndex(Value: Integer);
var
CurIndex: Integer;
begin
CurIndex := GetIndex;
if (CurIndex = 0) and (CurIndex <> Value) then
begin
//Prévoir le changement d'index
if FCollection<>nil then
FCollection.UpdateIndex(self);
Changed(True);
end
else if CurIndex>0 then
EIndexedCollectionError.Create('Impossible de modifier l''index d''un élément');
end;
end. |