unit ProductDetailScherm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.CheckLst, Subscherm, Navigator, ApplicationContext, ProductsAgent, ObserverPattern, CheckCombo; resourceString GEEN_DATA_CAPTION = 'Geen product geselecteerd'; NERGENS_DEEL_VAN_CAPTION = '--Nergens deel van--'; type TFormProductDetail = class(TFormSubscherm) LabelOmschrijving1: TLabel; LabelOmschrijving2: TLabel; Label1: TLabel; LabelProductNaam: TLabel; ListViewVerpakkingen: TListView; GroupBox1: TGroupBox; ButtonNieuw: TButton; ButtonVerwijder: TButton; LabelProductNummer: TLabel; Label17: TLabel; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ListViewVerpakkingenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); private FProductDetailsSubject: TSubject; FProductDetailsSubjectObserver: TSubjectObserver; FProductsAgent: TProductsAgent; LabelGeenData: TLabel; procedure UpdateGui(Sender: TObject); procedure UpdateControlsEnableStatus(); procedure UpdateGuiDataAanwezigheid(Aanwezig: Boolean); public Constructor Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductDetailsSubject: TSubject; ProductsAgent: TProductsAgent); end; implementation uses OptiServerService, Util, System.StrUtils, CustomPanelVerpakkingParameter; {$R *.dfm} Constructor TFormProductDetail.Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductDetailsSubject: TSubject; ProductsAgent: TProductsAgent); begin inherited Create(AOwner, Navigator, ApplicationContext); self.FProductDetailsSubject := ProductDetailsSubject; self.FProductDetailsSubjectObserver := TSubjectObserver.Create(self); self.FProductDetailsSubjectObserver.OnChange := UpdateGui; self.FProductsAgent := ProductsAgent; end; procedure TFormProductDetail.FormCreate(Sender: TObject); begin // Registreer bij observer FProductDetailsSubject.RegisterObserver(FProductDetailsSubjectObserver); end; procedure TFormProductDetail.FormDestroy(Sender: TObject); begin FProductDetailsSubject.UnregisterObserver(FProductDetailsSubjectObserver); end; procedure TFormProductDetail.ListViewVerpakkingenSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean); begin if Selected then begin ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking := TProductVerpakking(ListViewVerpakkingen.Selected.Data); ApplicationContext.ProductVerpakkingContext.NotifyChanged(); { TODO : Vervangen door notify changed in setter } end; end; procedure TFormProductDetail.UpdateGui(Sender: TObject); var GeselecteerdProduct: TProductInformatie; ErrorMessage: string; ProductVerpakking: TProductVerpakking; VorigeIndexInList: Integer; begin GeselecteerdProduct := ApplicationContext.ProductsContext.GeselecteerdProduct; if GeselecteerdProduct <> nil then begin // Product informatie ophalen FProductsAgent.ZoekProductVerpakkingenMetProductNr(GeselecteerdProduct.ProductNr, self.ApplicationContext.UserContext, self.ApplicationContext.ProductVerpakkingContext); if self.ApplicationContext.ProductVerpakkingContext.IsOK then // Gelukt Navigator.NavigeerNaar(NAVTOONPRODUCTVERPAKKING) else begin // Opzoeking gefaald ErrorMessage := self.ApplicationContext.ProductVerpakkingContext.ErrorMessage; if self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage <> '' then begin ErrorMessage := ErrorMessage + sLineBreak + self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage; end; MessageDlg(ErrorMessage, TMsgDlgType.mtInformation, [mbOK], 0, mbCancel); exit; end; end; if (GeselecteerdProduct = nil) or (ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen.Count = 0) then begin UpdateGuiDataAanwezigheid(False); exit; end else UpdateGuiDataAanwezigheid(True); UpdateControlsEnableStatus(); // Update met data uit ApplicationContext LabelOmschrijving1.Caption := GeselecteerdProduct.Omschrijving1; LabelOmschrijving2.Caption := GeselecteerdProduct.Omschrijving2; LabelProductNaam.Caption := GeselecteerdProduct.Kol1; LabelProductNummer.Caption := intToStr(GeselecteerdProduct.ProductNr); VorigeIndexInList := ListViewVerpakkingen.ItemIndex; // List view (grid) ListViewVerpakkingen.Items.BeginUpdate; ListViewVerpakkingen.Clear(); for ProductVerpakking in ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen do begin With ListViewVerpakkingen.Items.Add Do begin Data := ProductVerpakking; // Caption := IntToStr(Product.ProductNr); // Deze casting geeft geen access violation Caption := intToStr(ProductVerpakking.CutOrder); SubItems.Add(ProductVerpakking.MetaCaptionDisplay); SubItems.Add(ProductVerpakking.Breedte + ' x ' + ProductVerpakking.Diepte + ' x ' + ProductVerpakking.Hoogte); SubItems.Add(ProductVerpakking.Aantal); SubItems.Add(ProductVerpakking.MaxCombinAantal); if ProductVerpakking.DeelVan <> nil then SubItems.Add(ProductVerpakking.DeelVanCaption) else SubItems.Add(''); SubItems.Add(ProductVerpakking.ID); end; end; ListViewVerpakkingen.Items.EndUpdate; // Kijk of dezelfde verpakking als voor de update opnieuw geselecteerd kan worden if (VorigeIndexInList >= 0) and (VorigeIndexInList < ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen.Count) then ProductVerpakking := ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen[VorigeIndexInList] else if ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen.Count > 0 then // Default eerste verpakking selecteren ProductVerpakking := ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen[0] else ProductVerpakking := nil; // Geen verpakking aanwezig ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking := ProductVerpakking; end; procedure TFormProductDetail.UpdateControlsEnableStatus(); var VerpakkingAanwezigEnGeselecteerd: Boolean; begin // Details controls enable status aanpassen naargelang er een verpakking geselecteerd is. VerpakkingAanwezigEnGeselecteerd := (ListViewVerpakkingen.Items.Count > 0) and (ListViewVerpakkingen.ItemIndex >= 0); ButtonVerwijder.Enabled := VerpakkingAanwezigEnGeselecteerd; end; procedure TFormProductDetail.UpdateGuiDataAanwezigheid(Aanwezig: Boolean); var I: Integer; begin if Aanwezig then begin // Skip update als alles al zichtbaar staat. if Assigned(LabelGeenData) = False then exit; // Wel resultaten, toon alles. for I := 0 to self.ControlCount - 1 do if Assigned(self.Controls[I]) then self.Controls[I].Visible := True; LabelGeenData.Visible := False; FreeAndNil(LabelGeenData); end else begin // Skip update als melding al getoond wordt en alles onzichtbaar staat. if Assigned(LabelGeenData) then exit; // Geen resultaten, verberg kinderen, toon melding (label) en stop. for I := 0 to self.ControlCount - 1 do if Assigned(self.Controls[I]) then self.Controls[I].Visible := False; // Label tonen LabelGeenData := TLabel.Create(self); with LabelGeenData do begin Parent := self; Caption := GEEN_DATA_CAPTION; Font.Size := 15; // Centreren: https://stackoverflow.com/questions/18545937/make-a-component-appear-in-the-middle-of-form-no-matter-what-screen-resolution Left := (self.Width - Width) div 2; Top := (self.Height - Height) div 2; Anchors := []; // Relatief tegenover parent end; end end; end.