1
0
mirror of synced 2024-11-22 19:33:51 +00:00
unitswitcher/Source/BaseSwFilters.pas

126 lines
2.5 KiB
ObjectPascal
Raw Normal View History

{: Implements the base filtering visitors.
Last changed: $Date$
Revision: $Rev$
Author: $Author$
}
unit BaseSwFilters;
interface
uses
Classes,
2009-05-01 08:44:18 +00:00
Masks,
BaseSwObjects;
type
TBaseSwItemFilter = class(TInterfacedPersistent, IBaseSwVisitor)
private
FList: TBaseSwItemList;
protected
// IBaseSwVisitor
procedure VisitItem(const AItem: TBaseSwItem); virtual;
procedure FilterItem(const AItem: TBaseSwItem); virtual;
public
procedure FilterList(AList: TBaseSwItemList);
end;
TBaseSwItemSimpleFilter = class(TBaseSwItemFilter)
private
FFilter: String;
2009-05-01 08:44:18 +00:00
FFilterMask: TMask;
FWildchars: Boolean;
2009-05-01 08:44:18 +00:00
function GetFilterMask(): TMask;
procedure SetFilter(const Value: String);
2009-05-01 08:44:18 +00:00
protected
property FilterMask: TMask read GetFilterMask;
public
2009-05-01 08:44:18 +00:00
property Filter: String read FFilter write SetFilter;
property Wildchars: Boolean read FWildchars write FWildchars;
end;
TBaseSwItemSimpleNameFilter = class(TBaseSwItemSimpleFilter)
protected
procedure VisitItem(const AItem: TBaseSwItem); override;
end;
implementation
uses
SysUtils;
{ TBaseSwItemFilter }
procedure TBaseSwItemFilter.FilterList(AList: TBaseSwItemList);
begin
FList := AList;
try
FList.AcceptVisitor(Self);
finally
FList := nil;
end;
end;
procedure TBaseSwItemFilter.FilterItem(const AItem: TBaseSwItem);
begin
FList.Remove(AItem);
end;
procedure TBaseSwItemFilter.VisitItem(const AItem: TBaseSwItem);
begin
end;
{ TBaseSwItemSimpleFilter }
2009-05-01 08:44:18 +00:00
function TBaseSwItemSimpleFilter.GetFilterMask(): TMask;
begin
if not Assigned(FFilterMask) then
FFilterMask := TMask.Create('*' + FFilter + '*');
Result := FFilterMask;
end;
procedure TBaseSwItemSimpleFilter.SetFilter(const Value: String);
2009-05-01 08:44:18 +00:00
var
newValue: string;
begin
2009-05-01 08:44:18 +00:00
newValue := LowerCase(Value);
if newValue <> FFilter then
begin
FFilter := newValue;
FreeAndNil(FFilterMask);
end;
end;
{ TBaseSwItemSimpleNameFilter }
procedure TBaseSwItemSimpleNameFilter.VisitItem(const AItem: TBaseSwItem);
2009-05-01 08:44:18 +00:00
var
matches: Boolean;
itemName: string;
begin
2009-05-01 08:44:18 +00:00
if Length(Filter) > 0 then
begin
itemName := LowerCase(AItem.Name);
if Wildchars then
matches := FilterMask.Matches(itemName)
else
matches := (AnsiPos(Filter, itemName) > 0);
if not matches then
FilterItem(AItem);
end;
end;
end.