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

/Main/src/DynamicDataDisplay/ViewportConstraints/ConstraintCollection.cs

#
C# | 69 lines | 56 code | 7 blank | 6 comment | 8 complexity | 10e63bf087d2714e55c6b9943315b0b6 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
 1using System.Linq;
 2using System.Windows;
 3using Microsoft.Research.DynamicDataDisplay;
 4using Microsoft.Research.DynamicDataDisplay.Common;
 5using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
 6using System;
 7using System.Collections.Specialized;
 8
 9namespace Microsoft.Research.DynamicDataDisplay.ViewportConstraints
10{
11	/// <summary>
12	/// Represents a collection of <see cref="ViewportConstraint"/>s.
13	/// <remarks>
14	/// ViewportConstraint that is being added should not be null.
15	/// </remarks>
16	/// </summary>
17	public sealed class ConstraintCollection : D3Collection<ViewportConstraint>
18	{
19		private readonly Viewport2D viewport;
20		internal ConstraintCollection(Viewport2D viewport)
21		{
22			if (viewport == null)
23				throw new ArgumentNullException("viewport");
24
25			this.viewport = viewport;
26		}
27
28		protected override void OnItemAdding(ViewportConstraint item)
29		{
30			if (item == null)
31				throw new ArgumentNullException("item");
32		}
33
34		protected override void OnItemAdded(ViewportConstraint item)
35		{
36			item.Changed += OnItemChanged;
37			ISupportAttachToViewport attachable = item as ISupportAttachToViewport;
38			if (attachable != null)
39			{
40				attachable.Attach(viewport);
41			}
42		}
43
44		private void OnItemChanged(object sender, EventArgs e)
45		{
46			OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
47		}
48
49		protected override void OnItemRemoving(ViewportConstraint item)
50		{
51			ISupportAttachToViewport attachable = item as ISupportAttachToViewport;
52			if (attachable != null)
53			{
54				attachable.Detach(viewport);
55			}
56			item.Changed -= OnItemChanged;
57		}
58
59		internal DataRect Apply(DataRect oldVisible, DataRect newVisible, Viewport2D viewport)
60		{
61			DataRect res = newVisible;
62			foreach (var constraint in this)
63			{
64				res = constraint.Apply(oldVisible, res, viewport);
65			}
66			return res;
67		}
68	}
69}