unit fmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, uObjects;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    ListBox1: TListBox;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
  private
    { Private declarations }
    aNotifier : TDemoDP;
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent); override;
    Destructor Destroy; override;

    procedure ShowItemInfo(aItem : TItem; const sMsg : String);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  aItem : TItem;
begin
  aItem := TItem.Create;
  aNotifier.Notify(aItem, cnAdded);
  ShowItemInfo(aItem, '被新增了');
end;

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  aNotifier := TDemoDP.Create;
end;

destructor TForm1.Destroy;
begin
  FreeAndNil(aNotifier);
  inherited;
end;

procedure TForm1.ShowItemInfo(aItem : TItem; const sMsg : String);
begin
  ListBox1.Items.Add(aItem.ToString + ':' + sMsg);
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
var
  aItem : TItem;
  sID : String;
  iPos : Integer;
begin
  sID := ListBox1.Items[ListBox1.ItemIndex];
  iPos := Pos(':', sID);
  Delete(sID, iPos, Length(sID) - iPos + 1);
  aItem := aNotifier.FindItem(sID);
  ShowItemInfo(aItem, '被删除了');
  aNotifier.Notify(aItem, cnDeleting);
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
var
  aItem : TItem;
  sID : String;
  iPos : Integer;
begin
  sID := ListBox1.Items[ListBox1.ItemIndex];
  iPos := Pos(':', sID);
  Delete(sID, iPos, Length(sID) - iPos + 1);
  aItem := aNotifier.FindItem(sID);
  aNotifier.Notify(aItem, cnExtracting);
  ShowItemInfo(aItem, '被移除了');
  FreeAndNil(aItem);
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
begin
  Memo1.Lines.Add(aNotifier.GetMonitorMessages);
end;

end.
