2007-12-10 20:29:57 +00:00
|
|
|
unit CmpSwFilters;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2007-12-11 19:41:46 +00:00
|
|
|
Classes,
|
|
|
|
Contnrs,
|
|
|
|
|
2007-12-10 20:29:57 +00:00
|
|
|
BaseSwFilters,
|
|
|
|
BaseSwObjects;
|
|
|
|
|
|
|
|
|
|
|
|
type
|
2007-12-11 19:41:46 +00:00
|
|
|
TCmpSwFilterGroup = class(TCollectionItem)
|
|
|
|
private
|
2007-12-12 10:18:59 +00:00
|
|
|
FEnabled: Boolean;
|
|
|
|
FFilter: TStrings;
|
|
|
|
FFilterChanged: Boolean;
|
|
|
|
FFilterMasks: TObjectList;
|
|
|
|
FIncludeDescendants: Boolean;
|
|
|
|
FName: String;
|
|
|
|
FVisible: Boolean;
|
2007-12-11 19:41:46 +00:00
|
|
|
protected
|
|
|
|
procedure FilterChange(Sender: TObject);
|
|
|
|
|
|
|
|
function ContainsMask(const AFilter: String): Boolean;
|
|
|
|
procedure UpdateFilters();
|
|
|
|
|
|
|
|
property FilterMasks: TObjectList read FFilterMasks;
|
|
|
|
public
|
|
|
|
constructor Create(Collection: TCollection); override;
|
|
|
|
destructor Destroy(); override;
|
|
|
|
|
|
|
|
function Matches(const AValue: String): Boolean;
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
property Enabled: Boolean read FEnabled write FEnabled;
|
|
|
|
property Filter: TStrings read FFilter;
|
|
|
|
property IncludeDescendants: Boolean read FIncludeDescendants write FIncludeDescendants;
|
|
|
|
property Name: String read FName write FName;
|
|
|
|
property Visible: Boolean read FVisible write FVisible;
|
2007-12-11 19:41:46 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
TCmpSwMatchResult = (mrNoMatch, mrDisabledMatch, mrMatch);
|
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
TCmpSwFilterGroups = class(TCollection)
|
|
|
|
private
|
|
|
|
function GetItem(Index: Integer): TCmpSwFilterGroup;
|
|
|
|
procedure SetItem(Index: Integer; Value: TCmpSwFilterGroup);
|
|
|
|
public
|
|
|
|
constructor Create();
|
|
|
|
|
|
|
|
function Add(): TCmpSwFilterGroup;
|
2007-12-12 10:18:59 +00:00
|
|
|
function Matches(const AValue: String; AMatchDisabled: Boolean): TCmpSwMatchResult;
|
2007-12-11 19:41:46 +00:00
|
|
|
|
|
|
|
property Items[Index: Integer]: TCmpSwFilterGroup read GetItem write SetItem; default;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
TCmpSwComponentClassFilter = class(TBaseSwItemFilter)
|
|
|
|
private
|
2007-12-12 10:18:59 +00:00
|
|
|
FFilterUnmatched: Boolean;
|
|
|
|
FGroups: TCmpSwFilterGroups;
|
2007-12-14 15:38:55 +00:00
|
|
|
FOwnsGroups: Boolean;
|
|
|
|
|
|
|
|
function GetGroups(): TCmpSwFilterGroups;
|
|
|
|
procedure SetGroups(const Value: TCmpSwFilterGroups);
|
2007-12-10 20:29:57 +00:00
|
|
|
protected
|
|
|
|
procedure VisitItem(const AItem: TBaseSwItem); override;
|
2007-12-11 19:41:46 +00:00
|
|
|
public
|
|
|
|
destructor Destroy(); override;
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
property FilterUnmatched: Boolean read FFilterUnmatched write FFilterUnmatched;
|
2007-12-14 15:38:55 +00:00
|
|
|
property Groups: TCmpSwFilterGroups read GetGroups write SetGroups;
|
2007-12-10 20:29:57 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
uses
|
2007-12-11 19:41:46 +00:00
|
|
|
Masks,
|
2007-12-10 20:29:57 +00:00
|
|
|
SysUtils,
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
CmpSwObjects, Dialogs;
|
2007-12-10 20:29:57 +00:00
|
|
|
|
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
const
|
|
|
|
MaskChars = ['*', '?', '['];
|
|
|
|
|
|
|
|
|
|
|
|
{ TCmpSwFilterGroup }
|
|
|
|
constructor TCmpSwFilterGroup.Create(Collection: TCollection);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
FFilterMasks := TObjectList.Create(True);
|
|
|
|
FFilter := TStringList.Create();
|
|
|
|
TStringList(FFilter).OnChange := FilterChange;
|
|
|
|
|
|
|
|
FEnabled := True;
|
|
|
|
FVisible := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
destructor TCmpSwFilterGroup.Destroy();
|
|
|
|
begin
|
|
|
|
FreeAndNil(FFilter);
|
|
|
|
FreeAndNil(FFilterMasks);
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TCmpSwFilterGroup.Matches(const AValue: String): Boolean;
|
|
|
|
var
|
|
|
|
filterIndex: Integer;
|
2007-12-12 10:18:59 +00:00
|
|
|
valueClass: TClass;
|
|
|
|
filterClass: TClass;
|
2007-12-11 19:41:46 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
UpdateFilters();
|
|
|
|
|
|
|
|
for filterIndex := Pred(Filter.Count) downto 0 do
|
|
|
|
begin
|
|
|
|
if Assigned(Filter.Objects[filterIndex]) then
|
2007-12-12 10:18:59 +00:00
|
|
|
begin
|
|
|
|
Result := TMask(Filter.Objects[filterIndex]).Matches(AValue);
|
|
|
|
end else
|
|
|
|
begin
|
2007-12-11 19:41:46 +00:00
|
|
|
Result := SameText(Filter[filterIndex], AValue);
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
if (not Result) and IncludeDescendants then
|
|
|
|
begin
|
|
|
|
filterClass := GetClass(Filter[filterIndex]);
|
|
|
|
if Assigned(filterClass) then
|
|
|
|
begin
|
|
|
|
valueClass := GetClass(AValue);
|
|
|
|
if Assigned(valueClass) then
|
|
|
|
Result := valueClass.InheritsFrom(filterClass);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
if Result then
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TCmpSwFilterGroup.FilterChange(Sender: TObject);
|
|
|
|
begin
|
|
|
|
FFilterChanged := True;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TCmpSwFilterGroup.ContainsMask(const AFilter: String): Boolean;
|
|
|
|
var
|
|
|
|
charIndex: Integer;
|
|
|
|
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
|
|
|
|
for charIndex := Length(AFilter) downto 1 do
|
|
|
|
if AFilter[charIndex] in MaskChars then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TCmpSwFilterGroup.UpdateFilters();
|
|
|
|
var
|
|
|
|
filterIndex: Integer;
|
|
|
|
mask: TMask;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if not FFilterChanged then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
FilterMasks.Clear();
|
|
|
|
|
|
|
|
for filterIndex := Pred(Filter.Count) downto 0 do
|
|
|
|
begin
|
|
|
|
if ContainsMask(Filter[filterIndex]) then
|
|
|
|
begin
|
|
|
|
mask := TMask.Create(Filter[filterIndex]);
|
|
|
|
|
|
|
|
FilterMasks.Add(mask);
|
|
|
|
Filter.Objects[filterIndex] := mask;
|
|
|
|
end else
|
|
|
|
Filter.Objects[filterIndex] := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
FFilterChanged := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TCmpSwFilterGroups }
|
|
|
|
constructor TCmpSwFilterGroups.Create;
|
|
|
|
begin
|
|
|
|
inherited Create(TCmpSwFilterGroup);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TCmpSwFilterGroups.Add: TCmpSwFilterGroup;
|
|
|
|
begin
|
|
|
|
Result := TCmpSwFilterGroup(inherited Add);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TCmpSwFilterGroups.GetItem(Index: Integer): TCmpSwFilterGroup;
|
|
|
|
begin
|
|
|
|
Result := TCmpSwFilterGroup(inherited GetItem(Index));
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TCmpSwFilterGroups.SetItem(Index: Integer; Value: TCmpSwFilterGroup);
|
|
|
|
begin
|
|
|
|
inherited SetItem(Index, Value);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-12 10:18:59 +00:00
|
|
|
function TCmpSwFilterGroups.Matches(const AValue: String; AMatchDisabled: Boolean): TCmpSwMatchResult;
|
2007-12-10 20:29:57 +00:00
|
|
|
var
|
2007-12-11 19:41:46 +00:00
|
|
|
itemIndex: Integer;
|
2007-12-10 20:29:57 +00:00
|
|
|
|
|
|
|
begin
|
2007-12-12 10:18:59 +00:00
|
|
|
Result := mrNoMatch;
|
2007-12-10 20:29:57 +00:00
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
for itemIndex := Pred(Count) downto 0 do
|
|
|
|
begin
|
2007-12-12 10:18:59 +00:00
|
|
|
if AMatchDisabled or Items[itemIndex].Enabled then
|
2007-12-11 19:41:46 +00:00
|
|
|
begin
|
2007-12-12 10:18:59 +00:00
|
|
|
if Items[itemIndex].Matches(AValue) then
|
|
|
|
begin
|
|
|
|
if Items[itemIndex].Enabled then
|
|
|
|
Result := mrMatch
|
|
|
|
else
|
|
|
|
Result := mrDisabledMatch;
|
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
Break;
|
2007-12-12 10:18:59 +00:00
|
|
|
end;
|
2007-12-11 19:41:46 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{ TCmpSwComponentClassFilter }
|
2007-12-14 15:38:55 +00:00
|
|
|
destructor TCmpSwComponentClassFilter.Destroy();
|
2007-12-11 19:41:46 +00:00
|
|
|
begin
|
2007-12-14 15:38:55 +00:00
|
|
|
if FOwnsGroups then
|
|
|
|
FreeAndNil(FGroups);
|
|
|
|
|
2007-12-11 19:41:46 +00:00
|
|
|
inherited;
|
2007-12-14 15:38:55 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
function TCmpSwComponentClassFilter.GetGroups(): TCmpSwFilterGroups;
|
|
|
|
begin
|
|
|
|
if not Assigned(FGroups) then
|
|
|
|
begin
|
|
|
|
FGroups := TCmpSwFilterGroups.Create();
|
|
|
|
FOwnsGroups := True;
|
|
|
|
end;
|
2007-12-11 19:41:46 +00:00
|
|
|
|
2007-12-14 15:38:55 +00:00
|
|
|
Result := FGroups;
|
2007-12-11 19:41:46 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2007-12-14 15:38:55 +00:00
|
|
|
procedure TCmpSwComponentClassFilter.SetGroups(const Value: TCmpSwFilterGroups);
|
2007-12-11 19:41:46 +00:00
|
|
|
begin
|
2007-12-14 15:38:55 +00:00
|
|
|
if Value <> FGroups then
|
|
|
|
begin
|
|
|
|
if FOwnsGroups then
|
|
|
|
FreeAndNil(FGroups);
|
2007-12-11 19:41:46 +00:00
|
|
|
|
2007-12-14 15:38:55 +00:00
|
|
|
FGroups := Value;
|
|
|
|
FOwnsGroups := False;
|
|
|
|
end;
|
2007-12-11 19:41:46 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TCmpSwComponentClassFilter.VisitItem(const AItem: TBaseSwItem);
|
|
|
|
begin
|
2007-12-12 10:18:59 +00:00
|
|
|
case Groups.Matches(TCmpSwComponent(AItem).ComponentClass, FilterUnmatched) of
|
|
|
|
mrMatch:
|
|
|
|
FilterItem(AItem);
|
|
|
|
mrNoMatch:
|
|
|
|
if FilterUnmatched then
|
|
|
|
FilterItem(AItem);
|
|
|
|
end;
|
2007-12-10 20:29:57 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|