(*****************************************************************************)
(* MODULE: BSECOMP*                                                          *)
(* RIGHTS: Reggatta Systems, Inc. - COPYRIGHT 1995 - 2000                    *)
(* DESCR.: This module contains the property editors and registration code   *)
(*****************************************************************************)
Unit BseComp;
{$I BSE.INC}
Interface
Uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  DesignIntf,
  DesignEditors,
  Dialogs;

Type
  TttStringPropertyEditor =
  Class( TStringProperty )
    Public
      Function  GetAttributes: TPropertyAttributes; Override;
      Procedure GetValueList(List: TStrings); Virtual; Abstract;
      Procedure GetValues(Proc: TGetStrProc); Override;
  end;

Procedure RegisterBasePropertyEditors;

implementation
Uses
  Db,
  BseFld,
  BseTable,
  BseParam,
  BseMain;


Function TttStringPropertyEditor.GetAttributes : TPropertyAttributes;
begin
  Result := [paValueList, paSortList, paMultiSelect];
end;


Procedure TttStringPropertyEditor.GetValues(Proc: TGetStrProc);
var
  I      : Integer;
  Values : TStringList;
begin
  Values := TStringList.Create;
  Try
    GetValueList( Values );
    for I := 0 to Values.Count - 1 do
      Proc( Values[ I ] );
  Finally
    Values.Free;
  end;
end;


Type
  TttDataSourcePropertyEditor =
  Class( TComponentProperty )
    Private
      FCheckProc: TGetStrProc;
      Procedure CheckComponent( const Value : string );
    Public
      Procedure GetValues(Proc: TGetStrProc); override;
  end;


Procedure TttDataSourcePropertyEditor.CheckComponent( const Value : string );
var
  J: Integer;
  DataSource: TDataSource;
begin
  DataSource := TDataSource( Designer.GetComponent(Value ) );
  for J := 0 to Pred( PropCount ) do
    if TDataSet( GetComponent( J ) ).IsLinkedTo( DataSource ) then
      Exit;
  FCheckProc( Value );
end;


Procedure TttDataSourcePropertyEditor.GetValues( Proc : TGetStrProc );
begin
  FCheckProc := Proc;
  Inherited GetValues( CheckComponent );
end;


Type
  TttDatabaseNamePropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList(List: TStrings); override;
  end;


Procedure TttDatabaseNamePropertyEditor.GetValueList( List : TStrings );
begin
  ( GetComponent( 0 ) as TttDBDataSet ).DBSession.GetDatabaseNames( List );
end;


Type
  TttTableNamePropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList(List: TStrings); override;
  end;


Procedure TttTableNamePropertyEditor.GetValueList(List: TStrings);
var
  Table: TttTable;
begin
  Table := GetComponent( 0 ) as TttTable;
  Table.DBSession.GetTableNames( Table.DatabaseName, '', TRUE, FALSE, List );
end;


Type
  TttIndexNamePropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList(List: TStrings); override;
  end;


Procedure TttIndexNamePropertyEditor.GetValueList( List : TStrings );
begin
  ( GetComponent( 0 ) as TttTable ).GetIndexNames( List );
end;


Type
  TttProcedureNamePropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList( List : TStrings ); Override;
  end;


Procedure TttProcedureNamePropertyEditor.GetValueList( List : TStrings );
var
  DBDataSet: TttDBDataSet;
begin
  DBDataSet := GetComponent( 0 ) as TttDBDataSet;
  DBDataSet.DBSession.GetStoredProcNames( DBDataSet.DatabaseName, List );
end;


Type
  TttIndexFieldNamesPropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList(List: TStrings); override;
  end;


Procedure TttIndexFieldNamesPropertyEditor.GetValueList( List : TStrings );
var
  I: Integer;
begin
  with GetComponent( 0 ) as TttTable do
  begin
    IndexDefs.Update;
    for I := 0 to IndexDefs.Count - 1 do
      with IndexDefs[ I ] do
        if not (ixExpression in Options) then
          List.Add(Fields);
  end;
end;


Type
  TttAliasNamePropertyEditor =
  Class( TttStringPropertyEditor )
    Public
      Procedure GetValueList(List: TStrings); override;
  end;


Procedure TttAliasNamePropertyEditor.GetValueList( List : TStrings );
begin
  ( GetComponent( 0 ) as TttDatabase ).Session.GetAliasNames( List );
