summaryrefslogtreecommitdiff
path: root/extras/tiopf/demos/Common
diff options
context:
space:
mode:
authorgraemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf>2008-06-09 14:55:33 +0000
committergraemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf>2008-06-09 14:55:33 +0000
commitd76651d046c91a81f56416a7dac77479abaa245b (patch)
tree13767e4bf9349740e4dcdcbca958121eca7ca5d0 /extras/tiopf/demos/Common
parent990f428fb090074f37a5c3a7994f035808044a7d (diff)
downloadfpGUI-d76651d046c91a81f56416a7dac77479abaa245b.tar.xz
* Created a quick and easy StringGrid mediator demo to show you what it can do.
Diffstat (limited to 'extras/tiopf/demos/Common')
-rw-r--r--extras/tiopf/demos/Common/Constants.pas2
-rw-r--r--extras/tiopf/demos/Common/Model.pas42
-rw-r--r--extras/tiopf/demos/Common/Model_View.pas50
3 files changed, 90 insertions, 4 deletions
diff --git a/extras/tiopf/demos/Common/Constants.pas b/extras/tiopf/demos/Common/Constants.pas
index 6c93719d..9820ae5e 100644
--- a/extras/tiopf/demos/Common/Constants.pas
+++ b/extras/tiopf/demos/Common/Constants.pas
@@ -6,7 +6,7 @@ interface
const
cNameMissing = 'Please enter a name';
- cAgeOutofRange = 'Please enter a valid age';
+ cAgeOutofRange = 'Please enter a valid age between 1 - 100';
implementation
diff --git a/extras/tiopf/demos/Common/Model.pas b/extras/tiopf/demos/Common/Model.pas
index 148f94b9..3f2c048c 100644
--- a/extras/tiopf/demos/Common/Model.pas
+++ b/extras/tiopf/demos/Common/Model.pas
@@ -17,6 +17,18 @@ 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
@@ -24,16 +36,19 @@ type
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;
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;
@@ -53,7 +68,7 @@ type
procedure Add(const pObject: TPerson); reintroduce;
end;
-
+
function GeneratePersonList: TPersonList;
@@ -104,7 +119,7 @@ begin
if Name = '' then
pErrors.AddError('Name', cNameMissing);
- if Age < 1 then
+ if (Age < 1) or (Age > 100) then
pErrors.AddError('Age', cAgeOutofRange);
Result := pErrors.Count = 0;
@@ -130,6 +145,18 @@ begin
// 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;
@@ -163,6 +190,17 @@ 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;
diff --git a/extras/tiopf/demos/Common/Model_View.pas b/extras/tiopf/demos/Common/Model_View.pas
index 7cc68e8a..82a9027b 100644
--- a/extras/tiopf/demos/Common/Model_View.pas
+++ b/extras/tiopf/demos/Common/Model_View.pas
@@ -14,11 +14,23 @@ uses
;
type
- { TEdit - Name }
+ { TfpgEdit - Name }
TPerson_Name_TextEdit_View = class(TMediatorEditView)
+ private
+ procedure OnTextChanged(Sender: TObject);
protected
procedure SetupGUIandObject; override;
end;
+
+ { TfpgEdit - Age }
+ TPerson_Age_TextEdit_View = class(TMediatorEditView)
+ private
+ procedure OnTextChanged(Sender: TObject);
+ protected
+ procedure SetupGUIandObject; override;
+ procedure GuiToObject; override;
+ procedure ObjectToGui; override;
+ end;
{ TSpinEdit - Age }
@@ -66,14 +78,22 @@ type
implementation
+uses
+ Model, SysUtils;
{ TPersonNameView }
+procedure TPerson_Name_TextEdit_View.OnTextChanged(Sender: TObject);
+begin
+ GUIChanged;
+end;
+
procedure TPerson_Name_TextEdit_View.SetupGUIandObject;
begin
inherited;
{ The Name field my only contain 25 characters max. }
EditControl.MaxLength := 25;
+ EditControl.OnChange := @OnTextChanged;
end;
@@ -133,6 +153,34 @@ begin
EditControl.Max := 100;
end;
+{ TPerson_Age_TextEdit_View }
+
+procedure TPerson_Age_TextEdit_View.OnTextChanged(Sender: TObject);
+begin
+ GUIChanged;
+end;
+
+procedure TPerson_Age_TextEdit_View.SetupGUIandObject;
+begin
+ inherited SetupGUIandObject;
+ EditControl.MaxLength := 3;
+ EditControl.OnChange := @OnTextChanged;
+end;
+
+procedure TPerson_Age_TextEdit_View.GuiToObject;
+begin
+ inherited GuiToObject;
+ // manual example without RTTI
+// TPerson(Subject).Age := StrToInt(EditControl.Text);
+end;
+
+procedure TPerson_Age_TextEdit_View.ObjectToGui;
+begin
+ inherited ObjectToGui;
+ // manual example without RTTI
+// EditControl.Text := IntToStr(TPerson(Subject).Age);
+end;
+
initialization
{-----------------------------------------------------------------------------
Register all your Mediator Views here