summaryrefslogtreecommitdiff
path: root/extras/tiopf/demos/Common/Model.pas
blob: 8fe815cbb0375ce84278c019fb576258004751a0 (plain)
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
unit Model;

{$mode objfpc}{$H+}

interface
uses
  tiObject
  ;

type
  TGender = (genMale, genFemale);

const
  cGender: array[TGender] of string = ('Male', 'Female');

type
  TPerson = class;
  TPersonList = class;


  { Undo feature for TPerson }
  TPersonMemento = class(TObject)
  private
    FOID: string;
    FObjectState: TPerObjectState;
    FName: string;
    FAge: integer;
    FGender: TGender;
  end;


  { TPerson - The subject being observed }
  TPerson = class(TtiObject)
  private
    FGender: TGender;
    FName: string;
    FAge: integer;
    function    GetGenderGUI: string;
    function    GetMemento: TPersonMemento;
    procedure   SetGender(const AValue: TGender);
    procedure   SetGenderGUI(const AValue: string);
    procedure   SetName(const Value: string);
    procedure   SetAge(const Value: integer);
    procedure   SetMemento(const AValue: TPersonMemento);
  protected
    function    GetCaption: string; override;
    procedure   DoGetFieldBounds(const AFieldName: String; var MinValue, MaxValue: Integer; var HasBounds: Boolean); override;
  public
    constructor Create; override;
    function    IsValid(const pErrors: TtiObjectErrors): Boolean; override;
    procedure   NotifyObservers; override; 
    property    Memento: TPersonMemento read GetMemento write SetMemento;
    property    Gender: TGender read FGender write SetGender;
  published
    property    Name: string read FName write SetName;
    property    Age: integer read FAge write SetAge;
    property    GenderGUI: string read GetGenderGUI write SetGenderGUI;
  end;


  { TPersonList }
  TPersonList = class(TtiObjectList)
  private
  protected
    function    GetItems(i: integer): TPerson; reintroduce;
    procedure   SetItems(i: integer; const Value: TPerson); reintroduce;
  public
    property    Items[i: integer]: TPerson read GetItems write SetItems;
    procedure   Add(const pObject: TPerson); reintroduce;
  end;
  

function GeneratePersonList: TPersonList;


implementation
uses
  Constants
  ;


function GeneratePersonList: TPersonList;
var
  lData: TPerson;
begin
  Result := TPersonList.Create;

  lData := TPerson.Create;
  lData.Name    := 'Graeme Geldenhuys';
  lData.Age     := 23;
  Result.Add(lData);

  lData := TPerson.Create;
  lData.Name    := 'Peter Hinrichsen';
  lData.Age     := 34;
  Result.Add(lData);

  //lData := TPerson.Create;
  //lData.Name    := 'Ian Krigsman';
  //lData.Age     := 45;
  //lData.Deleted := True;
  //Result.Add(lData);

  lData := TPerson.Create;
  lData.Name    := 'John Guthrie';
  lData.Age     := 56;
  Result.Add(lData);
end;


{ TPerson }

function TPerson.IsValid(const pErrors: TtiObjectErrors): Boolean;
begin
  inherited IsValid(pErrors);

  if Name = '' then
    pErrors.AddError('Name', cNameMissing);

  if (Age < 1) or (Age > 100) then
    pErrors.AddError('Age', cAgeOutofRange);

  Result := pErrors.Count = 0;
end;

{ This was used for debugging, so you can see when NotifiObservers get called } 
procedure TPerson.NotifyObservers; 
begin
//  writeln('NotifyObservers');
  inherited NotifyObservers;
end;

procedure TPerson.SetAge(const Value: integer);
begin
  { BeginUpdate and EndUpdate are optional. They allow the observers to only
    get updated once, and not continuous for small updates. It doesn't really
    make a difference for this simple example though. }
  BeginUpdate;
  FAge := Value;
  EndUpdate;
  { If you don't use BeginUpdate and EndUpdate, you need to call NotifyObserver
    to they can be updated. }
//  NotifyObservers;
end;

procedure TPerson.SetMemento(const AValue: TPersonMemento);
begin
  // Update the Person state from the memento. Only if their OID's match.
  if (OID.AsString = AValue.FOID) then
  begin
    FName        := AValue.FName;
    FAge         := AValue.FAge;
    FGender      := AValue.FGender;
    ObjectState  := AValue.FObjectState;
  end;
end;

function TPerson.GetCaption: string;
begin
  Result := Name;
  if Deleted then
    Result := Result + ' (deleted)';
end;

procedure TPerson.DoGetFieldBounds(const AFieldName: String; var MinValue,
  MaxValue: Integer; var HasBounds: Boolean);
begin
  if AFieldName = 'Name' then
  begin
//    writeln('  Name - DoGetFieldBounds');
    HasBounds := True;
    MinValue := 1;
    MaxValue := 25;
  end
  else if AFieldName = 'Age' then
  begin
//    writeln('  Age - DoGetFieldBounds');
    HasBounds := True;
    MinValue := 1;
    MaxValue := 100;
  end
  else
  begin
//    writeln('  unknown property <', AFieldName, '> - DoGetFieldBounds');
    inherited DoGetFieldBounds(AFieldName, MinValue, MaxValue, HasBounds);
  end;
end;

constructor TPerson.Create;
begin
  inherited Create;
  FGender := genMale;
end;

procedure TPerson.SetName(const Value: string);
begin
  BeginUpdate;
  FName := Value;
  EndUpdate;
end;

procedure TPerson.SetGender(const AValue: TGender);
begin
  if FGender = AValue then exit;
  BeginUpdate;
  FGender := AValue;
  EndUpdate;
end;

function TPerson.GetGenderGUI: string;
begin
  result := cGender[FGender];
end;

function TPerson.GetMemento: TPersonMemento;
begin
  // Create a new memento, store the Centre state and return it.
  Result := TPersonMemento.Create;
  Result.FOID         := OID.AsString;
  Result.FObjectState := ObjectState;
  Result.FName        := FName;
  Result.FAge         := FAge;
  Result.FGender      := FGender;
end;

procedure TPerson.SetGenderGUI(const AValue: string);
var
  i: TGender;
begin
  for i := Low(TGender) to High(TGender) do
  begin
    if cGender[i] = AValue then
    begin
      Gender := i;
      Exit; //==>
    end;
  end;
  Gender := genMale;
end;


{ TPersonList }

function TPersonList.GetItems(i: integer): TPerson;
begin
  result := TPerson(inherited GetItems(i));
end;

procedure TPersonList.SetItems(i: integer; const Value: TPerson);
begin
  inherited SetItems(i, Value);
end;

procedure TPersonList.Add(const pObject: TPerson);
begin
  BeginUpdate;
  inherited Add(pObject);
  EndUpdate;
end;


end.