1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
unit frm_personmaint;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, fpg_base, fpg_main, fpg_edit,
fpg_form, fpg_label, fpg_button,
model, model_view;
type
TPersonMaintForm = class(TfpgForm)
private
FData: TPerson;
FMemento: TPersonMemento; // This form is the Caretaker
FmedName: TPerson_Name_TextEdit_View;
FmedAge: TPerson_Age_TextEdit_View;
procedure FormShow(Sender: TObject);
procedure SetData(const AValue: TPerson);
procedure SetupMediators;
public
{@VFD_HEAD_BEGIN: PersonMaintForm}
lblName1: TfpgLabel;
edtName: TfpgEdit;
lblName2: TfpgLabel;
edtAge: TfpgEdit;
btnOK: TfpgButton;
btnCancel: TfpgButton;
lblName3: TfpgLabel;
{@VFD_HEAD_END: PersonMaintForm}
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure AfterCreate; override;
property Data: TPerson read FData write SetData;
end;
procedure EditPerson(const AData: TPerson);
{@VFD_NEWFORM_DECL}
implementation
procedure EditPerson(const AData: TPerson);
var
frm: TPersonMaintForm;
begin
frm := TPersonMaintForm.Create(nil);
try
frm.Data := AData;
if frm.ShowModal = 2 then // Cancel clicked
begin
// undo changes
AData.BeginUpdate;
AData.Memento := frm.FMemento;
AData.EndUpdate;
end;
finally
frm.Free;
end;
end;
{@VFD_NEWFORM_IMPL}
procedure TPersonMaintForm.FormShow(Sender: TObject);
begin
SetupMediators;
end;
procedure TPersonMaintForm.SetData(const AValue: TPerson);
begin
if FData = AValue then
exit; //==>>
FData := AValue;
FreeAndNil(FMemento);
FMemento := FData.Memento;
end;
procedure TPersonMaintForm.SetupMediators;
begin
FmedName := TPerson_Name_TextEdit_View.CreateCustom(edtName, FData, 'Name', 'Text');
FmedAge := TPerson_Age_TextEdit_View.CreateCustom(edtAge, FData, 'Age', 'Text');
// edtName.Text := FData.Name;
// edtAge.Text := IntToStr(FData.Age);
// Notify all observers to update themselves.
FData.NotifyObservers;
end;
constructor TPersonMaintForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
OnShow := @FormShow;
end;
destructor TPersonMaintForm.Destroy;
begin
FmedName.Free;
FmedAge.Free;
inherited Destroy;
end;
procedure TPersonMaintForm.AfterCreate;
begin
{@VFD_BODY_BEGIN: PersonMaintForm}
Name := 'PersonMaintForm';
SetPosition(418, 244, 232, 190);
WindowTitle := 'Edit Person...';
lblName1 := TfpgLabel.Create(self);
with lblName1 do
begin
Name := 'lblName1';
SetPosition(8, 8, 212, 15);
FontDesc := '#Label1';
Text := 'Name:';
end;
edtName := TfpgEdit.Create(self);
with edtName do
begin
Name := 'edtName';
SetPosition(8, 24, 212, 21);
TabOrder := 1;
Text := '';
FontDesc := '#Edit1';
end;
lblName2 := TfpgLabel.Create(self);
with lblName2 do
begin
Name := 'lblName2';
SetPosition(8, 56, 212, 15);
FontDesc := '#Label1';
Text := 'Age:';
end;
edtAge := TfpgEdit.Create(self);
with edtAge do
begin
Name := 'edtAge';
SetPosition(8, 72, 64, 21);
TabOrder := 3;
Text := '';
FontDesc := '#Edit1';
end;
btnOK := TfpgButton.Create(self);
with btnOK do
begin
Name := 'btnOK';
SetPosition(56, 159, 80, 23);
Anchors := [anRight,anBottom];
Text := 'OK';
FontDesc := '#Label1';
ImageName := '';
ModalResult := 1;
TabOrder := 4;
end;
btnCancel := TfpgButton.Create(self);
with btnCancel do
begin
Name := 'btnCancel';
SetPosition(140, 159, 80, 23);
Anchors := [anRight,anBottom];
Text := 'Cancel';
FontDesc := '#Label1';
ImageName := '';
ModalResult := 2;
TabOrder := 5;
end;
lblName3 := TfpgLabel.Create(self);
with lblName3 do
begin
Name := 'lblName3';
SetPosition(8, 108, 212, 39);
Anchors := [anLeft,anRight,anTop,anBottom];
FontDesc := '#Label1';
Text := 'Notice as you change the values they are updated in the MainForm''s Grid.';
WrapText := True;
end;
{@VFD_BODY_END: PersonMaintForm}
end;
end.
|