Index: Riskeer/Common/src/Riskeer.Common.Forms/PresentationObjects/LocationCalculationsContext.cs =================================================================== diff -u --- Riskeer/Common/src/Riskeer.Common.Forms/PresentationObjects/LocationCalculationsContext.cs (revision 0) +++ Riskeer/Common/src/Riskeer.Common.Forms/PresentationObjects/LocationCalculationsContext.cs (revision dd9687af655f463ca43a66dd1e94a391a41bf056) @@ -0,0 +1,93 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Core.Common.Base; +using Core.Common.Controls.PresentationObjects; + +namespace Riskeer.Common.Forms.PresentationObjects +{ + public abstract class LocationCalculationsContext + : WrappedObjectContextBase, IObservable + where TObservable : class, IObservable + { + private readonly Collection observers = new Collection(); + + private RecursiveObserver, TObservable> recursiveObserver; + + protected LocationCalculationsContext(TWrappedData wrappedData) + : base(wrappedData) {} + + public IEnumerable Observers => observers; + + public virtual void Attach(IObserver observer) + { + if (!observers.Any()) + { + recursiveObserver = InitializeRecursiveObserver(); + } + + observers.Add(observer); + + LocationCalculationsListToObserve.Attach(observer); + } + + public virtual void Detach(IObserver observer) + { + LocationCalculationsListToObserve.Detach(observer); + + observers.Remove(observer); + + if (!observers.Any()) + { + recursiveObserver.Dispose(); + } + } + + public void NotifyObservers() + { + foreach (IObserver observer in observers) + { + try + { + observer.UpdateObserver(); + } + catch (InvalidOperationException) + { + // Catch any exception due to inevitably updating a tree node for data that was already removed + } + } + } + + protected abstract ObservableList LocationCalculationsListToObserve { get; } + + private RecursiveObserver, TObservable> InitializeRecursiveObserver() + { + return new RecursiveObserver, TObservable>(NotifyObservers, list => list) + { + Observable = LocationCalculationsListToObserve + }; + } + } +} \ No newline at end of file