Widow – Rev 9

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;
using Windows.Annotations;

namespace Windows
{
    [XmlRoot(Namespace = "urn:widow-windows-schema", ElementName = "Windows")]
    public class Windows : INotifyPropertyChanged, IDisposable
    {
        #region Public Enums, Properties and Fields

        [XmlElement(ElementName = "Window")]
        public ObservableCollection<Window> Window
        {
            get => _window;
            set
            {
                if (Equals(value, _window))
                {
                    return;
                }

                _window.CollectionChanged -= _window_CollectionChanged;
                _window = value;
                _windows.Clear();
                foreach (var window in value)
                {
                    _windows.Add(window.Name);
                }

                _window.CollectionChanged += _window_CollectionChanged;
                OnPropertyChanged();
            }
        }

        #endregion

        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private ObservableCollection<Window> _window = new ObservableCollection<Window>();

        private HashSet<string> _windows = new HashSet<string>();

        #endregion

        #region Constructors, Destructors and Finalizers

        [UsedImplicitly]
        public Windows()
        {
            _window.CollectionChanged += _window_CollectionChanged;
        }

        public void Dispose()
        {
            Window.CollectionChanged -= _window_CollectionChanged;
        }

        #endregion

        #region Interface

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Event Handlers

        private void _window_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (var window in e.OldItems.OfType<Window>())
                {
                    if (_windows.Contains(window.Name))
                    {
                        _windows.Remove(window.Name);
                    }
                }
            }


            if (e.NewItems != null)
            {
                foreach (var window in e.NewItems.OfType<Window>())
                {
                    if (!_windows.Contains(window.Name))
                    {
                        _windows.Add(window.Name);
                    }
                }
            }
        }

        #endregion

        #region Public Methods

        public bool Contains(string name)
        {
            return _windows.Contains(name);
        }

        #endregion

        #region Private Methods

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.