1 |
(* ***** BEGIN LICENSE BLOCK *****
|
2 |
* Version: MPL 1.1
|
3 |
*
|
4 |
* The contents of this file are subject to the Mozilla Public License Version
|
5 |
* 1.1 (the "License"); you may not use this file except in compliance with
|
6 |
* the License. You may obtain a copy of the License at
|
7 |
* http://www.mozilla.org/MPL/
|
8 |
*
|
9 |
* Software distributed under the License is distributed on an "AS IS" basis,
|
10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
11 |
* for the specific language governing rights and limitations under the
|
12 |
* License.
|
13 |
*
|
14 |
* The Original Code is TurboPower SysTools
|
15 |
*
|
16 |
* The Initial Developer of the Original Code is
|
17 |
* TurboPower Software
|
18 |
*
|
19 |
* Portions created by the Initial Developer are Copyright (C) 1996-2002
|
20 |
* the Initial Developer. All Rights Reserved.
|
21 |
*
|
22 |
* Contributor(s):
|
23 |
*
|
24 |
* ***** END LICENSE BLOCK ***** *)
|
25 |
|
26 |
unit Chain;
|
27 |
|
28 |
interface
|
29 |
|
30 |
uses
|
31 |
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
|
32 |
StdCtrls, StPtrns, ExtCtrls;
|
33 |
|
34 |
type
|
35 |
TInputData = class
|
36 |
public
|
37 |
{ Public declarations }
|
38 |
InData : integer;
|
39 |
end;
|
40 |
|
41 |
type
|
42 |
TOutputData = class
|
43 |
public
|
44 |
{ Public declarations }
|
45 |
OutData : integer;
|
46 |
end;
|
47 |
|
48 |
type
|
49 |
TChainForm = class(TForm)
|
50 |
Panel2: TPanel;
|
51 |
Ch1Lbl: TLabel;
|
52 |
Ch2Lbl: TLabel;
|
53 |
Ch3Lbl: TLabel;
|
54 |
Ch4Lbl: TLabel;
|
55 |
Ch5Lbl: TLabel;
|
56 |
Ch1Value: TEdit;
|
57 |
Ch2Value: TEdit;
|
58 |
Ch3Value: TEdit;
|
59 |
Ch4Value: TEdit;
|
60 |
Ch5Value: TEdit;
|
61 |
Ch1Msg: TEdit;
|
62 |
Ch2Msg: TEdit;
|
63 |
Ch3Msg: TEdit;
|
64 |
Ch4Msg: TEdit;
|
65 |
Ch5Msg: TEdit;
|
66 |
Panel1: TPanel;
|
67 |
RadioButton1: TRadioButton;
|
68 |
RadioButton2: TRadioButton;
|
69 |
InputValue: TEdit;
|
70 |
Start: TButton;
|
71 |
procedure RadioButton1Click(Sender: TObject);
|
72 |
procedure RadioButton2Click(Sender: TObject);
|
73 |
procedure FormCreate(Sender: TObject);
|
74 |
procedure StartClick(Sender: TObject);
|
75 |
procedure FormDestroy(Sender: TObject);
|
76 |
private
|
77 |
{ Private declarations }
|
78 |
// Code for the chain
|
79 |
TheChain : TStChain;
|
80 |
procedure Chain1Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
81 |
procedure Chain2Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
82 |
procedure Chain3Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
83 |
procedure Chain4Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
84 |
procedure Chain5Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
85 |
procedure ClearScreen;
|
86 |
|
87 |
public
|
88 |
{ Public declarations }
|
89 |
end;
|
90 |
|
91 |
var
|
92 |
ChainForm: TChainForm;
|
93 |
|
94 |
implementation
|
95 |
var
|
96 |
ChainPotato : Boolean;
|
97 |
TheChain: TStChain;
|
98 |
|
99 |
{$R *.DFM}
|
100 |
|
101 |
procedure TChainForm.RadioButton1Click(Sender: TObject);
|
102 |
begin
|
103 |
if (RadioButton1.Checked) then begin
|
104 |
Ch1Lbl.Caption := 'Handle < 10';
|
105 |
Ch2Lbl.Caption := 'Handle 10';
|
106 |
Ch3Lbl.Caption := 'Handle Odd';
|
107 |
Ch4Lbl.Caption := 'Handle > 100';
|
108 |
Ch5Lbl.Caption := 'Default Handler';
|
109 |
ChainPotato := true;
|
110 |
ClearScreen;
|
111 |
end;
|
112 |
end;
|
113 |
|
114 |
procedure TChainForm.RadioButton2Click(Sender: TObject);
|
115 |
begin
|
116 |
if (RadioButton2.Checked) then begin
|
117 |
Ch1Lbl.Caption := 'Add 10';
|
118 |
Ch2Lbl.Caption := 'Multiply by 10';
|
119 |
Ch3Lbl.Caption := 'Add 3';
|
120 |
Ch4Lbl.Caption := 'Subtract 4';
|
121 |
Ch5Lbl.Caption := 'Zero out';
|
122 |
ChainPotato := false;
|
123 |
ClearScreen;
|
124 |
end;
|
125 |
|
126 |
|
127 |
end;
|
128 |
procedure TChainForm.Chain1Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
129 |
var
|
130 |
myInputData : TInputData;
|
131 |
myOutputData : TOutputData;
|
132 |
begin
|
133 |
if (ChainPotato) then begin
|
134 |
myInputData := TInputData(aInputData);
|
135 |
if (myInputData.InData < 10) then begin
|
136 |
Ch1Value.text := Inttostr(myInputData.InData);
|
137 |
Ch1Msg.Text := 'I handled it';
|
138 |
aStopNow := true;
|
139 |
end else begin
|
140 |
Ch1Value.text := ' ';
|
141 |
Ch1Msg.Text := 'Not here';
|
142 |
aStopNow := false;
|
143 |
end
|
144 |
end else begin
|
145 |
myOutputData := TOutputData(aResultData);
|
146 |
myOutputData.OutData := myOutputData.OutData + 10;
|
147 |
Ch1Value.text := Inttostr(myOutputData.OutData);
|
148 |
Ch1Msg.Text := 'Added 10';
|
149 |
end
|
150 |
end;
|
151 |
|
152 |
procedure TChainForm.Chain2Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
153 |
var
|
154 |
myInputData : TInputData;
|
155 |
myOutputData : TOutputData;
|
156 |
begin
|
157 |
if (ChainPotato) then begin
|
158 |
myInputData := TInputData(aInputData);
|
159 |
if (myInputData.InData = 10) then begin
|
160 |
Ch2Value.text := Inttostr(myInputData.InData);
|
161 |
Ch2Msg.Text := 'I handled it';
|
162 |
aStopNow := true;
|
163 |
end else begin
|
164 |
Ch2Value.text := ' ';
|
165 |
Ch2Msg.Text := 'Not here';
|
166 |
aStopNow := false;
|
167 |
end
|
168 |
end else begin
|
169 |
myOutputData := TOutputData(aResultData);
|
170 |
myOutputData.OutData := myOutputData.OutData * 10;
|
171 |
Ch2Value.text := Inttostr(myOutputData.OutData);
|
172 |
Ch2Msg.Text := 'Mulitplied by 10';
|
173 |
end
|
174 |
end;
|
175 |
|
176 |
procedure TChainForm.Chain3Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
177 |
var
|
178 |
myInputData : TInputData;
|
179 |
myOutputData : TOutputData;
|
180 |
begin
|
181 |
if (ChainPotato) then begin
|
182 |
myInputData := TInputData(aInputData);
|
183 |
if odd(myInputData.InData) then begin
|
184 |
Ch3Value.text := Inttostr(myInputData.InData);
|
185 |
Ch3Msg.Text := 'I handled it';
|
186 |
aStopNow := true;
|
187 |
end else begin
|
188 |
Ch3Value.text := ' ';
|
189 |
Ch3Msg.Text := 'Not here';
|
190 |
aStopNow := false;
|
191 |
end
|
192 |
end else begin
|
193 |
myOutputData := TOutputData(aResultData);
|
194 |
myOutputData.OutData := myOutputData.OutData + 3;
|
195 |
Ch3Value.text := Inttostr(myOutputData.OutData);
|
196 |
Ch3Msg.Text := 'Added 3';
|
197 |
end
|
198 |
end;
|
199 |
|
200 |
procedure TChainForm.Chain4Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
201 |
var
|
202 |
myInputData : TInputData;
|
203 |
myOutputData : TOutputData;
|
204 |
begin
|
205 |
if (ChainPotato) then begin
|
206 |
myInputData := TInputData(aInputData);
|
207 |
if (myInputData.InData > 100) then begin
|
208 |
Ch4Value.text := Inttostr(myInputData.InData);
|
209 |
Ch4Msg.Text := 'I handled it';
|
210 |
aStopNow := true;
|
211 |
end else begin
|
212 |
Ch4Value.text := ' ';
|
213 |
Ch4Msg.Text := 'Not here';
|
214 |
aStopNow := false;
|
215 |
end
|
216 |
end else begin
|
217 |
myOutputData := TOutputData(aResultData);
|
218 |
myOutputData.OutData := myOutputData.OutData -4;
|
219 |
Ch4Value.text := Inttostr(myOutputData.OutData);
|
220 |
Ch4Msg.Text := 'Subtracted 4';
|
221 |
end
|
222 |
end;
|
223 |
|
224 |
procedure TChainForm.Chain5Proc(aInputData, aResultData : TObject; var aStopNow : boolean);
|
225 |
var
|
226 |
myInputData : TInputData;
|
227 |
myOutputData : TOutputData;
|
228 |
begin
|
229 |
if (ChainPotato) then begin
|
230 |
myInputData := TInputData(aInputData);
|
231 |
Ch5Value.text := Inttostr(myInputData.InData);
|
232 |
Ch5Msg.Text := 'I handled it';
|
233 |
aStopNow := true;
|
234 |
end else begin
|
235 |
myOutputData := TOutputData(aResultData);
|
236 |
myOutputData.OutData := 0;
|
237 |
Ch5Value.text := Inttostr(myOutputData.OutData);
|
238 |
Ch5Msg.Text := 'Zeroed out';
|
239 |
end
|
240 |
end;
|
241 |
|
242 |
procedure TChainForm.FormCreate(Sender: TObject);
|
243 |
begin
|
244 |
ChainPotato := true;
|
245 |
TheChain:= TStChain.create;
|
246 |
TheChain.Add(Chain1Proc);
|
247 |
TheChain.Add(Chain2Proc);
|
248 |
TheChain.Add(Chain3Proc);
|
249 |
TheChain.Add(Chain4Proc);
|
250 |
TheChain.Add(Chain5Proc);
|
251 |
end;
|
252 |
|
253 |
procedure TChainForm.StartClick(Sender: TObject);
|
254 |
var
|
255 |
myInputData : TInputData;
|
256 |
myOutputData : TOutputData;
|
257 |
begin
|
258 |
myInputData := nil;
|
259 |
myOutputData := nil;
|
260 |
try
|
261 |
myInputData := TInputData.Create;
|
262 |
myOutputData := TOutputData.Create;
|
263 |
myInputData.InData := Strtoint(InputValue.Text);
|
264 |
myOutputData.OutData := Strtoint(InputValue.Text);
|
265 |
ClearScreen;
|
266 |
TheChain.Handle(myInputData, myOutputData);
|
267 |
finally;
|
268 |
myInputData.free;
|
269 |
myOutputData.free;
|
270 |
end;
|
271 |
end;
|
272 |
|
273 |
procedure TChainForm.ClearScreen;
|
274 |
begin
|
275 |
Ch1Value.text := ' ';
|
276 |
Ch1Msg.Text := ' ';
|
277 |
Ch2Value.text := ' ';
|
278 |
Ch2Msg.Text := ' ';
|
279 |
Ch3Value.text := ' ';
|
280 |
Ch3Msg.Text := ' ';
|
281 |
Ch4Value.text := ' ';
|
282 |
Ch4Msg.Text := ' ';
|
283 |
Ch5Value.text := ' ';
|
284 |
Ch5Msg.Text := ' ';
|
285 |
end;
|
286 |
|
287 |
procedure TChainForm.FormDestroy(Sender: TObject);
|
288 |
begin
|
289 |
TheChain.free;
|
290 |
end;
|
291 |
|
292 |
end.
|