{
===============================================================================
  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 uEditorContactos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, uEditorGrid, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewContactos, uBizContacto, ActnList, DBActns, uViewGrid,
  Menus, uDataModuleBase, ImgList, PngImageList, TB2Dock, TB2Toolbar, TBX,
  TB2Item, StdActns, TB2ExtItems, TBXExtItems, TB2MRU, DB, uDADataTable,
  StdCtrls, ExtCtrls, uCustomView, uViewBase, uViewBarraSeleccion,
  dxLayoutLookAndFeels, JvAppStorage, JvAppRegistryStorage, JvFormPlacement,
  uEditorUtils, JvComponentBase, cxGridCustomTableView;

type
  IEditorContactos = interface(IEditorGrid)
    ['{21DD6B74-F218-4AFC-91EE-1ECD4ADF1553}']
    function GetContactos: IBizContacto;
    procedure SetContactos(const Value: IBizContacto);
    property Contactos: IBizContacto read GetContactos write SetContactos;
  end;

  TfEditorContactos = class(TfEditorGrid, IEditorContactos, IEditorMenu)
    actVerTodos: TAction;
    actVerPorCategorias: TAction;
    EditorMenu: TMainMenu;
    Contactos1: TMenuItem;
    VerTodos1: TMenuItem;
    Verporcategoras1: TMenuItem;
    frViewContactos1: TfrViewContactos;
    procedure actEliminarExecute(Sender: TObject);
    procedure actModificarExecute(Sender: TObject);
    procedure actNuevoExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
    procedure actVerTodosExecute(Sender: TObject);
    procedure actVerPorCategoriasExecute(Sender: TObject);
    procedure frViewBarraSeleccionactSeleccionarExecute(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);

  private
    FContactos: IBizContacto;

  protected
    function GetContactos: IBizContacto; virtual;
    procedure SetContactos(const Value: IBizContacto); virtual;
    procedure SetViewGrid(const Value: IViewGrid); override;
    procedure SetSelectionBarVisible(const Value: Boolean); override;

  public
    property Contactos: IBizContacto read GetContactos write SetContactos;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function GetEditorMenu: TMainMenu;
  end;

implementation

uses
  uCustomEditor, uDataModuleContactos, uEditorContacto,
  uPluginControllerContactos, cxGridDBTableView, uDBSelectionList,
  LiteralesContactos, cxGridCustomView, cxGrid, Math;

{$R *.dfm}


function ShowEditorContactos (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorContactos;
begin
  AEditor := TfEditorContactos.Create(Application);
  try
    AEditor.Contactos := (ABizObject as IBizContacto);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
    Application.ProcessMessages;    
  end;
end;

function ShowMultiSelectEditorContactos (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorContactos;
begin
  AEditor := TfEditorContactos.Create(Application);
  try
    AEditor.Contactos := (ABizObject as IBizContacto);
    AEditor.SelectionBarVisible := True;
    AEditor.MultiSelection := True;
    AEditor.actVerTodos.Execute;
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

function ShowSelectEditorContactos (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorContactos;
begin
  AEditor := TfEditorContactos.Create(Application);
  try
    AEditor.Contactos := (ABizObject as IBizContacto);
    AEditor.SelectionBarVisible := True;
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;


{
****************************** TfEditorContactos *******************************
}

procedure TfEditorContactos.actEliminarExecute(Sender: TObject);
begin
  if (MessageDlg(msgLitBorrar, mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
     inherited;
     ViewGrid.RefreshGrid;
  end;
end;

procedure TfEditorContactos.actModificarExecute(Sender: TObject);
begin
  inherited;
  Contactos.Show;
  try
    ViewGrid.Grid.BeginUpdate;  //Ojo no quitar porque si no pierde la posición cuando hacemos doble click
    ViewGrid.RefreshGrid;
    ViewGrid.SyncFocusedRecordsFromDataSet;
  finally
    ViewGrid.Grid.EndUpdate;
  end;
end;

procedure TfEditorContactos.actNuevoExecute(Sender: TObject);
begin
  inherited;
  Contactos.Insert;
  Contactos.Show;
  ViewGrid.RefreshGrid;
  ViewGrid.SyncFocusedRecordsFromDataSet;
end;

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

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

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

  Contactos.DataTable.Active := True;
end;

function TfEditorContactos.GetContactos: IBizContacto;
begin
  Result := FContactos;
end;

procedure TfEditorContactos.SetContactos(const Value: IBizContacto);
begin
  FContactos := Value;
  dsDataTable.DataTable := FContactos.DataTable;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewContactos).Contactos := Contactos;
end;

procedure TfEditorContactos.SetViewGrid(const Value: IViewGrid);
begin
  inherited;
  if Assigned(ViewGrid) and Assigned(Contactos) then
    (ViewGrid as IViewContactos).Contactos := Contactos;
end;

constructor TfEditorContactos.Create(AOwner: TComponent);
begin
  inherited;
//  ViewGrid := CreateView(TfrViewContactos) as IViewContactos;
  ViewGrid := frViewContactos1;
end;

destructor TfEditorContactos.Destroy;
begin
  FContactos := NIL;
  ViewGrid := Nil;
  inherited;
end;

procedure TfEditorContactos.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  if ModalResult <> mrOk then
     (Contactos as ISelectedRowList).SelectedRows.Clear;
end;

procedure TfEditorContactos.actVerTodosExecute(Sender: TObject);
begin
  (ViewGrid as IViewContactos).SetViewType(vtAll);
end;

procedure TfEditorContactos.actVerPorCategoriasExecute(Sender: TObject);
begin
  (ViewGrid as IViewContactos).SetViewType(vtCat);
end;

procedure TfEditorContactos.frViewBarraSeleccionactSeleccionarExecute(Sender: TObject);
begin
  inherited;
  ViewGrid.SyncFocusedRecordsFromGrid;
  ModalResult := mrOK;
end;

procedure TfEditorContactos.SetSelectionBarVisible(const Value: Boolean);
begin
  inherited;
  if Value then
     ViewGrid.OnDblClick := frViewBarraSeleccion.actSeleccionar.OnExecute;
end;

function TfEditorContactos.getEditorMenu: TMainMenu;
begin
  Result := EditorMenu;
end;

procedure TfEditorContactos.actModificarUpdate(Sender: TObject);
begin
  inherited;
  if (ViewGrid.Grid.ActiveView is TcxGridDBTableView) and
  Assigned((ViewGrid.Grid.ActiveView as TcxGridDBTableView).Controller.FocusedRow) then
     if not (ViewGrid.Grid.ActiveView as TcxGridDBTableView).Controller.FocusedRow.HasCells then
       (Sender as TAction).Enabled := False;
end;

procedure TfEditorContactos.actEliminarUpdate(Sender: TObject);
begin
  inherited;
  if (ViewGrid.Grid.ActiveView is TcxGridDBTableView) and
  Assigned((ViewGrid.Grid.ActiveView as TcxGridDBTableView).Controller.FocusedRow) then
     if not (ViewGrid.Grid.ActiveView as TcxGridDBTableView).Controller.FocusedRow.HasCells then
       (Sender as TAction).Enabled := False;
end;

initialization
  RegisterEditor(IBizContacto, ShowEditorContactos, etItems);
  RegisterEditor(IBizContacto, ShowMultiSelectEditorContactos, etSelectItems);
  RegisterEditor(IBizContacto, ShowSelectEditorContactos, etSelectItem);

end.

