unit VerpakkingDetailScherm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.CheckLst, System.Generics.Collections, Subscherm, Navigator, ProductsAgent, ApplicationContext, ObserverPattern, CheckCombo; Resourcestring NERGENS_DEEL_VAN_CAPTION = '--Nergens deel van--'; GEEN_DATA_CAPTION = 'Geen verpakking geselecteerd'; type TFormVerpakkingDetail = class(TFormSubscherm) PanelVerpakkingControls: TPanel; LabelParameters: TLabel; Label11: TLabel; Label12: TLabel; Label13: TLabel; Label2: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; Label3: TLabel; Label14: TLabel; ButtonMeta: TButton; ComboBoxDeelVan: TComboBox; ComboBoxPlaatsing: TComboBox; ComboBoxPositie: TComboBox; ComboBoxRichting: TComboBox; EditAantal: TEdit; EditBreedte: TEdit; EditDiepte: TEdit; EditHoogte: TEdit; EditMaxCombAantal: TEdit; EditMeta: TEdit; EditVolumeVan: TEdit; ComboBoxOptiType: TComboBox; ScrollBoxParameters: TScrollBox; ButtonOpslaan: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure ButtonOpslaanClick(Sender: TObject); procedure ButtonMetaClick(Sender: TObject); private FProductsAgent: TProductsAgent; FProductVerpakkingSubject: TSubject; FProductVerpakkingSubjectObserver: TSubjectObserver; CheckComboBoxPositie: TCheckedComboBox; CheckComboBoxRichting: TCheckedComboBox; LabelGeenData: TLabel; procedure LaadCheckedComboBoxen(); procedure UpdateGui(Sender: TObject); procedure ToonProductVerpakking(ProductVerpakking: TProductVerpakking); procedure RegistreerWijzigingenVerpakking(ProductVerpakking: TProductVerpakking); procedure UpdateControlsEnableStatus(); procedure LaadEnSelecteerWaardenComboBox(Waarden: TDictionary; ComboBox: TComboBox; GeselecteerdeWaarde: string; DefaultGeselecteerdeWaarde: string; Vertaling: TDictionary); procedure LaadEnSelecteerWaardenCheckComboBox(Waarden: TDictionary; CheckedComboBox: TCheckedComboBox; GeselecteerdeWaarden: TList; DefaultGeselecteerdeWaarden: TList; Vertaling: TDictionary); public Constructor Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductsAgent: TProductsAgent; ProductVerpakkingSubject: TSubject); end; implementation {$R *.dfm} uses Util, System.StrUtils, CustomPanelVerpakkingParameter; Constructor TFormVerpakkingDetail.Create(AOwner: TComponent; Navigator: INavigator; ApplicationContext: TApplicationContext; ProductsAgent: TProductsAgent; ProductVerpakkingSubject: TSubject); begin inherited Create(AOwner, Navigator, ApplicationContext); FProductsAgent := ProductsAgent; // Observer self.FProductVerpakkingSubject := ProductVerpakkingSubject; self.FProductVerpakkingSubjectObserver := TSubjectObserver.Create(self); self.FProductVerpakkingSubjectObserver.OnChange := UpdateGui; end; procedure TFormVerpakkingDetail.FormCreate(Sender: TObject); begin LaadCheckedComboBoxen(); // Registreer bij observer FProductVerpakkingSubject.RegisterObserver(FProductVerpakkingSubjectObserver); end; procedure TFormVerpakkingDetail.FormDestroy(Sender: TObject); begin FProductVerpakkingSubject.UnregisterObserver(FProductVerpakkingSubjectObserver); end; procedure TFormVerpakkingDetail.LaadCheckedComboBoxen(); begin // Vervang sommige comboboxen door TCheckedComboBox CheckComboBoxPositie := TCheckedComboBox.Create(self); with CheckComboBoxPositie do CheckComboBoxPositie.Delimiter := ';'; TUtil.VervangControl(ComboBoxPositie, CheckComboBoxPositie); CheckComboBoxRichting := TCheckedComboBox.Create(self); with CheckComboBoxRichting do CheckComboBoxRichting.Delimiter := ';'; TUtil.VervangControl(ComboBoxRichting, CheckComboBoxRichting); end; procedure TFormVerpakkingDetail.ButtonMetaClick(Sender: TObject); begin self.Navigator.NavigeerNaar(NAVTOONPRODUCTVERPAKKINGMETA); end; procedure TFormVerpakkingDetail.ButtonOpslaanClick(Sender: TObject); var ErrorMessage: string; begin // Wijzigingen van GUI overbrengen naar instantie RegistreerWijzigingenVerpakking(ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking); // Wijzigingen opslaan FProductsAgent.OpslaanGewijzigdeProductVerpakking(ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking, ApplicationContext.UserContext, ApplicationContext.ProductVerpakkingContext); if self.ApplicationContext.ProductVerpakkingContext.IsOK = false then begin // Opzoeking gefaald ErrorMessage := self.ApplicationContext.ProductVerpakkingContext.ErrorMessage; if self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage <> '' then ErrorMessage := ErrorMessage + sLineBreak + self.ApplicationContext.ProductVerpakkingContext.InternalErrorMessage; MessageDlg(ErrorMessage, TMsgDlgType.mtInformation, [mbOK], 0, mbCancel); end; end; procedure TFormVerpakkingDetail.UpdateGui(Sender: TObject); begin // Toon de geselecteerde verpakking if ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking = nil then begin // Product niet aanwezig TUtil.ToonLabelOverScherm(self, LabelGeenData, GEEN_DATA_CAPTION, false); exit; end; // Product aanwezig ToonProductVerpakking(ApplicationContext.ProductVerpakkingContext.GeselecteerdeVerpakking); TUtil.ToonLabelOverScherm(self, LabelGeenData, GEEN_DATA_CAPTION, true); UpdateControlsEnableStatus(); end; procedure TFormVerpakkingDetail.LaadEnSelecteerWaardenComboBox(Waarden: TDictionary; ComboBox: TComboBox; GeselecteerdeWaarde: string; DefaultGeselecteerdeWaarde: string; Vertaling: TDictionary); var Item: string; begin ComboBox.Items.Clear(); // Vullen for Item in Waarden.keys do ComboBox.Items.AddObject(Vertaling[Item], TObject(Item)); // Items selecteren. Als waarde niet aanwezig is, uit default halen. Als dat niet gaat eerste item selecteren. if (GeselecteerdeWaarde <> '') and (Vertaling.TryGetValue(GeselecteerdeWaarde, Item) = true) then ComboBox.ItemIndex := ComboBox.Items.IndexOf(Item) else if (DefaultGeselecteerdeWaarde <> '') and (Vertaling.TryGetValue(DefaultGeselecteerdeWaarde, Item) = true) then ComboBox.ItemIndex := ComboBox.Items.IndexOf(Item) // Default else if ComboBox.Items.Count > 0 then ComboBox.ItemIndex := 0; // Geen waarde, geen default, eerste item selecteren. end; procedure TFormVerpakkingDetail.LaadEnSelecteerWaardenCheckComboBox(Waarden: TDictionary; CheckedComboBox: TCheckedComboBox; GeselecteerdeWaarden: TList; DefaultGeselecteerdeWaarden: TList; Vertaling: TDictionary); var Item: string; TempStr: string; begin CheckedComboBox.Items.Clear(); // CheckComboBox vullen. for Item in Waarden.keys do CheckedComboBox.Items.AddObject(Waarden[Item], TObject(Item)); // Items selecteren. Als waarde niet aanwezig is, uit default halen. if GeselecteerdeWaarden.Count > 0 then begin for Item in GeselecteerdeWaarden do if Vertaling.TryGetValue(Item, TempStr) = true then begin if CheckedComboBox.Items.IndexOf(TempStr) >= 0 then CheckedComboBox.Checked[CheckedComboBox.Items.IndexOf(TempStr)] := true; end; end else // Default waarden for Item in DefaultGeselecteerdeWaarden do if Vertaling.TryGetValue(Item, TempStr) = true then begin if CheckedComboBox.Items.IndexOf(TempStr) >= 0 then CheckedComboBox.Checked[CheckedComboBox.Items.IndexOf(TempStr)] := true; end; end; procedure TFormVerpakkingDetail.ToonProductVerpakking(ProductVerpakking: TProductVerpakking); var TempProductVerpakking: TProductVerpakking; Parameter: TProductVerpakkingParameter; NieuweParameterControl: TCustomPanelVerpakkingParameter; begin // Linkerhelft EditMeta.Text := ProductVerpakking.MetaCaptionDisplay; EditVolumeVan.Text := ProductVerpakking.VolumeVanCaption; // DeelVan ComboBoxDeelVan.Items.Clear(); // ComboBox vullen en item selecteren. for TempProductVerpakking in ApplicationContext.ProductVerpakkingContext.GetDeelVanOpties(ProductVerpakking) do ComboBoxDeelVan.Items.Add(TempProductVerpakking.DeelVanDisplay); ComboBoxDeelVan.Items.Add(NERGENS_DEEL_VAN_CAPTION); // Item selecteren if ProductVerpakking.DeelVan <> nil then // Index zoeken in lijst van producten ComboBoxDeelVan.ItemIndex := ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen.IndexOf (ProductVerpakking.DeelVan) else // Default waarde: 'niks' optie selecteren ComboBoxDeelVan.ItemIndex := ComboBoxDeelVan.Items.Count - 1; // Breedte, Diepte, Hoogte. // Default waarde altijd als placeholder instellen EditBreedte.TextHint := ProductVerpakking.DefaultBreedte; // Default if ProductVerpakking.Breedte <> ProductVerpakking.DefaultBreedte then EditBreedte.Text := ProductVerpakking.Breedte; EditDiepte.TextHint := ProductVerpakking.DefaultDiepte; // Default if ProductVerpakking.Diepte <> ProductVerpakking.DefaultDiepte then EditDiepte.Text := ProductVerpakking.Diepte; EditHoogte.TextHint := ProductVerpakking.DefaultHoogte; // Default if ProductVerpakking.Hoogte <> ProductVerpakking.DefaultHoogte then EditHoogte.Text := ProductVerpakking.Hoogte; // Max.Comb.Aantal EditMaxCombAantal.Text := ProductVerpakking.MaxCombinAantal; // Aantal EditAantal.Text := ProductVerpakking.Aantal; // Plaatsing LaadEnSelecteerWaardenComboBox(ApplicationContext.UiInstellingenContext.Plaatsingen, ComboBoxPlaatsing, ProductVerpakking.Plaatsing, ProductVerpakking.DefaultPlaatsing, ApplicationContext.UiInstellingenContext.Plaatsingen); // Positie LaadEnSelecteerWaardenCheckComboBox(ApplicationContext.UiInstellingenContext.Posities, CheckComboBoxPositie, ProductVerpakking.Posities, ProductVerpakking.DefaultPosities, ApplicationContext.UiInstellingenContext.Posities); // Richting LaadEnSelecteerWaardenCheckComboBox(ApplicationContext.UiInstellingenContext.Richtingen, CheckComboBoxRichting, ProductVerpakking.Richtingen, ProductVerpakking.DefaultRichtingen, ApplicationContext.UiInstellingenContext.Richtingen); // OptiType LaadEnSelecteerWaardenComboBox(ApplicationContext.UiInstellingenContext.OptiTypes, ComboBoxOptiType, ProductVerpakking.OptiType, ProductVerpakking.DefaultOptiType, ApplicationContext.UiInstellingenContext.OptiTypes); // Rechterhelft // Parameters TUtil.FreeControlChildren(ScrollBoxParameters); for Parameter in ProductVerpakking.Parameters do begin NieuweParameterControl := TCustomPanelVerpakkingParameter.Create(ScrollBoxParameters); with NieuweParameterControl do begin Parent := ScrollBoxParameters; ProductVerpakkingParameter := Parameter; end; end; end; procedure TFormVerpakkingDetail.RegistreerWijzigingenVerpakking(ProductVerpakking: TProductVerpakking); var I: Integer; ParameterControl: TCustomPanelVerpakkingParameter; VerpakkingParameters: TList; begin // Linkerhelft // DeelVan // Kijken waar geselecteerde index ligt. Indien het een geldige selectie is, object selecteren. if (ComboBoxDeelVan.ItemIndex > 0) and (ComboBoxDeelVan.ItemIndex < ComboBoxDeelVan.Items.Count - 1) then ProductVerpakking.DeelVan := ApplicationContext.ProductVerpakkingContext.ProductVerpakkingen[ComboBoxDeelVan.ItemIndex] else // Geen 'deel van' geselecteerd. ProductVerpakking.DeelVan := nil; // Breedte, Diepte, Hoogte. ProductVerpakking.Breedte := EditBreedte.Text; ProductVerpakking.Diepte := EditDiepte.Text; ProductVerpakking.Hoogte := EditHoogte.Text; // Max.Comb.Aantal ProductVerpakking.MaxCombinAantal := EditMaxCombAantal.Text; // Aantal ProductVerpakking.Aantal := EditAantal.Text; // Plaatsing, geslecteerde tekst vertalen naar code en instellen. ProductVerpakking.Plaatsing := ApplicationContext.UiInstellingenContext.PlaatsingByValue [ComboBoxPlaatsing.Items[ComboBoxPlaatsing.ItemIndex]]; // Positie ProductVerpakking.Posities.Clear(); for I := 0 to CheckComboBoxPositie.Items.Count - 1 do begin if CheckComboBoxPositie.Checked[I] then // Checked, registreren ProductVerpakking.Posities.Add(ApplicationContext.UiInstellingenContext.PositiesByValue[CheckComboBoxPositie.Items[I]]); end; // Richting ProductVerpakking.Richtingen.Clear(); for I := 0 to CheckComboBoxRichting.Items.Count - 1 do begin if CheckComboBoxRichting.Checked[I] then // Checked, registreren ProductVerpakking.Richtingen.Add(ApplicationContext.UiInstellingenContext.RichtingenByValue [CheckComboBoxRichting.Items[I]]); end; // OptiType, geslecteerde tekst vertalen naar code en instellen. ProductVerpakking.OptiType := ApplicationContext.UiInstellingenContext.OptiTypesByValue [ComboBoxOptiType.Items[ComboBoxOptiType.ItemIndex]]; // Rechterhelft // Parameters. De controls in de scrollbox overlopen, op zoek naar TCustomPanelVerpakkingParameter. // Eenmaal die gevonden forceren we het updaten van het object met de ingegeven waarde. // Deze geüpdate ProductVerpakkingParameter voegen we toe aan een temp lijst. // Deze lijst registreren we bij de verpakking. VerpakkingParameters := TList.Create(); for I := 0 to ScrollBoxParameters.ControlCount - 1 do if (ScrollBoxParameters.Controls[I] is TCustomPanelVerpakkingParameter) then begin ParameterControl := TCustomPanelVerpakkingParameter(ScrollBoxParameters.Controls[I]); ParameterControl.Opslaan(); VerpakkingParameters.Add(ParameterControl.ProductVerpakkingParameter); end; ProductVerpakking.SetParameters(VerpakkingParameters); end; procedure TFormVerpakkingDetail.UpdateControlsEnableStatus(); begin EditMeta.Enabled := false; EditVolumeVan.Enabled := false; ComboBoxDeelVan.Enabled := (ComboBoxDeelVan.Items.Count > 0); ComboBoxPlaatsing.Enabled := (ComboBoxPlaatsing.Items.Count > 0); ComboBoxPositie.Enabled := (ComboBoxPositie.Items.Count > 0); ComboBoxRichting.Enabled := (ComboBoxRichting.Items.Count > 0); ComboBoxOptiType.Enabled := (ComboBoxOptiType.Items.Count > 0); // Parameters ScrollBoxParameters.visible := (ScrollBoxParameters.ControlCount > 0); LabelParameters.visible := (ScrollBoxParameters.ControlCount > 0); end; end.