function Load-Templates { $flow.SuspendLayout() $flow.Controls.Clear() $selectionTracker = @{ LastSelectedListBox = $null } $templatePath = $settingsManager.Get("TemplatePath") if (-not (Test-Path -Path $templatePath)) { $statusBar.SetText("Status", "Template-Ordner nicht gefunden: $templatePath") $flow.ResumeLayout() return } try { $categories = @(Get-ChildItem -Path $templatePath -Directory -ErrorAction Stop | Where-Object { @(Get-ChildItem -Path $_.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue).Count -gt 0 }) } catch { $statusBar.SetText("Status", "Fehler beim Laden: $($_.Exception.Message)") [System.Windows.Forms.MessageBox]::Show( "Konnte Kategorien nicht laden: $($_.Exception.Message)", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) $flow.ResumeLayout() return } if ($categories.Count -eq 0) { $labelNoCategories = ( [LabelBuilder]::new("Keine Templates gefunden.", 24). SetDock('Top'). SetTextAlign([System.Drawing.ContentAlignment]::MiddleLeft). SetPadding(5, 0, 0, 0). Build() ) $flow.Controls.Add($labelNoCategories) $flow.ResumeLayout() return } foreach ($category in $categories) { $groupBoxWidth = [Math]::Max(($flow.ClientSize.Width - 20), 220) $groupBox = ( [GroupBoxBuilder]::new($category.Name). SetWidth($groupBoxWidth). SetHeight(90). SetMargin(4). SetPadding(10). AddTo($flow) ) $groupBox.Tag = $category.Name $files = @(Get-ChildItem -Path $category.FullName -Filter "*.psd1" -File -ErrorAction SilentlyContinue | Where-Object { $_.BaseName -ne "Default" -and $_.BaseName -notlike "*.Default" } | Sort-Object Name) $listBox = ( [ListBoxBuilder]::new(). SetDock('Fill'). AddSelectedIndexChangedHandler({ param($s, $e) if ($null -ne $selectionTracker.LastSelectedListBox -and $selectionTracker.LastSelectedListBox -ne $s) { $selectionTracker.LastSelectedListBox.ClearSelected() } $selectionTracker.LastSelectedListBox = $s $selectedItem = $s.SelectedItem if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) { $currentGroupBox = $s.Parent $statusBar.SetText("Status", "Ausgewählt: $($selectedItem.FullName) [Kategorie: $($currentGroupBox.Tag)]") try { if (-not (Get-Command -Name Get-TemplatePreviewData -ErrorAction SilentlyContinue)) { $previewFunctionPath = Join-Path -Path $settingsManager.Get("FunctionsPath") -ChildPath "Get-TemplatePreviewData.ps1" if (Test-Path -Path $previewFunctionPath -PathType Leaf) { . $previewFunctionPath } } if (-not (Get-Command -Name Get-TemplatePreviewData -ErrorAction SilentlyContinue)) { $templateData = Import-OrderedPowerShellDataFile -Path $selectedItem.FullName -ErrorAction Stop Show-Template -Template $templateData $statusBar.SetText("Status", "Vorschau ohne Default-Merge geladen: Get-TemplatePreviewData ist nicht verfügbar") return } $preview = Get-TemplatePreviewData -TemplatePath $selectedItem.FullName -ErrorAction Stop $previewData = if ($null -ne $preview.Display) { $preview.Display } elseif ($null -ne $preview.Resolved) { $preview.Resolved } else { $preview.Merged } Show-Template -Template $previewData Update-ParametersPanel -ConfigurationData $preview.Merged if ($null -ne $preview.ResolveError) { $statusBar.SetText("Status", "Vorschau gemerged, Resolve unvollständig: $($preview.ResolveError.Exception.Message)") } else { $statusBar.SetText("Status", "Vorschau geladen: $($preview.Files.Count) Datei(en) inklusive Defaults") } } catch { [System.Windows.Forms.MessageBox]::Show( "Datei konnte nicht geladen werden: $($_.Exception.Message)", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } }.GetNewClosure()). AddMouseDoubleClickHandler({ param($s, $e) $selectedItem = $s.SelectedItem if ($null -ne $selectedItem -and ($selectedItem -isnot [string])) { $fullPath = $selectedItem.FullName if (-not (Test-Path $fullPath)) { [System.Windows.Forms.MessageBox]::Show("Datei nicht gefunden: $fullPath", "Fehler", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) return } $newItem = New-Object PSObject -Property @{ BaseName = [System.IO.Path]::GetFileNameWithoutExtension($fullPath) FullName = $fullPath } if (-not ($selectionList.Items | Where-Object { $_.FullName -eq $newItem.FullName })) { $selectionList.Items.Add($newItem) | Out-Null $stateManager.SetState("hasSelection", $true) $stateManager.SetState("isMerged", $false) $stateManager.UpdateAllButtons() $statusBar.SetText("Status", "Zur Auswahl hinzugefügt: $fullPath") } else { $statusBar.SetText("Status", "Datei bereits in der Auswahl") } } }). AddTo($groupBox) ) if($files.Count -eq 0){ $listBox.Items.Add("Keine Templates vorhanden") | Out-Null }else{ foreach ($file in $files) { $item = New-Object PSObject -Property @{ BaseName = $file.BaseName FullName = $file.FullName } $listBox.Items.Add($item) | Out-Null } } } $flow.ResumeLayout() foreach ($groupBox in $flow.Controls) { $groupBox.Width = [Math]::Max(($flow.ClientSize.Width - 20), 220) } $form.Form.Invoke([Action] { $splitPanel.AutoSizeSplitterToPanel1Content() }) }