unit FMXTee.Editor.DB;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Source,
  FMX.ListBox, Data.DB, FMX.Edit;

type
  TDBSourceEditor = class(TBaseSourceEditor)
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    procedure FillSourceDatasets;
    procedure FillSources;
  protected
    IChanging : Boolean;

    class var OnGetDesignerNames : TOnGetDesignerNamesEvent;

    Function DataSet:TDataSet;
    Procedure FillFields(const Combos:Array of TComboEdit);
    Function IsValid(AComponent:TComponent):Boolean; override;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Engine, FMXTee.DBChart, FMXTee.Constants, FMXTee.Editor.Picture;

{ TDBSourceEditor }

function TDBSourceEditor.DataSet: TDataSet;
begin
  if CBSources.Selected=nil then
     result:=nil
  else
     result:=TDataSet(CBSources.Selected.Data);
end;

procedure TDBSourceEditor.FillFields(const Combos: array of TComboEdit);

  Procedure AddField(Const tmpSt:String; tmpType:TFieldType);
  var t : Integer;
  begin
    Case TeeFieldType(tmpType) of
       tftNumber,
       tftDateTime,
       tftText     : begin
                       for t:=Low(Combos) to High(Combos) do
                           Combos[t].Items.Add(tmpSt);
                     end;
    end;
  end;

Var t : Integer;
Begin
  for t:=Low(Combos) to High(Combos) do
      Combos[t].Items.Clear;

  if DataSet<>nil then
    With DataSet do
    if FieldCount>0 then
       for t:=0 to FieldCount-1 do
           AddField(Fields[t].FieldName,Fields[t].DataType)
    else
    begin
      FieldDefs.Update;
      for t:=0 to FieldDefs.Count-1 do
          AddField(FieldDefs[t].Name,FieldDefs[t].DataType);
    end;
end;

procedure TDBSourceEditor.FillSourceDatasets;
begin
  CBSources.Clear;
  FillSources;
end;

procedure TDBSourceEditor.FillSources;

  Procedure FillSourcesForm(const ARoot:TComponent);
  var t : Integer;  { 5.01 }
  begin
    if Assigned(ARoot) and
       (ARoot<>Self) and
       (ARoot<>Self.Owner.Owner) then
    With ARoot do
    for t:=0 to ComponentCount-1 do
        AddComponentDataSource(Components[t],CBSources.ListBox,True);
  end;

var t : Integer;
begin
  if Assigned(TheSeries) then
  begin
    if (csDesigning in TheSeries.ComponentState) and
       Assigned(OnGetDesignerNames) then
          OnGetDesignerNames(AddComponentDataSource,TheSeries,CBSources.ListBox,
                             True)
    else
    begin
      with Screen do
      begin
        for t:=0 to DataModuleCount-1 do FillSourcesForm(DataModules[t]);
        for t:=0 to FormCount-1 do FillSourcesForm(Forms[t]); { 5.01 }
      end;

      if Assigned(TheSeries.ParentChart) then
      begin
        FillSourcesForm(TheSeries.ParentChart);        { 5.01 }
        FillSourcesForm(TheSeries.ParentChart.Owner);  { 5.01 }
      end;
    end;
  end;
end;

procedure TDBSourceEditor.FormActivate(Sender: TObject);
begin
  inherited;

  LLabel.Text:=StripHotKey(TeeMsg_AskDataSet);
  FillSourceDatasets;

  if Assigned(TheSeries) and Assigned(TheSeries.DataSource) then
  begin
    IChanging:=True;
    CBSources.ItemIndex:=CBSources.ListBox.Items.IndexOfObject(TheSeries.DataSource);
    IChanging:=False;
  end;
end;

function TDBSourceEditor.IsValid(AComponent: TComponent): Boolean;
begin
  result:=AComponent is TDataSet;
end;

end.
