Files
BeWoPlaner/BeWo/ViewModel/ListViewModel/AbstractDCListMapperVM.cs

279 lines
7.3 KiB
C#
Raw Normal View History

2016-06-27 01:45:38 +02:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
2024-08-28 17:19:20 +02:00
using System.Windows;
2024-09-23 14:34:56 +02:00
using BS.Shared.DataContracts;
2016-06-27 01:45:38 +02:00
using BS.Shared.Extensions;
namespace BeWo.ViewModel.ListViewModel
{
public abstract class AbstractDCListMapperVM<DCType, VMType> : AbstractBaseVM where VMType : AbstractDCMapperVM<DCType> where DCType : new()
{
public static string PropertyName_EditVM = "EditVM";
public static string PropertyName_NewVM = "NewVM";
private readonly ObservableCollection<DCType> _DCBackup;
private VMType _EditVM;
private VMType _NewVM;
2024-08-30 13:07:53 +02:00
private VMType _SelectedVM;
2016-06-27 01:45:38 +02:00
private BindingList<VMType> _VMList;
//public AbstractDCListMapperVM(IEnumerable<AbstractDCListMapperVM<DCType, VMType>> listVMs, bool ignore)
//{
// var dcLists = listVMs.SelectMany(x => x.DCBackup);
// _DCBackup = dcLists != null ? dcLists.ToObservableCollection() : new ObservableCollection<DCType>();
// _DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast<DCType>().Select(CreateVM));
// var vmLists = listVMs.SelectMany(x => x.VMList).ToList();
// _VMList = new BindingList<VMType>(vmLists);
//}
2016-06-27 01:45:38 +02:00
public AbstractDCListMapperVM(IEnumerable<DCType> pDCList)
{
_DCBackup = pDCList != null ? pDCList.ToObservableCollection() : new ObservableCollection<DCType>();
2024-09-12 14:00:17 +02:00
_DCBackup.CollectionChanged += (s, e) => _VMList.AddRange(e.NewItems.Cast<DCType>().Select(CreateVM));
2016-06-27 01:45:38 +02:00
}
2024-09-12 14:00:17 +02:00
public AbstractDCListMapperVM() : this(null) { }
2016-06-27 01:45:38 +02:00
public event EventHandler SelectedVMChanged;
2016-06-27 01:45:38 +02:00
public virtual int AddedCount
2016-06-27 01:45:38 +02:00
{
get
{
return this.VMList.Where(vm => vm.IsNew).Count();
}
}
public virtual int ChangedCount
{
get
{
return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Count();
}
}
public ObservableCollection<DCType> DCBackup
{
get
{
return this._DCBackup;
}
}
public VMType EditVM
{
get
{
return _EditVM;
2016-06-27 01:45:38 +02:00
}
set
{
_EditVM = value;
FirePropertyChanged(PropertyName_EditVM);
2016-06-27 01:45:38 +02:00
}
}
2024-08-30 13:07:53 +02:00
public VMType SelectedVM
{
get => _SelectedVM;
set
{
_SelectedVM = value;
FirePropertyChanged(nameof(SelectedVM));
SelectedVMChanged?.Invoke(this, EventArgs.Empty);
}
2024-08-30 13:07:53 +02:00
}
2016-06-27 01:45:38 +02:00
public override bool IsDirty
{
get
{
return ChangedCount != 0 || RemovedCount != 0 || AddedCount != 0;
}
}
public VMType NewVM
{
get
{
return _NewVM ?? (NewVM = CreateVM(new DCType()));
2016-06-27 01:45:38 +02:00
}
set
{
_NewVM = value;
FirePropertyChanged(PropertyName_NewVM);
2016-06-27 01:45:38 +02:00
}
}
public virtual int RemovedCount
{
get
{
return GetRemoved().Count;
2016-06-27 01:45:38 +02:00
}
}
public BindingList<VMType> VMList
{
2024-09-12 14:00:17 +02:00
get
{
if (_VMList == null)
{
_VMList = new BindingList<VMType>(_DCBackup.Select(CreateVM).ToList()) { AllowNew = true };
_VMList.AddingNew += (s, e) => e.NewObject = CreateVM(new DCType());
}
2016-06-27 01:45:38 +02:00
2024-09-12 14:00:17 +02:00
return _VMList;
}
2016-06-27 01:45:38 +02:00
set
{
_VMList = value;
}
}
protected virtual Comparison<VMType> Comparison
{
get
{
return null;
}
}
2024-07-16 16:15:57 +02:00
public virtual VMType AddNewVMToList()
2016-06-27 01:45:38 +02:00
{
2024-08-30 13:07:53 +02:00
VMList.Add(NewVM);
2016-06-27 01:45:38 +02:00
// VMList.Sort();
2024-08-30 13:07:53 +02:00
VMType lVM = NewVM;
NewVM = null;
2016-06-27 01:45:38 +02:00
2024-07-16 16:15:57 +02:00
//FirePropertyChanged("VMList");
2016-06-27 01:45:38 +02:00
return lVM;
}
public VMType AddNewVMToList(bool pSetAsEdit)
{
2024-08-30 13:07:53 +02:00
_VMList.Add(_NewVM);
2016-06-27 01:45:38 +02:00
// _VMList.Sort();
if (pSetAsEdit)
{
2024-08-30 13:07:53 +02:00
EditVM = _NewVM;
2016-06-27 01:45:38 +02:00
}
2024-08-30 13:07:53 +02:00
NewVM = null;
return EditVM;
2016-06-27 01:45:38 +02:00
}
2024-09-12 14:26:38 +02:00
public virtual VMType AddNewVMToListAndSelect(bool select)
{
// Wichtig! NewVM, wenn _NewVM null ist
2024-09-12 14:26:38 +02:00
var vm = NewVM;
2024-09-20 13:05:47 +02:00
VMList.Add(vm);
// _VMList.Sort();
2024-09-12 14:00:17 +02:00
if (select)
{
2024-09-12 14:26:38 +02:00
SelectedVM = vm;
}
NewVM = null;
2024-09-12 14:00:17 +02:00
2024-09-12 14:26:38 +02:00
FirePropertyChanged(nameof(VMList));
2024-09-12 14:00:17 +02:00
2024-09-12 14:26:38 +02:00
return vm;
}
2016-06-27 01:45:38 +02:00
public virtual List<DCType> CommitAdded()
{
return this.VMList.Where(vm => vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList();
}
public virtual List<DCType> CommitChanged()
{
return this.VMList.Where(vm => vm.IsDirty && !vm.IsNew).Select(vm => vm.CommitToDataContract()).ToList();
}
public virtual List<DCType> CopyToDCList(bool doCommit)
{
if (doCommit && this._VMList != null)
{
return doCommit
? this._VMList.Select(vm => vm.CommitToDataContract()).ToList()
: this._VMList.Select(i => i.CopyToEmptyDataContract()).ToList();
}
return null;
}
public void DeleteEditVM()
{
this._VMList.Remove(this._EditVM);
this.EditVM = null;
}
2024-09-12 14:26:38 +02:00
public bool DeleteSelectedVM(string displayname = null)
2024-09-12 14:34:14 +02:00
=> DeleteSelectedVM(SelectedVM, displayname);
public bool DeleteSelectedVM(VMType remove, string displayname = null)
2024-08-30 13:07:53 +02:00
{
if (displayname is object)
{
var res = MessageBox.Show($"Möchten Sie {displayname} wirklich löschen?", "Löschen bestätigen", MessageBoxButton.YesNo);
if (res != MessageBoxResult.Yes)
2024-09-12 14:26:38 +02:00
return false;
2024-08-30 13:07:53 +02:00
}
2024-09-12 14:34:14 +02:00
VMList.Remove(remove);
FirePropertyChanged(nameof(VMList));
2024-09-12 14:26:38 +02:00
return true;
2024-08-30 13:07:53 +02:00
}
2016-06-27 01:45:38 +02:00
public virtual List<DCType> GetRemoved()
{
return this._DCBackup.Where(dc => !this._VMList.Select(vm => vm.DataContract).Contains(dc)).ToList();
}
protected virtual VMType CreateVM(DCType pDC)
{
var lConstr = typeof(VMType).GetConstructor(new[] { typeof(DCType) });
return lConstr.Invoke(new object[] { pDC }) as VMType;
}
2024-09-23 14:34:56 +02:00
public void SetSelectByIndex(int index)
2024-09-23 14:34:56 +02:00
{
if(-1 < index && index < VMList.Count){
var x = VMList[index];
SelectedVM = x;
2024-09-23 14:34:56 +02:00
}
}
2025-03-31 16:42:49 +02:00
public virtual string GetVisibleInformationString()
{
return null;
}
2016-06-27 01:45:38 +02:00
}
}