unit uEditorObra;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewObra, uBizObras, JvNavigationPane, ActnList,
  uEditorBase, StdActns, TB2Dock, TB2Toolbar, TBX, ImgList, PngImageList,
  TB2Item, uEditorItem, DB, uDADataTable, uEditorDBBase, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, StdCtrls, pngimage, ExtCtrls,
  TBXDkPanels, JvButton, AppEvnts, uCustomView, uViewBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, JvComponentBase,

  uViewObras, uIEditorObra, uObrasController, JvExComCtrls,
  JvStatusBar, dxLayoutLookAndFeels, uDAInterfaces, dxGDIPlusClasses;

type
  TfEditorObra = class(TfEditorDBItem, IEditorObra)
    dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList;
    dxLayoutOfficeLookAndFeel1: TdxLayoutOfficeLookAndFeel;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
  protected
    FController : IObrasController;
    FObra: IBizObra;
    FViewObra : IViewObra;

    function GetController : IObrasController;
    procedure SetController (const Value : IObrasController); virtual;
    function GetObra: IBizObra; virtual;
    procedure SetObra(const Value: IBizObra); virtual;
    function GetViewObra: IViewObra;
    procedure SetViewObra(const Value: IViewObra);
    property ViewObra: IViewObra read GetViewObra write SetViewObra;

    procedure GuardarInterno; override;
    procedure EliminarInterno; override;

    procedure PonerTitulos(const ATitulo: string = ''); override;

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;

  public
    property Obra: IBizObra read GetObra write SetObra;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleObras, uDataModuleBase;

function ShowEditorObra (ABizObject : TDADataTableRules): TModalResult;
var
  AEditor: TfEditorObra;
begin
  AEditor := TfEditorObra.Create(Application);
  try
    AEditor.Obra := (ABizObject as IBizObra);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

{
******************************* TfEditorObra *******************************
}
function TfEditorObra.GetObra: IBizObra;
begin
  Result := FObra;
end;

function TfEditorObra.GetController: IObrasController;
begin
  Result := FController;
end;

function TfEditorObra.GetViewObra: IViewObra;
begin
  Result := FViewObra;
end;

procedure TfEditorObra.GuardarInterno;
begin
  inherited;
  FController.Guardar(FObra);
  Modified := False;
end;

procedure TfEditorObra.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  if Assigned(Obra) then
  begin
    if Obra.EsNuevo then
      FTitulo := 'Nueva obra'
    else
      FTitulo := 'Obra'  + ' - ' + Obra.NOMBRE;
  end;

  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorObra.SetObra(const Value: IBizObra);
begin
  FObra := Value;
  dsDataTable.DataTable := FObra.DataTable;

  if Assigned(FViewObra) and Assigned(Obra) then
    FViewObra.Obra := Obra;
end;

procedure TfEditorObra.SetController(const Value: IObrasController);
begin
  FController := Value;
  if Assigned(FViewObra) and Assigned(FController) then
     FViewObra.Controller := FController;
end;

procedure TfEditorObra.SetViewObra(const Value: IViewObra);
begin
  FViewObra := Value;

  if Assigned(FViewObra) and Assigned(Obra) then
    FViewObra.Obra := Obra;
end;

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

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

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

  Obra.DataTable.Active := True;
end;

destructor TfEditorObra.Destroy;
begin
  // Utilizar mejor OnClose;
  inherited;
end;

procedure TfEditorObra.AsignarVista;
var
  AViewObra: TfrViewObra;
begin
  AViewObra := TfrViewObra.create(Self);
  with AViewObra do
  begin
    Parent := pagGeneral;
    Align := alClient;
    dxLayoutControlObra.LookAndFeel := dxLayoutOfficeLookAndFeel1;
  end;
  ViewObra := AViewObra;
end;

constructor TfEditorObra.Create(AOwner: TComponent);
begin
  inherited;
  pgPaginas.ActivePageIndex := 0;
  AsignarVista;
end;

procedure TfEditorObra.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;
  FViewObra := NIL;
  FObra := NIL;
end;

procedure TfEditorObra.dsDataTableDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if Assigned(FObra) and (not (FObra.DataTable.Fetching) or
    not (FObra.DataTable.Opening) or not (FObra.DataTable.Closing)) then
    PonerTitulos;
end;

procedure TfEditorObra.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar este obra?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if not FController.Eliminar(FObra) then
       actRefrescar.Execute;
  end;
end;

end.

