#region CPL License /* Nuclex Framework Copyright (C) 2002-2019 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library 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 IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #endregion using System; using System.Collections.Concurrent; using Nuclex.Support; namespace Nuclex.Windows.Forms.ViewModels { /// Base class for view models that have multiple child view models /// Enum type by which pages can be indicated public abstract class MultiPageViewModel : Observable, IMultiPageViewModel, IDisposable { /// Initializes a new multi-page view model /// /// Window manager the view model uses to create child views /// /// /// Whether child view models will be kept alive and reused /// public MultiPageViewModel(IWindowManager windowManager, bool cachePageViewModels = false) { this.windowManager = windowManager; if(cachePageViewModels) { this.cachedViewModels = new ConcurrentDictionary(); } } /// Immediately releases all resources owned by the instance public virtual void Dispose() { if(this.cachedViewModels != null) { foreach(object cacheViewModel in this.cachedViewModels.Values) { disposeIfSupported(cacheViewModel); } this.activePageViewModel = null; this.cachedViewModels.Clear(); this.cachedViewModels = null; } else if(this.activePageViewModel != null) { disposeIfSupported(this.activePageViewModel); this.activePageViewModel = null; } } /// Child page that is currently being displayed by the view model public TPageEnumeration ActivePage { get { return this.activePage; } set { if(!this.activePage.Equals(value)) { this.activePage = value; if(this.activePageViewModel != null) { if(this.cachedViewModels == null) { disposeIfSupported(this.activePageViewModel); } this.activePageViewModel = null; } OnPropertyChanged(nameof(ActivePage)); } } } /// Retrieves (and, if needed, creates) the view model for the active page /// A view model for the active page on the multi-page view model public object GetActivePageViewModel() { if(this.cachedViewModels == null) { if(this.activePageViewModel == null) { this.activePageViewModel = CreateViewModelForPage(this.activePage); } } else if(this.activePageViewModel == null) { this.activePageViewModel = this.cachedViewModels.GetOrAdd( this.activePage, delegate(TPageEnumeration activePage) { return CreateViewModelForPage(this.activePage); } ); } return this.activePageViewModel; } /// Windowmanager that can create view models and display other views protected IWindowManager WindowManager { get { return this.windowManager; } } /// Creates a view model for the specified page /// Page for which a view model will be created /// The view model for the specified page protected abstract object CreateViewModelForPage(TPageEnumeration page); /// Disposes the specified object if it is disposable /// Object that will be disposed if supported private static void disposeIfSupported(object potentiallyDisposable) { var disposable = potentiallyDisposable as IDisposable; if(disposable != null) { disposable.Dispose(); } } /// Page that is currently active in the multi-page view model private TPageEnumeration activePage; /// Window manager that can be used to display other views private IWindowManager windowManager; /// View model for the active page private object activePageViewModel; /// Cached page view models, if caching is enabled private ConcurrentDictionary cachedViewModels; } } // namespace Nuclex.Windows.Forms.ViewModels