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
|
unit frmmain;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, gfxbase, fpgfx, gui_edit,
gfx_widget, gui_form, gui_label, gui_button,
gui_basegrid, gui_grid, gui_dialogs, gui_menu,
gui_panel, gui_popupcalendar, gui_gauge, tiFormMediator;
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: TFormMediator;
procedure FormShow(Sender: TObject);
procedure SetupMediators;
procedure miEditAddClick(Sender: TObject);
procedure miEditEditClick(Sender: TObject);
procedure miEditDeleteClick(Sender: TObject);
procedure miSystemCityList(Sender: TObject);
procedure miSystemCountryList(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, frmcontactmaint,
frmcitylist, frmcountrylist;
{@VFD_NEWFORM_IMPL}
procedure TMainForm.FormShow(Sender: TObject);
begin
// do nothing yet
end;
procedure TMainForm.SetupMediators;
begin
if not Assigned(FMediator) then
begin
FMediator := TFormMediator.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);
begin
//
end;
procedure TMainForm.miEditEditClick(Sender: TObject);
var
c: TContact;
begin
c := TContact(TStringGridMediator(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject);
// tiShowString(c.AsDebugString);
if not Assigned(c) then
Exit; //==>
if EditContact(c) then
begin
// we can save contact here
end;
end;
procedure TMainForm.miEditDeleteClick(Sender: TObject);
begin
//
end;
procedure TMainForm.miSystemCityList(Sender: TObject);
begin
ShowCities(gContactManager.CityList);
end;
procedure TMainForm.miSystemCountryList(Sender: TObject);
begin
ShowCountries(gContactManager.CountryList);
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';
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).Enabled := False;
AddMenuItem('Edit Contact', '', @miEditEditClick);
AddMenuItem('Delete Contact', '', @miEditDeleteClick).Enabled := False;
end;
miSystem := TfpgPopupMenu.Create(self);
with miSystem do
begin
Name := 'miSystem';
SetPosition(344, 176, 120, 20);
AddMenuItem('City List', '', @miSystemCityList);
AddMenuItem('Country List', '', @miSystemCountryList);
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
gMediatorManager.RegisterMediator(TStringGridMediator, TContactList);
gMediatorManager.RegisterMediator(TListViewMediator, TAddressList);
gMediatorManager.RegisterMediator(TStringGridMediator, TCityList);
gMediatorManager.RegisterMediator(TStringGridMediator, TCountryList);
end.
|