{ *************************************************************************** }
{                                                                             }
{ EControl Syntax Editor SDK                                                  }
{                                                                             }
{ Copyright (c) 2004 - 2008 EControl Ltd., Zaharov Michael                    }
{     www.econtrol.ru                                                         }
{     support@econtrol.ru                                                     }
{                                                                             }
{ *************************************************************************** }
{$I ecSyntEdit.INC}

unit ecImpRulesDlg;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ecSyntAnal, ComCtrls;

type
  TecCollectionType = (ctNotDefined, ctStyles, ctParserRules, ctRangeRules, SubLexerRulers);

  TImportRules = class(TForm)
    Button1: TButton;
    Button2: TButton;
    LList: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    RuleList: TListView;
    procedure LListClick(Sender: TObject);
    procedure RuleListClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    FCollection: TSyntCollection;
    procedure FillAnalyzers;
    procedure SetEnabling;
    function GetColl(lex: TSyntAnalyzer): TSyntCollection;
    procedure SetCollection(const Value: TSyntCollection);
  public
    LexOwner: TComponent;
    property Collection: TSyntCollection read FCollection write SetCollection;
  end;

implementation

{$R *.dfm}

{ TImportRules }

procedure TImportRules.SetCollection(const Value: TSyntCollection);
begin
  FCollection := Value;
  FillAnalyzers;
end;

function TImportRules.GetColl(lex: TSyntAnalyzer): TSyntCollection;
begin
  if FCollection.ClassType = TStylesCollection then Result := lex.Formats else
  if FCollection.ClassType = TTokenRuleCollection then Result := lex.TokenRules else
  if FCollection.ClassType = TBlockRuleCollection then Result := lex.BlockRules else
  if FCollection.ClassType = TSubAnalyzerRules then Result := lex.SubAnalyzers else
    Result := nil;
  if Result = nil then Abort;
end;

procedure TImportRules.FillAnalyzers;
var i: integer;
    lex: TSyntAnalyzer;
begin
  LList.Items.Clear;
  if (FCollection <> nil) and (LexOwner <> nil) then
   with LexOwner do
    for i := 0 to ComponentCount - 1 do
     if Components[i] is TSyntAnalyzer then
      begin
        lex := Components[i] as TSyntAnalyzer;
        if GetColl(lex).Count > 0 then
          LList.Items.AddObject(lex.LexerName, lex);
      end;
  if LList.Items.Count > 0 then
   begin
    LList.ItemIndex := 0;
    LListClick(nil);
   end;
end;

procedure TImportRules.LListClick(Sender: TObject);
var i: integer;
    FCol: TSyntCollection;
    li: TListItem;
begin
  RuleList.Items.Clear;
  if LList.ItemIndex >= 0 then
   begin
     FCol := GetColl(TSyntAnalyzer(LList.Items.Objects[LList.ItemIndex]));
     for i := 0 to FCol.Count - 1 do
      begin
       li := RuleList.Items.Add;
       li.Caption := FCol[i].DisplayName;
       li.Data := FCol[i];
      end;
   end;
  SetEnabling;
end;

procedure TImportRules.SetEnabling;
var i: integer;
begin
  for i := 0 to RuleList.Items.Count - 1 do
   if RuleList.Items[i].Checked then
    begin
      Button1.Enabled := True;
      Exit;
    end;
  Button1.Enabled := False;
end;

procedure TImportRules.RuleListClick(Sender: TObject);
begin
  SetEnabling;
end;

procedure TImportRules.FormClose(Sender: TObject;
  var Action: TCloseAction);
var i: integer;
    Item: TSyntCollectionItem;
begin
  if (ModalResult = mrOk) and Assigned(FCollection) then
   begin
     for i := 0 to RuleList.Items.Count - 1 do
      if RuleList.Items[i].Checked then
       begin
         Item := FCollection.Add as TSyntCollectionItem;
         Item.Assign(TSyntCollectionItem(RuleList.Items[i].Data));
       end;
   end;
end;

procedure TImportRules.FormShow(Sender: TObject);
var i: integer;
begin
  for i := 0 to RuleList.Items.Count - 1 do
   RuleList.Items[i].Checked := False;
end;

end.