end;



Type
  TttParamsProperty =
  Class( TPropertyEditor )
    Public
      Function GetValue: string; override;
      Function GetAttributes: TPropertyAttributes; override;
  end;


Function TttParamsProperty.GetValue: string;
begin
  Result := Format('(%s)', [ TttParams.ClassName ] );
end;


Function TttParamsProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [ paMultiSelect, paDialog ];
end;

Type
  TttQueryParamsProperty =
  Class( TttParamsProperty )
    Procedure Edit; override;
  end;


Procedure TttQueryParamsProperty.Edit;
var
  List  : TttParams;
  Query : TttQuery;
begin
  Query := GetComponent( 0 ) as TttQuery;
  List  := TttParams.Create;
  Try
    List.Assign( Query.Params );
    if EditTttQueryParams( Query, List ) and not List.IsEqual( Query.Params ) then
    begin
      Modified;
      Query.Close;
      Query.Params := List;
    end;
  finally
    List.Free;
  end;
end;

Type
  TttLinkFieldsPropertyEditor =
  Class( TPropertyEditor )
    Public
      procedure SetValue( Const Value : String ); override;
      function GetValue: string; override;
      Function GetAttributes: TPropertyAttributes; override;
      procedure Edit; override;
  end;

Procedure TttLinkFieldsPropertyEditor.SetValue( Const Value : String );
begin
  ( GetComponent( 0 ) as TttTable ).MasterFields := Value;
end;

Function TttLinkFieldsPropertyEditor.GetValue: string;
begin
  Result := ( GetComponent( 0 ) as TttTable ).MasterFields;
end;


Function TttLinkFieldsPropertyEditor.GetAttributes: TPropertyAttributes;
begin
  Result := [ paMultiSelect, paDialog ];
end;


Procedure TttLinkFieldsPropertyEditor.Edit;
var
  T     : TttLinkFields;
  tmpMF : String;
  tmpIN : String;
begin
  T := TttLinkFields.Create( Application.Owner );
  Try
    with ( GetComponent( 0 ) as TttTable ) do
    begin
      tmpMF := MasterFields;
      tmpIN := IndexName;
      T.Initialize( GetComponent( 0 ) as TttTable );
      if ( T.ShowModal = mrOK ) then
      begin
        MasterFields := T.GetMasterFields;
        IndexName    := T.GetCurrentIndex;
        Self.Modified;
      end
      else
      begin
        MasterFields := tmpMF;
        IndexName    := tmpIN;
      end;
    end;
  Finally
    T.Free;
  end;
end;


Procedure RegisterBasePropertyEditors;
begin
    RegisterPropertyEditor( TypeInfo( string ),      TttDatabase,   'AliasName',       TttAliasNamePropertyEditor       );
    RegisterPropertyEditor( TypeInfo( string ),      TttTable,      'DatabaseName',    TttDatabaseNamePropertyEditor    );
    RegisterPropertyEditor( TypeInfo( TFileName ),   TttTable,      'TableName',       TttTableNamePropertyEditor       );
    RegisterPropertyEditor( TypeInfo( string ),      TttTable,      'IndexName',       TttIndexNamePropertyEditor       );
    RegisterPropertyEditor( TypeInfo( string ),      TttTable,      'IndexFieldNames', TttIndexFieldNamesPropertyEditor );
    RegisterPropertyEditor( TypeInfo( TDataSource ), TttTable,      'MasterSource',    TttDataSourcePropertyEditor      );

    RegisterPropertyEditor( TypeInfo( string ),      TttQuery,      'DatabaseName',    TttDatabaseNamePropertyEditor    );
    RegisterPropertyEditor( TypeInfo( string ),      TttStoredProc, 'DatabaseName',    TttDatabaseNamePropertyEditor    );
    RegisterPropertyEditor( TypeInfo( string ),      TttStoredProc, 'StoredProcName',  TttProcedureNamePropertyEditor   );

    RegisterPropertyEditor( TypeInfo( TttParams ),   TttQuery,      'Params',          TttQueryParamsProperty           );
    RegisterPropertyEditor( TypeInfo( string ),      TttTable,      'MasterFields', TttLinkFieldsPropertyEditor );
end;


end.
