{
===============================================================================
  Copyright (©) 2006. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 22-05-2006
  Versión actual: 1.0.0
  Fecha versión actual: 22-05-2006
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit uEditorFamilias;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, StdCtrls, DBCtrls, uDADataTable, ActnList,
  uIEditorFamilias, uCustomEditor, uGUIBase, ExtCtrls,
  ComCtrls, uBizFamilias, uFamiliasController, uDAInterfaces;

type
  TfEditorFamilias = class(TCustomEditor, IEditorFamilias)
    ActionList1: TActionList;
    DADataSource: TDADataSource;
    ListaFamilias: TDBLookupListBox;
    actAnadir: TAction;
    actEliminar: TAction;
    bEliminar: TButton;
    actAceptar: TAction;
    actCancelar: TAction;
    bAceptar: TButton;
    bCancelar: TButton;
    actCerrar: TAction;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Bevel1: TBevel;
    eCategoria: TLabel;
    editFamilia: TEdit;
    bAnadir: TButton;
    procedure FormShow(Sender: TObject);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);

    procedure actAnadirExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actAceptarExecute(Sender: TObject);
    procedure actCancelarExecute(Sender: TObject);
    procedure actCerrarExecute(Sender: TObject);
    procedure actAnadirUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
  protected
    FFamilias: IBizFamilia;
    FController: IFamiliasController;
    function GetFamilias: IBizFamilia;
    procedure SetFamilias(const Value: IBizFamilia);
    function GetController : IFamiliasController;
    procedure SetController (const Value : IFamiliasController);
  public
    property Familias: IBizFamilia read GetFamilias write SetFamilias;
    property Controller : IFamiliasController read GetController write SetController;
  end;


implementation
{$R *.dfm}

{ TfEditorFamilias }

uses
  uDialogUtils;

function TfEditorFamilias.GetController: IFamiliasController;
begin
  Result := FController;
end;

function TfEditorFamilias.GetFamilias: IBizFamilia;
begin
  Result := FFamilias;
end;

procedure TfEditorFamilias.SetController(const Value: IFamiliasController);
begin
  FController := Value;
end;

procedure TfEditorFamilias.SetFamilias(const Value: IBizFamilia);
begin
  FFamilias := Value;
  DADataSource.DataTable := (FFamilias as IBizFamilia).DataTable;
  DADataSource.DataTable.Open;
end;

procedure TfEditorFamilias.actAnadirExecute(Sender: TObject);
begin
  if not FController.Localizar(Familias, editFamilia.Text) then
  begin
    Familias.Append;
    Familias.DESCRIPCION := editFamilia.Text;
    Familias.Post;
    editFamilia.Clear;
    ListaFamilias.SetFocus;
  end
  else
    ShowErrorMessage('Ya existe la familia ' + editFamilia.Text,
      'Ya existe la familia ''' + editFamilia.Text + ''' en la lista.');
end;

procedure TfEditorFamilias.actEliminarExecute(Sender: TObject);
begin
  Familias.Delete;
  ListaFamilias.SetFocus;
end;

procedure TfEditorFamilias.actEliminarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (FFamilias.DataTable.RecordCount > 0) and
                                  (Length(ListaFamilias.SelectedItem) > 0);

end;

procedure TfEditorFamilias.CustomEditorClose(Sender: TObject; var Action: TCloseAction);
begin
  FFamilias := Nil;
  FController := Nil;
end;

procedure TfEditorFamilias.actAceptarExecute(Sender: TObject);
begin
  try
    Familias.DataTable.ApplyUpdates;
  except
    on E : Exception do begin
      Familias.DataTable.CancelUpdates;
      ShowErrorMessage('Error al guardar cambios', 'Se ha producido un error grave', E);
      exit;
    end;
  end;
  actCerrar.Execute;
end;

procedure TfEditorFamilias.actCancelarExecute(Sender: TObject);
begin
  Familias.DataTable.CancelUpdates;
  actCerrar.Execute;
end;

procedure TfEditorFamilias.actCerrarExecute(Sender: TObject);
begin
  Close;
end;

procedure TfEditorFamilias.actAnadirUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (Length(editFamilia.Text) > 0)
end;

procedure TfEditorFamilias.FormShow(Sender: TObject);
begin
  if not FFamilias.DataTable.Active then
     FFamilias.DataTable.Active := true;

  EditFamilia.SetFocus;
end;

end.
