PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/PlotterChildrenCollection.cs

#
C# | 209 lines | 159 code | 36 blank | 14 comment | 26 complexity | 04f5be6a730acf5f4801de87fdbb2584 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Collections.ObjectModel;
  6using System.Windows.Markup;
  7using Microsoft.Research.DynamicDataDisplay.Charts;
  8using System.Windows;
  9using System.Windows.Controls.Primitives;
 10using System.Collections;
 11
 12namespace Microsoft.Research.DynamicDataDisplay.Common
 13{
 14	/// <summary>
 15	/// Contains all charts added to ChartPlotter.
 16	/// </summary>
 17	[ContentWrapper(typeof(ViewportUIContainer))]
 18	public sealed class PlotterChildrenCollection : D3Collection<IPlotterElement>, IList
 19	{
 20		/// <summary>
 21		/// Initializes a new instance of the <see cref="PlotterChildrenCollection"/> class.
 22		/// </summary>
 23		internal PlotterChildrenCollection(Plotter plotter)
 24		{
 25			if (plotter == null)
 26				throw new ArgumentNullException("plotter");
 27
 28			this.plotter = plotter;
 29		}
 30
 31		private readonly Plotter plotter;
 32		public Plotter Plotter
 33		{
 34			get { return plotter; }
 35		}
 36
 37		/// <summary>
 38		/// Called before item added to collection. Enables to perform validation.
 39		/// </summary>
 40		/// <param name="item">The adding item.</param>
 41		protected override void OnItemAdding(IPlotterElement item)
 42		{
 43			if (item == null)
 44				throw new ArgumentNullException("item");
 45		}
 46
 47		/// <summary>
 48		/// This override enables notifying about removing each element, instead of
 49		/// notifying about collection reset.
 50		/// </summary>
 51		protected override void ClearItems()
 52		{
 53			var items = new List<IPlotterElement>(base.Items);
 54			foreach (var item in items)
 55			{
 56				Remove(item);
 57			}
 58		}
 59
 60		#region Foreign content
 61
 62		public void Add(FrameworkElement content)
 63		{
 64			if (content == null)
 65				throw new ArgumentNullException("content");
 66
 67			IPlotterElement plotterElement = content as IPlotterElement;
 68			if (plotterElement != null)
 69			{
 70				Add(plotterElement);
 71			}
 72			else
 73			{
 74				ViewportUIContainer container = new ViewportUIContainer(content);
 75				Add(container);
 76			}
 77		}
 78
 79		#endregion // end of Foreign content
 80
 81		#region IList Members
 82
 83		int IList.Add(object value)
 84		{
 85			if (value == null)
 86				throw new ArgumentNullException("value");
 87
 88
 89			FrameworkElement content = value as FrameworkElement;
 90			if (content != null)
 91			{
 92				Add(content);
 93
 94				return 0;
 95			}
 96
 97			IPlotterElement element = value as IPlotterElement;
 98			if (element != null)
 99			{
100				Add(element);
101
102				return 0;
103			}
104
105			throw new ArgumentException(String.Format("Children of type '{0}' are not supported.", value.GetType()));
106		}
107
108		void IList.Clear()
109		{
110			Clear();
111		}
112
113		bool IList.Contains(object value)
114		{
115			IPlotterElement element = value as IPlotterElement;
116			return element != null && Contains(element);
117		}
118
119		int IList.IndexOf(object value)
120		{
121			IPlotterElement element = value as IPlotterElement;
122			if (element != null)
123				return IndexOf(element);
124			return -1;
125		}
126
127		void IList.Insert(int index, object value)
128		{
129			IPlotterElement element = value as IPlotterElement;
130			if (element != null)
131			{
132				Insert(index, element);
133			}
134		}
135
136		bool IList.IsFixedSize
137		{
138			get { return false; }
139		}
140
141		bool IList.IsReadOnly
142		{
143			get { return false; }
144		}
145
146		void IList.Remove(object value)
147		{
148			IPlotterElement element = value as IPlotterElement;
149			if (element != null)
150				Remove(element);
151		}
152
153		void IList.RemoveAt(int index)
154		{
155			RemoveAt(index);
156		}
157
158		object IList.this[int index]
159		{
160			get
161			{
162				return this[index];
163			}
164			set
165			{
166				IPlotterElement element = value as IPlotterElement;
167				if (element != null)
168					this[index] = element;
169			}
170		}
171
172		#endregion
173
174		#region ICollection Members
175
176		void ICollection.CopyTo(Array array, int index)
177		{
178			IPlotterElement[] elements = array as IPlotterElement[];
179			if (elements != null)
180				CopyTo(elements, index);
181		}
182
183		int ICollection.Count
184		{
185			get { return Count; }
186		}
187
188		bool ICollection.IsSynchronized
189		{
190			get { return false; }
191		}
192
193		object ICollection.SyncRoot
194		{
195			get { return null; }
196		}
197
198		#endregion
199
200		#region IEnumerable Members
201
202		IEnumerator IEnumerable.GetEnumerator()
203		{
204			return GetEnumerator();
205		}
206
207		#endregion
208	}
209}