AreaCommWindow now inherits from AdministrationWindow
This commit is contained in:
Elwig/Windows
@ -9,40 +9,56 @@ using System.Runtime.CompilerServices;
|
||||
using System;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public partial class AreaCommAdminWindow : Window {
|
||||
private readonly AppDbContext Context = new();
|
||||
public partial class AreaCommAdminWindow : AdministrationWindow {
|
||||
private readonly Member member;
|
||||
private bool IsEditing = false;
|
||||
private bool IsCreating = false;
|
||||
private IEnumerable<TextBox> TextBoxInputs = Array.Empty<TextBox>();
|
||||
private IEnumerable<ComboBox> ComboBoxInputs = Array.Empty<ComboBox>();
|
||||
private readonly Dictionary<Control, bool> Valid = new();
|
||||
private readonly Dictionary<Control, object?> OriginalValues = new();
|
||||
|
||||
public AreaCommAdminWindow(Member member) {
|
||||
InitializeComponent();
|
||||
this.member = member;
|
||||
this.Title = $"Flächenbindungen von {member.FamilyName} {member.GivenName} - Elwig";
|
||||
RefreshAreaCommitmentList();
|
||||
Title = $"Flächenbindungen - {member.FamilyName} {member.GivenName} - Elwig";
|
||||
RefreshContractList();
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e) {
|
||||
KgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
|
||||
RdInput.ItemsSource = Context.WbRde.OrderBy(r => r.Name).ToList();
|
||||
SortInput.ItemsSource = Context.WineVarieties.OrderBy(s => s.Name).ToList();
|
||||
AttrInput.ItemsSource = Context.WineAttributes.OrderBy(a => a.Name).ToList();
|
||||
CultInput.ItemsSource = Context.WineCultivations.OrderBy(c => c.Name).ToList();
|
||||
}
|
||||
|
||||
private void RefreshAreaCommitmentList() {
|
||||
Contract? contract = (Contract)ContractList.SelectedItem;
|
||||
|
||||
if (contract == null) {
|
||||
AreaCommitmentList.ItemsSource = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Context.AreaCommitments.Load();
|
||||
List<AreaCommitment> commitments = Context.AreaCommitments.Where(a => a.Contract.MgNr == member.MgNr).ToList();
|
||||
List<AreaCommitment> commitments = Context.AreaCommitments.Where(a => a.VNr == contract.VNr).ToList();
|
||||
|
||||
AreaCommitmentList.ItemsSource = commitments;
|
||||
if (commitments.Count == 1)
|
||||
AreaCommitmentList.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
private void RefreshContractList() {
|
||||
Context.Contracts.Load();
|
||||
List<Contract> contracts = Context.Contracts.Where(c => c.MgNr == member.MgNr).ToList();
|
||||
|
||||
ContractList.ItemsSource = contracts;
|
||||
if (contracts.Count == 1)
|
||||
ContractList.SelectedIndex = 0;
|
||||
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
|
||||
private void RefreshInputs(bool validate = false) {
|
||||
ClearInputStates();
|
||||
AreaCommitment a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
AreaCommitment? a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
if (a != null) {
|
||||
//EditMemberButton.IsEnabled = true;
|
||||
//DeleteMemberButton.IsEnabled = true;
|
||||
@ -59,42 +75,25 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
private void FillInputs(AreaCommitment a) {
|
||||
OriginalValues.Clear();
|
||||
ClearOriginalValues();
|
||||
|
||||
VNr.Text = a.VNr.ToString();
|
||||
MgNr.Text = a.Contract.MgNr.ToString();
|
||||
YearFrom.Text = a.Contract.YearFrom.ToString();
|
||||
YearTo.Text = a.Contract.YearTo.ToString();
|
||||
|
||||
KgInput.SelectedItem = a.Kg.Kg;
|
||||
RdInput.SelectedItem = a.Rd;
|
||||
GstNr.Text = a.GstNr;
|
||||
Area.Text = a.Area.ToString() + "m²";
|
||||
|
||||
SortInput.SelectedItem = a.WineVar;
|
||||
AttrInput.SelectedItem = a.WineAttr;
|
||||
CultInput.SelectedItem = a.WineCult;
|
||||
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private void FillOriginalValues() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
OriginalValues[tb] = tb.Text;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
OriginalValues[cb] = cb.SelectedItem;
|
||||
}
|
||||
|
||||
private void ClearInputStates() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
Utils.ClearInputState(tb);
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
Utils.ClearInputState(cb);
|
||||
}
|
||||
|
||||
private void ClearInputs() {
|
||||
OriginalValues.Clear();
|
||||
foreach (var tb in TextBoxInputs) {
|
||||
tb.Text = " ";
|
||||
tb.Text = "";
|
||||
}
|
||||
foreach (var cb in ComboBoxInputs) {
|
||||
cb.SelectedItem = null;
|
||||
if (cb.ItemsSource != null)
|
||||
Utils.SetInputInvalid(cb);
|
||||
}
|
||||
}
|
||||
|
||||
private void NewAreaCommitmentButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsCreating = true;
|
||||
AreaCommitmentList.IsEnabled = false;
|
||||
@ -123,7 +122,7 @@ namespace Elwig.Windows {
|
||||
|
||||
var r = MessageBox.Show(
|
||||
$"Soll die Flächenbindung \"{a.Kg.Kg.Name}, {a.Rd.Name}\", (MgNr. {a.GstNr}) wirklich unwiderruflich gelöscht werden?",
|
||||
"Mitglied löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
"Flächenbindung löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
if (r == MessageBoxResult.Yes) {
|
||||
Context.Remove(a);
|
||||
Context.SaveChanges();
|
||||
@ -131,13 +130,39 @@ namespace Elwig.Windows {
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveButton_Click(object sender, RoutedEventArgs evt) {
|
||||
private void NewContractButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsCreating = true;
|
||||
AreaCommitmentList.IsEnabled = false;
|
||||
AreaCommitmentList.SelectedItem = null;
|
||||
HideNewEditDeleteButtons();
|
||||
ShowSaveResetCancelButtons();
|
||||
UnlockInputs();
|
||||
InitInputs();
|
||||
}
|
||||
|
||||
private void DeleteContractButton_Click(object sender, RoutedEventArgs evt) {
|
||||
Contract c = (Contract)ContractList.SelectedItem;
|
||||
if (c == null) return;
|
||||
|
||||
var r = MessageBox.Show(
|
||||
$"Soll der Vertrag \"{c.VNr} und alle enthaltenen Flächenbindungen wirklich unwiderruflich gelöscht werden?",
|
||||
"Vertrag löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
|
||||
if (r == MessageBoxResult.Yes) {
|
||||
Context.Remove(c);
|
||||
Context.SaveChanges();
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
}
|
||||
|
||||
private void AreaCommitmentSaveButton_Click(object sender, RoutedEventArgs evt) {
|
||||
AreaCommitment? a = new();
|
||||
if (IsEditing)
|
||||
a = (AreaCommitment)AreaCommitmentList.SelectedItem;
|
||||
else if (IsCreating)
|
||||
a = new();
|
||||
|
||||
//TODO
|
||||
|
||||
a.KgNr = ((AT_Kg)KgInput.SelectedItem).KgNr;
|
||||
a.RdNr = ((WbRd)RdInput.SelectedItem).RdNr;
|
||||
a.GstNr = GstNr.Text;
|
||||
@ -163,7 +188,7 @@ namespace Elwig.Windows {
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
|
||||
private void ResetButton_Click(object sender, RoutedEventArgs evt) {
|
||||
private void AreaCommitmentResetButton_Click(object sender, RoutedEventArgs evt) {
|
||||
if (IsEditing) {
|
||||
RefreshInputs();
|
||||
} else if (IsCreating) {
|
||||
@ -172,7 +197,7 @@ namespace Elwig.Windows {
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs evt) {
|
||||
private void AreaCommitmentCancelButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
AreaCommitmentList.IsEnabled = true;
|
||||
@ -183,69 +208,48 @@ namespace Elwig.Windows {
|
||||
LockInputs();
|
||||
}
|
||||
|
||||
private void LockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = true;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = false;
|
||||
private void ContractSaveButton_Click(object sender, RoutedEventArgs evt) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
private void UnlockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = false;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = true;
|
||||
private void ContractCancelButton_Click(object sender, RoutedEventArgs evt) {
|
||||
IsEditing = false;
|
||||
IsCreating = false;
|
||||
ContractList.IsEnabled = true;
|
||||
HideSaveResetCancelButtons();
|
||||
ShowNewEditDeleteButtons();
|
||||
RefreshInputs();
|
||||
ClearInputStates();
|
||||
LockInputs();
|
||||
}
|
||||
|
||||
private void UpdateButtons() {
|
||||
override protected void UpdateButtons() {
|
||||
if (!IsEditing && !IsCreating) return;
|
||||
bool ch = HasChanged(), v = IsValid();
|
||||
ResetButton.IsEnabled = (ch);
|
||||
SaveButton.IsEnabled = (v && ch);
|
||||
AreaCommitmentResetButton.IsEnabled = (ch);
|
||||
AreaCommitmentSaveButton.IsEnabled = (v && ch);
|
||||
}
|
||||
|
||||
private void InitInputs() {
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private bool HasChanged() {
|
||||
return !IsValid() ||
|
||||
TextBoxInputs.Any(InputHasChanged) ||
|
||||
ComboBoxInputs.Any(InputHasChanged);
|
||||
}
|
||||
|
||||
private bool InputHasChanged(Control input) {
|
||||
if (!OriginalValues.ContainsKey(input)) {
|
||||
return false;
|
||||
} else if (input is TextBox tb) {
|
||||
return OriginalValues[tb]?.ToString() != tb.Text;
|
||||
} else if (input is ComboBox sb) {
|
||||
return OriginalValues[sb] != sb.SelectedItem;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsValid() {
|
||||
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
|
||||
}
|
||||
|
||||
private void ShowSaveResetCancelButtons() {
|
||||
SaveButton.IsEnabled = false;
|
||||
ResetButton.IsEnabled = false;
|
||||
CancelButton.IsEnabled = true;
|
||||
SaveButton.Visibility = Visibility.Visible;
|
||||
ResetButton.Visibility = Visibility.Visible;
|
||||
CancelButton.Visibility = Visibility.Visible;
|
||||
AreaCommitmentSaveButton.IsEnabled = false;
|
||||
AreaCommitmentResetButton.IsEnabled = false;
|
||||
AreaCommitmentCancelButton.IsEnabled = true;
|
||||
AreaCommitmentSaveButton.Visibility = Visibility.Visible;
|
||||
AreaCommitmentResetButton.Visibility = Visibility.Visible;
|
||||
AreaCommitmentCancelButton.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
private void HideSaveResetCancelButtons() {
|
||||
SaveButton.IsEnabled = false;
|
||||
ResetButton.IsEnabled = false;
|
||||
CancelButton.IsEnabled = false;
|
||||
SaveButton.Visibility = Visibility.Hidden;
|
||||
ResetButton.Visibility = Visibility.Hidden;
|
||||
CancelButton.Visibility = Visibility.Hidden;
|
||||
AreaCommitmentSaveButton.IsEnabled = false;
|
||||
AreaCommitmentResetButton.IsEnabled = false;
|
||||
AreaCommitmentCancelButton.IsEnabled = false;
|
||||
AreaCommitmentSaveButton.Visibility = Visibility.Hidden;
|
||||
AreaCommitmentResetButton.Visibility = Visibility.Hidden;
|
||||
AreaCommitmentCancelButton.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
private void ShowNewEditDeleteButtons() {
|
||||
@ -266,6 +270,10 @@ namespace Elwig.Windows {
|
||||
DeleteAreaCommitmentButton.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
private void ContractList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
RefreshAreaCommitmentList();
|
||||
}
|
||||
|
||||
private void AreaCommitmentList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
RefreshInputs();
|
||||
}
|
||||
|
Reference in New Issue
Block a user