unit uEditorControlesBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBBase, Menus, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TB2ExtItems, TBXExtItems,
  TBX, TB2Item, TB2Dock, TB2Toolbar, ExtCtrls, JvExControls, JvComponent,
  JvNavigationPane, pngimage, uBizControlesBase, uEditorBase,
  uViewControlesBase;

type
  IEditorControlesBase = interface(IEditorDBBase)
  ['{9B15BCEA-4C31-442A-89EC-773B66460A3B}']
    function GetControl: IBizControlBase;
    procedure SetControl(const Value: IBizControlBase);
    property Control: IBizControlBase read GetControl write SetControl;
  end;

  TfEditorControlesBase = class(TfEditorDBBase, IEditorControlesBase)
    ViewPopupMenu: TPopupMenu;
    Modificar1: TMenuItem;
    Eliminar1: TMenuItem;
    N1: TMenuItem;
    Nuevo1: TMenuItem;
    pngImageNuevo: TPngImageList;
    actPropiedades: TAction;
    TBXSeparatorItem14: TTBXSeparatorItem;
    TBXItem7: TTBXItem;
    N2: TMenuItem;
    Propiedades1: TMenuItem;
    procedure actNuevoExecute(Sender: TObject);
    procedure actModificarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPropiedadesExecute(Sender: TObject);
    procedure actPropiedadesUpdate(Sender: TObject);
  protected
    FControl : IBizControlBase;
    FViewControl : IViewControlesBase;
    function GetControl: IBizControlBase; virtual;
    procedure SetControl(const Value: IBizControlBase); virtual;
  public
    destructor Destroy; override;  
    property Control: IBizControlBase read GetControl write SetControl;
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{$R *.dfm}

{ TfEditorControlesBase }

function TfEditorControlesBase.GetControl: IBizControlBase;
begin
  Result := FControl;
end;

procedure TfEditorControlesBase.SetControl(const Value: IBizControlBase);
begin
  FControl := Value;
  dsDataTable.DataTable := FControl.DataTable;
  if Assigned(FViewControl) then
  begin
    FViewControl.Control := FControl;
  end;
end;

procedure TfEditorControlesBase.actNuevoExecute(Sender: TObject);
begin
  inherited;
  try
    FControl.Insert;
    FControl.Show;
  finally
    FViewControl.Refresh;
  end;
end;

procedure TfEditorControlesBase.actModificarExecute(Sender: TObject);
begin
  inherited;
  try
    FControl.Show;
  finally
    FViewControl.Refresh;
  end;
end;

procedure TfEditorControlesBase.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(FViewControl) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(Control) then
    raise Exception.Create('No hay ningún control asignado');

  FViewControl.OnDblClick := actModificar.OnExecute;
  FViewControl.PopupMenu := ViewPopupMenu;

  Control.DataTable.Active := True;

  FViewControl.Refresh;
end;

destructor TfEditorControlesBase.Destroy;
begin
  FViewControl := NIL;
  FControl := NIL;
  inherited;
end;

procedure TfEditorControlesBase.actEliminarExecute(Sender: TObject);
begin
  try
    if not FControl.EsModificable then
      ShowMessage('Este control no se puede borrar')
    else
      if (Application.MessageBox('¿Desea borrar este control?', 'Atención', MB_YESNO) = IDYES) then
        inherited;
  finally
    FViewControl.Refresh;
  end;
end;

procedure TfEditorControlesBase.actRefrescarExecute(Sender: TObject);
begin
  inherited;
  if Assigned(FViewControl) then
    FViewControl.Refresh;
end;

procedure TfEditorControlesBase.actPropiedadesExecute(Sender: TObject);
begin
  inherited;
  if Assigned(FControl) then
    try
      FControl.VerPropiedades;
      actGuardar.Execute;
    finally
      FViewControl.Refresh;
    end;
end;

procedure TfEditorControlesBase.actPropiedadesUpdate(Sender: TObject);
begin
  inherited;
  if Assigned(dsDataTable.DataTable) then
    (Sender as TAction).Enabled := (not dsDataTable.DataTable.IsEmpty)
      and not (dsDataTable.DataTable.State = dsInsert)
  else
    (Sender as TAction).Enabled := False;
end;

constructor TfEditorControlesBase.Create(AOwner: TComponent);
begin
  inherited;
  actModificar.ShortCut := ShortCut(VK_RETURN, []);
end;

end.
