{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uDBSelectionList;

interface

uses
  Classes, DB, cxGridTableView, uDADataTable, uIntegerList, cxGridCustomView;

type
  TRecID = Integer;
  TSelectedRowList = class;

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

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


  procedure SeleccionarFilasDesdeGrid(const AView : TcxCustomGridView;
    var ASelectedRowList : ISelectedRowList);


implementation

uses
  DBConsts, cxGridCustomTableView, cxControls, Math, Variants, Dialogs;

{ TSelectedRowList }

constructor TSelectedRowList.Create(ADataTable : TDADataTable);
begin
  inherited Create;
  FListRecID := TIntegerList.Create;
  
  FDataTable := ADataTable;
end;

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

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

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

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

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

function TSelectedRowList.Find(const Item: TRecID; var Index: Integer): Boolean;
begin
  Result := FListRecID.Find(Item, Index)
end;

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

function TSelectedRowList.GetItem(Index: Integer): TRecID;
begin
  Result := FListRecID.Integers[Index];
end;

function TSelectedRowList.IndexOf(const Item: TRecID): Integer;
var
  AIndex : Integer;
begin
  Result := -1;
  if FListRecID.Find(Item, AIndex) then
    Result := AIndex
end;

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



procedure SeleccionarFilasDesdeGrid(const AView : TcxCustomGridView;
    var ASelectedRowList : ISelectedRowList);
var
  ADataTable : TDADataTable;
  ABookmark : Pointer;
  ARecord: TcxCustomGridRecord;
  i : Integer;
begin
  ADataTable := ASelectedRowList.SelectedRows.FDataTable;
  AView.BeginUpdate;
  ShowHourglassCursor;
  ABookmark := ADataTable.GetBookMark;
  try
    ASelectedRowList.SelectedRows.Clear;
    for i := 0 to TcxCustomGridTableController(AView.Controller).SelectedRecordCount-1 do
    begin
      ARecord := TcxCustomGridTableController(AView.Controller).SelectedRecords[i];
      if (ARecord is TcxGridDataRow) then
      begin
        (ARecord as TcxGridDataRow).Focused := True;
        ASelectedRowList.SelectedRows.CurrentRowSelected := True;
      end;
    end;
    ADataTable.GotoBookmark(ABookmark);
  finally
    AView.EndUpdate;
    ADataTable.FreeBookmark(ABookmark);
    HideHourglassCursor;
  end;
end;


function TSelectedRowList.LocateItem(const Index: Integer) : Boolean;
begin
  if not FDataTable.Active then
    raise EDatabaseError.Create(sDataSetClosed);
  Result := FDataTable.Locate('RecID', Items[Index], []);
end;

end.
