unit uDBSelectionList;

interface

uses
  uDADataTable, Classes, DB;

type
  TSelectedRowList = class;

  ISelectedRowList = interface
  ['{1886B04A-DB0D-40AE-BCAE-DA57CD4CD582}']
    function GetSelectedRows : TSelectedRowList;
    property SelectedRows : TSelectedRowList read GetSelectedRows;
  end;

  TSelectedRowList = class
  private
    FDataTable: TDADataTable;
    FList: TList;
    function GetCount: Integer;
    function GetCurrentRowSelected: Boolean;
    function GetItem(Index: Integer): TBookmark;
    procedure SetCurrentRowSelected(Value: Boolean);
  protected
    function CurrentRow: TBookmark;
    function Compare(const Item1, Item2: TBookmark): Boolean;
  public
    constructor Create(ADataTable : TDADataTable);
    destructor Destroy; override;
    procedure Clear;           // free all bookmarks
    function  Find(const Item: TBookmark; var Index: Integer): Boolean;
    function  IndexOf(const Item: TBookmark): Integer;
    property Count: Integer read GetCount;
    property CurrentRowSelected: Boolean read GetCurrentRowSelected
      write SetCurrentRowSelected;
    property Items[Index: Integer]: TBookmark read GetItem; default;
  end;


implementation

uses
  DBConsts;

{ TSelectedRowList }

constructor TSelectedRowList.Create(ADataTable : TDADataTable);
begin
  inherited Create;
  FList := TList.Create;
  FDataTable := ADataTable;
end;

destructor TSelectedRowList.Destroy;
begin
  Clear;
  FList.Free;
  FDataTable := NIL;
  inherited Destroy;
end;

procedure TSelectedRowList.Clear;
begin
  if FList.Count = 0 then
    Exit;
  FList.Clear;
end;

function TSelectedRowList.Compare(const Item1, Item2: TBookmark): Boolean;
begin
  Result := (Item1 = Item2);
end;

function TSelectedRowList.CurrentRow: TBookmark;
begin
  if not FDataTable.Active then
    raise EDatabaseError.Create(sDataSetClosed);
  Result := FDataTable.GetBookMark
end;

function TSelectedRowList.GetCurrentRowSelected: Boolean;
var
  Index: Integer;
begin
  Result := Find(CurrentRow, Index);
end;

function TSelectedRowList.Find(const Item: TBookmark; var Index: Integer): Boolean;
begin
  Index := FList.IndexOf(Item);
  Result := (Index > -1)
end;

function TSelectedRowList.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TSelectedRowList.GetItem(Index: Integer): TBookmark;
begin
  Result := FList[Index];
end;

function TSelectedRowList.IndexOf(const Item: TBookmark): Integer;
begin
  Result := FList.IndexOf(Item);
end;

procedure TSelectedRowList.SetCurrentRowSelected(Value: Boolean);
var
  Index: Integer;
  Current: TBookmark;
begin
  Current := CurrentRow;
  if (Find(Current, Index) = Value) then
    Exit;
  if Value then
    FList.Add(Current)
  else
    FList.Delete(Index);
end;


end.
