1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Windows;
|
---|
5 | using System.Collections;
|
---|
6 |
|
---|
7 | namespace Microsoft.Research.DynamicDataDisplay.DataSources
|
---|
8 | {
|
---|
9 | /// <summary>Data source that is a composer from several other data sources.</summary>
|
---|
10 | public class CompositeDataSource : IPointDataSource {
|
---|
11 | /// <summary>Initializes a new instance of the <see cref="CompositeDataSource"/> class</summary>
|
---|
12 | public CompositeDataSource() { }
|
---|
13 |
|
---|
14 | /// <summary>
|
---|
15 | /// Initializes a new instance of the <see cref="CompositeDataSource"/> class.
|
---|
16 | /// </summary>
|
---|
17 | /// <param name="dataSources">Data sources.</param>
|
---|
18 | public CompositeDataSource(params IPointDataSource[] dataSources) {
|
---|
19 | if (dataSources == null)
|
---|
20 | throw new ArgumentNullException("dataSources");
|
---|
21 |
|
---|
22 | foreach (var dataSource in dataSources) {
|
---|
23 | AddDataPart(dataSource);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | private readonly List<IPointDataSource> dataParts = new List<IPointDataSource>();
|
---|
28 |
|
---|
29 | public IEnumerable<IPointDataSource> DataParts {
|
---|
30 | get { return dataParts; }
|
---|
31 | }
|
---|
32 |
|
---|
33 | /// <summary>
|
---|
34 | /// Adds data part.
|
---|
35 | /// </summary>
|
---|
36 | /// <param name="dataPart">The data part.</param>
|
---|
37 | public void AddDataPart(IPointDataSource dataPart) {
|
---|
38 | if (dataPart == null)
|
---|
39 | throw new ArgumentNullException("dataPart");
|
---|
40 |
|
---|
41 | dataParts.Add(dataPart);
|
---|
42 | dataPart.DataChanged += OnPartDataChanged;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void OnPartDataChanged(object sender, EventArgs e) {
|
---|
46 | RaiseDataChanged();
|
---|
47 | }
|
---|
48 |
|
---|
49 | #region IPointSource Members
|
---|
50 |
|
---|
51 | public event EventHandler DataChanged;
|
---|
52 | protected void RaiseDataChanged() {
|
---|
53 | if (DataChanged != null) {
|
---|
54 | DataChanged(this, EventArgs.Empty);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public IPointEnumerator GetEnumerator(DependencyObject context) {
|
---|
59 | return new CompositeEnumerator(this, context);
|
---|
60 | }
|
---|
61 |
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | private sealed class CompositeEnumerator : IPointEnumerator {
|
---|
65 | private readonly IEnumerable<IPointEnumerator> enumerators;
|
---|
66 |
|
---|
67 | public CompositeEnumerator(CompositeDataSource dataSource, DependencyObject context) {
|
---|
68 | enumerators = dataSource.dataParts.Select(part => part.GetEnumerator(context)).ToList();
|
---|
69 | }
|
---|
70 |
|
---|
71 | #region IChartPointEnumerator Members
|
---|
72 |
|
---|
73 | public bool MoveNext() {
|
---|
74 | bool res = true;
|
---|
75 | foreach (var enumerator in enumerators) {
|
---|
76 | res &= enumerator.MoveNext();
|
---|
77 | }
|
---|
78 | return res;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void ApplyMappings(DependencyObject glyph) {
|
---|
82 | foreach (var enumerator in enumerators) {
|
---|
83 | enumerator.ApplyMappings(glyph);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void GetCurrent(ref Point p) {
|
---|
88 | foreach (var enumerator in enumerators) {
|
---|
89 | enumerator.GetCurrent(ref p);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public void Dispose() {
|
---|
94 | foreach (var enumerator in enumerators) {
|
---|
95 | enumerator.Dispose();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endregion
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|