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

301 lines
6.8 KiB
ObjectPascal
Raw Normal View History

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
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;
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;
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;
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
FFilterUnmatched: Boolean;
FGroups: TCmpSwFilterGroups;
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;
property FilterUnmatched: Boolean read FFilterUnmatched write FFilterUnmatched;
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,
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;
2007-12-16 13:06:21 +00:00
FEnabled := False;
2007-12-11 19:41:46 +00:00
FVisible := False;
end;
destructor TCmpSwFilterGroup.Destroy();
begin
FreeAndNil(FFilter);
FreeAndNil(FFilterMasks);
inherited;
end;
function TCmpSwFilterGroup.Matches(const AValue: String): Boolean;
var
filterIndex: Integer;
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
begin
Result := TMask(Filter.Objects[filterIndex]).Matches(AValue);
end else
begin
2007-12-11 19:41:46 +00:00
Result := SameText(Filter[filterIndex], AValue);
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
2007-12-16 13:06:21 +00:00
if Length(Trim(Filter[filterIndex])) = 0 then
2007-12-11 19:41:46 +00:00
begin
2007-12-16 13:06:21 +00:00
Filter.Delete(filterIndex);
2007-12-11 19:41:46 +00:00
end else
2007-12-16 13:06:21 +00:00
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;
2007-12-11 19:41:46 +00:00
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;
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
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
if AMatchDisabled or Items[itemIndex].Enabled then
2007-12-11 19:41:46 +00:00
begin
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;
end;
2007-12-11 19:41:46 +00:00
end;
end;
end;
{ TCmpSwComponentClassFilter }
destructor TCmpSwComponentClassFilter.Destroy();
2007-12-11 19:41:46 +00:00
begin
if FOwnsGroups then
FreeAndNil(FGroups);
2007-12-11 19:41:46 +00:00
inherited;
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
Result := FGroups;
2007-12-11 19:41:46 +00:00
end;
procedure TCmpSwComponentClassFilter.SetGroups(const Value: TCmpSwFilterGroups);
2007-12-11 19:41:46 +00:00
begin
if Value <> FGroups then
begin
if FOwnsGroups then
FreeAndNil(FGroups);
2007-12-11 19:41:46 +00:00
FGroups := Value;
FOwnsGroups := False;
end;
2007-12-11 19:41:46 +00:00
end;
procedure TCmpSwComponentClassFilter.VisitItem(const AItem: TBaseSwItem);
begin
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.