unit ecsnRuleStates;

interface

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

type
  TRuleStatesFrame = class(TFrame)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    procedure Edit1Exit(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    FRule: TRuleCollectionItem;
    procedure SetRule(const Value: TRuleCollectionItem);
    procedure UpdateState;
  public
    constructor Create(AOwner: TComponent); override;
    property Rule: TRuleCollectionItem read FRule write SetRule;
  end;

implementation

{$R *.dfm}

{ TRuleStatesFrame }

constructor TRuleStatesFrame.Create(AOwner: TComponent);
begin
  inherited;
  UpdateState;
end;

procedure TRuleStatesFrame.SetRule(const Value: TRuleCollectionItem);
begin
  if FRule <> Value then
    begin
      FRule := Value;
      UpdateState;
    end;
end;

procedure TRuleStatesFrame.UpdateState;
  procedure EnableEdit(Edit: TEdit);
  begin
    if Assigned(Rule) then
      begin
        Edit.Enabled := True;
        Edit.Color := clWindow;
        Edit.Modified := False;
      end else
      begin
        Edit.Enabled := False;
        Edit.Color := clBtnFace;
        Edit.Text := '';
      end;
  end;
begin
  EnableEdit(Edit1);
  EnableEdit(Edit2);
  EnableEdit(Edit3);
  EnableEdit(Edit4);
  if Assigned(FRule) then
    begin
      Edit1.Text := IntToStr(Rule.StatesPresent);
      Edit2.Text := IntToStr(Rule.StatesAbsent);
      Edit3.Text := IntToStr(Rule.StatesAdd);
      Edit4.Text := IntToStr(Rule.StatesRemove);
    end;
end;

procedure TRuleStatesFrame.Edit1Exit(Sender: TObject);
begin
  with TEdit(Sender) do
    if Modified then
      try
        case integer(Tag) of
          0: FRule.StatesPresent := StrToInt(Text);
          1: FRule.StatesAbsent := StrToInt(Text);
          2: FRule.StatesAdd := StrToInt(Text);
          3: FRule.StatesRemove := StrToInt(Text);
        end;
        Modified := False;
      except
        Application.HandleException(Self);
        UpdateState;
      end;
end;

procedure TRuleStatesFrame.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    Edit1Exit(Sender);
end;

end.
