unit FMX.RS.TreeViewHelper;
//=== File Prolog ============================================================
//    This code was developed by RiverSoftAVG (www.RiverSoftAVG.com).
//
//--- Notes ------------------------------------------------------------------
//
//--- Development History  ---------------------------------------------------
//
//      03/2016 T. Grubb
//              Initial Version
//
//      File Contents:
//           TreeView Helper Functions
//
//
//--- Warning ----------------------------------------------------------------
//  This software is property of RiverSoftAVG. Unauthorized use or
//  duplication of this software is strictly prohibited. Authorized users
//  are subject to the following restrictions:
//    * RiverSoftAVG is not responsible for
//      any consequence of the use of this software.
//    * The origin of this software must not be misrepresented either by
//      explicit claim or by omission.
//    * Altered versions of this software must be plainly marked as such.
//    * This notice may not be removed or altered.
//
//      © 2016, Thomas G. Grubb
//
//=== End File Prolog ========================================================

interface

uses
  Classes,
  FMX.TreeView,
  SysUtils;

type
  TTreeViewCompareFunction = reference to function (const Item: TTreeViewItem): Boolean;
  TTreeViewItemHelper = class helper for TTreeViewItem
  private
    { private declarations }
  protected
    { protected declarations }
  public
    { public declarations }
    function Add( const aText: String ): TTreeViewItem; overload;
    function Add( const aText, aData: String ): TTreeViewItem; overload;
    function AddObject( const aText: String; const aData: TObject ): TTreeViewItem; overload;
    function ItemBy( const Compare: TTreeViewCompareFunction ): TTreeViewItem; overload;
    function ItemByTag( const aText: String ): TTreeViewItem; overload;
    function ItemByTag( const aData: TObject ): TTreeViewItem; overload;
  end;

  TTreeViewHelper = class helper for TCustomTreeView
  private
    { private declarations }
  protected
    { protected declarations }
  public
    { public declarations }
    function Add( const aText: String ): TTreeViewItem; overload;
    function Add( const aText, aData: String ): TTreeViewItem; overload;
    function AddObject( const aText: String; const aData: TObject ): TTreeViewItem; overload;
    function ItemBy( const Compare: TTreeViewCompareFunction ): TTreeViewItem; overload;
    function ItemByTag( const aText: String ): TTreeViewItem; overload;
    function ItemByTag( const aData: TObject ): TTreeViewItem; overload;
  end;

implementation

{ TTreeViewHelper }

function TTreeViewHelper.Add(const aText: String): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.Parent := Self;
end;

function TTreeViewHelper.Add(const aText, aData: String): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.TagString := aData;
  result.Parent := Self;
end;

function TTreeViewHelper.AddObject(const aText: String;
  const aData: TObject): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.TagObject := aData;
  result.Parent := Self;
end;

function TTreeViewHelper.ItemByTag(const aData: TObject): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Items[I].TagObject = aData then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemByTag(aData);
    if Result <> nil then Break;
  end;
end;

function TTreeViewHelper.ItemBy(const Compare: TTreeViewCompareFunction): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Compare(Items[I]) then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemBy(Compare);
    if Result <> nil then Break;
  end;
end;

function TTreeViewHelper.ItemByTag(const aText: String): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Items[I].TagString = aText then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemByTag(aText);
    if Result <> nil then Break;
  end;
end;

{ TTreeViewItemHelper }

function TTreeViewItemHelper.Add(const aText: String): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.Parent := Self;
end;

function TTreeViewItemHelper.Add(const aText, aData: String): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.TagString := aData;
  result.Parent := Self;
end;

function TTreeViewItemHelper.AddObject(const aText: String;
  const aData: TObject): TTreeViewItem;
begin
  result := TTreeViewItem.Create(Self);
  result.Text := aText;
  result.TagObject := aData;
  result.Parent := Self;
end;

function TTreeViewItemHelper.ItemByTag(const aData: TObject): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Items[I].TagObject = aData then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemByTag(aData);
    if Result <> nil then Break;
  end;
end;

function TTreeViewItemHelper.ItemBy(const Compare: TTreeViewCompareFunction): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Compare(Items[I]) then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemBy(Compare);
    if Result <> nil then Break;
  end;
end;

function TTreeViewItemHelper.ItemByTag(const aText: String): TTreeViewItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count-1 do
  begin
    if Items[I].TagString = aText then
    begin
      Result := Items[I];
      Break;
    end;
    Result := Items[I].ItemByTag(aText);
    if Result <> nil then Break;
  end;
end;

end.
