223 lines
7.4 KiB
C#
223 lines
7.4 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.Media;
|
|
using Project_Periodensystem.Controller;
|
|
using Project_Periodensystem.Model;
|
|
using Project_Periodensystem.View.Converters;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace Project_Periodensystem.View
|
|
{
|
|
/// <summary>
|
|
/// Code-Behind für das Periodensystem - nur UI-Logik, keine Geschäftslogik
|
|
/// </summary>
|
|
public partial class PeriodicTablePage : UserControl
|
|
{
|
|
private readonly Grid? periodicGrid;
|
|
private readonly PeriodensystemController _controller;
|
|
|
|
/// <summary>
|
|
/// Konstruktor - initialisiert UI und Controller
|
|
/// </summary>
|
|
public PeriodicTablePage()
|
|
{
|
|
InitializeComponent();
|
|
|
|
// Controller initialisieren (MVC-Pattern)
|
|
_controller = new PeriodensystemController();
|
|
|
|
// Grid-Referenz für Element-Buttons
|
|
periodicGrid = this.FindControl<Grid>("PeriodicGrid");
|
|
|
|
// Elemente laden und anzeigen
|
|
LoadAndDisplayElements();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Lädt Elemente über Controller und zeigt sie an
|
|
/// </summary>
|
|
private void LoadAndDisplayElements()
|
|
{
|
|
try
|
|
{
|
|
// Daten über Controller laden (nicht direkt!)
|
|
var elements = _controller.GetAllElements();
|
|
|
|
if (!elements.Any())
|
|
{
|
|
Logger.Log("Keine Elemente vom Controller erhalten");
|
|
return;
|
|
}
|
|
|
|
// Datenvalidierung über Controller
|
|
if (!_controller.ValidateData())
|
|
{
|
|
Logger.Log("Datenvalidierung fehlgeschlagen");
|
|
return;
|
|
}
|
|
|
|
Logger.Log($"Lade {elements.Count} Elemente in das Grid");
|
|
|
|
// UI-Update für jedes Element
|
|
int successCount = 0;
|
|
foreach (var element in elements)
|
|
{
|
|
if (_controller.ValidateElementPosition(element))
|
|
{
|
|
CreateElementButton(element);
|
|
successCount++;
|
|
}
|
|
else
|
|
{
|
|
Logger.Log($"Element {element?.Symbol ?? "NULL"} übersprungen - ungültige Position");
|
|
}
|
|
}
|
|
|
|
Logger.Log($"{successCount} von {elements.Count} Elementen erfolgreich geladen");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"EXCEPTION in LoadAndDisplayElements: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellt UI-Button für ein Element (nur View-Logik)
|
|
/// </summary>
|
|
/// <param name="element">Element für das der Button erstellt wird</param>
|
|
private void CreateElementButton(Element element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
Logger.Log("Null-Element kann nicht angezeigt werden");
|
|
return;
|
|
}
|
|
|
|
// UI-Komponenten erstellen
|
|
var button = new Button { Classes = { "ElementTile" } };
|
|
var panel = new StackPanel();
|
|
|
|
// Hintergrundfarbe über Converter setzen
|
|
var backgroundColor = new SeriesToColorConverter()
|
|
.Convert(element.Series, typeof(Brush), null, CultureInfo.InvariantCulture) as Brush;
|
|
|
|
if (backgroundColor != null)
|
|
{
|
|
button.Background = backgroundColor;
|
|
}
|
|
|
|
// Text-Elemente erstellen
|
|
var symbolText = new TextBlock
|
|
{
|
|
Text = element.Symbol,
|
|
Classes = { "Symbol" },
|
|
Foreground = new SolidColorBrush(Colors.White),
|
|
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center
|
|
};
|
|
|
|
var numberText = new TextBlock
|
|
{
|
|
Text = element.AtomicNumber.ToString(),
|
|
Classes = { "Number" },
|
|
Foreground = new SolidColorBrush(Colors.White),
|
|
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center
|
|
};
|
|
|
|
// Layout zusammenbauen
|
|
panel.Children.Add(numberText); // Number on top
|
|
panel.Children.Add(symbolText); // Symbol below
|
|
button.Content = panel;
|
|
|
|
// Grid-Position setzen (0-basiert)
|
|
int gridRow = element.Row;
|
|
int gridColumn = element.Column;
|
|
|
|
Logger.Log($"Element {element.Symbol}: Grid({gridRow},{gridColumn})");
|
|
|
|
Grid.SetRow(button, gridRow);
|
|
Grid.SetColumn(button, gridColumn);
|
|
|
|
// Button zum Grid hinzufügen
|
|
if (periodicGrid != null)
|
|
{
|
|
periodicGrid.Children.Add(button);
|
|
}
|
|
else
|
|
{
|
|
Logger.Log("PeriodicGrid ist null - Button kann nicht hinzugefügt werden");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event-Handler für Theme-Button
|
|
/// </summary>
|
|
private void ThemeButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// Theme-Wechsel über Application
|
|
var app = Application.Current;
|
|
if (app != null)
|
|
{
|
|
var currentTheme = app.ActualThemeVariant;
|
|
app.RequestedThemeVariant = currentTheme == Avalonia.Styling.ThemeVariant.Dark
|
|
? Avalonia.Styling.ThemeVariant.Light
|
|
: Avalonia.Styling.ThemeVariant.Dark;
|
|
|
|
Logger.Log($"Theme gewechselt zu: {app.RequestedThemeVariant}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"Fehler beim Theme-Wechsel: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event-Handler für About-Button
|
|
/// </summary>
|
|
private void AboutButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// Navigation zur About-Seite
|
|
var mainWindow = TopLevel.GetTopLevel(this) as Window;
|
|
if (mainWindow != null)
|
|
{
|
|
var aboutPage = new AboutPage();
|
|
mainWindow.Content = aboutPage;
|
|
Logger.Log("Navigation zur About-Seite");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"Fehler bei Navigation zu About: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event-Handler für Zurück-Button (falls gewünscht)
|
|
/// </summary>
|
|
private void BackButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
var mainWindow = TopLevel.GetTopLevel(this) as Window;
|
|
if (mainWindow != null)
|
|
{
|
|
var landingPage = new LandingPage();
|
|
mainWindow.Content = landingPage;
|
|
Logger.Log("Navigation zurück zur Landing Page");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Log($"Fehler bei Navigation zurück: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|