using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Collections; namespace Microsoft.Research.DynamicDataDisplay.DataSources { /// Data source that is a composer from several other data sources. public class CompositeDataSource : IPointDataSource { /// Initializes a new instance of the class public CompositeDataSource() { } /// /// Initializes a new instance of the class. /// /// Data sources. public CompositeDataSource(params IPointDataSource[] dataSources) { if (dataSources == null) throw new ArgumentNullException("dataSources"); foreach (var dataSource in dataSources) { AddDataPart(dataSource); } } private readonly List dataParts = new List(); public IEnumerable DataParts { get { return dataParts; } } /// /// Adds data part. /// /// The data part. public void AddDataPart(IPointDataSource dataPart) { if (dataPart == null) throw new ArgumentNullException("dataPart"); dataParts.Add(dataPart); dataPart.DataChanged += OnPartDataChanged; } private void OnPartDataChanged(object sender, EventArgs e) { RaiseDataChanged(); } #region IPointSource Members public event EventHandler DataChanged; protected void RaiseDataChanged() { if (DataChanged != null) { DataChanged(this, EventArgs.Empty); } } public IPointEnumerator GetEnumerator(DependencyObject context) { return new CompositeEnumerator(this, context); } #endregion private sealed class CompositeEnumerator : IPointEnumerator { private readonly IEnumerable enumerators; public CompositeEnumerator(CompositeDataSource dataSource, DependencyObject context) { enumerators = dataSource.dataParts.Select(part => part.GetEnumerator(context)).ToList(); } #region IChartPointEnumerator Members public bool MoveNext() { bool res = true; foreach (var enumerator in enumerators) { res &= enumerator.MoveNext(); } return res; } public void ApplyMappings(DependencyObject glyph) { foreach (var enumerator in enumerators) { enumerator.ApplyMappings(glyph); } } public void GetCurrent(ref Point p) { foreach (var enumerator in enumerators) { enumerator.GetCurrent(ref p); } } public void Dispose() { foreach (var enumerator in enumerators) { enumerator.Dispose(); } } #endregion } } }