OSDN Git Service

Version string
[winbottle/winbottle.git] / bottleclient / BottleChainRuleEditor.pas
1 unit BottleChainRuleEditor;
2
3 interface
4
5 uses
6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7   Dialogs, StdCtrls, CheckLst, ComCtrls, BottleChainRule;
8
9 type
10   TfrmBottleChainRuleEditor = class(TForm)
11     PageControl: TPageControl;
12     tstGeneral: TTabSheet;
13     tstCondition: TTabSheet;
14     tstAction: TTabSheet;
15     cbxRuleEnabled: TCheckBox;
16     lblTitle: TLabel;
17     edtTitle: TEdit;
18     btnClose: TButton;
19     memStringExpression: TMemo;
20     lstCondition: TCheckListBox;
21     lstAction: TCheckListBox;
22     procedure btnCloseClick(Sender: TObject);
23     procedure lstConditionClickCheck(Sender: TObject);
24     procedure lstActionClickCheck(Sender: TObject);
25     procedure lstConditionDblClick(Sender: TObject);
26     procedure lstActionDblClick(Sender: TObject);
27     procedure edtTitleChange(Sender: TObject);
28     procedure cbxRuleEnabledClick(Sender: TObject);
29   private
30     { Private \90é\8c¾ }
31     FRule: TBottleChainRule;
32     FConditionsCheckStatus: array [0..High(BottleChainConditions)] of boolean;
33     FActionsCheckStatus: array [0..High(BottleChainActions)] of boolean;
34     procedure lstConditionChecked(Index: integer; Checked: boolean);
35     procedure lstActionChecked(Index: integer; Checked: boolean);
36   public
37     { Public \90é\8c¾ }
38     procedure InitializeDisplay;
39     procedure UpdateDisplay;
40     procedure Execute(EditRule: TBottleChainRule);
41   end;
42
43 var
44   frmBottleChainRuleEditor: TfrmBottleChainRuleEditor;
45
46 implementation
47
48 {$R *.dfm}
49
50 { TfrmBottleChainListEditor }
51
52 procedure TfrmBottleChainRuleEditor.Execute(EditRule: TBottleChainRule);
53 begin
54   if EditRule = nil then raise Exception.Create('EditRule is null');
55   FRule := EditRule;
56   InitializeDisplay;
57   UpdateDisplay;
58   ShowModal;
59 end;
60
61 procedure TfrmBottleChainRuleEditor.UpdateDisplay;
62 var i, j: integer;
63     f: boolean;
64 begin
65   memStringExpression.Text := FRule.StringExpression;
66   edtTitle.Text := FRule.Title;
67   cbxRuleEnabled.Checked := FRule.Enabled;
68
69   for i := 0 to High(BottleChainConditions) do begin
70     f := false;
71     for j := 0 to FRule.Conditions.Count-1 do
72       if FRule.Conditions[j].ClassType = BottleChainConditions[i] then begin
73         f := true;
74         lstCondition.Items[i] := (FRule.Conditions[j] as TBottleChainCondition).StringExpression;
75         Break;
76       end;
77     lstCondition.Checked[i] := f;
78     FConditionsCheckStatus[i] := f;
79     if not f then
80       lstCondition.Items[i] := BottleChainConditions[i].Title;
81   end;
82
83   for i := 0 to High(BottleChainActions) do begin
84     f := false;
85     for j := 0 to FRule.Actions.Count-1 do
86       if FRule.Actions[j].ClassType = BottleChainActions[i] then begin
87         f := true;
88         lstAction.Items[i] := (FRule.Actions[j] as TBottleChainAction).StringExpression;
89         Break;
90       end;
91     lstAction.Checked[i] := f;
92     FActionsCheckStatus[i] := f;
93     if not f then
94       lstAction.Items[i] := BottleChainActions[i].Title;
95   end;
96 end;
97
98 procedure TfrmBottleChainRuleEditor.btnCloseClick(Sender: TObject);
99 begin
100   ModalResult := mrOk;
101 end;
102
103 procedure TfrmBottleChainRuleEditor.InitializeDisplay;
104 var i: integer;
105     ConditionClass: TBottleChainConditionClass;
106     ActionClass: TBottleChainActionClass;
107 begin
108   lstCondition.Items.Clear;
109   for i := 0 to High(BottleChainConditions) do begin
110     ConditionClass := BottleChainConditions[i];
111     lstCondition.Items.Add(ConditionClass.Title);
112   end;
113   lstAction.Items.Clear;
114   for i := 0 to High(BottleChainActions) do begin
115     ActionClass := BottleChainActions[i];
116     lstAction.Items.Add(ActionClass.Title);
117   end;
118 end;
119
120 procedure TfrmBottleChainRuleEditor.lstConditionClickCheck(
121   Sender: TObject);
122 var i: integer;
123 begin
124   // \82±\82Ì\82Ü\82Ü\82¾\82Æ\95s\95Ö\82È\82Ì\82Å\8f\88\97\9d
125   for i := 0 to High(BottleChainConditions) do begin
126     if lstCondition.Checked[i] <> FConditionsCheckStatus[i] then
127       lstConditionChecked(i, lstCondition.Checked[i]);
128   end;
129 end;
130
131 procedure TfrmBottleChainRuleEditor.lstActionClickCheck(Sender: TObject);
132 var i: integer;
133 begin
134   // \82±\82Ì\82Ü\82Ü\82¾\82Æ\95s\95Ö\82È\82Ì\82Å\8f\88\97\9d
135   for i := 0 to High(BottleChainActions) do begin
136     if lstAction.Checked[i] <> FActionsCheckStatus[i] then
137       lstActionChecked(i, lstAction.Checked[i]);
138   end;
139 end;
140
141 procedure TfrmBottleChainRuleEditor.lstActionChecked(Index: integer;
142   Checked: boolean);
143 var Action: TBottleChainAction;
144     i: integer;
145 begin
146   if Checked then begin
147     Action := BottleChainActions[Index].Create(FRule);
148     Action.Rule := FRule;
149     if Action.Edit then begin
150     end else begin
151       Action.Free;
152       lstAction.Checked[Index] := false;
153     end;
154   end else begin
155     for i := FRule.Actions.Count-1 downto 0 do begin
156       if FRule.Actions[i].ClassType = BottleChainActions[Index] then
157         FRule.Actions[i].Free;
158     end;
159   end;
160   UpdateDisplay;
161 end;
162
163 procedure TfrmBottleChainRuleEditor.lstConditionChecked(Index: integer;
164   Checked: boolean);
165 var Condition: TBottleChainCondition;
166     i: integer;
167 begin
168   if Checked then begin
169     Condition := BottleChainConditions[Index].Create(FRule);
170     Condition.Rule := FRule;
171     if Condition.Edit then begin
172     end else begin
173       Condition.Free;
174       lstCondition.Checked[Index] := false;
175     end;
176   end else begin
177     for i := FRule.Conditions.Count-1 downto 0 do begin
178       if FRule.Conditions[i].ClassType = BottleChainConditions[Index] then
179         FRule.Conditions[i].Free;
180     end;
181   end;
182   UpdateDisplay;
183 end;
184
185
186 procedure TfrmBottleChainRuleEditor.lstConditionDblClick(Sender: TObject);
187 var index, i: integer;
188 begin
189   index := lstCondition.ItemIndex;
190   if Index < 0 then Exit;
191   if lstCondition.Checked[Index] then begin
192     for i := 0 to FRule.Conditions.Count-1 do
193       if FRule.Conditions[i].ClassType = BottleChainConditions[Index] then begin
194         (FRule.Conditions[i] as TBottleChainCondition).Edit;
195         Break;
196       end;
197   end else begin
198     lstCondition.Checked[Index] := true;
199     lstConditionChecked(Index, true);
200   end;
201   UpdateDisplay;
202 end;
203
204 procedure TfrmBottleChainRuleEditor.lstActionDblClick(Sender: TObject);
205 var index, i: integer;
206 begin
207   index := lstAction.ItemIndex;
208   if Index < 0 then Exit;
209   if lstAction.Checked[Index] then begin
210     for i := 0 to FRule.Actions.Count-1 do
211       if FRule.Actions[i].ClassType = BottleChainActions[Index] then begin
212         (FRule.Actions[i] as TBottleChainAction).Edit;
213         Break;
214       end;
215   end else begin
216     lstAction.Checked[Index] := true;
217     lstActionChecked(Index, true);
218   end;
219   UpdateDisplay;
220 end;
221
222 procedure TfrmBottleChainRuleEditor.edtTitleChange(Sender: TObject);
223 begin
224   FRule.Title := edtTitle.Text;
225 end;
226
227 procedure TfrmBottleChainRuleEditor.cbxRuleEnabledClick(Sender: TObject);
228 begin
229   FRule.Enabled := cbxRuleEnabled.Checked;
230 end;
231
232 end.