unit bsefld;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, BseMain, StdCtrls, ExtCtrls;

type
  TttLinkFields = class(TForm)
    Bevel1: TBevel;
    Bevel2: TBevel;
    Label30: TLabel;
    Label31: TLabel;
    IndexLabel: TLabel;
    Label2: TLabel;
    DetailList: TListBox;
    MasterList: TListBox;
    BindList: TListBox;
    IndexList: TComboBox;
    AddButton: TButton;
    DeleteButton: TButton;
    ClearButton: TButton;
    OkButton: TButton;
    Button2: TButton;
    procedure BindListClick(Sender: TObject);
    procedure BindingListClick(Sender: TObject);
    procedure IndexListChange(Sender: TObject);
    procedure AddButtonClick(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    OrderedDetailList : TStringList;
    OrderedMasterList : TStringList;
    TempTable  : TttTable;
    FWasActive : Boolean;
    Procedure LoadMasterList( DataSet : TDataSet );
    { Private declarations }
  public
    MasterFieldList : String;
    IndexFieldList  : String;
    Function GetMasterFields : String;
    Function GetCurrentIndex : String;
    Procedure AddToBindList( Const Str1, Str2 : String );
    Procedure Initialize( Table : TttTable );
    { Public declarations }
    Constructor Create( AOwner : TComponent ); Override;
    Constructor CreateNew( AOwner : TComponent; Dummy : Integer );
    Constructor CreateParented( ParentWindow : HWnd );
    Destructor  Destroy; Override;
  end;

implementation

{$R *.DFM}

Constructor TttLinkFields.Create( AOwner : TComponent );
begin
  Inherited Create( AOwner )
end;


Constructor TttLinkFields.CreateNew( AOwner : TComponent; Dummy : Integer );
begin
  Create( AOwner );
end;


Destructor TttLinkFields.Destroy;
begin
  TempTable.Active := FWasActive;
  Inherited Destroy;
end;


Constructor TttLinkFields.CreateParented( ParentWindow : HWnd );
begin
  Inherited CreateParented( ParentWindow );
end;


Procedure TttLinkFields.AddToBindList( Const Str1, Str2 : String );
begin
  BindList.Items.Add( Str1 + ' -> ' + Str2 );
  OrderedDetailList.Add( Str1 );
  OrderedMasterList.Add( Str2 );
  ClearButton.Enabled := TRUE;
end;


Procedure TttLinkFields.Initialize( Table : TttTable );
begin
  TempTable  := Table;
  FWasActive := Table.Active;
  TempTable.Active := TRUE;
  TempTable.GetIndexNames( IndexList.Items );
  If ( TempTable.IndexName = '' ) and ( IndexList.Items.Count > 0 ) then
    TempTable.IndexName := IndexList.Items[ 0 ];
  IndexList.ItemIndex := IndexList.Items.IndexOf( TempTable.IndexName );
  IndexListChange( NIL );
end;


procedure TttLinkFields.BindListClick(Sender: TObject);
begin
  DeleteButton.Enabled := ( BindList.ItemIndex <> -1 );
end;


procedure TttLinkFields.BindingListClick(Sender: TObject);
begin
  AddButton.Enabled := ( DetailList.ItemIndex = 0 ) and
                       ( MasterList.ItemIndex <> -1 );
end;


procedure TttLinkFields.IndexListChange(Sender: TObject);
var
  i     : Integer;
  Field : TField;
begin
  If ( Sender <> NIL ) then
  begin
    TempTable.MasterFields := '';
    OkButton.Enabled := TRUE;
  end;
  BindList.Clear;
  DetailList.Clear;
  OrderedDetailList.Clear;
  OrderedMasterList.Clear;
  MasterList.Items.Clear;
  If ( IndexList.ItemIndex < 0 ) then
    IndexList.ItemIndex := 0;
  TempTable.IndexName := IndexList.Items[ IndexList.ItemIndex ];
  For i := 1 to TempTable.IndexFieldCount do
  begin
    Field := TempTable.IndexFields[ Pred( i ) ];
    DetailList.Items.Add( Field.FieldName );
  end;
  if ( TempTable.MasterSource <> NIL ) then
    LoadMasterList( TempTable.MasterSource.DataSet );
  If ( TempTable.MasterFields <> '' ) then
  begin
    For i := 0 to Pred( MasterList.Items.Count ) do
      If ( Pos( MasterList.Items[ i ], TempTable.MasterFields ) > 0 ) then
      begin
        AddToBindList( DetailList.Items[ 0 ], MasterList.Items[ i ] );
        DetailList.Items.Delete( 0 );
      end;
    For i := Pred( MasterList.Items.Count ) downto 0 do
      If ( Pos( MasterList.Items[ i ], TempTable.MasterFields ) > 0 ) then
        MasterList.Items.Delete( i );
  end;
end;


procedure TttLinkFields.LoadMasterList( DataSet : TDataSet );
var
  FOld : Boolean;
begin
  If ( DataSet <> NIL ) then
  begin
    MasterList.Items.Clear;
    FOld := DataSet.Active;
    Try
      DataSet.Active := TRUE;  
      DataSet.GetFieldNames( MasterList.Items );
    Finally
      Dataset.Active := FOld;
    end;
  end;
end;


procedure TttLinkFields.AddButtonClick(Sender: TObject);
begin
  AddToBindList( DetailList.Items[ DetailList.ItemIndex ], MasterList.Items[ MasterList.ItemIndex ] );
  With DetailList do
    Items.Delete( ItemIndex );
  With MasterList do
    Items.Delete( ItemIndex );
  AddButton.Enabled := FALSE;
  OkButton.Enabled  := TRUE;
end;


procedure TttLinkFields.DeleteButtonClick(Sender: TObject);
var
  tmp : String;
begin
  tmp := OrderedDetailList[ BindList.ItemIndex ];
  DetailList.Items.Insert( 0, tmp );
  OrderedDetailList.Delete( BindList.ItemIndex );

  tmp := OrderedMasterList[ BindList.ItemIndex ];
  MasterList.Items.Insert( 0, tmp );
  OrderedMasterList.Delete( BindList.ItemIndex );

  BindList.Items.Delete( BindList.ItemIndex );
  AddButton.Enabled    := FALSE;
  DeleteButton.Enabled := FALSE;
  ClearButton.Enabled  := FALSE;
  OkButton.Enabled     := TRUE;
end;


procedure TttLinkFields.ClearButtonClick(Sender: TObject);
begin
  BindList.Clear;
  AddButton.Enabled      := FALSE;
  DeleteButton.Enabled   := FALSE;
  ClearButton.Enabled    := FALSE;
  OkButton.Enabled       := TRUE;
  TempTable.MasterFields := '';
  if ( Sender <> NIL ) then
    IndexListChange( Sender );
end;


procedure TttLinkFields.FormCreate(Sender: TObject);
begin
  OrderedDetailList    := TStringList.Create;
  OrderedMasterList    := TStringList.Create;
  AddButton.Enabled    := FALSE;
  DeleteButton.Enabled := FALSE;
  ClearButton.Enabled  := FALSE;
  { May need to read existing peoperty value here }
end;


procedure TttLinkFields.FormDestroy(Sender: TObject);
begin
  OrderedDetailList.Free;
  OrderedMasterList.Free;
end;


Function TttLinkFields.GetMasterFields : String;
var
  i : Integer;
begin
  Result := '';
  For i := 0 to Pred( OrderedMasterList.Count ) do
  begin
    If ( i > 0 ) then
      Result := Result + ';' + OrderedMasterList[ i ]
    else
      Result := OrderedMasterList[ i ];
  end;
end;


Function TttLinkFields.GetCurrentIndex : String;
begin
  Result := TempTable.IndexName;
end;

end.
