diff options
author | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2009-10-09 13:07:38 +0200 |
---|---|---|
committer | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2009-10-09 13:07:38 +0200 |
commit | 441528ff12011732221996990d0a206e610649ed (patch) | |
tree | 7efeaf93db7880aa75660fac603132db21386778 /extras | |
parent | dc701533a65ad6634500268e178b62b90aff4ecf (diff) | |
download | fpGUI-441528ff12011732221996990d0a206e610649ed.tar.xz |
Refactored Mediators used with tiOPF project.
The mediators are now a lot more streamlined.
Removed lots of code duplications
Changed class namse to be more consistent with tiOPF naming convention.
** This is a CODE BREAKING change **
Signed-off-by: Graeme Geldenhuys <graeme@mastermaths.co.za>
Diffstat (limited to 'extras')
-rw-r--r-- | extras/tiopf/gui/tiListMediators.pas | 334 | ||||
-rw-r--r-- | extras/tiopf/gui/tiMediators.pas | 878 |
2 files changed, 497 insertions, 715 deletions
diff --git a/extras/tiopf/gui/tiListMediators.pas b/extras/tiopf/gui/tiListMediators.pas index c0c5f5a5..8f924306 100644 --- a/extras/tiopf/gui/tiListMediators.pas +++ b/extras/tiopf/gui/tiListMediators.pas @@ -19,78 +19,61 @@ uses type { Composite mediator for TfpgListView } - TListViewMediator = class(TCustomListMediator) + TtiListViewMediatorView = class(TtiCustomListMediatorView) private FObserversInTransit: TList; - FView: TfpgListView; - procedure SetView(const AValue: TfpgListView); protected - function GetSelectedObject: TtiObject; override; - procedure SetSelectedObject(const AValue: TtiObject); override; - procedure CreateColumns; override; - procedure DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); override; - procedure DoDeleteItemMediator(AIndex: Integer; AMediator: TListItemMediator); override; - function GetGuiControl: TComponent; override; - procedure SetGuiControl(const AValue: TComponent); override; - procedure SetupGUIandObject; override; - procedure ClearList; override; - procedure RebuildList; override; + function GetSelectedObject: TtiObject; override; + procedure SetSelectedObject(const AValue: TtiObject); override; + procedure CreateColumns; override; + procedure DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); override; + procedure DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); override; + procedure SetupGUIandObject; override; + procedure ClearList; override; + procedure RebuildList; override; + procedure SetActive(const AValue: Boolean); override; public constructor CreateCustom(AModel: TtiObjectList; AView: TfpgListView; ADisplayNames: string; AIsObserving: Boolean = True); overload; - constructor CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean = True); overload; + constructor CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TtiOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean = True); overload; class function ComponentClass: TClass; override; constructor Create; override; - destructor Destroy; override; - procedure HandleSelectionChanged; override; - function GetObjectFromItem(AItem: TfpgLVItem): TtiObject; - published - property View: TfpgListView read FView write SetView; + destructor Destroy; override; + function View: TfpgListView; reintroduce; + procedure HandleSelectionChanged; override; + function GetObjectFromItem(AItem: TfpgLVItem): TtiObject; end; - // for backwards compatibility - TCompositeListViewMediator = TListViewMediator; - - { Composite mediator for TfpgStringGrid } - TStringGridMediator = class(TCustomListMediator) + TtiStringGridMediatorView = class(TtiCustomListMediatorView) private - FView: TfpgStringGrid; - procedure DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); override; - procedure DoDeleteItemMediator(AIndex: Integer; AMediator: TListItemMediator); override; - procedure SetView(const AValue: TfpgStringGrid); + procedure DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); override; + procedure DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); override; protected - function GetSelectedObject: TtiObject; override; - procedure SetSelectedObject(const AValue: TtiObject); override; - procedure CreateColumns; override; - function GetGuiControl: TComponent; override; - procedure SetGuiControl(const AValue: TComponent); override; - procedure SetupGUIandObject; override; - procedure ClearList; override; - procedure RebuildList; override; + function GetSelectedObject: TtiObject; override; + procedure SetSelectedObject(const AValue: TtiObject); override; + procedure CreateColumns; override; + procedure SetupGUIandObject; override; + procedure ClearList; override; + procedure RebuildList; override; public constructor CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean = True); reintroduce; overload; - destructor Destroy; override; + destructor Destroy; override; class function ComponentClass: TClass; override; - function GetObjectFromRow(ARow: Integer): TtiObject; - published - property View: TfpgStringGrid read FView write SetView; + function GetObjectFromRow(ARow: Integer): TtiObject; + function View: TfpgStringGrid; reintroduce; end; - // for backwards compatibility - TCompositeStringGridMediator = TStringGridMediator; - - { Used internally for sub-mediators in ListView mediator. Moved to interface section so it can be overridden. } - TListViewListItemMediator = class(TListItemMediator) + TtiListViewListItemMediator = class(TtiListItemMediator) private FView: TfpgLVItem; procedure SetupFields; virtual; public constructor CreateCustom(AModel: TtiObject; AView: TfpgLVItem; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean = True); reintroduce; overload; - constructor CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean = True); reintroduce; overload; + constructor CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TtiOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean = True); reintroduce; overload; procedure BeforeDestruction; override; procedure Update(ASubject: TtiObject); override; published @@ -100,7 +83,7 @@ type { Used internally for sub-mediators in StringGrid mediator. Moved to interface section so it can be overridden. } - TStringGridRowMediator = class(TListItemMediator) + TtiStringGridRowMediator = class(TtiListItemMediator) private FView: TfpgStringGrid; FRowIndex: integer; @@ -113,73 +96,67 @@ type end; -implementation +procedure RegisterFallBackListMediators; -{ TListViewMediator } +implementation -procedure TListViewMediator.SetView(const AValue: TfpgListView); -begin - FView := AValue; - SetGUIControl(AValue); -end; +uses + tiRTTI; -function TListViewMediator.GetGuiControl: TComponent; -begin - Result := FView; -end; -procedure TListViewMediator.SetGuiControl(const AValue: TComponent); +procedure RegisterFallBackListMediators; begin - FView := AValue as TfpgListView; - inherited SetGuiControl(AValue); + gMediatorManager.RegisterMediator(TtiListViewMediatorView, TtiObjectList); + gMediatorManager.RegisterMediator(TtiStringGridMediatorView, TtiObjectList); end; -procedure TListViewMediator.SetSelectedObject(const AValue: TtiObject); +{ TtiListViewMediatorView } + +procedure TtiListViewMediatorView.SetSelectedObject(const AValue: TtiObject); var i: integer; begin - for i := 0 to FView.Items.Count - 1 do - if TtiObject(FView.Items.Item[i].UserData) = AValue then + for i := 0 to View.Items.Count - 1 do + if TtiObject(View.Items.Item[i].UserData) = AValue then begin - FView.ItemIndex := i; + View.ItemIndex := i; HandleSelectionChanged; {$Note Is this line required?} Exit; //==> end; end; -function TListViewMediator.GetSelectedObject: TtiObject; +function TtiListViewMediatorView.GetSelectedObject: TtiObject; begin - Result := GetObjectFromItem(FView.Items.Item[FView.ItemIndex]); + Result := GetObjectFromItem(View.Items.Item[View.ItemIndex]); end; -procedure TListViewMediator.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); +procedure TtiListViewMediatorView.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); var li: TfpgLVItem; - m: TListViewListItemMediator; + m: TtiListViewListItemMediator; begin DataAndPropertyValid(AData); - { Create ListItem and Mediator } - FView.BeginUpdate; + View.BeginUpdate; try - li := TfpgLVItem.Create(FView.Items); - FView.Items.Add(li); - m := TListViewListItemMediator.CreateCustom(AData, li, OnBeforeSetupField, FieldsInfo, Active); + li := TfpgLVItem.Create(View.Items); + View.Items.Add(li); + m := TtiListViewListItemMediator.CreateCustom(AData, li, OnBeforeSetupField, FieldsInfo, Active); li.UserData := m; MediatorList.Add(m); finally - FView.EndUpdate; + View.EndUpdate; end; end; -procedure TListViewMediator.DoDeleteItemMediator(AIndex: Integer; AMediator: TListItemMediator); +procedure TtiListViewMediatorView.DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); begin - FView.Items.Delete(FView.Items.IndexOf(TListViewListItemMediator(AMediator).FView)); + View.Items.Delete(View.Items.IndexOf(TtiListViewListItemMediator(AMediator).View)); inherited DoDeleteItemMediator(AIndex, AMediator); end; -procedure TListViewMediator.CreateColumns; +procedure TtiListViewMediatorView.CreateColumns; var c: integer; lc: TfpgLVColumn; @@ -204,24 +181,26 @@ begin end; end; -procedure TListViewMediator.SetupGUIandObject; +procedure TtiListViewMediatorView.SetupGUIandObject; begin { Setup TfpgListView defaults } - FView.Columns.Clear; - FView.Items.Clear; + View.Columns.Clear; + View.Items.Clear; // FView.ViewStyle := vsReport; - FView.ShowHeaders := True; + View.ShowHeaders := True; // FView.RowSelect := True; // FView.AutoSize := False; // FView.ScrollBars := ssAutoBoth; end; -procedure TListViewMediator.ClearList; +procedure TtiListViewMediatorView.ClearList; begin - View.Items.Clear; + MediatorList.Clear; + if View <> nil then + View.Items.Clear; end; -procedure TListViewMediator.RebuildList; +procedure TtiListViewMediatorView.RebuildList; begin View.BeginUpdate; try @@ -232,42 +211,53 @@ begin end; end; -constructor TListViewMediator.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean); +procedure TtiListViewMediatorView.SetActive(const AValue: Boolean); +begin + if not AValue then + ClearList; + inherited SetActive(AValue); +end; + +constructor TtiListViewMediatorView.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; AOnBeforeSetupField: TtiOnBeforeSetupField; ADisplayNames: string; AIsObserving: Boolean); begin Create; // don't forget this OnBeforeSetupField := AOnBeforeSetupField; - DisplayNames := ADisplayNames; // Will call ParseDisplaynames. - Subject := AModel; - GUIControl := AView; // Will call SetupGUIandObject; + DisplayNames := ADisplayNames; // Will call ParseDisplaynames. + Subject := AModel; + SetView(AView); // Will call SetupGUIandObject; CreateSubMediators; - Active := AIsObserving; // Will attach/Detach + Active := AIsObserving; // Will attach/Detach end; -class function TListViewMediator.ComponentClass: TClass; +class function TtiListViewMediatorView.ComponentClass: TClass; begin Result := TfpgListView; end; -constructor TListViewMediator.Create; +constructor TtiListViewMediatorView.Create; begin inherited Create; FObserversInTransit := TList.Create; end; -constructor TListViewMediator.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; ADisplayNames: string; AIsObserving: Boolean); +constructor TtiListViewMediatorView.CreateCustom(AModel: TtiObjectList; AView: TfpgListView; ADisplayNames: string; AIsObserving: Boolean); begin CreateCustom(AModel, AView, nil, ADisplayNames, AIsObserving); end; -destructor TListViewMediator.Destroy; +destructor TtiListViewMediatorView.Destroy; begin IsObserving := False; - FView := nil; FObserversInTransit.Free; - inherited; + inherited Destroy; +end; + +function TtiListViewMediatorView.View: TfpgListView; +begin + Result := TfpgListView(inherited View); end; -procedure TListViewMediator.HandleSelectionChanged; +procedure TtiListViewMediatorView.HandleSelectionChanged; var i: integer; begin @@ -292,7 +282,7 @@ begin { Set the Observers Subject property to the selected object } for i := 0 to SelectedObject.ObserverList.Count - 1 do - TMediatorView(SelectedObject.ObserverList.Items[i]).Subject := + TtiMediatorView(SelectedObject.ObserverList.Items[i]).Subject := SelectedObject; // execute the NotifyObservers event to update the observers. @@ -300,44 +290,44 @@ begin end; end; -function TListViewMediator.GetObjectFromItem(AItem: TfpgLVItem): TtiObject; +function TtiListViewMediatorView.GetObjectFromItem(AItem: TfpgLVItem): TtiObject; begin if (AItem = nil) or (AItem.UserData = nil) then Result := nil else - Result := TListItemMediator(AItem.UserData).Model; + Result := TtiListItemMediator(AItem.UserData).Model; end; -{ TListViewListItemMediator } +{ TtiListViewListItemMediator } -procedure TListViewListItemMediator.SetupFields; +procedure TtiListViewListItemMediator.SetupFields; var c: integer; lMemberName: string; lValue: string; begin lMemberName := FFieldsInfo[0].PropName; - lValue := Model.PropValue[lMemberName]; + lValue := tiGetProperty(Model, lMemberName); if Assigned(OnBeforeSetupField) then OnBeforeSetupField(Model, lMemberName, lValue); FView.Caption := lValue; for c := 1 to FFieldsInfo.Count - 1 do begin lMemberName := FFieldsInfo[c].PropName; - lValue := Model.PropValue[lMemberName]; + lValue := tiGetProperty(Model, lMemberName); if Assigned(OnBeforeSetupField) then OnBeforeSetupField(Model, lMemberName, lValue); FView.SubItems.Add(lValue); end; end; -constructor TListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean); +constructor TtiListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean); begin CreateCustom(AModel, AView, nil, AFieldsInfo, IsObserving); end; -constructor TListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean); +constructor TtiListViewListItemMediator.CreateCustom(AModel: TtiObject; AView: TfpgLVItem; AOnBeforeSetupField: TtiOnBeforeSetupField; const AFieldsInfo: TtiMediatorFieldInfoList; IsObserving: Boolean); begin inherited Create; Model := AModel; @@ -348,7 +338,7 @@ begin Active := IsObserving; // Will attach end; -procedure TListViewListItemMediator.BeforeDestruction; +procedure TtiListViewListItemMediator.BeforeDestruction; begin Model.DetachObserver(self); Model := nil; @@ -356,7 +346,7 @@ begin inherited BeforeDestruction; end; -procedure TListViewListItemMediator.Update(ASubject: TtiObject); +procedure TtiListViewListItemMediator.Update(ASubject: TtiObject); var c: integer; lMemberName: string; @@ -365,7 +355,7 @@ begin Assert(Model = ASubject); lMemberName := FFieldsInfo[0].PropName; - lValue := Model.PropValue[lMemberName]; + lValue := tiGetProperty(Model, lMemberName); if Assigned(OnBeforeSetupField) then OnBeforeSetupField(Model, lMemberName, lValue); @@ -382,74 +372,59 @@ begin end; -{ TStringGridMediator } +{ TtiStringGridMediatorView } -function TStringGridMediator.GetSelectedObject: TtiObject; +function TtiStringGridMediatorView.GetSelectedObject: TtiObject; begin - Result := GetObjectFromRow(FView.FocusRow); + Result := GetObjectFromRow(View.FocusRow); end; -procedure TStringGridMediator.SetSelectedObject(const AValue: TtiObject); +procedure TtiStringGridMediatorView.SetSelectedObject(const AValue: TtiObject); var i: integer; o: TObject; begin - for i := 0 to FView.RowCount - 1 do + for i := 0 to View.RowCount - 1 do begin - o := FView.Objects[0, i]; - if Assigned(o) and (TtiObject(o) = AValue) then + o := View.Objects[0, i]; + if Assigned(o) and (TtiListItemMediator(o).Model = AValue) then begin - FView.FocusRow := i; + View.FocusRow := i; Exit; //==> end; end; { for } end; -procedure TStringGridMediator.SetView(const AValue: TfpgStringGrid); -begin - SetGUIControl(AValue); -end; - -function TStringGridMediator.GetGuiControl: TComponent; -begin - Result := fView; -end; - -procedure TStringGridMediator.SetGuiControl(const AValue: TComponent); -begin - FView := AValue as TfpgStringGrid; -end; - -procedure TStringGridMediator.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); +procedure TtiStringGridMediatorView.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); var i: integer; lFieldName: string; - lMediatorView: TStringGridRowMediator; + lMediatorView: TtiStringGridRowMediator; begin - FView.BeginUpdate; + View.BeginUpdate; try - if ARowIdx = FView.RowCount then // In case of add notification - FView.RowCount := FView.RowCount+1; + if ARowIdx = View.RowCount then // In case of add notification + View.RowCount := View.RowCount+1; for i := 0 to FieldsInfo.Count - 1 do begin lFieldName := FieldsInfo[i].PropName; - FView.Cells[i, ARowIdx] := AData.PropValue[lFieldName]; // set Cell text + View.Cells[i, ARowIdx] := tiGetProperty(AData, lFieldName); // set Cell text end; - lMediatorView := TStringGridRowMediator.CreateCustom(AData, FView, FieldsInfo, ARowIdx, Active); - FView.Objects[0, ARowIdx] := lMediatorView; // set Object reference inside grid. It used to be AData. + lMediatorView := TtiStringGridRowMediator.CreateCustom(AData, View, FieldsInfo, ARowIdx, Active); + View.Objects[0, ARowIdx] := lMediatorView; // set Object reference inside grid. It used to be AData. MediatorList.Add(lMediatorView); finally - FView.EndUpdate; + View.EndUpdate; end; end; -procedure TStringGridMediator.DoDeleteItemMediator(AIndex: Integer; AMediator: TListItemMediator); +procedure TtiStringGridMediatorView.DoDeleteItemMediator(AIndex: Integer; AMediator: TtiListItemMediator); begin - FView.DeleteRow(AIndex); + View.DeleteRow(AIndex); inherited DoDeleteItemMediator(AIndex, AMediator); end; -procedure TStringGridMediator.CreateColumns; +procedure TtiStringGridMediatorView.CreateColumns; var i: integer; lColumnTotalWidth: integer; @@ -461,43 +436,43 @@ begin lGridNonContentWidth := 2 + 2 + (FieldsInfo.Count - 1) + 15; for i := 0 to FieldsInfo.Count - 1 do begin - FView.ColumnWidth[i] := FieldsInfo[i].FieldWidth; - FView.ColumnTitle[i] := FieldsInfo[i].Caption; - FView.Columns[i].Alignment := FieldsInfo[i].Alignment; + View.ColumnWidth[i] := FieldsInfo[i].FieldWidth; + View.ColumnTitle[i] := FieldsInfo[i].Caption; + View.Columns[i].Alignment := FieldsInfo[i].Alignment; //resize the last column to fill the grid. if i = FieldsInfo.Count - 1 then begin - if FView.Width > (lColumnTotalWidth + lGridNonContentWidth) then + if View.Width > (lColumnTotalWidth + lGridNonContentWidth) then begin - lLastColumnWidth := FView.Width - (lColumnTotalWidth + lGridNonContentWidth); + lLastColumnWidth := View.Width - (lColumnTotalWidth + lGridNonContentWidth); if lLastColumnWidth > 10 then - FView.ColumnWidth[i] := lLastColumnWidth; + View.ColumnWidth[i] := lLastColumnWidth; end; end else - lColumnTotalWidth := lColumnTotalWidth + FView.ColumnWidth[i]; + lColumnTotalWidth := lColumnTotalWidth + View.ColumnWidth[i]; end; end; -procedure TStringGridMediator.SetupGUIandObject; +procedure TtiStringGridMediatorView.SetupGUIandObject; begin //Setup default properties for the StringGrid - FView.RowSelect := True; - FView.ColumnCount := FieldsInfo.Count; + View.RowSelect := True; + View.ColumnCount := FieldsInfo.Count; if ShowDeleted then - FView.RowCount := Model.Count + View.RowCount := Model.Count else - FView.RowCount := Model.CountNotDeleted; + View.RowCount := Model.CountNotDeleted; end; -procedure TStringGridMediator.ClearList; +procedure TtiStringGridMediatorView.ClearList; begin MediatorList.Clear; - if Assigned(View) then + if View <> nil then View.RowCount := 1; {$Note Double check if this is desired. Shouldn't it be 0 instead. } end; -procedure TStringGridMediator.RebuildList; +procedure TtiStringGridMediatorView.RebuildList; begin // writeln('--- TStringGridMediator.RebuildList'); { This rebuilds the whole list. Not very efficient. } @@ -505,62 +480,64 @@ begin try SetupGUIandObject; MediatorList.Clear; - // for i := View.ColumnCount-1 downto 0 do - // View.DeleteColumn(i); CreateSubMediators; finally View.EndUpdate; end; end; -constructor TStringGridMediator.CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean); +constructor TtiStringGridMediatorView.CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean); begin inherited Create; DisplayNames := ADisplayNames; - Subject := AModel; - GUIControl := AGrid; + Subject := AModel; + SetView(AGrid); CreateSubMediators; - IsObserving := AIsObserving; + IsObserving := AIsObserving; end; -destructor TStringGridMediator.Destroy; +destructor TtiStringGridMediatorView.Destroy; begin IsObserving := False; - FView := nil; inherited Destroy; end; -class function TStringGridMediator.ComponentClass: TClass; +class function TtiStringGridMediatorView.ComponentClass: TClass; begin Result := TfpgStringGrid; end; -function TStringGridMediator.GetObjectFromRow(ARow: Integer): TtiObject; +function TtiStringGridMediatorView.GetObjectFromRow(ARow: Integer): TtiObject; var O: TObject; begin - if FView.RowCount = 0 then + if View.RowCount = 0 then begin Result := nil; Exit; end; - if FView.FocusRow = -1 then + if ARow = -1 then Result := nil else begin - O := FView.Objects[0, ARow]; + O := View.Objects[0, ARow]; if O <> nil then - Result := TListItemMediator(O).Model + Result := TtiListItemMediator(O).Model else Result := nil; end; end; +function TtiStringGridMediatorView.View: TfpgStringGrid; +begin + Result := TfpgStringGrid(inherited View); +end; + -{ TStringGridRowMediator } +{ TtiStringGridRowMediator } -constructor TStringGridRowMediator.CreateCustom(AModel: TtiObject; AGrid: TfpgStringGrid; const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean); +constructor TtiStringGridRowMediator.CreateCustom(AModel: TtiObject; AGrid: TfpgStringGrid; const AFieldsInfo: TtiMediatorFieldInfoList; ARowIndex: integer; IsObserving: Boolean); begin inherited Create; Model := AModel; @@ -570,16 +547,17 @@ begin Active := IsObserving; // Will attach end; -procedure TStringGridRowMediator.Update(ASubject: TtiObject); +procedure TtiStringGridRowMediator.Update(ASubject: TtiObject); var i: integer; - lvalue, lFieldName: string; + lFieldName: string; + lValue: string; begin Assert(Model = ASubject); for i := 0 to FFieldsInfo.Count - 1 do begin lFieldName := FFieldsInfo[I].PropName; - lValue := Model.PropValue[lFieldName]; + lValue := tiGetProperty(Model, lFieldName); if Assigned(OnBeforeSetupField) then OnBeforeSetupField(Model, lFieldName, lValue); FView.Cells[i, FRowIndex] := lValue; diff --git a/extras/tiopf/gui/tiMediators.pas b/extras/tiopf/gui/tiMediators.pas index 7047180a..33b947af 100644 --- a/extras/tiopf/gui/tiMediators.pas +++ b/extras/tiopf/gui/tiMediators.pas @@ -14,6 +14,7 @@ uses tiObject ,Classes ,tiBaseMediator + ,fpg_base // for predefined colors ,fpg_main ,fpg_widget ,fpg_edit @@ -27,177 +28,152 @@ uses ; type - - { Base class to handle TfpgEdit controls } - TMediatorEditView = class(TMediatorView) + { Base class to handle TfpgWidget controls } + TtiControlMediatorView = class(TtiMediatorView) private + FViewColor: TfpgColor; + FViewHint: TfpgString; + FViewErrorColor: TfpgColor; FEditControl: TfpgEdit; + procedure SetViewErrorColor(const AValue: TfpgColor); protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent); override; + function GetCurrentControlColor: TfpgColor; virtual; procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; - procedure SetupGUIandObject; override; - procedure SetObjectUpdateMoment (const AValue: TObjectUpdateMoment); override; public constructor Create; override; - destructor Destroy; override; - property EditControl: TfpgEdit read FEditControl write FEditControl; + property ViewErrorColor: TfpgColor read FViewErrorColor write SetViewErrorColor; + procedure SetView(const AValue: TComponent); override; + function View: TfpgWidget; reintroduce; class function ComponentClass: TClass; override; end; - { TMediatorEditIntegerView } - TMediatorEditIntegerView = class(TMediatorView) + { Base class to handle TfpgBaseEdit controls (TfpgEdit, TfpgNumericEdit, ...) } + TtiBaseEditMediatorView = class(TtiControlMediatorView) private - FEditControl: TfpgEditInteger; + FControlReadOnlyColor: TfpgColor; + procedure SetControlReadOnlyColor(const AValue: TfpgColor); protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent); override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; - procedure SetObjectUpdateMoment (const AValue: TObjectUpdateMoment); override; + function GetCurrentControlColor: TfpgColor; override; + procedure SetupGUIandObject; override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; destructor Destroy; override; - property EditControl: TfpgEditInteger read FEditControl write FEditControl; + property ControlReadOnlyColor: TfpgColor read FControlReadOnlyColor write SetControlReadOnlyColor; + function View: TfpgBaseEdit; reintroduce; class function ComponentClass: TClass; override; end; - { TMediatorEditFloatView } - TMediatorEditFloatView = class(TMediatorView) - private - FEditControl: TfpgEditFloat; - protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent); override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; - procedure SetObjectUpdateMoment (const AValue: TObjectUpdateMoment); override; + { Base class to handle TfpgEdit controls } + TtiEditMediatorView = class(TtiBaseEditMediatorView) + public + function View: TfpgEdit; reintroduce; + class function ComponentClass: TClass; override; + end; + + + TtiEditIntegerMediatorView = class(TtiBaseEditMediatorView) public constructor Create; override; - destructor Destroy; override; - property EditControl: TfpgEditFloat read FEditControl write FEditControl; + function View: TfpgEditInteger; reintroduce; class function ComponentClass: TClass; override; end; - { TMediatorEditCurrencyView } - TMediatorEditCurrencyView = class(TMediatorView) - private - FEditControl: TfpgEditCurrency; - protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent); override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; - procedure SetObjectUpdateMoment (Const AValue : TObjectUpdateMoment); override; + TtiEditFloatMediatorView = class(TtiBaseEditMediatorView) public constructor Create; override; - destructor Destroy; override; - property EditControl: TfpgEditCurrency read FEditControl write FEditControl; + function View: TfpgEditFloat; reintroduce; + class function ComponentClass: TClass; override; + end; + + + TtiEditCurrencyMediatorView = class(TtiBaseEditMediatorView) + public + constructor Create; override; + function View: TfpgEditCurrency; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgCheckBox controls } - TMediatorCheckBoxView = class(TMediatorView) - private - FEditControl: TfpgCheckBox; + TtiCheckBoxMediatorView = class(TtiControlMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; + procedure DoObjectToGUI; override; + procedure DoGUIToObject; override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgCheckBox read FEditControl write FEditControl; + function View: TfpgCheckBox; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgLabel controls } - TMediatorStaticTextView = class(TMediatorView) - private - FEditControl: TfpgLabel; + TtiStaticTextMediatorView = class(TtiControlMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; procedure SetupGUIandObject; override; public constructor Create; override; - property EditControl: TfpgLabel read FEditControl write FEditControl; + function View: TfpgLabel; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgSpinEdit controls } - TMediatorSpinEditView = class(TMediatorView) - private - FEditControl: TfpgSpinEdit; + TtiSpinEditMediatorView = class(TtiControlMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; procedure SetupGUIandObject; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgSpinEdit read FEditControl write FEditControl; + function View: TfpgSpinEdit; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgSpinEditFloat controls } - TMediatorSpinEditFloatView = class(TMediatorView) - private - FEditControl: TfpgSpinEditFloat; + TtiSpinEditFloatMediatorView = class(TtiControlMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; procedure SetupGUIandObject; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgSpinEditFloat read FEditControl write FEditControl; + function View: TfpgSpinEditFloat; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgTrackBar controls } - TMediatorTrackBarView = class(TMediatorView) + TtiTrackBarMediatorView = class(TtiControlMediatorView) private - FEditControl: TfpgTrackBar; procedure DoTrackBarChanged(Sender: TObject; APosition: integer); protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; procedure SetupGUIandObject; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgTrackBar read FEditControl write FEditControl; + function View: TfpgTrackBar; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgComboBox controls } - TMediatorComboBoxView = class(TMediatorView) - private - FEditControl: TfpgComboBox; + TtiComboBoxMediatorView = class(TtiControlMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent); override; - procedure UpdateGuiValidStatus(pErrors: TtiObjectErrors); override; procedure DoObjectToGui; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; + procedure SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgComboBox read FEditControl write FEditControl; + function View: TfpgComboBox; reintroduce; class function ComponentClass: TClass; override; end; { Sets ItemIndex based on integer property } - TMediatorItemComboBoxView = class(TMediatorComboBoxView) + TtiItemComboBoxMediatorView = class(TtiComboBoxMediatorView) protected procedure DoGUIToObject; override; procedure DoObjectToGUI; override; @@ -206,8 +182,8 @@ type end; - { TComboBox observing a list and setting a Object property } - TMediatorDynamicComboBoxView = class(TMediatorComboBoxView) + { TfpgComboBox observing a list and setting a Object property } + TtiDynamicComboBoxMediatorView = class(TtiComboBoxMediatorView) private FDisplayFieldName: string; FExternalOnChange: TNotifyEvent; @@ -226,34 +202,24 @@ type { Base class to handle TfpgMemo controls } - TMediatorMemoView = class(TMediatorView) - private - FEditControl: TfpgMemo; + TtiMemoMediatorView = class(TtiBaseEditMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; procedure SetupGUIandObject; override; procedure DoObjectToGui; override; procedure DoGuiToObject; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; public - property EditControl: TfpgMemo read FEditControl write FEditControl; + function View: TfpgMemo; reintroduce; class function ComponentClass: TClass; override; end; { Base class to handle TfpgCalendarCombo controls } - TMediatorCalendarComboView = class(TMediatorView) - private - FEditControl: TfpgCalendarCombo; + TtiCalendarComboMediatorView = class(TtiBaseEditMediatorView) protected - function GetGUIControl: TComponent; override; - procedure SetGUIControl(const AValue: TComponent);override; procedure SetupGUIandObject; override; - procedure SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); override; public constructor Create; override; - property EditControl: TfpgCalendarCombo read FEditControl write FEditControl; + function View: TfpgCalendarCombo; reintroduce; class function ComponentClass: TClass; override; end; @@ -267,10 +233,15 @@ uses SysUtils ,TypInfo ,fpg_dialogs // for TfpgMessageDialog - ,fpg_base // for predefined colors ,tiGUIConstants // for error color ; +type + // Friend class to get access to protected methods + THackWidget = class(TfpgWidget); + THackBaseEdit = class(TfpgBaseEdit); + + const cErrorListHasNotBeenAssigned = 'List has not been assigned'; cErrorPropertyNotClass = 'Property is not a class type!'; @@ -280,332 +251,347 @@ const procedure RegisterFallBackMediators; begin - gMediatorManager.RegisterMediator(TMediatorEditView, TtiObject, [tkSString,tkAString,tkInteger,tkFloat]); - gMediatorManager.RegisterMediator(TMediatorEditIntegerView, TtiObject, [tkInteger]); - gMediatorManager.RegisterMediator(TMediatorEditFloatView, TtiObject, [tkFloat]); - gMediatorManager.RegisterMediator(TMediatorEditCurrencyView, TtiObject, [tkFloat]); - gMediatorManager.RegisterMediator(TMediatorCheckBoxView, TtiObject, [tkBool]); - gMediatorManager.RegisterMediator(TMediatorComboboxView, TtiObject, [tkSString,tkAString]); - gMediatorManager.RegisterMediator(TMediatorItemComboBoxView, TtiObject, [tkInteger, tkEnumeration]); - gMediatorManager.RegisterMediator(TMediatorDynamicComboBoxView, TtiObject, [tkClass]); - gMediatorManager.RegisterMediator(TMediatorStaticTextView, TtiObject); - gMediatorManager.RegisterMediator(TMediatorTrackBarView, TtiObject, [tkInteger]); - gMediatorManager.RegisterMediator(TMediatorMemoView, TtiObject, [tkSString,tkAString]); - gMediatorManager.RegisterMediator(TMediatorCalendarComboView, TtiObject, [tkFloat]); - gMediatorManager.RegisterMediator(TMediatorSpinEditView, TtiObject, [tkInteger]); - gMediatorManager.RegisterMediator(TMediatorSpinEditFloatView, TtiObject, [tkFloat]); + gMediatorManager.RegisterMediator(TtiControlMediatorView, TtiObject, [tkSString,tkAString,tkInteger,tkFloat]); + gMediatorManager.RegisterMediator(TtiEditIntegerMediatorView, TtiObject, [tkInteger]); + gMediatorManager.RegisterMediator(TtiEditFloatMediatorView, TtiObject, [tkFloat]); + gMediatorManager.RegisterMediator(TtiEditCurrencyMediatorView, TtiObject, [tkFloat]); + gMediatorManager.RegisterMediator(TtiCheckBoxMediatorView, TtiObject, [tkBool]); + gMediatorManager.RegisterMediator(TtiComboBoxMediatorView, TtiObject, [tkSString,tkAString]); + gMediatorManager.RegisterMediator(TtiItemComboBoxMediatorView, TtiObject, [tkInteger, tkEnumeration]); + gMediatorManager.RegisterMediator(TtiDynamicComboBoxMediatorView, TtiObject, [tkClass]); + gMediatorManager.RegisterMediator(TtiStaticTextMediatorView, TtiObject); + gMediatorManager.RegisterMediator(TtiTrackBarMediatorView, TtiObject, [tkInteger]); + gMediatorManager.RegisterMediator(TtiMemoMediatorView, TtiObject, [tkSString,tkAString]); + gMediatorManager.RegisterMediator(TtiCalendarComboMediatorView, TtiObject, [tkFloat]); + gMediatorManager.RegisterMediator(TtiSpinEditMediatorView, TtiObject, [tkInteger]); + gMediatorManager.RegisterMediator(TtiSpinEditFloatMediatorView, TtiObject, [tkFloat]); end; -{ TMediatorEditView } +{ TtiControlMediatorView } -function TMediatorEditView.GetGUIControl: TComponent; +procedure TtiControlMediatorView.SetViewErrorColor(const AValue: TfpgColor); begin - Result := FEditControl; + if AValue <> FViewErrorColor then + begin + FViewErrorColor := AValue; + TestIfValid; // Update view + end; end; -procedure TMediatorEditView.SetGUIControl(const AValue: TComponent); +function TtiControlMediatorView.GetCurrentControlColor: TfpgColor; begin - FEditControl := AValue as TfpgEdit; - inherited SetGUIControl(AValue); + Result := fpgColorToRGB(FViewColor); end; -procedure TMediatorEditView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); +procedure TtiControlMediatorView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); var oError: TtiObjectError; begin inherited UpdateGuiValidStatus(pErrors); - oError := pErrors.FindByErrorProperty(FieldName); + oError := pErrors.FindByErrorProperty(RootFieldName); if oError <> nil then begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; + View.BackgroundColor := ViewErrorColor; + View.Hint := oError.ErrorMessage; end else begin - EditControl.BackgroundColor := clBoxColor; - EditControl.Hint := ''; + View.BackgroundColor := GetCurrentControlColor; + View.Hint := FViewHint; end; end; -procedure TMediatorEditView.SetupGUIandObject; +constructor TtiControlMediatorView.Create; +begin + inherited Create; + FViewErrorColor := clError; +end; + +procedure TtiControlMediatorView.SetView(const AValue: TComponent); +var + LValue: TfpgWidget; +begin + Assert((AValue = nil) or (AValue is TfpgWidget), 'Expected TfpgWidget'); + LValue := AValue as TfpgWidget; + + if LValue <> View then + begin + // Restore state of previous view + if View <> nil then + begin + View.Hint := FViewHint; + View.BackgroundColor := FViewColor; + end; + + // Preserve state of new view + if Assigned(LValue) then + begin + FViewHint := LValue.Hint; + FViewColor := LValue.BackgroundColor; + end; + end; + + inherited SetView(AValue); +end; + +function TtiControlMediatorView.View: TfpgWidget; +begin + Result := TfpgWidget(inherited View); +end; + +class function TtiControlMediatorView.ComponentClass: TClass; +begin + Result := TfpgEdit; +end; + +{ TtiBaseEditMediatorView } + +procedure TtiBaseEditMediatorView.SetControlReadOnlyColor(const AValue: TfpgColor); +begin + if AValue <> FControlReadOnlyColor then + begin + FControlReadOnlyColor := AValue; + TestIfValid; // Update view + end; +end; + +function TtiBaseEditMediatorView.GetCurrentControlColor: TfpgColor; +begin + if THackBaseEdit(View).ReadOnly then + Result := fpgColorToRGB(ControlReadOnlyColor) + else + Result := inherited GetCurrentControlColor; +end; + +procedure TtiBaseEditMediatorView.SetupGUIandObject; var Mi, Ma: Integer; begin inherited SetupGUIandObject; - if Subject.GetFieldBounds(FieldName, Mi, Ma) and (Ma > 0) then - FEditControl.MaxLength := Ma; + if Subject.GetFieldBounds(FieldName,Mi,Ma) and (Ma>0) then + THackBaseEdit(View).MaxLength := Ma; end; -procedure TMediatorEditView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiBaseEditMediatorView.SetObjectUpdateMoment( + const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnchange,ouCustom] then - FeditControl.OnChange := @DoOnChange + THackBaseEdit(View).OnChange := @DoOnChange else - FeditControl.OnExit := @DoOnChange; + THackBaseEdit(View).OnExit := @DoOnChange; end; -constructor TMediatorEditView.Create; +constructor TtiBaseEditMediatorView.Create; begin inherited Create; - GuiFieldName := 'Text'; + FControlReadOnlyColor := clWindowBackground; + GUIFieldName := 'Text'; end; -destructor TMediatorEditView.Destroy; +destructor TtiBaseEditMediatorView.Destroy; begin - if Assigned(EditControl) and Assigned(EditControl.OnChange) then - EditControl.OnChange := nil; + if View <> nil then + begin + if Assigned(THackBaseEdit(View).OnChange) then + THackBaseEdit(View).OnChange := nil; + end; inherited Destroy; end; -class function TMediatorEditView.ComponentClass: TClass; +function TtiBaseEditMediatorView.View: TfpgBaseEdit; begin - Result := TfpgEdit; + Result := TfpgBaseEdit(inherited View); end; - -{ TMediatorSpinEditView} - -class function TMediatorSpinEditView.ComponentClass: TClass; +class function TtiBaseEditMediatorView.ComponentClass: TClass; begin - Result := TfpgSpinEdit; + Result := TfpgBaseEdit; end; -function TMediatorSpinEditView.GetGUIControl: TComponent; +{ TtiEditMediatorView } + +function TtiEditMediatorView.View: TfpgEdit; begin - Result := FEditControl; + Result := TfpgEdit(inherited View); end; -procedure TMediatorSpinEditView.SetGUIControl(const AValue: TComponent); +class function TtiEditMediatorView.ComponentClass: TClass; begin - FEditControl := AValue as TfpgSpinEdit; - inherited SetGUIControl(AValue); + Result := TfpgEdit; end; -procedure TMediatorSpinEditView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; -begin - inherited UpdateGuiValidStatus(pErrors); +{ TtiSpinEditMediatorView} - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clWindowBackground; - EditControl.Hint := ''; - end; +class function TtiSpinEditMediatorView.ComponentClass: TClass; +begin + Result := TfpgSpinEdit; end; -procedure TMediatorSpinEditView.SetupGUIandObject; +procedure TtiSpinEditMediatorView.SetupGUIandObject; var Mi, Ma: Integer; begin inherited SetupGUIandObject; if Subject.GetFieldBounds(FieldName, Mi, Ma) then begin - FEditControl.MinValue := Mi; - FEditControl.MaxValue := Ma; + View.MinValue := Mi; + View.MaxValue := Ma; end; end; -procedure TMediatorSpinEditView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiSpinEditMediatorView.SetObjectUpdateMoment( + const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange + View.OnChange := @DoOnChange else - FEditControl.OnExit := @DoOnChange; + View.OnExit := @DoOnChange; end; -constructor TMediatorSpinEditView.Create; +constructor TtiSpinEditMediatorView.Create; begin inherited Create; GuiFieldName := 'Value'; end; - -{ TMediatorTrackBarView} - -function TMediatorTrackBarView.GetGUIControl: TComponent; +function TtiSpinEditMediatorView.View: TfpgSpinEdit; begin - Result := FEditControl; + Result := TfpgSpinEdit(inherited View); end; -procedure TMediatorTrackBarView.SetGUIControl(const AValue: TComponent); -begin - FEditControl := AValue as TfpgTrackBar; - inherited SetGUIControl(AValue); -end; -procedure TMediatorTrackBarView.DoTrackBarChanged(Sender: TObject; APosition: integer); +{ TtiTrackBarMediatorView} + +procedure TtiTrackBarMediatorView.DoTrackBarChanged(Sender: TObject; APosition: integer); begin GUIChanged; end; -procedure TMediatorTrackBarView.SetupGUIandObject; +procedure TtiTrackBarMediatorView.SetupGUIandObject; var Mi, Ma: Integer; begin inherited SetupGUIandObject; if Subject.GetFieldBounds(FieldName, Mi, Ma) then begin - FEditControl.Min := Mi; - FEditControl.Max := Ma; + View.Min := Mi; + View.Max := Ma; end; end; -procedure TMediatorTrackBarView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiTrackBarMediatorView.SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoTrackBarChanged + View.OnChange := @DoTrackBarChanged // TfpgTrackBar has a different event signature else - FeditControl.OnExit := @DoOnChange; + View.OnExit := @DoOnChange; end; -constructor TMediatorTrackBarView.Create; +constructor TtiTrackBarMediatorView.Create; begin inherited; GuiFieldName := 'Position'; end; -class function TMediatorTrackBarView.ComponentClass: TClass; +function TtiTrackBarMediatorView.View: TfpgTrackBar; begin - Result := TfpgTrackBar; + Result := TfpgTrackBar(inherited View); end; - -{ TMediatorComboBoxView } - -class function TMediatorComboBoxView.ComponentClass: TClass; +class function TtiTrackBarMediatorView.ComponentClass: TClass; begin - Result := TfpgComboBox; + Result := TfpgTrackBar; end; -function TMediatorComboBoxView.GetGUIControl: TComponent; -begin - Result := FEditControl; -end; -procedure TMediatorComboBoxView.SetGUIControl(const AValue: TComponent); +{ TtiComboBoxMediatorView } + +class function TtiComboBoxMediatorView.ComponentClass: TClass; begin - FEditControl := AValue as TfpgComboBox; - inherited SetGUIControl(AValue); + Result := TfpgComboBox; end; -procedure TMediatorComboBoxView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; +constructor TtiComboBoxMediatorView.Create; begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clBoxColor; - EditControl.Hint := ''; - end; + inherited Create; + GuiFieldName := 'Text'; end; -constructor TMediatorComboBoxView.Create; +function TtiComboBoxMediatorView.View: TfpgComboBox; begin - inherited Create; - GuiFieldName := 'Text'; //'FocusItem'; + Result := TfpgComboBox(inherited View); end; -procedure TMediatorComboBoxView.DoObjectToGui; +procedure TtiComboBoxMediatorView.DoObjectToGui; begin - EditControl.FocusItem := - EditControl.Items.IndexOf(Subject.PropValue[FieldName]); + View.FocusItem := View.Items.IndexOf(Subject.PropValue[FieldName]); end; -procedure TMediatorComboBoxView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiComboBoxMediatorView.SetObjectUpdateMoment(const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange + View.OnChange := @DoOnChange else - FEditControl.OnExit := @DoOnChange; + View.OnExit := @DoOnChange; end; -{ TMediatorMemoView } +{ TtiMemoMediatorView } -class function TMediatorMemoView.ComponentClass: TClass; +class function TtiMemoMediatorView.ComponentClass: TClass; begin Result := TfpgMemo; end; -procedure TMediatorMemoView.DoGuiToObject; -begin - Subject.PropValue[FieldName] := EditControl.Lines.Text; -end; - -procedure TMediatorMemoView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiMemoMediatorView.DoGuiToObject; begin - inherited SetObjectUpdateMoment(AValue); - if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange - else - FEditControl.OnExit := @DoOnChange; + Subject.PropValue[FieldName] := View.Lines.Text; end; -procedure TMediatorMemoView.DoObjectToGui; +function TtiMemoMediatorView.View: TfpgMemo; begin - EditControl.Lines.Text := Subject.PropValue[FieldName]; + Result := TfpgMemo(inherited View); end; -function TMediatorMemoView.GetGUIControl: TComponent; +procedure TtiMemoMediatorView.DoObjectToGui; begin - Result:=FEditControl; + View.Lines.Text := Subject.PropValue[FieldName]; end; -procedure TMediatorMemoView.SetGUIControl(const AValue: TComponent); -begin - FEditControl:=AValue as TfpgMemo; - inherited SetGUIControl(AValue); -end; - -procedure TMediatorMemoView.SetupGUIandObject; +procedure TtiMemoMediatorView.SetupGUIandObject; begin inherited SetupGUIandObject; - EditControl.Lines.Clear; + View.Lines.Clear; end; -{ TMediatorDynamicComboBoxView } +{ TtiDynamicComboBoxMediatorView } -procedure TMediatorDynamicComboBoxView.SetListObject(const AValue: TtiObjectList); +procedure TtiDynamicComboBoxMediatorView.SetListObject(const AValue: TtiObjectList); begin inherited; InternalListRefresh; if Assigned(ValueList) then - EditControl.Enabled := ValueList.Count > 0; + View.Enabled := ValueList.Count > 0; end; -procedure TMediatorDynamicComboBoxView.InternalListRefresh; +procedure TtiDynamicComboBoxMediatorView.InternalListRefresh; var lItems: TStrings; i: Integer; begin - lItems := EditControl.Items; + lItems := View.Items; lItems.Clear; - EditControl.Text := ''; + View.Text := ''; if (ValueList = nil) or (ValueList.Count < 1) or @@ -622,53 +608,53 @@ begin ObjectToGui; end; -function TMediatorDynamicComboBoxView.GetDisplayFieldName: string; +function TtiDynamicComboBoxMediatorView.GetDisplayFieldName: string; begin Result := FDisplayFieldName; if (Result = '') then Result := 'Caption'; // Do not localize. end; -procedure TMediatorDynamicComboBoxView.SetOnChangeActive(AValue: Boolean); +procedure TtiDynamicComboBoxMediatorView.SetOnChangeActive(AValue: Boolean); begin if AValue then begin if not UseInternalOnChange then - EditControl.OnChange := FExternalOnChange + View.OnChange := FExternalOnChange else - EditControl.OnChange := @DoOnChange; + View.OnChange := @DoOnChange; end else begin if not UseInternalOnChange then - FExternalOnChange := EditControl.OnChange; - EditControl.OnChange := nil; + FExternalOnChange := View.OnChange; + View.OnChange := nil; end; end; -procedure TMediatorDynamicComboBoxView.SetupGUIandObject; +procedure TtiDynamicComboBoxMediatorView.SetupGUIandObject; begin inherited SetupGUIandObject; if UseInternalOnChange then - EditControl.OnChange := @DoOnChange; // default OnChange event handler + View.OnChange := @DoOnChange; // default OnChange event handler {$Note As far as I can see, ValueList is always going to be nil here! - Graeme } if ValueList <> nil then - EditControl.Enabled := (ValueList.Count > 0); + View.Enabled := (ValueList.Count > 0); end; -procedure TMediatorDynamicComboBoxView.DoGuiToObject; +procedure TtiDynamicComboBoxMediatorView.DoGuiToObject; var lValue: TtiObject; lPropType: TTypeKind; begin if not DataAndPropertyValid then Exit; //==> - if EditControl.FocusItem < 0 then + if View.FocusItem < 0 then Exit; //==> - lValue := TtiObject(ValueList.Items[EditControl.FocusItem]); + lValue := TtiObject(ValueList.Items[View.FocusItem]); lPropType := typinfo.PropType(Subject, FieldName); if lPropType = tkClass then @@ -677,7 +663,7 @@ begin RaiseMediatorError(cErrorPropertyNotClass); end; -procedure TMediatorDynamicComboBoxView.DoObjectToGui; +procedure TtiDynamicComboBoxMediatorView.DoObjectToGui; var i: Integer; lValue: TtiObject; @@ -686,13 +672,14 @@ begin SetOnChangeActive(false); // Set the index only (We're assuming the item is present in the list) - EditControl.FocusItem := -1; + View.FocusItem := -1; if Subject = nil then Exit; //==> if not Assigned(ValueList) then RaiseMediatorError(cErrorListHasNotBeenAssigned); + lValue := nil; lPropType := typinfo.PropType(Subject, FieldName); if lPropType = tkClass then lValue := TtiObject(typinfo.GetObjectProp(Subject, FieldName)) @@ -702,413 +689,230 @@ begin for i := 0 to ValueList.Count - 1 do if ValueList.Items[i] = lValue then begin - EditControl.FocusItem := i; + View.FocusItem := i; Break; //==> end; SetOnChangeActive(true); end; -procedure TMediatorDynamicComboBoxView.RefreshList; +procedure TtiDynamicComboBoxMediatorView.RefreshList; begin InternalListRefresh; end; -{ TMediatorCheckBoxView } +{ TtiCheckBoxMediatorView } -function TMediatorCheckBoxView.GetGUIControl: TComponent; +procedure TtiCheckBoxMediatorView.DoObjectToGUI; begin - Result:=FEditControl; + inherited DoObjectToGUI; + View.Checked := (Subject.PropValue[FieldName] = 'True'); end; -procedure TMediatorCheckBoxView.SetGUIControl(const AValue: TComponent); +procedure TtiCheckBoxMediatorView.DoGUIToObject; begin - FEditControl:=AValue as TfpgCheckBox; - inherited SetGUIControl(AValue); + inherited DoGUIToObject; + Subject.PropValue[FieldName] := View.Checked end; -procedure TMediatorCheckBoxView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; -begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clWindowBackground; - EditControl.Hint := ''; - end; -end; - -procedure TMediatorCheckBoxView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiCheckBoxMediatorView.SetObjectUpdateMoment( + const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange + View.OnChange := @DoOnChange else - FEditControl.OnExit := @DoOnChange; + View.OnExit := @DoOnChange; end; -constructor TMediatorCheckBoxView.Create; +constructor TtiCheckBoxMediatorView.Create; begin inherited Create; GuiFieldName:='Checked'; end; -class function TMediatorCheckBoxView.ComponentClass: TClass; +function TtiCheckBoxMediatorView.View: TfpgCheckBox; +begin + Result := TfpgCheckBox(inherited View); +end; + +class function TtiCheckBoxMediatorView.ComponentClass: TClass; begin Result := TfpgCheckBox; end; -{ TMediatorStaticTextView } +{ TtiStaticTextMediatorView } -procedure TMediatorStaticTextView.SetupGUIandObject; +procedure TtiStaticTextMediatorView.SetupGUIandObject; begin inherited SetupGUIandObject; - EditControl.Text := ''; + View.Text := ''; end; -constructor TMediatorStaticTextView.Create; +constructor TtiStaticTextMediatorView.Create; begin inherited Create; GuiFieldName := 'Text'; end; -function TMediatorStaticTextView.GetGUIControl: TComponent; -begin - Result := FEditControl; -end; - -procedure TMediatorStaticTextView.SetGUIControl(const AValue: TComponent); +function TtiStaticTextMediatorView.View: TfpgLabel; begin - FEditControl := AValue as TfpgLabel; - inherited SetGUIControl(AValue); + Result := TfpgLabel(inherited View); end; -class function TMediatorStaticTextView.ComponentClass: TClass; +class function TtiStaticTextMediatorView.ComponentClass: TClass; begin Result := TfpgLabel; end; -{ TMediatorCalendarComboView } - -function TMediatorCalendarComboView.GetGUIControl: TComponent; -begin - Result := FEditControl; -end; - -procedure TMediatorCalendarComboView.SetGUIControl(const AValue: TComponent); -begin - FEditControl := AValue as TfpgCalendarCombo; - inherited SetGUIControl(AValue); -end; +{ TtiCalendarComboMediatorView } -procedure TMediatorCalendarComboView.SetupGUIandObject; +procedure TtiCalendarComboMediatorView.SetupGUIandObject; var Mi, Ma: TDateTime; begin - inherited SetupGUIandObject; +// inherited SetupGUIandObject; if Subject.GetFieldBounds(FieldName, Mi, Ma) then begin - FEditControl.MinDate := Mi; - FEditControl.MaxDate := Ma; + View.MinDate := Mi; + View.MaxDate := Ma; end; end; -procedure TMediatorCalendarComboView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +constructor TtiCalendarComboMediatorView.Create; begin - inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then - if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange - else - FEditControl.OnExit := @DoOnChange; + inherited Create; + GUIFieldName := 'DateValue'; end; -constructor TMediatorCalendarComboView.Create; +function TtiCalendarComboMediatorView.View: TfpgCalendarCombo; begin - inherited Create; - GUIFieldName := 'DateValue'; + Result := TfpgCalendarCombo(inherited View); end; -class function TMediatorCalendarComboView.ComponentClass: TClass; +class function TtiCalendarComboMediatorView.ComponentClass: TClass; begin Result := TfpgCalendarCombo; end; -{ TMediatorItemComboBoxView } +{ TtiItemComboBoxMediatorView } -procedure TMediatorItemComboBoxView.DoGUIToObject; +procedure TtiItemComboBoxMediatorView.DoGUIToObject; begin - SetOrdProp(Subject,FieldName,EditControl.FocusItem); + SetOrdProp(Subject, FieldName, View.FocusItem); end; -procedure TMediatorItemComboBoxView.DoObjectToGUI; +procedure TtiItemComboBoxMediatorView.DoObjectToGUI; begin - EditCOntrol.FocusItem := GetOrdProp(Subject,FieldName); + View.FocusItem := GetOrdProp(Subject, FieldName); end; -constructor TMediatorItemComboBoxView.Create; +constructor TtiItemComboBoxMediatorView.Create; begin inherited Create; GuiFieldName := 'FocusItem'; end; -{ TMediatorSpinEditFloatView } - -function TMediatorSpinEditFloatView.GetGUIControl: TComponent; -begin - Result := FEditControl; -end; - -procedure TMediatorSpinEditFloatView.SetGUIControl(const AValue: TComponent); -begin - FEditControl := AValue as TfpgSpinEditFloat; - inherited SetGUIControl(AValue); -end; +{ TtiSpinEditFloatMediatorView } -procedure TMediatorSpinEditFloatView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; -begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clWindowBackground; - EditControl.Hint := ''; - end; -end; - -procedure TMediatorSpinEditFloatView.SetupGUIandObject; +procedure TtiSpinEditFloatMediatorView.SetupGUIandObject; var Mi, Ma: Integer; begin inherited SetupGUIandObject; if Subject.GetFieldBounds(FieldName, Mi, Ma) then begin - FEditControl.MinValue := Mi; - FEditControl.MaxValue := Ma; + View.MinValue := Mi; + View.MaxValue := Ma; end; end; -procedure TMediatorSpinEditFloatView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); +procedure TtiSpinEditFloatMediatorView.SetObjectUpdateMoment( + const AValue: TtiObjectUpdateMoment); begin inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then + if View <> nil then if ObjectUpdateMoment in [ouOnChange,ouCustom] then - FEditControl.OnChange := @DoOnChange + View.OnChange := @DoOnChange else - FEditControl.OnExit := @DoOnChange; + View.OnExit := @DoOnChange; end; -constructor TMediatorSpinEditFloatView.Create; +constructor TtiSpinEditFloatMediatorView.Create; begin inherited Create; GuiFieldName := 'Value'; end; -class function TMediatorSpinEditFloatView.ComponentClass: TClass; -begin - Result := TfpgSpinEditFloat; -end; - -{ TMediatorEditIntegerView } - -function TMediatorEditIntegerView.GetGUIControl: TComponent; -begin - Result:= FEditControl; -end; - -procedure TMediatorEditIntegerView.SetGUIControl(const AValue: TComponent); +function TtiSpinEditFloatMediatorView.View: TfpgSpinEditFloat; begin - FEditControl := AValue as TfpgEditInteger; - inherited SetGUIControl(AValue); + Result := TfpgSpinEditFloat(inherited View); end; -procedure TMediatorEditIntegerView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; +class function TtiSpinEditFloatMediatorView.ComponentClass: TClass; begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clBoxColor; - EditControl.Hint := ''; - end; + Result := TfpgSpinEditFloat; end; -procedure TMediatorEditIntegerView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); -begin - inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then - if ObjectUpdateMoment in [ouOnchange,ouCustom] then - FEditControl.OnChange := @DoOnChange - else - FEditControl.OnExit := @DoOnChange; -end; +{ TtiEditIntegerMediatorView } -constructor TMediatorEditIntegerView.Create; +constructor TtiEditIntegerMediatorView.Create; begin inherited Create; GUIFieldName := 'Value'; end; -destructor TMediatorEditIntegerView.Destroy; +function TtiEditIntegerMediatorView.View: TfpgEditInteger; begin - if Assigned(EditControl) and Assigned(EditControl.OnChange) then - EditControl.OnChange := nil; - inherited Destroy; + Result := TfpgEditInteger(inherited View); end; -class function TMediatorEditIntegerView.ComponentClass: TClass; +class function TtiEditIntegerMediatorView.ComponentClass: TClass; begin Result := TfpgEditInteger; end; +{ TtiEditFloatMediatorView } -{ TMediatorEditFloatView } -function TMediatorEditFloatView.GetGUIControl: TComponent; -begin - Result:= FEditControl; -end; - -procedure TMediatorEditFloatView.SetGUIControl(const AValue: TComponent); -begin - FEditControl := AValue as TfpgEditFloat; - inherited SetGUIControl(AValue); -end; - -procedure TMediatorEditFloatView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; -begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clBoxColor; - EditControl.Hint := ''; - end; -end; - -procedure TMediatorEditFloatView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); -begin - inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then - If ObjectUpdateMoment in [ouOnchange,ouCustom] then - FEditControl.OnChange := @DoOnChange - else - FEditControl.OnExit := @DoOnChange; -end; - -constructor TMediatorEditFloatView.Create; +constructor TtiEditFloatMediatorView.Create; begin inherited Create; GUIFieldName := 'Value'; end; -destructor TMediatorEditFloatView.Destroy; +function TtiEditFloatMediatorView.View: TfpgEditFloat; begin - if Assigned(EditControl) and Assigned(EditControl.OnChange) then - EditControl.OnChange := nil; - inherited Destroy; + Result := TfpgEditFloat(inherited View); end; -class function TMediatorEditFloatView.ComponentClass: TClass; +class function TtiEditFloatMediatorView.ComponentClass: TClass; begin Result := TfpgEditFloat; end; -{ TMediatorEditCurrencyView } - -function TMediatorEditCurrencyView.GetGUIControl: TComponent; -begin - Result:= FEditControl; -end; +{ TtiEditCurrencyMediatorView } -procedure TMediatorEditCurrencyView.SetGUIControl(const AValue: TComponent); -begin - FEditControl := AValue as TfpgEditCurrency; - inherited SetGUIControl(AValue); -end; - -procedure TMediatorEditCurrencyView.UpdateGuiValidStatus(pErrors: TtiObjectErrors); -var - oError: TtiObjectError; -begin - inherited UpdateGuiValidStatus(pErrors); - - oError := pErrors.FindByErrorProperty(FieldName); - if oError <> nil then - begin - EditControl.BackgroundColor := clError; - EditControl.Hint := oError.ErrorMessage; - end - else - begin - EditControl.BackgroundColor := clBoxColor; - EditControl.Hint := ''; - end; -end; - -procedure TMediatorEditCurrencyView.SetObjectUpdateMoment(const AValue: TObjectUpdateMoment); -begin - inherited SetObjectUpdateMoment(AValue); - if Assigned(FEditControl) then - if ObjectUpdateMoment in [ouOnchange,ouCustom] then - FEditControl.OnChange := @DoOnChange - else - FEditControl.OnExit := @DoOnChange; -end; - -constructor TMediatorEditCurrencyView.Create; +constructor TtiEditCurrencyMediatorView.Create; begin inherited Create; GUIFieldName := 'Value'; end; -destructor TMediatorEditCurrencyView.Destroy; +function TtiEditCurrencyMediatorView.View: TfpgEditCurrency; begin - if Assigned(EditControl) and Assigned(EditControl.OnChange) then - EditControl.OnChange := nil; - inherited Destroy; + Result := TfpgEditCurrency(inherited View); end; -class function TMediatorEditCurrencyView.ComponentClass: TClass; +class function TtiEditCurrencyMediatorView.ComponentClass: TClass; begin Result := TfpgEditCurrency; end; + end. |