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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
unit frmmain;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes,
{ fpGUI Toolkit }
fpg_base, fpg_main, fpg_widget, fpg_form, fpg_button,
fpg_grid, fpg_dialogs, fpg_menu,
{ tiOPF }
tiModelMediator;
type
{ The main application window }
TMainForm = class(TfpgForm)
private
{@VFD_HEAD_BEGIN: MainForm}
grdContacts: TfpgStringGrid;
btnAdd: TfpgButton;
btnEdit: TfpgButton;
btnDelete: TfpgButton;
MainMenu: TfpgMenuBar;
miFile: TfpgPopupMenu;
miEdit: TfpgPopupMenu;
miSystem: TfpgPopupMenu;
{@VFD_HEAD_END: MainForm}
FMediator: TtiModelMediator;
procedure FormShow(Sender: TObject);
procedure GridDoubleClicked(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
procedure SetupMediators;
procedure miEditAddClick(Sender: TObject);
procedure miEditEditClick(Sender: TObject);
procedure miEditDeleteClick(Sender: TObject);
procedure miSystemCityList(Sender: TObject);
procedure miSystemCountryList(Sender: TObject);
procedure miSystemAddressTypeList(Sender: TObject);
procedure miFileExit(Sender: TObject);
public
procedure AfterCreate; override;
procedure AfterConstruction; override;
constructor Create(AOwner: TComponent); override;
end;
{@VFD_NEWFORM_DECL}
implementation
uses
model, contactmanager, tiListMediators, tiBaseMediator, tiMediators,
frmcontactmaint, frmcitylist, frmcountrylist, tiDialogs, tiObject;
{@VFD_NEWFORM_IMPL}
procedure TMainForm.FormShow(Sender: TObject);
begin
// do nothing yet
end;
procedure TMainForm.GridDoubleClicked(Sender: TObject; AButton: TMouseButton;
AShift: TShiftState; const AMousePos: TPoint);
begin
miEditEditClick(nil);
end;
procedure TMainForm.SetupMediators;
begin
if not Assigned(FMediator) then
begin
FMediator := TtiModelMediator.Create(self);
FMediator.AddComposite('FirstName;LastName(130);EMail(180);Mobile(130);Comments(200)', grdContacts);
end;
FMediator.Subject := gContactManager.ContactList;
FMediator.Active := True;
end;
procedure TMainForm.miEditAddClick(Sender: TObject);
var
c: TContact;
begin
c := TContact.CreateNew;
if EditContact(c) then
gContactManager.ContactList.Add(c)
else
c.Free;
end;
procedure TMainForm.miEditEditClick(Sender: TObject);
var
c: TContact;
begin
if grdContacts.FocusRow < 0 then
begin
tiAppError('You need to select a Contact first');
Exit;
end;
c := TContact(TtiStringGridMediatorView(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject);
if not Assigned(c) then
Exit; //==>
if EditContact(c) then
begin
// we can save contact here if we wanted
end;
end;
procedure TMainForm.miEditDeleteClick(Sender: TObject);
var
c: TContact;
begin
if grdContacts.FocusRow < 0 then
begin
tiAppError('You need to select a Contact first');
Exit;
end;
c := TContact(TtiStringGridMediatorView(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject);
if tiAppConfirmation('Are you sure you want to delete <%s>', [c.FirstName + ' ' + c.LastName]) then
begin
{ We can't use .Deleted property here, because we don't actually save
changes. This means the ObjectState will only be posDelete and not
posDeleted, which is what .FreeDeleted is looking for. }
// c.Deleted := True;
c.ObjectState := posDeleted;
gContactManager.ContactList.FreeDeleted;
end;
end;
procedure TMainForm.miSystemCityList(Sender: TObject);
begin
ShowCities(gContactManager.CityList);
end;
procedure TMainForm.miSystemCountryList(Sender: TObject);
begin
ShowCountries(gContactManager.CountryList);
end;
procedure TMainForm.miSystemAddressTypeList(Sender: TObject);
begin
// ShowAddressTypeList(gContactManager.AddressTypeList);
end;
procedure TMainForm.miFileExit(Sender: TObject);
begin
Close;
end;
procedure TMainForm.AfterCreate;
begin
{@VFD_BODY_BEGIN: MainForm}
Name := 'MainForm';
SetPosition(373, 273, 540, 404);
WindowTitle := 'Demo 21: Address Book Demo using MGM';
grdContacts := TfpgStringGrid.Create(self);
with grdContacts do
begin
Name := 'grdContacts';
SetPosition(12, 56, 516, 336);
Anchors := [anLeft,anRight,anTop,anBottom];
FontDesc := '#Grid';
HeaderFontDesc := '#GridHeader';
OnDoubleClick := @GridDoubleClicked;
end;
btnAdd := TfpgButton.Create(self);
with btnAdd do
begin
Name := 'btnAdd';
SetPosition(12, 28, 52, 24);
Text := 'Add';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 1;
OnClick := @miEditAddClick;
end;
btnEdit := TfpgButton.Create(self);
with btnEdit do
begin
Name := 'btnEdit';
SetPosition(68, 28, 52, 24);
Text := 'Edit';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 2;
OnClick := @miEditEditClick;
end;
btnDelete := TfpgButton.Create(self);
with btnDelete do
begin
Name := 'btnDelete';
SetPosition(124, 28, 52, 24);
Text := 'Delete';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 3;
OnClick := @miEditDeleteClick;
end;
MainMenu := TfpgMenuBar.Create(self);
with MainMenu do
begin
Name := 'MainMenu';
SetPosition(0, 0, 540, 24);
Anchors := [anLeft,anRight,anTop];
end;
miFile := TfpgPopupMenu.Create(self);
with miFile do
begin
Name := 'miFile';
SetPosition(344, 136, 120, 20);
AddMenuItem('E&xit', 'Alt+F4', @miFileExit);
end;
miEdit := TfpgPopupMenu.Create(self);
with miEdit do
begin
Name := 'miEdit';
SetPosition(344, 156, 120, 20);
AddMenuItem('Add Contact', '', @miEditAddClick);
AddMenuItem('Edit Contact', '', @miEditEditClick);
AddMenuItem('Delete Contact', '', @miEditDeleteClick);
end;
miSystem := TfpgPopupMenu.Create(self);
with miSystem do
begin
Name := 'miSystem';
SetPosition(344, 176, 120, 20);
AddMenuItem('City List', '', @miSystemCityList);
AddMenuItem('Country List', '', @miSystemCountryList);
AddMenuItem('Address Type List', '', @miSystemAddressTypeList).Enabled := False;
end;
{@VFD_BODY_END: MainForm}
// setup main menu
MainMenu.AddMenuItem('&File', nil).SubMenu := miFile;
MainMenu.AddMenuItem('&Edit', nil).SubMenu := miEdit;
MainMenu.AddMenuItem('&System', nil).SubMenu := miSystem;
end;
procedure TMainForm.AfterConstruction;
begin
inherited AfterConstruction;
SetupMediators;
end;
constructor TMainForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
gContactManager.PopulateContacts;
OnShow := @FormShow;
end;
initialization
RegisterFallBackMediators;
gMediatorManager.RegisterMediator(TtiStringGridMediatorView, TContactList);
gMediatorManager.RegisterMediator(TtiListViewMediatorView, TAddressList);
gMediatorManager.RegisterMediator(TtiStringGridMediatorView, TCityList);
gMediatorManager.RegisterMediator(TtiStringGridMediatorView, TCountryList);
gMediatorManager.RegisterMediator(TtiDynamicComboBoxMediatorView, TCity, 'Country');
gMediatorManager.RegisterMediator(TtiDynamicComboBoxMediatorView, TAddressType, 'AddressType');
end.
|