From dea2d0a67858676c4e152c566e71268c2e40c00f Mon Sep 17 00:00:00 2001 From: graemeg Date: Mon, 3 Nov 2008 08:10:53 +0000 Subject: * Minor fixes to tiListMediators and SelectedObject property. * Introduced easy access functions to selected Object in tiListMediators * Updated the Demo 21 AddressBook to work correctly with SelectedObject. --- .../demos/Demo_21_AdrsBook_MGM/frmcontactmaint.pas | 2 +- .../tiopf/demos/Demo_21_AdrsBook_MGM/frmmain.pas | 8 +--- extras/tiopf/gui/tiListMediators.pas | 50 +++++++++++++++------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmcontactmaint.pas b/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmcontactmaint.pas index b166c05e..5832af55 100644 --- a/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmcontactmaint.pas +++ b/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmcontactmaint.pas @@ -106,7 +106,7 @@ var obj: TAddress; begin obj := TAddress(TListViewMediator(FAdrsMediator.FindByComponent(lvAddresses).Mediator).SelectedObject); -// tiShowString(obj.AsDebugString); + tiShowString(obj.AsDebugString); if not Assigned(obj) then Exit; //==> diff --git a/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmmain.pas b/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmmain.pas index dd5029e5..374a971a 100644 --- a/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmmain.pas +++ b/extras/tiopf/demos/Demo_21_AdrsBook_MGM/frmmain.pas @@ -89,15 +89,13 @@ end; procedure TMainForm.miEditEditClick(Sender: TObject); var c: TContact; - rowmed: TStringGridRowMediator; begin if grdContacts.FocusRow < 0 then begin tiAppError('You need to select a Contact first'); Exit; end; - rowmed := TStringGridRowMediator(TStringGridMediator(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject); - c := TContact(rowmed.Model); + c := TContact(TStringGridMediator(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject); if not Assigned(c) then Exit; //==> @@ -111,15 +109,13 @@ end; procedure TMainForm.miEditDeleteClick(Sender: TObject); var c: TContact; - rowmed: TStringGridRowMediator; begin if grdContacts.FocusRow < 0 then begin tiAppError('You need to select a Contact first'); Exit; end; - rowmed := TStringGridRowMediator(TStringGridMediator(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject); - c := TContact(rowmed.Model); + c := TContact(TStringGridMediator(FMediator.FindByComponent(grdContacts).Mediator).SelectedObject); if tiAppConfirmation('Are you sure you want to delete <%s>', [c.FirstName + ' ' + c.LastName]) then begin diff --git a/extras/tiopf/gui/tiListMediators.pas b/extras/tiopf/gui/tiListMediators.pas index 92e8c93e..913bcd4b 100644 --- a/extras/tiopf/gui/tiListMediators.pas +++ b/extras/tiopf/gui/tiListMediators.pas @@ -42,6 +42,7 @@ type constructor Create; override; destructor Destroy; override; procedure HandleSelectionChanged; override; + function GetObjectFromItem(AItem: TfpgLVItem): TtiObject; published property View: TfpgListView read FView write SetView; end; @@ -70,6 +71,7 @@ type public constructor CreateCustom(AModel: TtiObjectList; AGrid: TfpgStringGrid; ADisplayNames: string; AIsObserving: Boolean = True); class function ComponentClass: TClass; override; + function GetObjectFromRow(ARow: Integer): TtiObject; published property View: TfpgStringGrid read FView write SetView; property SelectedObject: TtiObject read GetSelectedObject write SetSelectedObject; @@ -148,10 +150,7 @@ end; function TListViewMediator.GetSelectedObject: TtiObject; begin - if FView.ItemIndex = -1 then - Result := nil - else - Result := TtiObject(FView.Items.Item[FView.ItemIndex].UserData); + Result := GetObjectFromItem(FView.Items.Item[FView.ItemIndex]); end; procedure TListViewMediator.DoCreateItemMediator(AData: TtiObject; ARowIdx: integer); @@ -165,9 +164,9 @@ begin FView.BeginUpdate; try li := TfpgLVItem.Create(FView.Items); - li.UserData := AData; FView.Items.Add(li); m := TListViewListItemMediator.CreateCustom(AData, li, OnBeforeSetupField, FieldsInfo, Active); + li.UserData := m; MediatorList.Add(m); finally FView.EndUpdate; @@ -299,6 +298,14 @@ begin end; end; +function TListViewMediator.GetObjectFromItem(AItem: TfpgLVItem): TtiObject; +begin + if (AItem = nil) or (AItem.UserData = nil) then + Result := nil + else + Result := TListItemMediator(AItem.UserData).Model; +end; + { TListViewListItemMediator } @@ -377,16 +384,7 @@ end; function TStringGridMediator.GetSelectedObject: TtiObject; begin - if FView.RowCount = 0 then - begin - Result := nil; - Exit; - end; - - if FView.FocusRow = -1 then - Result := nil - else - Result := TtiObject(FView.Objects[0, FView.FocusRow]); + Result := GetObjectFromRow(FView.FocusRow); end; procedure TStringGridMediator.SetSelectedObject(const AValue: TtiObject); @@ -516,6 +514,28 @@ begin Result := TfpgStringGrid; end; +function TStringGridMediator.GetObjectFromRow(ARow: Integer): TtiObject; +var + O: TObject; +begin + if FView.RowCount = 0 then + begin + Result := nil; + Exit; + end; + + if FView.FocusRow = -1 then + Result := nil + else + begin + O := FView.Objects[0, ARow]; + if O <> nil then + Result := TListItemMediator(O).Model + else + Result := nil; + end; +end; + { TStringGridRowMediator } -- cgit v1.2.3-70-g09d2